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.sales.server.command; 018 019import com.echothree.control.user.sales.common.edit.SalesEditFactory; 020import com.echothree.control.user.sales.common.edit.SalesOrderShipmentGroupEdit; 021import com.echothree.control.user.sales.common.form.EditSalesOrderShipmentGroupForm; 022import com.echothree.control.user.sales.common.result.EditSalesOrderShipmentGroupResult; 023import com.echothree.control.user.sales.common.result.SalesResultFactory; 024import com.echothree.control.user.sales.common.spec.SalesOrderShipmentGroupSpec; 025import com.echothree.model.control.contact.server.logic.ContactMechanismLogic; 026import com.echothree.model.control.contact.server.logic.PartyContactMechanismLogic; 027import com.echothree.model.control.order.common.OrderTypes; 028import com.echothree.model.control.order.server.control.OrderShipmentGroupControl; 029import com.echothree.model.control.order.server.logic.OrderLogic; 030import com.echothree.model.control.party.common.PartyTypes; 031import com.echothree.model.control.party.server.logic.PartyLogic; 032import com.echothree.model.control.sales.server.logic.SalesOrderLogic; 033import com.echothree.model.control.sales.server.logic.SalesOrderShipmentGroupLogic; 034import com.echothree.model.control.security.common.SecurityRoleGroups; 035import com.echothree.model.control.security.common.SecurityRoles; 036import com.echothree.model.control.shipping.server.logic.ShippingMethodLogic; 037import com.echothree.model.data.contact.server.entity.PartyContactMechanism; 038import com.echothree.model.data.order.server.entity.OrderShipmentGroup; 039import com.echothree.model.data.shipping.server.entity.ShippingMethod; 040import com.echothree.model.data.user.common.pk.UserVisitPK; 041import com.echothree.util.common.command.EditMode; 042import com.echothree.util.common.message.ExecutionErrors; 043import com.echothree.util.common.validation.FieldDefinition; 044import com.echothree.util.common.validation.FieldType; 045import com.echothree.util.server.control.BaseAbstractEditCommand; 046import com.echothree.util.server.control.CommandSecurityDefinition; 047import com.echothree.util.server.control.PartyTypeDefinition; 048import com.echothree.util.server.control.SecurityRoleDefinition; 049import java.util.List; 050import javax.enterprise.context.Dependent; 051import javax.inject.Inject; 052 053@Dependent 054public class EditSalesOrderShipmentGroupCommand 055 extends BaseAbstractEditCommand<SalesOrderShipmentGroupSpec, SalesOrderShipmentGroupEdit, EditSalesOrderShipmentGroupResult, OrderShipmentGroup, OrderShipmentGroup> { 056 057 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 058 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 059 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 060 061 static { 062 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 063 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 064 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 065 new SecurityRoleDefinition(SecurityRoleGroups.SalesOrderShipmentGroup.name(), SecurityRoles.Edit.name()) 066 )) 067 )); 068 069 SPEC_FIELD_DEFINITIONS = List.of( 070 new FieldDefinition("OrderName", FieldType.ENTITY_NAME, true, null, null), 071 new FieldDefinition("OrderShipmentGroupSequence", FieldType.UNSIGNED_INTEGER, true, null, null) 072 ); 073 074 EDIT_FIELD_DEFINITIONS = List.of( 075 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 076 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 077 new FieldDefinition("ContactMechanismName", FieldType.ENTITY_NAME, false, null, null), 078 new FieldDefinition("ShippingMethodName", FieldType.ENTITY_NAME, false, null, null), 079 new FieldDefinition("HoldUntilComplete", FieldType.BOOLEAN, true, null, null) 080 ); 081 } 082 083 @Inject 084 OrderShipmentGroupControl orderShipmentGroupControl; 085 086 @Inject 087 ContactMechanismLogic contactMechanismLogic; 088 089 @Inject 090 OrderLogic orderLogic; 091 092 @Inject 093 PartyContactMechanismLogic partyContactMechanismLogic; 094 095 @Inject 096 PartyLogic partyLogic; 097 098 @Inject 099 SalesOrderLogic salesOrderLogic; 100 101 @Inject 102 SalesOrderShipmentGroupLogic salesOrderShipmentGroupLogic; 103 104 @Inject 105 ShippingMethodLogic shippingMethodLogic; 106 107 /** Creates a new instance of EditSalesOrderShipmentGroupCommand */ 108 public EditSalesOrderShipmentGroupCommand() { 109 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 110 } 111 112 @Override 113 public EditSalesOrderShipmentGroupResult getResult() { 114 return SalesResultFactory.getEditSalesOrderShipmentGroupResult(); 115 } 116 117 @Override 118 public SalesOrderShipmentGroupEdit getEdit() { 119 return SalesEditFactory.getSalesOrderShipmentGroupEdit(); 120 } 121 122 @Override 123 public OrderShipmentGroup getEntity(EditSalesOrderShipmentGroupResult result) { 124 var orderName = spec.getOrderName(); 125 var order = orderLogic.getOrderByName(this, OrderTypes.SALES_ORDER.name(), orderName); 126 OrderShipmentGroup orderShipmentGroup = null; 127 128 if(!hasExecutionErrors()) { 129 var orderShipmentGroupSequence = Integer.valueOf(spec.getOrderShipmentGroupSequence()); 130 131 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 132 orderShipmentGroup = salesOrderShipmentGroupLogic.getOrderShipmentGroup(this, order, orderShipmentGroupSequence); 133 } else { // EditMode.UPDATE 134 orderShipmentGroup = salesOrderShipmentGroupLogic.getOrderShipmentGroupForUpdate(this, order, orderShipmentGroupSequence); 135 } 136 } 137 138 return orderShipmentGroup; 139 } 140 141 @Override 142 public OrderShipmentGroup getLockEntity(OrderShipmentGroup orderShipmentGroup) { 143 return orderShipmentGroup; 144 } 145 146 @Override 147 public void fillInResult(EditSalesOrderShipmentGroupResult result, OrderShipmentGroup orderShipmentGroup) { 148 var salesOrderShipmentGroupControl = orderShipmentGroupControl; 149 150 result.setOrderShipmentGroup(salesOrderShipmentGroupControl.getOrderShipmentGroupTransfer(getUserVisit(), orderShipmentGroup)); 151 } 152 153 @Override 154 public void doLock(SalesOrderShipmentGroupEdit edit, OrderShipmentGroup orderShipmentGroup) { 155 var orderShipmentGroupDetail = orderShipmentGroup.getLastDetail(); 156 var partyContactMechanism = orderShipmentGroupDetail.getPartyContactMechanism(); 157 var partyContactMechanismDetail = partyContactMechanism == null ? null : partyContactMechanism.getLastDetail(); 158 var shippingMethod = orderShipmentGroupDetail.getShippingMethod(); 159 160 edit.setIsDefault(orderShipmentGroupDetail.getIsDefault().toString()); 161 edit.setPartyName(partyContactMechanismDetail == null ? null : partyContactMechanismDetail.getParty().getLastDetail().getPartyName()); 162 edit.setContactMechanismName(partyContactMechanismDetail == null ? null : partyContactMechanismDetail.getContactMechanism().getLastDetail().getContactMechanismName()); 163 edit.setShippingMethodName(shippingMethod == null ? null : shippingMethod.getLastDetail().getShippingMethodName()); 164 edit.setHoldUntilComplete(orderShipmentGroupDetail.getHoldUntilComplete().toString()); 165 } 166 167 PartyContactMechanism partyContactMechanism; 168 ShippingMethod shippingMethod; 169 170 @Override 171 public void canUpdate(OrderShipmentGroup orderShipmentGroup) { 172 var order = orderShipmentGroup.getLastDetail().getOrder(); 173 174 salesOrderLogic.checkOrderAvailableForModification(session, this, order, getPartyPK()); 175 176 if(!hasExecutionErrors()) { 177 var partyName = edit.getPartyName(); 178 var party = partyName == null ? null : partyLogic.getPartyByName(this, partyName); 179 var contactMechanismName = edit.getContactMechanismName(); 180 var contactMechanism = contactMechanismName == null ? null : contactMechanismLogic.getContactMechanismByName(this, contactMechanismName); 181 var shippingMethodName = edit.getShippingMethodName(); 182 183 shippingMethod = shippingMethodName == null ? null : shippingMethodLogic.getShippingMethodByName(this, shippingMethodName); 184 185 if(!hasExecutionErrors()) { 186 var parameterCount = (party == null ? 0 : 1) + (contactMechanism == null ? 0 : 1); 187 188 switch(parameterCount) { 189 case 2 -> 190 partyContactMechanism = partyContactMechanismLogic.getPartyContactMechanism(this, party, contactMechanism); 191 case 0 -> { 192 } 193 default -> addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 194 } 195 } 196 } 197 } 198 199 @Override 200 public void doUpdate(OrderShipmentGroup orderShipmentGroup) { 201 var partyPK = getPartyPK(); 202 var orderShipmentGroupDetailValue = orderShipmentGroupControl.getOrderShipmentGroupDetailValueForUpdate(orderShipmentGroup); 203 204 orderShipmentGroupDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 205 orderShipmentGroupDetailValue.setPartyContactMechanismPK(partyContactMechanism == null ? null : partyContactMechanism.getPrimaryKey()); 206 orderShipmentGroupDetailValue.setHoldUntilComplete(Boolean.valueOf(edit.getHoldUntilComplete())); 207 orderShipmentGroupDetailValue.setShippingMethodPK(shippingMethod == null ? null : shippingMethod.getPrimaryKey()); 208 209 orderShipmentGroupControl.updateOrderShipmentGroupFromValue(orderShipmentGroupDetailValue, partyPK); 210 } 211 212}