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.LocationDescriptionEdit; 020import com.echothree.control.user.warehouse.common.edit.WarehouseEditFactory; 021import com.echothree.control.user.warehouse.common.form.EditLocationDescriptionForm; 022import com.echothree.control.user.warehouse.common.result.WarehouseResultFactory; 023import com.echothree.control.user.warehouse.common.spec.LocationDescriptionSpec; 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 EditLocationDescriptionCommand 047 extends BaseEditCommand<LocationDescriptionSpec, LocationDescriptionEdit> { 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.Location.name(), SecurityRoles.Description.name()) 058 )) 059 )); 060 061 List<FieldDefinition> temp = new ArrayList<>(3); 062 temp.add(new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null)); 063 temp.add(new FieldDefinition("LocationName", FieldType.ENTITY_NAME, true, null, null)); 064 temp.add(new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)); 065 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 066 067 temp = new ArrayList<>(1); 068 temp.add(new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)); 069 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 070 } 071 072 /** Creates a new instance of EditLocationDescriptionCommand */ 073 public EditLocationDescriptionCommand() { 074 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 075 } 076 077 @Override 078 protected BaseResult execute() { 079 var warehouseControl = Session.getModelController(WarehouseControl.class); 080 var result = WarehouseResultFactory.getEditLocationDescriptionResult(); 081 var warehouseName = spec.getWarehouseName(); 082 var warehouse = warehouseControl.getWarehouseByName(warehouseName); 083 084 if(warehouse != null) { 085 var warehouseParty = warehouse.getParty(); 086 var locationName = spec.getLocationName(); 087 var location = warehouseControl.getLocationByName(warehouseParty, locationName); 088 089 if(location != null) { 090 var partyControl = Session.getModelController(PartyControl.class); 091 var languageIsoName = spec.getLanguageIsoName(); 092 var language = partyControl.getLanguageByIsoName(languageIsoName); 093 094 if(language != null) { 095 if(editMode.equals(EditMode.LOCK)) { 096 var locationDescription = warehouseControl.getLocationDescription(location, language); 097 098 if(locationDescription != null) { 099 result.setLocationDescription(warehouseControl.getLocationDescriptionTransfer(getUserVisit(), locationDescription)); 100 101 if(lockEntity(location)) { 102 var edit = WarehouseEditFactory.getLocationDescriptionEdit(); 103 104 result.setEdit(edit); 105 edit.setDescription(locationDescription.getDescription()); 106 } else { 107 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 108 } 109 110 result.setEntityLock(getEntityLockTransfer(location)); 111 } else { 112 addExecutionError(ExecutionErrors.UnknownLocationDescription.name()); 113 } 114 } else if(editMode.equals(EditMode.UPDATE)) { 115 var locationDescriptionValue = warehouseControl.getLocationDescriptionValueForUpdate(location, language); 116 117 if(locationDescriptionValue != null) { 118 if(lockEntityForUpdate(location)) { 119 try { 120 var description = edit.getDescription(); 121 122 locationDescriptionValue.setDescription(description); 123 124 warehouseControl.updateLocationDescriptionFromValue(locationDescriptionValue, getPartyPK()); 125 } finally { 126 unlockEntity(location); 127 } 128 } else { 129 addExecutionError(ExecutionErrors.EntityLockStale.name()); 130 } 131 } else { 132 addExecutionError(ExecutionErrors.UnknownLocationDescription.name()); 133 } 134 } 135 } else { 136 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 137 } 138 } else { 139 addExecutionError(ExecutionErrors.UnknownLocationName.name(), locationName); 140 } 141 } else { 142 addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName); 143 } 144 145 return result; 146 } 147 148}