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.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 com.echothree.util.server.persistence.Session;
040import java.util.Arrays;
041import java.util.Collections;
042import java.util.List;
043import javax.enterprise.context.RequestScoped;
044
045@RequestScoped
046public class EditContactListFrequencyCommand
047        extends BaseAbstractEditCommand<ContactListFrequencySpec, ContactListFrequencyEdit, EditContactListFrequencyResult, ContactListFrequency, ContactListFrequency> {
048
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
051    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
052
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                        new SecurityRoleDefinition(SecurityRoleGroups.ContactListFrequency.name(), SecurityRoles.Edit.name())
058                        )))
059                )));
060
061        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("ContactListFrequencyName", FieldType.ENTITY_NAME, true, null, null)
063                ));
064
065        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("ContactListFrequencyName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
068                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
069                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
070                ));
071    }
072
073    /** Creates a new instance of EditContactListFrequencyCommand */
074    public EditContactListFrequencyCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077
078    @Override
079    public EditContactListFrequencyResult getResult() {
080        return ContactListResultFactory.getEditContactListFrequencyResult();
081    }
082
083    @Override
084    public ContactListFrequencyEdit getEdit() {
085        return ContactListEditFactory.getContactListFrequencyEdit();
086    }
087
088    @Override
089    public ContactListFrequency getEntity(EditContactListFrequencyResult result) {
090        var contactListControl = Session.getModelController(ContactListControl.class);
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        var contactListControl = Session.getModelController(ContactListControl.class);
115
116        result.setContactListFrequency(contactListControl.getContactListFrequencyTransfer(getUserVisit(), contactListFrequency));
117    }
118
119    @Override
120    public void doLock(ContactListFrequencyEdit edit, ContactListFrequency contactListFrequency) {
121        var contactListControl = Session.getModelController(ContactListControl.class);
122        var contactListFrequencyDescription = contactListControl.getContactListFrequencyDescription(contactListFrequency, getPreferredLanguage());
123        var contactListFrequencyDetail = contactListFrequency.getLastDetail();
124
125        edit.setContactListFrequencyName(contactListFrequencyDetail.getContactListFrequencyName());
126        edit.setIsDefault(contactListFrequencyDetail.getIsDefault().toString());
127        edit.setSortOrder(contactListFrequencyDetail.getSortOrder().toString());
128
129        if(contactListFrequencyDescription != null) {
130            edit.setDescription(contactListFrequencyDescription.getDescription());
131        }
132    }
133
134    @Override
135    public void canUpdate(ContactListFrequency contactListFrequency) {
136        var contactListControl = Session.getModelController(ContactListControl.class);
137        var contactListFrequencyName = edit.getContactListFrequencyName();
138        var duplicateContactListFrequency = contactListControl.getContactListFrequencyByName(contactListFrequencyName);
139
140        if(duplicateContactListFrequency != null && !contactListFrequency.equals(duplicateContactListFrequency)) {
141            addExecutionError(ExecutionErrors.DuplicateContactListFrequencyName.name(), contactListFrequencyName);
142        }
143    }
144
145    @Override
146    public void doUpdate(ContactListFrequency contactListFrequency) {
147        var contactListControl = Session.getModelController(ContactListControl.class);
148        var partyPK = getPartyPK();
149        var contactListFrequencyDetailValue = contactListControl.getContactListFrequencyDetailValueForUpdate(contactListFrequency);
150        var contactListFrequencyDescription = contactListControl.getContactListFrequencyDescriptionForUpdate(contactListFrequency, getPreferredLanguage());
151        var description = edit.getDescription();
152
153        contactListFrequencyDetailValue.setContactListFrequencyName(edit.getContactListFrequencyName());
154        contactListFrequencyDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
155        contactListFrequencyDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
156
157        contactListControl.updateContactListFrequencyFromValue(contactListFrequencyDetailValue, partyPK);
158
159        if(contactListFrequencyDescription == null && description != null) {
160            contactListControl.createContactListFrequencyDescription(contactListFrequency, getPreferredLanguage(), description, partyPK);
161        } else if(contactListFrequencyDescription != null && description == null) {
162            contactListControl.deleteContactListFrequencyDescription(contactListFrequencyDescription, partyPK);
163        } else if(contactListFrequencyDescription != null && description != null) {
164            var contactListFrequencyDescriptionValue = contactListControl.getContactListFrequencyDescriptionValue(contactListFrequencyDescription);
165
166            contactListFrequencyDescriptionValue.setDescription(description);
167            contactListControl.updateContactListFrequencyDescriptionFromValue(contactListFrequencyDescriptionValue, partyPK);
168        }
169    }
170
171}