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.offer.server.command;
018
019import com.echothree.control.user.offer.common.edit.OfferEditFactory;
020import com.echothree.control.user.offer.common.edit.OfferUseEdit;
021import com.echothree.control.user.offer.common.form.EditOfferUseForm;
022import com.echothree.control.user.offer.common.result.OfferResultFactory;
023import com.echothree.control.user.offer.common.spec.OfferUseSpec;
024import com.echothree.model.control.offer.server.control.OfferControl;
025import com.echothree.model.control.offer.server.control.OfferUseControl;
026import com.echothree.model.control.offer.server.control.UseControl;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.control.sequence.common.SequenceTypes;
031import com.echothree.model.control.sequence.server.control.SequenceControl;
032import com.echothree.model.data.sequence.server.entity.Sequence;
033import com.echothree.model.data.user.common.pk.UserVisitPK;
034import com.echothree.util.common.command.BaseResult;
035import com.echothree.util.common.command.EditMode;
036import com.echothree.util.common.message.ExecutionErrors;
037import com.echothree.util.common.validation.FieldDefinition;
038import com.echothree.util.common.validation.FieldType;
039import com.echothree.util.server.control.BaseEditCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import java.util.List;
044import javax.enterprise.context.Dependent;
045import javax.inject.Inject;
046
047@Dependent
048public class EditOfferUseCommand
049        extends BaseEditCommand<OfferUseSpec, OfferUseEdit> {
050    
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
053    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
054    
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
059                        new SecurityRoleDefinition(SecurityRoleGroups.OfferUse.name(), SecurityRoles.Edit.name())
060                ))
061        ));
062        
063        SPEC_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null)
066        );
067        
068        EDIT_FIELD_DEFINITIONS = List.of(
069                new FieldDefinition("SalesOrderSequenceName", FieldType.ENTITY_NAME, false, null, null)
070        );
071    }
072
073    @Inject
074    OfferControl offerControl;
075
076    @Inject
077    OfferUseControl offerUseControl;
078
079    @Inject
080    SequenceControl sequenceControl;
081
082    @Inject
083    UseControl useControl;
084
085    
086    /** Creates a new instance of EditOfferUseCommand */
087    public EditOfferUseCommand() {
088        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
089    }
090    
091    @Override
092    protected BaseResult execute() {
093        var result = OfferResultFactory.getEditOfferUseResult();
094        var offerName = spec.getOfferName();
095        var offer = offerControl.getOfferByName(offerName);
096        
097        if(offer != null) {
098            var useName = spec.getUseName();
099            var use = useControl.getUseByName(useName);
100            
101            if(use != null) {
102                if(editMode.equals(EditMode.LOCK)) {
103                    var offerUse = offerUseControl.getOfferUse(offer, use);
104                    
105                    if(offerUse != null) {
106                        result.setOfferUse(offerUseControl.getOfferUseTransfer(getUserVisit(), offerUse));
107                        
108                        if(lockEntity(offerUse)) {
109                            var edit = OfferEditFactory.getOfferUseEdit();
110                            var offerUseDetail = offerUse.getLastDetail();
111                            var salesOrderSequence = offerUseDetail.getSalesOrderSequence();
112                            
113                            result.setEdit(edit);
114                            edit.setSalesOrderSequenceName(salesOrderSequence == null? null: salesOrderSequence.getLastDetail().getSequenceName());
115                        } else {
116                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
117                        }
118                        
119                        result.setEntityLock(getEntityLockTransfer(offerUse));
120                    } else {
121                        addExecutionError(ExecutionErrors.UnknownOfferUse.name());
122                    }
123                } else if(editMode.equals(EditMode.UPDATE)) {
124                    var offerUse = offerUseControl.getOfferUseForUpdate(offer, use);
125                    
126                    if(offerUse != null) {
127                        var salesOrderSequenceName = edit.getSalesOrderSequenceName();
128                        Sequence salesOrderSequence = null;
129                        
130                        if(salesOrderSequenceName != null) {
131                            var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.SALES_ORDER.name());
132                            
133                            if(sequenceType != null) {
134                                salesOrderSequence = sequenceControl.getSequenceByName(sequenceType, salesOrderSequenceName);
135                            } else {
136                                addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.SALES_ORDER.name());
137                            }
138                        }
139                        
140                        if(salesOrderSequenceName == null || salesOrderSequence != null) {
141                            if(lockEntityForUpdate(offerUse)) {
142                                try {
143                                    var offerUseDetailValue = offerUseControl.getOfferUseDetailValueForUpdate(offerUse);
144                                    
145                                    offerUseDetailValue.setSalesOrderSequencePK(salesOrderSequence == null? null: salesOrderSequence.getPrimaryKey());
146
147                                    offerUseControl.updateOfferUseFromValue(offerUseDetailValue, getPartyPK());
148                                } finally {
149                                    unlockEntity(offerUse);
150                                }
151                            } else {
152                                addExecutionError(ExecutionErrors.EntityLockStale.name());
153                            }
154                        } else {
155                            addExecutionError(ExecutionErrors.UnknownSalesOrderSequenceName.name(), salesOrderSequenceName);
156                        }
157                    } else {
158                        addExecutionError(ExecutionErrors.UnknownOfferUse.name());
159                    }
160                }
161            } else {
162                addExecutionError(ExecutionErrors.UnknownUseName.name(), useName);
163            }
164        }  else {
165            addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName);
166        }
167        
168        return result;
169    }
170    
171}