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.shipment.server.command; 018 019import com.echothree.control.user.shipment.common.edit.ShipmentAliasEdit; 020import com.echothree.control.user.shipment.common.edit.ShipmentEditFactory; 021import com.echothree.control.user.shipment.common.result.EditShipmentAliasResult; 022import com.echothree.control.user.shipment.common.result.ShipmentResultFactory; 023import com.echothree.control.user.shipment.common.spec.ShipmentAliasSpec; 024import com.echothree.control.user.shipment.server.command.util.ShipmentAliasUtil; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.control.shipment.server.control.ShipmentControl; 028import com.echothree.model.data.shipment.server.entity.ShipmentAlias; 029import com.echothree.model.data.shipment.server.entity.ShipmentAliasType; 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.BaseAbstractEditCommand; 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.List; 040import javax.enterprise.context.Dependent; 041 042@Dependent 043public class EditShipmentAliasCommand 044 extends BaseAbstractEditCommand<ShipmentAliasSpec, ShipmentAliasEdit, EditShipmentAliasResult, ShipmentAlias, ShipmentAlias> { 045 046 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 047 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 048 049 static { 050 SPEC_FIELD_DEFINITIONS = List.of( 051 new FieldDefinition("ShipmentTypeName", FieldType.ENTITY_NAME, true, null, null), 052 new FieldDefinition("ShipmentName", FieldType.ENTITY_NAME, true, null, null), 053 new FieldDefinition("ShipmentAliasTypeName", FieldType.ENTITY_NAME, true, null, null) 054 ); 055 056 EDIT_FIELD_DEFINITIONS = List.of( 057 new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null) 058 ); 059 } 060 061 /** Creates a new instance of EditShipmentAliasCommand */ 062 public EditShipmentAliasCommand() { 063 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 064 } 065 066 @Override 067 protected CommandSecurityDefinition getCommandSecurityDefinition() { 068 return new CommandSecurityDefinition(List.of( 069 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 070 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 071 new SecurityRoleDefinition(ShipmentAliasUtil.getInstance().getSecurityRoleGroupNameByShipmentTypeSpec(spec == null ? null : spec), SecurityRoles.Edit.name()) 072 )) 073 )); 074 } 075 076 @Override 077 public EditShipmentAliasResult getResult() { 078 return ShipmentResultFactory.getEditShipmentAliasResult(); 079 } 080 081 @Override 082 public ShipmentAliasEdit getEdit() { 083 return ShipmentEditFactory.getShipmentAliasEdit(); 084 } 085 086 ShipmentAliasType shipmentAliasType; 087 088 @Override 089 public ShipmentAlias getEntity(EditShipmentAliasResult result) { 090 var shipmentControl = Session.getModelController(ShipmentControl.class); 091 ShipmentAlias shipmentAlias = null; 092 var shipmentTypeName = spec.getShipmentTypeName(); 093 var shipmentType = shipmentControl.getShipmentTypeByName(shipmentTypeName); 094 095 if(shipmentType != null) { 096 var shipmentName = spec.getShipmentName(); 097 var shipment = shipmentControl.getShipmentByName(shipmentType, shipmentName); 098 099 if(shipment != null) { 100 var shipmentAliasTypeName = spec.getShipmentAliasTypeName(); 101 102 shipmentAliasType = shipmentControl.getShipmentAliasTypeByName(shipmentType, shipmentAliasTypeName); 103 104 if(shipmentAliasType != null) { 105 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 106 shipmentAlias = shipmentControl.getShipmentAlias(shipment, shipmentAliasType); 107 } else { // EditMode.UPDATE 108 shipmentAlias = shipmentControl.getShipmentAliasForUpdate(shipment, shipmentAliasType); 109 } 110 111 if(shipmentAlias != null) { 112 result.setShipmentAlias(shipmentControl.getShipmentAliasTransfer(getUserVisit(), shipmentAlias)); 113 } else { 114 addExecutionError(ExecutionErrors.UnknownShipmentAlias.name(), shipmentTypeName, shipmentName, shipmentAliasTypeName); 115 } 116 } else { 117 addExecutionError(ExecutionErrors.UnknownShipmentAliasTypeName.name(), shipmentTypeName, shipmentAliasTypeName); 118 } 119 } else { 120 addExecutionError(ExecutionErrors.UnknownShipmentName.name(), shipmentTypeName, shipmentName); 121 } 122 } else { 123 addExecutionError(ExecutionErrors.UnknownShipmentTypeName.name(), shipmentTypeName); 124 } 125 126 return shipmentAlias; 127 } 128 129 @Override 130 public ShipmentAlias getLockEntity(ShipmentAlias shipmentAlias) { 131 return shipmentAlias; 132 } 133 134 @Override 135 public void fillInResult(EditShipmentAliasResult result, ShipmentAlias shipmentAlias) { 136 var shipmentControl = Session.getModelController(ShipmentControl.class); 137 138 result.setShipmentAlias(shipmentControl.getShipmentAliasTransfer(getUserVisit(), shipmentAlias)); 139 } 140 141 @Override 142 public void doLock(ShipmentAliasEdit edit, ShipmentAlias shipmentAlias) { 143 edit.setAlias(shipmentAlias.getAlias()); 144 } 145 146 @Override 147 public void canUpdate(ShipmentAlias shipmentAlias) { 148 var shipmentControl = Session.getModelController(ShipmentControl.class); 149 var alias = edit.getAlias(); 150 var duplicateShipmentAlias = shipmentControl.getShipmentAliasByAlias(shipmentAliasType, alias); 151 152 if(duplicateShipmentAlias != null && !shipmentAlias.equals(duplicateShipmentAlias)) { 153 var shipmentAliasTypeDetail = shipmentAlias.getShipmentAliasType().getLastDetail(); 154 155 addExecutionError(ExecutionErrors.DuplicateShipmentAlias.name(), shipmentAliasTypeDetail.getShipmentType().getLastDetail().getShipmentTypeName(), 156 shipmentAliasTypeDetail.getShipmentAliasTypeName(), alias); 157 } 158 } 159 160 @Override 161 public void doUpdate(ShipmentAlias shipmentAlias) { 162 var shipmentControl = Session.getModelController(ShipmentControl.class); 163 var shipmentAliasValue = shipmentControl.getShipmentAliasValue(shipmentAlias); 164 165 shipmentAliasValue.setAlias(edit.getAlias()); 166 167 shipmentControl.updateShipmentAliasFromValue(shipmentAliasValue, getPartyPK()); 168 } 169 170}