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 com.echothree.util.server.persistence.Session;
050import java.util.List;
051import javax.enterprise.context.Dependent;
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    /** Creates a new instance of EditSalesOrderShipmentGroupCommand */
084    public EditSalesOrderShipmentGroupCommand() {
085        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
086    }
087
088    @Override
089    public EditSalesOrderShipmentGroupResult getResult() {
090        return SalesResultFactory.getEditSalesOrderShipmentGroupResult();
091    }
092
093    @Override
094    public SalesOrderShipmentGroupEdit getEdit() {
095        return SalesEditFactory.getSalesOrderShipmentGroupEdit();
096    }
097
098    @Override
099    public OrderShipmentGroup getEntity(EditSalesOrderShipmentGroupResult result) {
100        var orderName = spec.getOrderName();
101        var order = OrderLogic.getInstance().getOrderByName(this, OrderTypes.SALES_ORDER.name(), orderName);
102        OrderShipmentGroup orderShipmentGroup = null;
103
104        if(!hasExecutionErrors()) {
105            var orderShipmentGroupSequence = Integer.valueOf(spec.getOrderShipmentGroupSequence());
106
107            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
108                orderShipmentGroup = SalesOrderShipmentGroupLogic.getInstance().getOrderShipmentGroup(this, order, orderShipmentGroupSequence);
109            } else { // EditMode.UPDATE
110                orderShipmentGroup = SalesOrderShipmentGroupLogic.getInstance().getOrderShipmentGroupForUpdate(this, order, orderShipmentGroupSequence);
111            }
112        }
113
114        return orderShipmentGroup;
115    }
116
117    @Override
118    public OrderShipmentGroup getLockEntity(OrderShipmentGroup orderShipmentGroup) {
119        return orderShipmentGroup;
120    }
121
122    @Override
123    public void fillInResult(EditSalesOrderShipmentGroupResult result, OrderShipmentGroup orderShipmentGroup) {
124        var salesOrderShipmentGroupControl = Session.getModelController(OrderShipmentGroupControl.class);
125
126        result.setOrderShipmentGroup(salesOrderShipmentGroupControl.getOrderShipmentGroupTransfer(getUserVisit(), orderShipmentGroup));
127    }
128
129    @Override
130    public void doLock(SalesOrderShipmentGroupEdit edit, OrderShipmentGroup orderShipmentGroup) {
131        var orderShipmentGroupDetail = orderShipmentGroup.getLastDetail();
132        var partyContactMechanism = orderShipmentGroupDetail.getPartyContactMechanism();
133        var partyContactMechanismDetail = partyContactMechanism == null ? null : partyContactMechanism.getLastDetail();
134        var shippingMethod = orderShipmentGroupDetail.getShippingMethod();
135
136        edit.setIsDefault(orderShipmentGroupDetail.getIsDefault().toString());
137        edit.setPartyName(partyContactMechanismDetail == null ? null : partyContactMechanismDetail.getParty().getLastDetail().getPartyName());
138        edit.setContactMechanismName(partyContactMechanismDetail == null ? null : partyContactMechanismDetail.getContactMechanism().getLastDetail().getContactMechanismName());
139        edit.setShippingMethodName(shippingMethod == null ? null : shippingMethod.getLastDetail().getShippingMethodName());
140        edit.setHoldUntilComplete(orderShipmentGroupDetail.getHoldUntilComplete().toString());
141    }
142
143    PartyContactMechanism partyContactMechanism;
144    ShippingMethod shippingMethod;
145
146    @Override
147    public void canUpdate(OrderShipmentGroup orderShipmentGroup) {
148        var order = orderShipmentGroup.getLastDetail().getOrder();
149
150        SalesOrderLogic.getInstance().checkOrderAvailableForModification(session, this, order, getPartyPK());
151
152        if(!hasExecutionErrors()) {
153            var partyName = edit.getPartyName();
154            var party = partyName == null ? null : PartyLogic.getInstance().getPartyByName(this, partyName);
155            var contactMechanismName = edit.getContactMechanismName();
156            var contactMechanism = contactMechanismName == null ? null : ContactMechanismLogic.getInstance().getContactMechanismByName(this, contactMechanismName);
157            var shippingMethodName = edit.getShippingMethodName();
158
159            shippingMethod = shippingMethodName == null ? null : ShippingMethodLogic.getInstance().getShippingMethodByName(this, shippingMethodName);
160
161            if(!hasExecutionErrors()) {
162                var parameterCount = (party == null ? 0 : 1) + (contactMechanism == null ? 0 : 1);
163
164                switch(parameterCount) {
165                    case 2 ->
166                            partyContactMechanism = PartyContactMechanismLogic.getInstance().getPartyContactMechanism(this, party, contactMechanism);
167                    case 0 -> {
168                    }
169                    default -> addExecutionError(ExecutionErrors.InvalidParameterCount.name());
170                }
171            }
172        }
173    }
174
175    @Override
176    public void doUpdate(OrderShipmentGroup orderShipmentGroup) {
177        var partyPK = getPartyPK();
178        var orderShipmentGroupControl = Session.getModelController(OrderShipmentGroupControl.class);
179        var orderShipmentGroupDetailValue = orderShipmentGroupControl.getOrderShipmentGroupDetailValueForUpdate(orderShipmentGroup);
180
181        orderShipmentGroupDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
182        orderShipmentGroupDetailValue.setPartyContactMechanismPK(partyContactMechanism == null ? null : partyContactMechanism.getPrimaryKey());
183        orderShipmentGroupDetailValue.setHoldUntilComplete(Boolean.valueOf(edit.getHoldUntilComplete()));
184        orderShipmentGroupDetailValue.setShippingMethodPK(shippingMethod == null ? null : shippingMethod.getPrimaryKey());
185
186        orderShipmentGroupControl.updateOrderShipmentGroupFromValue(orderShipmentGroupDetailValue, partyPK);
187    }
188
189}