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.warehouse.server.command; 018 019import com.echothree.control.user.warehouse.common.edit.LocationNameElementEdit; 020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory; 021import com.echothree.control.user.warehouse.common.form.EditLocationNameElementForm; 022import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory; 023import com.echothree.control.user.warehouse.common.spec.LocationNameElementSpec; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.control.warehouse.server.control.WarehouseControl; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.command.BaseResult; 030import com.echothree.util.common.command.EditMode; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.server.control.BaseEditCommand; 035import com.echothree.util.server.control.CommandSecurityDefinition; 036import com.echothree.util.server.control.PartyTypeDefinition; 037import com.echothree.util.server.control.SecurityRoleDefinition; 038import com.echothree.util.server.persistence.Session; 039import java.util.Arrays; 040import java.util.Collections; 041import java.util.List; 042import javax.enterprise.context.RequestScoped; 043 044@RequestScoped 045public class EditLocationNameElementCommand 046 extends BaseEditCommand<LocationNameElementSpec, LocationNameElementEdit> { 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.LocationNameElement.name(), SecurityRoles.Edit.name()) 057 )) 058 )); 059 060 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 061 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("LocationTypeName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("LocationNameElementName", FieldType.ENTITY_NAME, true, null, null) 064 )); 065 066 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 067 new FieldDefinition("LocationNameElementName", FieldType.ENTITY_NAME, true, null, null), 068 new FieldDefinition("Offset", FieldType.UNSIGNED_INTEGER, true, null, null), 069 new FieldDefinition("Length", FieldType.UNSIGNED_INTEGER, true, null, null), 070 new FieldDefinition("ValidationPattern", FieldType.REGULAR_EXPRESSION, false, null, null), 071 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 072 )); 073 } 074 075 /** Creates a new instance of EditLocationNameElementCommand */ 076 public EditLocationNameElementCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 protected BaseResult execute() { 082 var warehouseControl = Session.getModelController(WarehouseControl.class); 083 var result = WarehouseResultFactory.getEditLocationNameElementResult(); 084 var warehouseName = spec.getWarehouseName(); 085 var warehouse = warehouseControl.getWarehouseByName(warehouseName); 086 087 if(warehouse != null) { 088 var warehouseParty = warehouse.getParty(); 089 var locationTypeName = spec.getLocationTypeName(); 090 var locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName); 091 092 if(locationType != null) { 093 if(editMode.equals(EditMode.LOCK)) { 094 var locationNameElementName = spec.getLocationNameElementName(); 095 var locationNameElement = warehouseControl.getLocationNameElementByName(locationType, locationNameElementName); 096 097 if(locationNameElement != null) { 098 result.setLocationNameElement(warehouseControl.getLocationNameElementTransfer(getUserVisit(), locationNameElement)); 099 100 if(lockEntity(locationNameElement)) { 101 var locationNameElementDescription = warehouseControl.getLocationNameElementDescription(locationNameElement, getPreferredLanguage()); 102 var edit = WarehouseEditFactory.getLocationNameElementEdit(); 103 var locationNameElementDetail = locationNameElement.getLastDetail(); 104 105 result.setEdit(edit); 106 edit.setLocationNameElementName(locationNameElementDetail.getLocationNameElementName()); 107 edit.setOffset(locationNameElementDetail.getOffset().toString()); 108 edit.setLength(locationNameElementDetail.getLength().toString()); 109 edit.setValidationPattern(locationNameElementDetail.getValidationPattern()); 110 111 if(locationNameElementDescription != null) 112 edit.setDescription(locationNameElementDescription.getDescription()); 113 } else { 114 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 115 } 116 117 result.setEntityLock(getEntityLockTransfer(locationNameElement)); 118 } else { 119 addExecutionError(ExecutionErrors.UnknownLocationNameElementName.name(), locationNameElementName); 120 } 121 } else if(editMode.equals(EditMode.UPDATE)) { 122 var locationNameElementName = spec.getLocationNameElementName(); 123 var locationNameElement = warehouseControl.getLocationNameElementByNameForUpdate(locationType, locationNameElementName); 124 125 if(locationNameElement != null) { 126 locationNameElementName = edit.getLocationNameElementName(); 127 var duplicateLocationNameElement = warehouseControl.getLocationNameElementByName(locationType, locationNameElementName); 128 129 if(duplicateLocationNameElement == null || locationNameElement.equals(duplicateLocationNameElement)) { 130 if(lockEntityForUpdate(locationNameElement)) { 131 try { 132 var partyPK = getPartyPK(); 133 var locationNameElementDetailValue = warehouseControl.getLocationNameElementDetailValueForUpdate(locationNameElement); 134 var locationNameElementDescription = warehouseControl.getLocationNameElementDescriptionForUpdate(locationNameElement, getPreferredLanguage()); 135 var description = edit.getDescription(); 136 137 locationNameElementDetailValue.setLocationNameElementName(edit.getLocationNameElementName()); 138 locationNameElementDetailValue.setOffset(Integer.valueOf(edit.getOffset())); 139 locationNameElementDetailValue.setLength(Integer.valueOf(edit.getLength())); 140 locationNameElementDetailValue.setValidationPattern(edit.getValidationPattern()); 141 142 warehouseControl.updateLocationNameElementFromValue(locationNameElementDetailValue, partyPK); 143 144 if(locationNameElementDescription == null && description != null) { 145 warehouseControl.createLocationNameElementDescription(locationNameElement, getPreferredLanguage(), description, partyPK); 146 } else if(locationNameElementDescription != null && description == null) { 147 warehouseControl.deleteLocationNameElementDescription(locationNameElementDescription, partyPK); 148 } else if(locationNameElementDescription != null && description != null) { 149 var locationNameElementDescriptionValue = warehouseControl.getLocationNameElementDescriptionValue(locationNameElementDescription); 150 151 locationNameElementDescriptionValue.setDescription(description); 152 warehouseControl.updateLocationNameElementDescriptionFromValue(locationNameElementDescriptionValue, partyPK); 153 } 154 } finally { 155 unlockEntity(locationNameElement); 156 } 157 } else { 158 addExecutionError(ExecutionErrors.EntityLockStale.name()); 159 } 160 } else { 161 addExecutionError(ExecutionErrors.DuplicateLocationNameElementName.name(), locationNameElementName); 162 } 163 } else { 164 addExecutionError(ExecutionErrors.UnknownLocationNameElementName.name(), locationNameElementName); 165 } 166 } 167 } else { 168 addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), locationTypeName); 169 } 170 } else { 171 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 172 } 173 174 return result; 175 } 176 177}