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.ContactListEditFactory;
020import com.echothree.control.user.contactlist.common.edit.PartyContactListEdit;
021import com.echothree.control.user.contactlist.common.form.EditPartyContactListForm;
022import com.echothree.control.user.contactlist.common.result.ContactListResultFactory;
023import com.echothree.control.user.contactlist.common.result.EditPartyContactListResult;
024import com.echothree.control.user.contactlist.common.spec.PartyContactListSpec;
025import com.echothree.model.control.contact.server.control.ContactControl;
026import com.echothree.model.control.contactlist.server.ContactListControl;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.party.server.control.PartyControl;
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.PartyContactList;
035import com.echothree.model.data.contactlist.server.entity.PartyContactListDetail;
036import com.echothree.model.data.contactlist.server.value.PartyContactListDetailValue;
037import com.echothree.model.data.party.server.entity.Party;
038import com.echothree.model.data.user.common.pk.UserVisitPK;
039import com.echothree.util.common.message.ExecutionErrors;
040import com.echothree.util.common.validation.FieldDefinition;
041import com.echothree.util.common.validation.FieldType;
042import com.echothree.util.common.command.EditMode;
043import com.echothree.util.server.control.BaseAbstractEditCommand;
044import com.echothree.util.server.control.CommandSecurityDefinition;
045import com.echothree.util.server.control.PartyTypeDefinition;
046import com.echothree.util.server.control.SecurityRoleDefinition;
047import com.echothree.util.server.persistence.Session;
048import java.util.Arrays;
049import java.util.Collections;
050import java.util.List;
051
052public class EditPartyContactListCommand
053        extends BaseAbstractEditCommand<PartyContactListSpec, PartyContactListEdit, EditPartyContactListResult, PartyContactList, PartyContactList> {
054
055    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
056    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
057    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
058
059    static {
060        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
061                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
062                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
063                        new SecurityRoleDefinition(SecurityRoleGroups.PartyContactList.name(), SecurityRoles.Edit.name())
064                        )))
065                )));
066
067        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
068                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, true, null, null),
069                new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null)
070                ));
071
072        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
073                new FieldDefinition("PreferredContactMechanismPurposeName", FieldType.ENTITY_NAME, false, null, null)
074                ));
075    }
076
077    /** Creates a new instance of EditContactListCommand */
078    public EditPartyContactListCommand(UserVisitPK userVisitPK, EditPartyContactListForm form) {
079        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081
082    @Override
083    public EditPartyContactListResult getResult() {
084        return ContactListResultFactory.getEditPartyContactListResult();
085    }
086
087    @Override
088    public PartyContactListEdit getEdit() {
089        return ContactListEditFactory.getPartyContactListEdit();
090    }
091
092    ContactList contactList;
093    
094    @Override
095    public PartyContactList getEntity(EditPartyContactListResult result) {
096        var partyControl = Session.getModelController(PartyControl.class);
097        PartyContactList partyContactList = null;
098        String partyName = spec.getPartyName();
099        Party party = partyControl.getPartyByName(partyName);
100        
101        if(party != null) {
102            var contactListControl = Session.getModelController(ContactListControl.class);
103            String contactListName = spec.getContactListName();
104            
105            contactList = contactListControl.getContactListByName(contactListName);
106            
107            if(contactList != null) {
108                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
109                    partyContactList = contactListControl.getPartyContactList(party, contactList);
110                } else { // EditMode.UPDATE
111                    partyContactList = contactListControl.getPartyContactListForUpdate(party, contactList);
112                }
113
114                if(partyContactList == null) {
115                    addExecutionError(ExecutionErrors.UnknownPartyContactList.name(), partyName, contactListName);
116                }
117            } else {
118                addExecutionError(ExecutionErrors.UnknownContactListName.name(), contactListName);
119            }
120        } else {
121            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
122        }
123
124        return partyContactList;
125    }
126
127    @Override
128    public PartyContactList getLockEntity(PartyContactList partyContactList) {
129        return partyContactList;
130    }
131
132    @Override
133    public void fillInResult(EditPartyContactListResult result, PartyContactList partyContactList) {
134        var contactListControl = Session.getModelController(ContactListControl.class);
135
136        result.setPartyContactList(contactListControl.getPartyContactListTransfer(getUserVisit(), partyContactList));
137    }
138
139    ContactListContactMechanismPurpose preferredContactListContactMechanismPurpose;
140    
141    @Override
142    public void doLock(PartyContactListEdit edit, PartyContactList partyContactList) {
143        PartyContactListDetail partyContactListDetail = partyContactList.getLastDetail();
144        
145        preferredContactListContactMechanismPurpose = partyContactListDetail.getPreferredContactListContactMechanismPurpose();
146        
147        edit.setPreferredContactMechanismPurposeName(preferredContactListContactMechanismPurpose == null ? null : preferredContactListContactMechanismPurpose.getLastDetail().getContactMechanismPurpose().getContactMechanismPurposeName());
148    }
149
150    @Override
151    public void canUpdate(PartyContactList partyContactList) {
152        var contactControl = Session.getModelController(ContactControl.class);
153        String preferredContactMechanismPurposeName = edit.getPreferredContactMechanismPurposeName();
154        ContactMechanismPurpose preferredContactMechanismPurpose = preferredContactMechanismPurposeName == null ? null : contactControl.getContactMechanismPurposeByName(preferredContactMechanismPurposeName);
155
156        if(preferredContactMechanismPurposeName == null || preferredContactMechanismPurpose != null) {
157            var contactListControl = Session.getModelController(ContactListControl.class);
158            
159            preferredContactListContactMechanismPurpose = preferredContactMechanismPurpose == null ? null : contactListControl.getContactListContactMechanismPurpose(contactList, preferredContactMechanismPurpose);
160
161            if(preferredContactMechanismPurpose != null && preferredContactListContactMechanismPurpose == null) {
162                addExecutionError(ExecutionErrors.UnknownContactListContactMechanismPurpose.name(), contactList.getLastDetail().getContactListName(),
163                        preferredContactMechanismPurposeName);
164            }
165        } else {
166            addExecutionError(ExecutionErrors.UnknownContactMechanismPurposeName.name(), preferredContactMechanismPurposeName);
167        }
168    }
169
170    @Override
171    public void doUpdate(PartyContactList partyContactList) {
172        var contactListControl = Session.getModelController(ContactListControl.class);
173        var partyPK = getPartyPK();
174        PartyContactListDetailValue partyContactListDetailValue = contactListControl.getPartyContactListDetailValueForUpdate(partyContactList);
175
176        partyContactListDetailValue.setPreferredContactListContactMechanismPurposePK(preferredContactListContactMechanismPurpose == null ? null : preferredContactListContactMechanismPurpose.getPrimaryKey());
177
178        contactListControl.updatePartyContactListFromValue(partyContactListDetailValue, partyPK);
179    }
180
181}