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