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