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;
052import javax.inject.Inject;
053
054@Dependent
055public class EditSalesOrderShipmentGroupCommand
056        extends BaseAbstractEditCommand<SalesOrderShipmentGroupSpec, SalesOrderShipmentGroupEdit, EditSalesOrderShipmentGroupResult, OrderShipmentGroup, OrderShipmentGroup> {
057
058    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
059    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
060    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
061
062    static {
063        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
064                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
065                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
066                        new SecurityRoleDefinition(SecurityRoleGroups.SalesOrderShipmentGroup.name(), SecurityRoles.Edit.name())
067                        ))
068                ));
069
070        SPEC_FIELD_DEFINITIONS = List.of(
071                new FieldDefinition("OrderName", FieldType.ENTITY_NAME, true, null, null),
072                new FieldDefinition("OrderShipmentGroupSequence", FieldType.UNSIGNED_INTEGER, true, null, null)
073                );
074
075        EDIT_FIELD_DEFINITIONS = List.of(
076                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
077                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
078                new FieldDefinition("ContactMechanismName", FieldType.ENTITY_NAME, false, null, null),
079                new FieldDefinition("ShippingMethodName", FieldType.ENTITY_NAME, false, null, null),
080                new FieldDefinition("HoldUntilComplete", FieldType.BOOLEAN, true, null, null)
081                );
082    }
083
084    @Inject
085    PartyContactMechanismLogic partyContactMechanismLogic;
086
087    /** Creates a new instance of EditSalesOrderShipmentGroupCommand */
088    public EditSalesOrderShipmentGroupCommand() {
089        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
090    }
091
092    @Override
093    public EditSalesOrderShipmentGroupResult getResult() {
094        return SalesResultFactory.getEditSalesOrderShipmentGroupResult();
095    }
096
097    @Override
098    public SalesOrderShipmentGroupEdit getEdit() {
099        return SalesEditFactory.getSalesOrderShipmentGroupEdit();
100    }
101
102    @Override
103    public OrderShipmentGroup getEntity(EditSalesOrderShipmentGroupResult result) {
104        var orderName = spec.getOrderName();
105        var order = OrderLogic.getInstance().getOrderByName(this, OrderTypes.SALES_ORDER.name(), orderName);
106        OrderShipmentGroup orderShipmentGroup = null;
107
108        if(!hasExecutionErrors()) {
109            var orderShipmentGroupSequence = Integer.valueOf(spec.getOrderShipmentGroupSequence());
110
111            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
112                orderShipmentGroup = SalesOrderShipmentGroupLogic.getInstance().getOrderShipmentGroup(this, order, orderShipmentGroupSequence);
113            } else { // EditMode.UPDATE
114                orderShipmentGroup = SalesOrderShipmentGroupLogic.getInstance().getOrderShipmentGroupForUpdate(this, order, orderShipmentGroupSequence);
115            }
116        }
117
118        return orderShipmentGroup;
119    }
120
121    @Override
122    public OrderShipmentGroup getLockEntity(OrderShipmentGroup orderShipmentGroup) {
123        return orderShipmentGroup;
124    }
125
126    @Override
127    public void fillInResult(EditSalesOrderShipmentGroupResult result, OrderShipmentGroup orderShipmentGroup) {
128        var salesOrderShipmentGroupControl = Session.getModelController(OrderShipmentGroupControl.class);
129
130        result.setOrderShipmentGroup(salesOrderShipmentGroupControl.getOrderShipmentGroupTransfer(getUserVisit(), orderShipmentGroup));
131    }
132
133    @Override
134    public void doLock(SalesOrderShipmentGroupEdit edit, OrderShipmentGroup orderShipmentGroup) {
135        var orderShipmentGroupDetail = orderShipmentGroup.getLastDetail();
136        var partyContactMechanism = orderShipmentGroupDetail.getPartyContactMechanism();
137        var partyContactMechanismDetail = partyContactMechanism == null ? null : partyContactMechanism.getLastDetail();
138        var shippingMethod = orderShipmentGroupDetail.getShippingMethod();
139
140        edit.setIsDefault(orderShipmentGroupDetail.getIsDefault().toString());
141        edit.setPartyName(partyContactMechanismDetail == null ? null : partyContactMechanismDetail.getParty().getLastDetail().getPartyName());
142        edit.setContactMechanismName(partyContactMechanismDetail == null ? null : partyContactMechanismDetail.getContactMechanism().getLastDetail().getContactMechanismName());
143        edit.setShippingMethodName(shippingMethod == null ? null : shippingMethod.getLastDetail().getShippingMethodName());
144        edit.setHoldUntilComplete(orderShipmentGroupDetail.getHoldUntilComplete().toString());
145    }
146
147    PartyContactMechanism partyContactMechanism;
148    ShippingMethod shippingMethod;
149
150    @Override
151    public void canUpdate(OrderShipmentGroup orderShipmentGroup) {
152        var order = orderShipmentGroup.getLastDetail().getOrder();
153
154        SalesOrderLogic.getInstance().checkOrderAvailableForModification(session, this, order, getPartyPK());
155
156        if(!hasExecutionErrors()) {
157            var partyName = edit.getPartyName();
158            var party = partyName == null ? null : PartyLogic.getInstance().getPartyByName(this, partyName);
159            var contactMechanismName = edit.getContactMechanismName();
160            var contactMechanism = contactMechanismName == null ? null : ContactMechanismLogic.getInstance().getContactMechanismByName(this, contactMechanismName);
161            var shippingMethodName = edit.getShippingMethodName();
162
163            shippingMethod = shippingMethodName == null ? null : ShippingMethodLogic.getInstance().getShippingMethodByName(this, shippingMethodName);
164
165            if(!hasExecutionErrors()) {
166                var parameterCount = (party == null ? 0 : 1) + (contactMechanism == null ? 0 : 1);
167
168                switch(parameterCount) {
169                    case 2 ->
170                            partyContactMechanism = partyContactMechanismLogic.getPartyContactMechanism(this, party, contactMechanism);
171                    case 0 -> {
172                    }
173                    default -> addExecutionError(ExecutionErrors.InvalidParameterCount.name());
174                }
175            }
176        }
177    }
178
179    @Override
180    public void doUpdate(OrderShipmentGroup orderShipmentGroup) {
181        var partyPK = getPartyPK();
182        var orderShipmentGroupControl = Session.getModelController(OrderShipmentGroupControl.class);
183        var orderShipmentGroupDetailValue = orderShipmentGroupControl.getOrderShipmentGroupDetailValueForUpdate(orderShipmentGroup);
184
185        orderShipmentGroupDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
186        orderShipmentGroupDetailValue.setPartyContactMechanismPK(partyContactMechanism == null ? null : partyContactMechanism.getPrimaryKey());
187        orderShipmentGroupDetailValue.setHoldUntilComplete(Boolean.valueOf(edit.getHoldUntilComplete()));
188        orderShipmentGroupDetailValue.setShippingMethodPK(shippingMethod == null ? null : shippingMethod.getPrimaryKey());
189
190        orderShipmentGroupControl.updateOrderShipmentGroupFromValue(orderShipmentGroupDetailValue, partyPK);
191    }
192
193}