001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.control.user.contactlist.server.command;
018
019import com.echothree.control.user.contactlist.common.edit.ContactListContactMechanismPurposeEdit;
020import com.echothree.control.user.contactlist.common.edit.ContactListEditFactory;
021import com.echothree.control.user.contactlist.common.form.EditContactListContactMechanismPurposeForm;
022import com.echothree.control.user.contactlist.common.result.ContactListResultFactory;
023import com.echothree.control.user.contactlist.common.result.EditContactListContactMechanismPurposeResult;
024import com.echothree.control.user.contactlist.common.spec.ContactListContactMechanismPurposeSpec;
025import com.echothree.model.control.contact.server.logic.ContactMechanismPurposeLogic;
026import com.echothree.model.control.contactlist.server.control.ContactListControl;
027import com.echothree.model.control.contactlist.server.logic.ContactListLogic;
028import com.echothree.model.control.party.common.PartyTypes;
029import com.echothree.model.control.security.common.SecurityRoleGroups;
030import com.echothree.model.control.security.common.SecurityRoles;
031import com.echothree.model.data.contactlist.server.entity.ContactListContactMechanismPurpose;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.common.command.EditMode;
037import com.echothree.util.server.control.BaseAbstractEditCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044
045@Dependent
046public class EditContactListContactMechanismPurposeCommand
047        extends BaseAbstractEditCommand<ContactListContactMechanismPurposeSpec, ContactListContactMechanismPurposeEdit, EditContactListContactMechanismPurposeResult, ContactListContactMechanismPurpose, ContactListContactMechanismPurpose> {
048    
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
051    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
057                        new SecurityRoleDefinition(SecurityRoleGroups.ContactList.name(), SecurityRoles.ContactListContactMechanismPurpose.name())
058                        ))
059                ));
060        
061        SPEC_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("ContactMechanismPurposeName", FieldType.ENTITY_NAME, true, null, null)
064                );
065        
066        EDIT_FIELD_DEFINITIONS = List.of(
067                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
068                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
069                );
070    }
071    
072    /** Creates a new instance of EditContactListContactMechanismPurposeCommand */
073    public EditContactListContactMechanismPurposeCommand() {
074        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
075    }
076    
077    @Override
078    public EditContactListContactMechanismPurposeResult getResult() {
079        return ContactListResultFactory.getEditContactListContactMechanismPurposeResult();
080    }
081
082    @Override
083    public ContactListContactMechanismPurposeEdit getEdit() {
084        return ContactListEditFactory.getContactListContactMechanismPurposeEdit();
085    }
086
087    @Override
088    public ContactListContactMechanismPurpose getEntity(EditContactListContactMechanismPurposeResult result) {
089        ContactListContactMechanismPurpose contactListContactMechanismPurpose = null;
090        var contactListName = spec.getContactListName();
091        var contactList = ContactListLogic.getInstance().getContactListByName(this, contactListName);
092        
093        if(!hasExecutionErrors()) {
094            var contactMechanismPurposeName = spec.getContactMechanismPurposeName();
095            var contactMechanismPurpose = ContactMechanismPurposeLogic.getInstance().getContactMechanismPurposeByName(this, contactMechanismPurposeName);
096            
097            if(!hasExecutionErrors()) {
098                var contactListControl = Session.getModelController(ContactListControl.class);
099                
100                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
101                    contactListContactMechanismPurpose = contactListControl.getContactListContactMechanismPurpose(contactList, contactMechanismPurpose);
102                } else { // EditMode.UPDATE
103                    contactListContactMechanismPurpose = contactListControl.getContactListContactMechanismPurposeForUpdate(contactList, contactMechanismPurpose);
104                }
105
106                if(contactListContactMechanismPurpose == null) {
107                    addExecutionError(ExecutionErrors.UnknownContactListContactMechanismPurpose.name(), contactListName, contactMechanismPurposeName);
108                }
109            }
110        }
111
112        return contactListContactMechanismPurpose;
113    }
114
115    @Override
116    public ContactListContactMechanismPurpose getLockEntity(ContactListContactMechanismPurpose contactListContactMechanismPurpose) {
117        return contactListContactMechanismPurpose;
118    }
119
120    @Override
121    public void fillInResult(EditContactListContactMechanismPurposeResult result, ContactListContactMechanismPurpose contactListContactMechanismPurpose) {
122        var contactListControl = Session.getModelController(ContactListControl.class);
123
124        result.setContactListContactMechanismPurpose(contactListControl.getContactListContactMechanismPurposeTransfer(getUserVisit(), contactListContactMechanismPurpose));
125    }
126
127    @Override
128    public void doLock(ContactListContactMechanismPurposeEdit edit, ContactListContactMechanismPurpose contactListContactMechanismPurpose) {
129        var contactListControl = Session.getModelController(ContactListControl.class);
130        var contactListContactMechanismPurposeDetail = contactListContactMechanismPurpose.getLastDetail();
131
132        edit.setIsDefault(contactListContactMechanismPurposeDetail.getIsDefault().toString());
133        edit.setSortOrder(contactListContactMechanismPurposeDetail.getSortOrder().toString());
134    }
135
136    @Override
137    public void doUpdate(ContactListContactMechanismPurpose contactListContactMechanismPurpose) {
138        var contactListControl = Session.getModelController(ContactListControl.class);
139        var partyPK = getPartyPK();
140        var contactListContactMechanismPurposeDetailValue = contactListControl.getContactListContactMechanismPurposeDetailValueForUpdate(contactListContactMechanismPurpose);
141
142        contactListContactMechanismPurposeDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
143        contactListContactMechanismPurposeDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
144
145        contactListControl.updateContactListContactMechanismPurposeFromValue(contactListContactMechanismPurposeDetailValue, partyPK);
146    }
147    
148}