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