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.ContactListEditFactory;
020import com.echothree.control.user.contactlist.common.edit.ContactListFrequencyEdit;
021import com.echothree.control.user.contactlist.common.form.EditContactListFrequencyForm;
022import com.echothree.control.user.contactlist.common.result.ContactListResultFactory;
023import com.echothree.control.user.contactlist.common.result.EditContactListFrequencyResult;
024import com.echothree.control.user.contactlist.common.spec.ContactListFrequencySpec;
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.data.contactlist.server.entity.ContactListFrequency;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.common.command.EditMode;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditContactListFrequencyCommand
045        extends BaseAbstractEditCommand<ContactListFrequencySpec, ContactListFrequencyEdit, EditContactListFrequencyResult, ContactListFrequency, ContactListFrequency> {
046
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.ContactListFrequency.name(), SecurityRoles.Edit.name())
056                ))
057        ));
058
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("ContactListFrequencyName", FieldType.ENTITY_NAME, true, null, null)
061        );
062
063        EDIT_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("ContactListFrequencyName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
066                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
067                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
068        );
069    }
070
071    @Inject
072    ContactListControl contactListControl;
073
074    /** Creates a new instance of EditContactListFrequencyCommand */
075    public EditContactListFrequencyCommand() {
076        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
077    }
078
079    @Override
080    public EditContactListFrequencyResult getResult() {
081        return ContactListResultFactory.getEditContactListFrequencyResult();
082    }
083
084    @Override
085    public ContactListFrequencyEdit getEdit() {
086        return ContactListEditFactory.getContactListFrequencyEdit();
087    }
088
089    @Override
090    public ContactListFrequency getEntity(EditContactListFrequencyResult result) {
091        ContactListFrequency contactListFrequency;
092        var contactListFrequencyName = spec.getContactListFrequencyName();
093
094        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
095            contactListFrequency = contactListControl.getContactListFrequencyByName(contactListFrequencyName);
096        } else { // EditMode.UPDATE
097            contactListFrequency = contactListControl.getContactListFrequencyByNameForUpdate(contactListFrequencyName);
098        }
099
100        if(contactListFrequency == null) {
101            addExecutionError(ExecutionErrors.UnknownContactListFrequencyName.name(), contactListFrequencyName);
102        }
103
104        return contactListFrequency;
105    }
106
107    @Override
108    public ContactListFrequency getLockEntity(ContactListFrequency contactListFrequency) {
109        return contactListFrequency;
110    }
111
112    @Override
113    public void fillInResult(EditContactListFrequencyResult result, ContactListFrequency contactListFrequency) {
114        result.setContactListFrequency(contactListControl.getContactListFrequencyTransfer(getUserVisit(), contactListFrequency));
115    }
116
117    @Override
118    public void doLock(ContactListFrequencyEdit edit, ContactListFrequency contactListFrequency) {
119        var contactListFrequencyDescription = contactListControl.getContactListFrequencyDescription(contactListFrequency, getPreferredLanguage());
120        var contactListFrequencyDetail = contactListFrequency.getLastDetail();
121
122        edit.setContactListFrequencyName(contactListFrequencyDetail.getContactListFrequencyName());
123        edit.setIsDefault(contactListFrequencyDetail.getIsDefault().toString());
124        edit.setSortOrder(contactListFrequencyDetail.getSortOrder().toString());
125
126        if(contactListFrequencyDescription != null) {
127            edit.setDescription(contactListFrequencyDescription.getDescription());
128        }
129    }
130
131    @Override
132    public void canUpdate(ContactListFrequency contactListFrequency) {
133        var contactListFrequencyName = edit.getContactListFrequencyName();
134        var duplicateContactListFrequency = contactListControl.getContactListFrequencyByName(contactListFrequencyName);
135
136        if(duplicateContactListFrequency != null && !contactListFrequency.equals(duplicateContactListFrequency)) {
137            addExecutionError(ExecutionErrors.DuplicateContactListFrequencyName.name(), contactListFrequencyName);
138        }
139    }
140
141    @Override
142    public void doUpdate(ContactListFrequency contactListFrequency) {
143        var partyPK = getPartyPK();
144        var contactListFrequencyDetailValue = contactListControl.getContactListFrequencyDetailValueForUpdate(contactListFrequency);
145        var contactListFrequencyDescription = contactListControl.getContactListFrequencyDescriptionForUpdate(contactListFrequency, getPreferredLanguage());
146        var description = edit.getDescription();
147
148        contactListFrequencyDetailValue.setContactListFrequencyName(edit.getContactListFrequencyName());
149        contactListFrequencyDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
150        contactListFrequencyDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
151
152        contactListControl.updateContactListFrequencyFromValue(contactListFrequencyDetailValue, partyPK);
153
154        if(contactListFrequencyDescription == null && description != null) {
155            contactListControl.createContactListFrequencyDescription(contactListFrequency, getPreferredLanguage(), description, partyPK);
156        } else if(contactListFrequencyDescription != null && description == null) {
157            contactListControl.deleteContactListFrequencyDescription(contactListFrequencyDescription, partyPK);
158        } else if(contactListFrequencyDescription != null && description != null) {
159            var contactListFrequencyDescriptionValue = contactListControl.getContactListFrequencyDescriptionValue(contactListFrequencyDescription);
160
161            contactListFrequencyDescriptionValue.setDescription(description);
162            contactListControl.updateContactListFrequencyDescriptionFromValue(contactListFrequencyDescriptionValue, partyPK);
163        }
164    }
165
166}