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