001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.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.contact.server.entity.ContactMechanismPurpose;
032import com.echothree.model.data.contactlist.server.entity.ContactList;
033import com.echothree.model.data.contactlist.server.entity.ContactListContactMechanismPurpose;
034import com.echothree.model.data.contactlist.server.entity.ContactListContactMechanismPurposeDetail;
035import com.echothree.model.data.contactlist.server.value.ContactListContactMechanismPurposeDetailValue;
036import com.echothree.model.data.party.common.pk.PartyPK;
037import com.echothree.model.data.user.common.pk.UserVisitPK;
038import com.echothree.util.common.message.ExecutionErrors;
039import com.echothree.util.common.validation.FieldDefinition;
040import com.echothree.util.common.validation.FieldType;
041import com.echothree.util.common.command.EditMode;
042import com.echothree.util.server.control.BaseAbstractEditCommand;
043import com.echothree.util.server.control.CommandSecurityDefinition;
044import com.echothree.util.server.control.PartyTypeDefinition;
045import com.echothree.util.server.control.SecurityRoleDefinition;
046import com.echothree.util.server.persistence.Session;
047import java.util.Arrays;
048import java.util.Collections;
049import java.util.List;
050
051public class EditContactListContactMechanismPurposeCommand
052        extends BaseAbstractEditCommand<ContactListContactMechanismPurposeSpec, ContactListContactMechanismPurposeEdit, EditContactListContactMechanismPurposeResult, ContactListContactMechanismPurpose, ContactListContactMechanismPurpose> {
053    
054    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
055    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
056    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
057    
058    static {
059        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
060                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
061                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
062                        new SecurityRoleDefinition(SecurityRoleGroups.ContactList.name(), SecurityRoles.ContactListContactMechanismPurpose.name())
063                        )))
064                )));
065        
066        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
067                new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null),
068                new FieldDefinition("ContactMechanismPurposeName", FieldType.ENTITY_NAME, true, null, null)
069                ));
070        
071        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
072                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
073                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
074                ));
075    }
076    
077    /** Creates a new instance of EditContactListContactMechanismPurposeCommand */
078    public EditContactListContactMechanismPurposeCommand(UserVisitPK userVisitPK, EditContactListContactMechanismPurposeForm form) {
079        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081    
082    @Override
083    public EditContactListContactMechanismPurposeResult getResult() {
084        return ContactListResultFactory.getEditContactListContactMechanismPurposeResult();
085    }
086
087    @Override
088    public ContactListContactMechanismPurposeEdit getEdit() {
089        return ContactListEditFactory.getContactListContactMechanismPurposeEdit();
090    }
091
092    @Override
093    public ContactListContactMechanismPurpose getEntity(EditContactListContactMechanismPurposeResult result) {
094        ContactListContactMechanismPurpose contactListContactMechanismPurpose = null;
095        String contactListName = spec.getContactListName();
096        ContactList contactList = ContactListLogic.getInstance().getContactListByName(this, contactListName);
097        
098        if(!hasExecutionErrors()) {
099            String contactMechanismPurposeName = spec.getContactMechanismPurposeName();
100            ContactMechanismPurpose contactMechanismPurpose = ContactMechanismPurposeLogic.getInstance().getContactMechanismPurposeByName(this, contactMechanismPurposeName);
101            
102            if(!hasExecutionErrors()) {
103                var contactListControl = Session.getModelController(ContactListControl.class);
104                
105                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
106                    contactListContactMechanismPurpose = contactListControl.getContactListContactMechanismPurpose(contactList, contactMechanismPurpose);
107                } else { // EditMode.UPDATE
108                    contactListContactMechanismPurpose = contactListControl.getContactListContactMechanismPurposeForUpdate(contactList, contactMechanismPurpose);
109                }
110
111                if(contactListContactMechanismPurpose == null) {
112                    addExecutionError(ExecutionErrors.UnknownContactListContactMechanismPurpose.name(), contactListName, contactMechanismPurposeName);
113                }
114            }
115        }
116
117        return contactListContactMechanismPurpose;
118    }
119
120    @Override
121    public ContactListContactMechanismPurpose getLockEntity(ContactListContactMechanismPurpose contactListContactMechanismPurpose) {
122        return contactListContactMechanismPurpose;
123    }
124
125    @Override
126    public void fillInResult(EditContactListContactMechanismPurposeResult result, ContactListContactMechanismPurpose contactListContactMechanismPurpose) {
127        var contactListControl = Session.getModelController(ContactListControl.class);
128
129        result.setContactListContactMechanismPurpose(contactListControl.getContactListContactMechanismPurposeTransfer(getUserVisit(), contactListContactMechanismPurpose));
130    }
131
132    @Override
133    public void doLock(ContactListContactMechanismPurposeEdit edit, ContactListContactMechanismPurpose contactListContactMechanismPurpose) {
134        var contactListControl = Session.getModelController(ContactListControl.class);
135        ContactListContactMechanismPurposeDetail contactListContactMechanismPurposeDetail = contactListContactMechanismPurpose.getLastDetail();
136
137        edit.setIsDefault(contactListContactMechanismPurposeDetail.getIsDefault().toString());
138        edit.setSortOrder(contactListContactMechanismPurposeDetail.getSortOrder().toString());
139    }
140
141    @Override
142    public void doUpdate(ContactListContactMechanismPurpose contactListContactMechanismPurpose) {
143        var contactListControl = Session.getModelController(ContactListControl.class);
144        var partyPK = getPartyPK();
145        ContactListContactMechanismPurposeDetailValue contactListContactMechanismPurposeDetailValue = contactListControl.getContactListContactMechanismPurposeDetailValueForUpdate(contactListContactMechanismPurpose);
146
147        contactListContactMechanismPurposeDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
148        contactListContactMechanismPurposeDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
149
150        contactListControl.updateContactListContactMechanismPurposeFromValue(contactListContactMechanismPurposeDetailValue, partyPK);
151    }
152    
153}