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