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.LocationNameElementDescriptionEdit; 020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory; 021import com.echothree.control.user.warehouse.common.form.EditLocationNameElementDescriptionForm; 022import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory; 023import com.echothree.control.user.warehouse.common.spec.LocationNameElementDescriptionSpec; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.party.server.control.PartyControl; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.control.warehouse.server.control.WarehouseControl; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.common.command.EditMode; 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.BaseEditCommand; 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.ArrayList; 041import java.util.Collections; 042import java.util.List; 043import javax.enterprise.context.RequestScoped; 044 045@RequestScoped 046public class EditLocationNameElementDescriptionCommand 047 extends BaseEditCommand<LocationNameElementDescriptionSpec, LocationNameElementDescriptionEdit> { 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(List.of( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 057 new SecurityRoleDefinition(SecurityRoleGroups.LocationNameElement.name(), SecurityRoles.Description.name()) 058 )) 059 )); 060 061 List<FieldDefinition> temp = new ArrayList<>(4); 062 temp.add(new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null)); 063 temp.add(new FieldDefinition("LocationTypeName", FieldType.ENTITY_NAME, true, null, null)); 064 temp.add(new FieldDefinition("LocationNameElementName", FieldType.ENTITY_NAME, true, null, null)); 065 temp.add(new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)); 066 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 067 068 temp = new ArrayList<>(1); 069 temp.add(new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)); 070 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 071 } 072 073 /** Creates a new instance of EditLocationNameElementDescriptionCommand */ 074 public EditLocationNameElementDescriptionCommand() { 075 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 076 } 077 078 @Override 079 protected BaseResult execute() { 080 var warehouseControl = Session.getModelController(WarehouseControl.class); 081 var result = WarehouseResultFactory.getEditLocationNameElementDescriptionResult(); 082 var warehouseName = spec.getWarehouseName(); 083 var warehouse = warehouseControl.getWarehouseByName(warehouseName); 084 085 if(warehouse != null) { 086 var warehouseParty = warehouse.getParty(); 087 var locationTypeName = spec.getLocationTypeName(); 088 var locationType = warehouseControl.getLocationTypeByName(warehouseParty, locationTypeName); 089 090 if(locationType != null) { 091 var locationNameElementName = spec.getLocationNameElementName(); 092 var locationNameElement = warehouseControl.getLocationNameElementByName(locationType, locationNameElementName); 093 094 if(locationNameElement != null) { 095 var partyControl = Session.getModelController(PartyControl.class); 096 var languageIsoName = spec.getLanguageIsoName(); 097 var language = partyControl.getLanguageByIsoName(languageIsoName); 098 099 if(language != null) { 100 if(editMode.equals(EditMode.LOCK)) { 101 var locationTypeDescription = warehouseControl.getLocationNameElementDescription(locationNameElement, language); 102 103 if(locationTypeDescription != null) { 104 result.setLocationNameElementDescription(warehouseControl.getLocationNameElementDescriptionTransfer(getUserVisit(), locationTypeDescription)); 105 106 if(lockEntity(locationType)) { 107 var edit = WarehouseEditFactory.getLocationNameElementDescriptionEdit(); 108 109 result.setEdit(edit); 110 edit.setDescription(locationTypeDescription.getDescription()); 111 } else { 112 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 113 } 114 115 result.setEntityLock(getEntityLockTransfer(locationType)); 116 } else { 117 addExecutionError(ExecutionErrors.UnknownLocationNameElementDescription.name()); 118 } 119 } else if(editMode.equals(EditMode.UPDATE)) { 120 var locationTypeDescriptionValue = warehouseControl.getLocationNameElementDescriptionValueForUpdate(locationNameElement, language); 121 122 if(locationTypeDescriptionValue != null) { 123 if(lockEntityForUpdate(locationType)) { 124 try { 125 var description = edit.getDescription(); 126 127 locationTypeDescriptionValue.setDescription(description); 128 129 warehouseControl.updateLocationNameElementDescriptionFromValue(locationTypeDescriptionValue, getPartyPK()); 130 } finally { 131 unlockEntity(locationType); 132 } 133 } else { 134 addExecutionError(ExecutionErrors.EntityLockStale.name()); 135 } 136 } else { 137 addExecutionError(ExecutionErrors.UnknownLocationNameElementDescription.name()); 138 } 139 } 140 } else { 141 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 142 } 143 } else { 144 addExecutionError(ExecutionErrors.UnknownLocationNameElementName.name(), locationNameElementName); 145 } 146 } else { 147 addExecutionError(ExecutionErrors.UnknownLocationTypeName.name(), locationTypeName); 148 } 149 } else { 150 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 151 } 152 153 return result; 154 } 155 156}