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.ContactListEdit;
020import com.echothree.control.user.contactlist.common.edit.ContactListEditFactory;
021import com.echothree.control.user.contactlist.common.form.EditContactListForm;
022import com.echothree.control.user.contactlist.common.result.ContactListResultFactory;
023import com.echothree.control.user.contactlist.common.result.EditContactListResult;
024import com.echothree.control.user.contactlist.common.spec.ContactListSpec;
025import com.echothree.model.control.contactlist.server.control.ContactListControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.control.contactlist.common.workflow.PartyContactListStatusConstants;
030import com.echothree.model.control.workflow.server.control.WorkflowControl;
031import com.echothree.model.data.contactlist.server.entity.ContactList;
032import com.echothree.model.data.contactlist.server.entity.ContactListFrequency;
033import com.echothree.model.data.contactlist.server.entity.ContactListGroup;
034import com.echothree.model.data.contactlist.server.entity.ContactListType;
035import com.echothree.model.data.user.common.pk.UserVisitPK;
036import com.echothree.model.data.workflow.server.entity.WorkflowEntrance;
037import com.echothree.util.common.message.ExecutionErrors;
038import com.echothree.util.common.validation.FieldDefinition;
039import com.echothree.util.common.validation.FieldType;
040import com.echothree.util.common.command.EditMode;
041import com.echothree.util.server.control.BaseAbstractEditCommand;
042import com.echothree.util.server.control.CommandSecurityDefinition;
043import com.echothree.util.server.control.PartyTypeDefinition;
044import com.echothree.util.server.control.SecurityRoleDefinition;
045import java.util.List;
046import javax.enterprise.context.Dependent;
047import javax.inject.Inject;
048
049@Dependent
050public class EditContactListCommand
051        extends BaseAbstractEditCommand<ContactListSpec, ContactListEdit, EditContactListResult, ContactList, ContactList> {
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(List.of(
059                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
060                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
061                        new SecurityRoleDefinition(SecurityRoleGroups.ContactList.name(), SecurityRoles.Edit.name())
062                ))
063        ));
064
065        SPEC_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null)
067        );
068
069        EDIT_FIELD_DEFINITIONS = List.of(
070                new FieldDefinition("ContactListName", FieldType.ENTITY_NAME, true, null, null),
071                new FieldDefinition("ContactListGroupName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("ContactListTypeName", FieldType.ENTITY_NAME, true, null, null),
073                new FieldDefinition("ContactListFrequencyName", FieldType.ENTITY_NAME, false, null, null),
074                new FieldDefinition("DefaultPartyContactListStatusChoice", FieldType.ENTITY_NAME, true, null, null),
075                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
076                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
077                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
078        );
079    }
080
081    @Inject
082    ContactListControl contactListControl;
083
084    @Inject
085    WorkflowControl workflowControl;
086
087    /** Creates a new instance of EditContactListCommand */
088    public EditContactListCommand() {
089        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
090    }
091
092    @Override
093    public EditContactListResult getResult() {
094        return ContactListResultFactory.getEditContactListResult();
095    }
096
097    @Override
098    public ContactListEdit getEdit() {
099        return ContactListEditFactory.getContactListEdit();
100    }
101
102    @Override
103    public ContactList getEntity(EditContactListResult result) {
104        ContactList contactList;
105        var contactListName = spec.getContactListName();
106
107        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
108            contactList = contactListControl.getContactListByName(contactListName);
109        } else { // EditMode.UPDATE
110            contactList = contactListControl.getContactListByNameForUpdate(contactListName);
111        }
112
113        if(contactList == null) {
114            addExecutionError(ExecutionErrors.UnknownContactListName.name(), contactListName);
115        }
116
117        return contactList;
118    }
119
120    @Override
121    public ContactList getLockEntity(ContactList contactList) {
122        return contactList;
123    }
124
125    @Override
126    public void fillInResult(EditContactListResult result, ContactList contactList) {
127        result.setContactList(contactListControl.getContactListTransfer(getUserVisit(), contactList));
128    }
129
130    ContactListFrequency contactListFrequency;
131
132    @Override
133    public void doLock(ContactListEdit edit, ContactList contactList) {
134        var contactListDescription = contactListControl.getContactListDescription(contactList, getPreferredLanguage());
135        var contactListDetail = contactList.getLastDetail();
136
137        contactListFrequency = contactListDetail.getContactListFrequency();
138
139        edit.setContactListName(contactListDetail.getContactListName());
140        edit.setContactListGroupName(contactListDetail.getContactListGroup().getLastDetail().getContactListGroupName());
141        edit.setContactListTypeName(contactListDetail.getContactListType().getLastDetail().getContactListTypeName());
142        edit.setContactListFrequencyName(contactListFrequency == null ? null : contactListFrequency.getLastDetail().getContactListFrequencyName());
143        edit.setDefaultPartyContactListStatusChoice(contactListDetail.getDefaultPartyContactListStatus().getLastDetail().getWorkflowEntranceName());
144        edit.setIsDefault(contactListDetail.getIsDefault().toString());
145        edit.setSortOrder(contactListDetail.getSortOrder().toString());
146
147        if(contactListDescription != null) {
148            edit.setDescription(contactListDescription.getDescription());
149        }
150    }
151
152    ContactListGroup contactListGroup;
153    ContactListType contactListType;
154    WorkflowEntrance defaultPartyContactListStatus;
155
156    @Override
157    public void canUpdate(ContactList contactList) {
158        var contactListName = edit.getContactListName();
159        var duplicateContactList = contactListControl.getContactListByName(contactListName);
160
161        if(duplicateContactList != null && !contactList.equals(duplicateContactList)) {
162            addExecutionError(ExecutionErrors.DuplicateContactListName.name(), contactListName);
163        } else {
164            var contactListGroupName = edit.getContactListGroupName();
165
166            contactListGroup = contactListControl.getContactListGroupByName(contactListGroupName);
167
168            if(contactListGroup != null) {
169                var contactListTypeName = edit.getContactListTypeName();
170
171                contactListType = contactListControl.getContactListTypeByName(contactListTypeName);
172
173                if(contactListType != null) {
174                    var contactListFrequencyName = edit.getContactListFrequencyName();
175
176                    contactListFrequency = contactListFrequencyName == null ? null : contactListControl.getContactListFrequencyByName(contactListFrequencyName);
177
178                    if(contactListFrequencyName == null || contactListFrequency != null) {
179                        var defaultPartyContactListStatusChoice = edit.getDefaultPartyContactListStatusChoice();
180                        var workflow = workflowControl.getWorkflowByName(PartyContactListStatusConstants.Workflow_PARTY_CONTACT_LIST_STATUS);
181
182                        defaultPartyContactListStatus = workflowControl.getWorkflowEntranceByName(workflow, defaultPartyContactListStatusChoice);
183
184                        if(defaultPartyContactListStatus == null) {
185                            addExecutionError(ExecutionErrors.UnknownDefaultPartyContactListStatusChoice.name(), PartyContactListStatusConstants.Workflow_PARTY_CONTACT_LIST_STATUS,
186                                    defaultPartyContactListStatusChoice);
187                        }
188                    } else {
189                        addExecutionError(ExecutionErrors.UnknownContactListFrequencyName.name(), contactListFrequencyName);
190                    }
191                } else {
192                    addExecutionError(ExecutionErrors.UnknownContactListTypeName.name(), contactListTypeName);
193                }
194            } else {
195                addExecutionError(ExecutionErrors.UnknownContactListGroupName.name(), contactListGroupName);
196            }
197        }
198    }
199
200    @Override
201    public void doUpdate(ContactList contactList) {
202        var partyPK = getPartyPK();
203        var contactListDetailValue = contactListControl.getContactListDetailValueForUpdate(contactList);
204        var contactListDescription = contactListControl.getContactListDescriptionForUpdate(contactList, getPreferredLanguage());
205        var description = edit.getDescription();
206
207        contactListDetailValue.setContactListName(edit.getContactListName());
208        contactListDetailValue.setContactListGroupPK(contactListGroup.getPrimaryKey());
209        contactListDetailValue.setContactListTypePK(contactListType.getPrimaryKey());
210        contactListDetailValue.setContactListFrequencyPK(contactListFrequency == null ? null : contactListFrequency.getPrimaryKey());
211        contactListDetailValue.setDefaultPartyContactListStatusPK(defaultPartyContactListStatus.getPrimaryKey());
212        contactListDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
213        contactListDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
214
215        contactListControl.updateContactListFromValue(contactListDetailValue, partyPK);
216
217        if(contactListDescription == null && description != null) {
218            contactListControl.createContactListDescription(contactList, getPreferredLanguage(), description, partyPK);
219        } else if(contactListDescription != null && description == null) {
220            contactListControl.deleteContactListDescription(contactListDescription, partyPK);
221        } else if(contactListDescription != null && description != null) {
222            var contactListDescriptionValue = contactListControl.getContactListDescriptionValue(contactListDescription);
223
224            contactListDescriptionValue.setDescription(description);
225            contactListControl.updateContactListDescriptionFromValue(contactListDescriptionValue, partyPK);
226        }
227    }
228
229}