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.inventory.server.command;
018
019import com.echothree.control.user.inventory.common.edit.AllocationPriorityEdit;
020import com.echothree.control.user.inventory.common.edit.InventoryEditFactory;
021import com.echothree.control.user.inventory.common.form.EditAllocationPriorityForm;
022import com.echothree.control.user.inventory.common.result.EditAllocationPriorityResult;
023import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
024import com.echothree.control.user.inventory.common.spec.AllocationPriorityUniversalSpec;
025import com.echothree.model.control.inventory.server.control.InventoryControl;
026import com.echothree.model.control.inventory.server.logic.AllocationPriorityLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.inventory.server.entity.AllocationPriority;
031import com.echothree.model.data.inventory.server.entity.AllocationPriorityDescription;
032import com.echothree.model.data.inventory.server.entity.AllocationPriorityDetail;
033import com.echothree.model.data.inventory.server.value.AllocationPriorityDescriptionValue;
034import com.echothree.model.data.inventory.server.value.AllocationPriorityDetailValue;
035import com.echothree.model.data.user.common.pk.UserVisitPK;
036import com.echothree.util.common.command.EditMode;
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.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 EditAllocationPriorityCommand
050        extends BaseAbstractEditCommand<AllocationPriorityUniversalSpec, AllocationPriorityEdit, EditAllocationPriorityResult, AllocationPriority, AllocationPriority> {
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.AllocationPriority.name(), SecurityRoles.Edit.name())
061                    )))
062                )));
063        
064        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
065                new FieldDefinition("AllocationPriorityName", FieldType.ENTITY_NAME, true, null, null)
066                ));
067        
068        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
069                new FieldDefinition("AllocationPriorityName", FieldType.ENTITY_NAME, true, null, null),
070                new FieldDefinition("Priority", FieldType.UNSIGNED_INTEGER, true, null, null),
071                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
072                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
073                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
074                ));
075    }
076    
077    /** Creates a new instance of EditAllocationPriorityCommand */
078    public EditAllocationPriorityCommand(UserVisitPK userVisitPK, EditAllocationPriorityForm form) {
079        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
080    }
081
082    @Override
083    public EditAllocationPriorityResult getResult() {
084        return InventoryResultFactory.getEditAllocationPriorityResult();
085    }
086
087    @Override
088    public AllocationPriorityEdit getEdit() {
089        return InventoryEditFactory.getAllocationPriorityEdit();
090    }
091
092    @Override
093    public AllocationPriority getEntity(EditAllocationPriorityResult result) {
094        AllocationPriority allocationPriority;
095
096        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
097            allocationPriority = AllocationPriorityLogic.getInstance().getAllocationPriorityByUniversalSpec(this, spec, false);
098        } else { // EditMode.UPDATE
099            allocationPriority = AllocationPriorityLogic.getInstance().getAllocationPriorityByUniversalSpecForUpdate(this, spec, false);
100        }
101
102        if(!hasExecutionErrors()) {
103            var inventoryControl = Session.getModelController(InventoryControl.class);
104
105            result.setAllocationPriority(inventoryControl.getAllocationPriorityTransfer(getUserVisit(), allocationPriority));
106        }
107
108        return allocationPriority;
109    }
110
111    @Override
112    public AllocationPriority getLockEntity(AllocationPriority allocationPriority) {
113        return allocationPriority;
114    }
115
116    @Override
117    public void fillInResult(EditAllocationPriorityResult result, AllocationPriority allocationPriority) {
118        var inventoryControl = Session.getModelController(InventoryControl.class);
119
120        result.setAllocationPriority(inventoryControl.getAllocationPriorityTransfer(getUserVisit(), allocationPriority));
121    }
122
123    @Override
124    public void doLock(AllocationPriorityEdit edit, AllocationPriority allocationPriority) {
125        var inventoryControl = Session.getModelController(InventoryControl.class);
126        AllocationPriorityDescription allocationPriorityDescription = inventoryControl.getAllocationPriorityDescription(allocationPriority, getPreferredLanguage());
127        AllocationPriorityDetail allocationPriorityDetail = allocationPriority.getLastDetail();
128
129        edit.setAllocationPriorityName(allocationPriorityDetail.getAllocationPriorityName());
130        edit.setPriority(allocationPriorityDetail.getPriority().toString());
131        edit.setIsDefault(allocationPriorityDetail.getIsDefault().toString());
132        edit.setSortOrder(allocationPriorityDetail.getSortOrder().toString());
133
134        if(allocationPriorityDescription != null) {
135            edit.setDescription(allocationPriorityDescription.getDescription());
136        }
137    }
138
139    @Override
140    public void canUpdate(AllocationPriority allocationPriority) {
141        var inventoryControl = Session.getModelController(InventoryControl.class);
142        String allocationPriorityName = edit.getAllocationPriorityName();
143        AllocationPriority duplicateAllocationPriority = inventoryControl.getAllocationPriorityByName(allocationPriorityName);
144
145        if(duplicateAllocationPriority != null && !allocationPriority.equals(duplicateAllocationPriority)) {
146            addExecutionError(ExecutionErrors.DuplicateAllocationPriorityName.name(), allocationPriorityName);
147        }
148    }
149
150    @Override
151    public void doUpdate(AllocationPriority allocationPriority) {
152        var inventoryControl = Session.getModelController(InventoryControl.class);
153        var partyPK = getPartyPK();
154        AllocationPriorityDetailValue allocationPriorityDetailValue = inventoryControl.getAllocationPriorityDetailValueForUpdate(allocationPriority);
155        AllocationPriorityDescription allocationPriorityDescription = inventoryControl.getAllocationPriorityDescriptionForUpdate(allocationPriority, getPreferredLanguage());
156        String description = edit.getDescription();
157
158        allocationPriorityDetailValue.setAllocationPriorityName(edit.getAllocationPriorityName());
159        allocationPriorityDetailValue.setPriority(Integer.valueOf(edit.getPriority()));
160        allocationPriorityDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
161        allocationPriorityDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
162
163        AllocationPriorityLogic.getInstance().updateAllocationPriorityFromValue(allocationPriorityDetailValue, partyPK);
164
165        if(allocationPriorityDescription == null && description != null) {
166            inventoryControl.createAllocationPriorityDescription(allocationPriority, getPreferredLanguage(), description, partyPK);
167        } else {
168            if(allocationPriorityDescription != null && description == null) {
169                inventoryControl.deleteAllocationPriorityDescription(allocationPriorityDescription, partyPK);
170            } else {
171                if(allocationPriorityDescription != null && description != null) {
172                    AllocationPriorityDescriptionValue allocationPriorityDescriptionValue = inventoryControl.getAllocationPriorityDescriptionValue(allocationPriorityDescription);
173
174                    allocationPriorityDescriptionValue.setDescription(description);
175                    inventoryControl.updateAllocationPriorityDescriptionFromValue(allocationPriorityDescriptionValue, partyPK);
176                }
177            }
178        }
179    }
180
181}