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.order.server.command;
018
019import com.echothree.control.user.order.common.edit.OrderEditFactory;
020import com.echothree.control.user.order.common.edit.OrderPriorityDescriptionEdit;
021import com.echothree.control.user.order.common.form.EditOrderPriorityDescriptionForm;
022import com.echothree.control.user.order.common.result.EditOrderPriorityDescriptionResult;
023import com.echothree.control.user.order.common.result.OrderResultFactory;
024import com.echothree.control.user.order.common.spec.OrderPriorityDescriptionSpec;
025import com.echothree.model.control.order.server.control.OrderPriorityControl;
026import com.echothree.model.control.order.server.control.OrderTypeControl;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.party.server.control.PartyControl;
029import com.echothree.model.control.security.common.SecurityRoleGroups;
030import com.echothree.model.control.security.common.SecurityRoles;
031import com.echothree.model.data.order.server.entity.OrderPriority;
032import com.echothree.model.data.order.server.entity.OrderPriorityDescription;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.command.EditMode;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.server.control.BaseAbstractEditCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import com.echothree.util.server.persistence.Session;
043import java.util.List;
044import javax.enterprise.context.Dependent;
045
046@Dependent
047public class EditOrderPriorityDescriptionCommand
048        extends BaseAbstractEditCommand<OrderPriorityDescriptionSpec, OrderPriorityDescriptionEdit, EditOrderPriorityDescriptionResult, OrderPriorityDescription, OrderPriority> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
052    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
058                        new SecurityRoleDefinition(SecurityRoleGroups.OrderPriority.name(), SecurityRoles.Description.name())
059                        ))
060                ));
061        
062        SPEC_FIELD_DEFINITIONS = List.of(
063                new FieldDefinition("OrderTypeName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("OrderPriorityName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
066                );
067        
068        EDIT_FIELD_DEFINITIONS = List.of(
069                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
070                );
071    }
072    
073    /** Creates a new instance of EditOrderPriorityDescriptionCommand */
074    public EditOrderPriorityDescriptionCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077    
078    @Override
079    public EditOrderPriorityDescriptionResult getResult() {
080        return OrderResultFactory.getEditOrderPriorityDescriptionResult();
081    }
082
083    @Override
084    public OrderPriorityDescriptionEdit getEdit() {
085        return OrderEditFactory.getOrderPriorityDescriptionEdit();
086    }
087
088    @Override
089    public OrderPriorityDescription getEntity(EditOrderPriorityDescriptionResult result) {
090        var orderTypeControl = Session.getModelController(OrderTypeControl.class);
091        OrderPriorityDescription orderPriorityDescription = null;
092        var orderTypeName = spec.getOrderTypeName();
093        var orderType = orderTypeControl.getOrderTypeByName(orderTypeName);
094
095        if(orderType != null) {
096            var orderPriorityControl = Session.getModelController(OrderPriorityControl.class);
097            var orderPriorityName = spec.getOrderPriorityName();
098            var orderPriority = orderPriorityControl.getOrderPriorityByName(orderType, orderPriorityName);
099
100            if(orderPriority != null) {
101                var partyControl = Session.getModelController(PartyControl.class);
102                var languageIsoName = spec.getLanguageIsoName();
103                var language = partyControl.getLanguageByIsoName(languageIsoName);
104
105                if(language != null) {
106                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
107                        orderPriorityDescription = orderPriorityControl.getOrderPriorityDescription(orderPriority, language);
108                    } else { // EditMode.UPDATE
109                        orderPriorityDescription = orderPriorityControl.getOrderPriorityDescriptionForUpdate(orderPriority, language);
110                    }
111
112                    if(orderPriorityDescription == null) {
113                        addExecutionError(ExecutionErrors.UnknownOrderPriorityDescription.name(), orderTypeName, orderPriorityName, languageIsoName);
114                    }
115                } else {
116                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
117                }
118            } else {
119                addExecutionError(ExecutionErrors.UnknownOrderPriorityName.name(), orderTypeName, orderPriorityName);
120            }
121        } else {
122            addExecutionError(ExecutionErrors.UnknownOrderTypeName.name(), orderTypeName);
123        }
124
125        return orderPriorityDescription;
126    }
127
128    @Override
129    public OrderPriority getLockEntity(OrderPriorityDescription orderPriorityDescription) {
130        return orderPriorityDescription.getOrderPriority();
131    }
132
133    @Override
134    public void fillInResult(EditOrderPriorityDescriptionResult result, OrderPriorityDescription orderPriorityDescription) {
135        var orderPriorityControl = Session.getModelController(OrderPriorityControl.class);
136
137        result.setOrderPriorityDescription(orderPriorityControl.getOrderPriorityDescriptionTransfer(getUserVisit(), orderPriorityDescription));
138    }
139
140    @Override
141    public void doLock(OrderPriorityDescriptionEdit edit, OrderPriorityDescription orderPriorityDescription) {
142        edit.setDescription(orderPriorityDescription.getDescription());
143    }
144
145    @Override
146    public void doUpdate(OrderPriorityDescription orderPriorityDescription) {
147        var orderPriorityControl = Session.getModelController(OrderPriorityControl.class);
148        var orderPriorityDescriptionValue = orderPriorityControl.getOrderPriorityDescriptionValue(orderPriorityDescription);
149        orderPriorityDescriptionValue.setDescription(edit.getDescription());
150
151        orderPriorityControl.updateOrderPriorityDescriptionFromValue(orderPriorityDescriptionValue, getPartyPK());
152    }
153    
154}