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