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.form.CreateOfferUseForm;
020import com.echothree.control.user.offer.common.result.OfferResultFactory;
021import com.echothree.model.control.offer.server.control.OfferControl;
022import com.echothree.model.control.offer.server.control.OfferUseControl;
023import com.echothree.model.control.offer.server.control.SourceControl;
024import com.echothree.model.control.offer.server.control.UseControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.sequence.common.SequenceTypes;
029import com.echothree.model.control.sequence.server.control.SequenceControl;
030import com.echothree.model.data.offer.server.entity.OfferUse;
031import com.echothree.model.data.sequence.server.entity.Sequence;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.persistence.BasePK;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.server.control.BaseSimpleCommand;
039import com.echothree.util.server.control.CommandSecurityDefinition;
040import com.echothree.util.server.control.PartyTypeDefinition;
041import com.echothree.util.server.control.SecurityRoleDefinition;
042import java.util.List;
043import javax.enterprise.context.Dependent;
044import javax.inject.Inject;
045
046@Dependent
047public class CreateOfferUseCommand
048        extends BaseSimpleCommand<CreateOfferUseForm> {
049
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
052
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
057                        new SecurityRoleDefinition(SecurityRoleGroups.OfferUse.name(), SecurityRoles.Create.name())
058                ))
059        ));
060        
061        FORM_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, 20L),
063                new FieldDefinition("UseName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("SalesOrderSequenceName", FieldType.ENTITY_NAME, false, null, null)
065        );
066    }
067
068    @Inject
069    OfferControl offerControl;
070
071    @Inject
072    OfferUseControl offerUseControl;
073
074    @Inject
075    SequenceControl sequenceControl;
076
077    @Inject
078    SourceControl sourceControl;
079
080    @Inject
081    UseControl useControl;
082
083    
084    /** Creates a new instance of CreateOfferUseCommand */
085    public CreateOfferUseCommand() {
086        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
087    }
088    
089    @Override
090    protected BaseResult execute() {
091        var result = OfferResultFactory.getCreateOfferUseResult();
092        OfferUse offerUse = null;
093        var offerName = form.getOfferName();
094        var offer = offerControl.getOfferByName(offerName);
095        
096        if(offer != null) {
097            var useName = form.getUseName();
098            var use = useControl.getUseByName(useName);
099            
100            if(use != null) {
101                offerUse = offerUseControl.getOfferUse(offer, use);
102                
103                if(offerUse == null) {
104                    var salesOrderSequenceName = form.getSalesOrderSequenceName();
105                    Sequence salesOrderSequence = null;
106                    
107                    if(salesOrderSequenceName != null) {
108                        var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.SALES_ORDER.name());
109                        
110                        if(sequenceType != null) {
111                            salesOrderSequence = sequenceControl.getSequenceByName(sequenceType, salesOrderSequenceName);
112                        } else {
113                            addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.SALES_ORDER.name());
114                        }
115                    }
116                    
117                    if(salesOrderSequenceName == null || salesOrderSequence != null) {
118                        var sourceName = offerName + useName;
119                        var source = sourceControl.getSourceByName(sourceName);
120                        
121                        if(source == null) {
122                            BasePK partyPK = getPartyPK();
123                            
124                            offerUse = offerUseControl.createOfferUse(offer, use, salesOrderSequence, partyPK);
125                            sourceControl.createSource(sourceName, offerUse, false, 1, partyPK);
126                        }  else {
127                            addExecutionError(ExecutionErrors.DuplicateSourceName.name(), sourceName);
128                        }
129                    } else {
130                        addExecutionError(ExecutionErrors.UnknownSalesOrderSequenceName.name(), salesOrderSequenceName);
131                    }
132                } else {
133                    addExecutionError(ExecutionErrors.DuplicateOfferUse.name());
134                }
135            } else {
136                addExecutionError(ExecutionErrors.UnknownUseName.name(), useName);
137            }
138        }  else {
139            addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName);
140        }
141
142        if(offerUse != null) {
143            var offerUseDetail = offerUse.getLastDetail();
144
145            result.setOfferName(offerUseDetail.getOffer().getLastDetail().getOfferName());
146            result.setUseName(offerUseDetail.getUse().getLastDetail().getUseName());
147            result.setEntityRef(offerUseDetail.getPrimaryKey().getEntityRef());
148        }
149
150        return result;
151    }
152    
153}