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.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.user.common.pk.UserVisitPK;
032import com.echothree.util.common.command.EditMode;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseAbstractEditCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.List;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class EditAllocationPriorityCommand
046        extends BaseAbstractEditCommand<AllocationPriorityUniversalSpec, AllocationPriorityEdit, EditAllocationPriorityResult, AllocationPriority, AllocationPriority> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051    
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                    new SecurityRoleDefinition(SecurityRoleGroups.AllocationPriority.name(), SecurityRoles.Edit.name())
057                    ))
058                ));
059        
060        SPEC_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("AllocationPriorityName", FieldType.ENTITY_NAME, true, null, null)
062                );
063        
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("AllocationPriorityName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("Priority", FieldType.UNSIGNED_INTEGER, 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 EditAllocationPriorityCommand */
074    public EditAllocationPriorityCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077
078    @Override
079    public EditAllocationPriorityResult getResult() {
080        return InventoryResultFactory.getEditAllocationPriorityResult();
081    }
082
083    @Override
084    public AllocationPriorityEdit getEdit() {
085        return InventoryEditFactory.getAllocationPriorityEdit();
086    }
087
088    @Override
089    public AllocationPriority getEntity(EditAllocationPriorityResult result) {
090        AllocationPriority allocationPriority;
091
092        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
093            allocationPriority = AllocationPriorityLogic.getInstance().getAllocationPriorityByUniversalSpec(this, spec, false);
094        } else { // EditMode.UPDATE
095            allocationPriority = AllocationPriorityLogic.getInstance().getAllocationPriorityByUniversalSpecForUpdate(this, spec, false);
096        }
097
098        if(!hasExecutionErrors()) {
099            var inventoryControl = Session.getModelController(InventoryControl.class);
100
101            result.setAllocationPriority(inventoryControl.getAllocationPriorityTransfer(getUserVisit(), allocationPriority));
102        }
103
104        return allocationPriority;
105    }
106
107    @Override
108    public AllocationPriority getLockEntity(AllocationPriority allocationPriority) {
109        return allocationPriority;
110    }
111
112    @Override
113    public void fillInResult(EditAllocationPriorityResult result, AllocationPriority allocationPriority) {
114        var inventoryControl = Session.getModelController(InventoryControl.class);
115
116        result.setAllocationPriority(inventoryControl.getAllocationPriorityTransfer(getUserVisit(), allocationPriority));
117    }
118
119    @Override
120    public void doLock(AllocationPriorityEdit edit, AllocationPriority allocationPriority) {
121        var inventoryControl = Session.getModelController(InventoryControl.class);
122        var allocationPriorityDescription = inventoryControl.getAllocationPriorityDescription(allocationPriority, getPreferredLanguage());
123        var allocationPriorityDetail = allocationPriority.getLastDetail();
124
125        edit.setAllocationPriorityName(allocationPriorityDetail.getAllocationPriorityName());
126        edit.setPriority(allocationPriorityDetail.getPriority().toString());
127        edit.setIsDefault(allocationPriorityDetail.getIsDefault().toString());
128        edit.setSortOrder(allocationPriorityDetail.getSortOrder().toString());
129
130        if(allocationPriorityDescription != null) {
131            edit.setDescription(allocationPriorityDescription.getDescription());
132        }
133    }
134
135    @Override
136    public void canUpdate(AllocationPriority allocationPriority) {
137        var inventoryControl = Session.getModelController(InventoryControl.class);
138        var allocationPriorityName = edit.getAllocationPriorityName();
139        var duplicateAllocationPriority = inventoryControl.getAllocationPriorityByName(allocationPriorityName);
140
141        if(duplicateAllocationPriority != null && !allocationPriority.equals(duplicateAllocationPriority)) {
142            addExecutionError(ExecutionErrors.DuplicateAllocationPriorityName.name(), allocationPriorityName);
143        }
144    }
145
146    @Override
147    public void doUpdate(AllocationPriority allocationPriority) {
148        var inventoryControl = Session.getModelController(InventoryControl.class);
149        var partyPK = getPartyPK();
150        var allocationPriorityDetailValue = inventoryControl.getAllocationPriorityDetailValueForUpdate(allocationPriority);
151        var allocationPriorityDescription = inventoryControl.getAllocationPriorityDescriptionForUpdate(allocationPriority, getPreferredLanguage());
152        var description = edit.getDescription();
153
154        allocationPriorityDetailValue.setAllocationPriorityName(edit.getAllocationPriorityName());
155        allocationPriorityDetailValue.setPriority(Integer.valueOf(edit.getPriority()));
156        allocationPriorityDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
157        allocationPriorityDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
158
159        AllocationPriorityLogic.getInstance().updateAllocationPriorityFromValue(allocationPriorityDetailValue, partyPK);
160
161        if(allocationPriorityDescription == null && description != null) {
162            inventoryControl.createAllocationPriorityDescription(allocationPriority, getPreferredLanguage(), description, partyPK);
163        } else {
164            if(allocationPriorityDescription != null && description == null) {
165                inventoryControl.deleteAllocationPriorityDescription(allocationPriorityDescription, partyPK);
166            } else {
167                if(allocationPriorityDescription != null && description != null) {
168                    var allocationPriorityDescriptionValue = inventoryControl.getAllocationPriorityDescriptionValue(allocationPriorityDescription);
169
170                    allocationPriorityDescriptionValue.setDescription(description);
171                    inventoryControl.updateAllocationPriorityDescriptionFromValue(allocationPriorityDescriptionValue, partyPK);
172                }
173            }
174        }
175    }
176
177}