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.InventoryEditFactory; 020import com.echothree.control.user.inventory.common.edit.InventoryConditionEdit; 021import com.echothree.control.user.inventory.common.form.EditInventoryConditionForm; 022import com.echothree.control.user.inventory.common.result.InventoryResultFactory; 023import com.echothree.control.user.inventory.common.result.EditInventoryConditionResult; 024import com.echothree.control.user.inventory.common.spec.InventoryConditionUniversalSpec; 025import com.echothree.model.control.inventory.server.control.InventoryControl; 026import com.echothree.model.control.inventory.server.logic.InventoryConditionLogic; 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.InventoryCondition; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 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.List; 041import javax.enterprise.context.Dependent; 042 043@Dependent 044public class EditInventoryConditionCommand 045 extends BaseAbstractEditCommand<InventoryConditionUniversalSpec, InventoryConditionEdit, EditInventoryConditionResult, InventoryCondition, InventoryCondition> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 049 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.InventoryCondition.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 062 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 063 ); 064 065 EDIT_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("InventoryConditionName", 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 EditInventoryConditionCommand */ 074 public EditInventoryConditionCommand() { 075 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 public EditInventoryConditionResult getResult() { 080 return InventoryResultFactory.getEditInventoryConditionResult(); 081 } 082 083 @Override 084 public InventoryConditionEdit getEdit() { 085 return InventoryEditFactory.getInventoryConditionEdit(); 086 } 087 088 @Override 089 public InventoryCondition getEntity(EditInventoryConditionResult result) { 090 return InventoryConditionLogic.getInstance().getInventoryConditionByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode)); 091 } 092 093 @Override 094 public InventoryCondition getLockEntity(InventoryCondition inventoryCondition) { 095 return inventoryCondition; 096 } 097 098 @Override 099 public void fillInResult(EditInventoryConditionResult result, InventoryCondition inventoryCondition) { 100 var inventoryControl = Session.getModelController(InventoryControl.class); 101 102 result.setInventoryCondition(inventoryControl.getInventoryConditionTransfer(getUserVisit(), inventoryCondition)); 103 } 104 105 @Override 106 public void doLock(InventoryConditionEdit edit, InventoryCondition inventoryCondition) { 107 var inventoryControl = Session.getModelController(InventoryControl.class); 108 var inventoryConditionDescription = inventoryControl.getInventoryConditionDescription(inventoryCondition, getPreferredLanguage()); 109 var inventoryConditionDetail = inventoryCondition.getLastDetail(); 110 111 edit.setInventoryConditionName(inventoryConditionDetail.getInventoryConditionName()); 112 edit.setIsDefault(inventoryConditionDetail.getIsDefault().toString()); 113 edit.setSortOrder(inventoryConditionDetail.getSortOrder().toString()); 114 115 if(inventoryConditionDescription != null) { 116 edit.setDescription(inventoryConditionDescription.getDescription()); 117 } 118 } 119 120 @Override 121 public void canUpdate(InventoryCondition inventoryCondition) { 122 var inventoryControl = Session.getModelController(InventoryControl.class); 123 var inventoryConditionName = edit.getInventoryConditionName(); 124 var duplicateInventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName); 125 126 if(duplicateInventoryCondition != null && !inventoryCondition.equals(duplicateInventoryCondition)) { 127 addExecutionError(ExecutionErrors.DuplicateInventoryConditionName.name(), inventoryConditionName); 128 } 129 } 130 131 @Override 132 public void doUpdate(InventoryCondition inventoryCondition) { 133 var inventoryControl = Session.getModelController(InventoryControl.class); 134 var partyPK = getPartyPK(); 135 var inventoryConditionDetailValue = inventoryControl.getInventoryConditionDetailValueForUpdate(inventoryCondition); 136 var inventoryConditionDescription = inventoryControl.getInventoryConditionDescriptionForUpdate(inventoryCondition, getPreferredLanguage()); 137 var description = edit.getDescription(); 138 139 inventoryConditionDetailValue.setInventoryConditionName(edit.getInventoryConditionName()); 140 inventoryConditionDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 141 inventoryConditionDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 142 143 inventoryControl.updateInventoryConditionFromValue(inventoryConditionDetailValue, partyPK); 144 145 if(inventoryConditionDescription == null && description != null) { 146 inventoryControl.createInventoryConditionDescription(inventoryCondition, getPreferredLanguage(), description, partyPK); 147 } else if(inventoryConditionDescription != null && description == null) { 148 inventoryControl.deleteInventoryConditionDescription(inventoryConditionDescription, partyPK); 149 } else if(inventoryConditionDescription != null && description != null) { 150 var inventoryConditionDescriptionValue = inventoryControl.getInventoryConditionDescriptionValue(inventoryConditionDescription); 151 152 inventoryConditionDescriptionValue.setDescription(description); 153 inventoryControl.updateInventoryConditionDescriptionFromValue(inventoryConditionDescriptionValue, partyPK); 154 } 155 } 156 157}