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.associate.server.command;
018
019import com.echothree.control.user.associate.common.form.CreateAssociateProgramForm;
020import com.echothree.model.control.associate.server.control.AssociateControl;
021import com.echothree.model.control.sequence.common.SequenceTypes;
022import com.echothree.model.control.sequence.server.control.SequenceControl;
023import com.echothree.model.data.sequence.server.entity.Sequence;
024import com.echothree.model.data.user.common.pk.UserVisitPK;
025import com.echothree.util.common.message.ExecutionErrors;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.common.validation.FieldType;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.server.control.BaseSimpleCommand;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
033
034@Dependent
035public class CreateAssociateProgramCommand
036        extends BaseSimpleCommand<CreateAssociateProgramForm> {
037    
038    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
039    
040    static {
041        FORM_FIELD_DEFINITIONS = List.of(
042                new FieldDefinition("AssociateProgramName", FieldType.ENTITY_NAME, true, null, null),
043                new FieldDefinition("AssociateSequenceName", FieldType.ENTITY_NAME, false, null, null),
044                new FieldDefinition("AssociatePartyContactMechanismSequenceName", FieldType.ENTITY_NAME, false, null, null),
045                new FieldDefinition("AssociateReferralSequenceName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("ItemIndirectSalePercent", FieldType.FRACTIONAL_PERCENT, false, null, null),
047                new FieldDefinition("ItemDirectSalePercent", FieldType.FRACTIONAL_PERCENT, false, null, null),
048                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
049                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
050                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
051        );
052    }
053
054    @Inject
055    AssociateControl associateControl;
056
057    @Inject
058    SequenceControl sequenceControl;
059
060    
061    /** Creates a new instance of CreateAssociateProgramCommand */
062    public CreateAssociateProgramCommand() {
063        super(null, FORM_FIELD_DEFINITIONS, false);
064    }
065    
066    @Override
067    protected BaseResult execute() {
068        var associateProgramName = form.getAssociateProgramName();
069        var associateProgram = associateControl.getAssociateProgramByName(associateProgramName);
070        
071        if(associateProgram == null) {
072            var associateSequenceName = form.getAssociateSequenceName();
073            Sequence associateSequence = null;
074            
075            if(associateSequenceName != null) {
076                var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.ASSOCIATE.name());
077                
078                if(sequenceType != null) {
079                    associateSequence = sequenceControl.getSequenceByName(sequenceType, associateSequenceName);
080                } else {
081                    addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.ASSOCIATE.name());
082                }
083            }
084            
085            if(associateSequenceName == null || associateSequence != null) {
086                var associatePartyContactMechanismSequenceName = form.getAssociatePartyContactMechanismSequenceName();
087                Sequence associatePartyContactMechanismSequence = null;
088                
089                if(associatePartyContactMechanismSequenceName != null) {
090                    var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.ASSOCIATE_PARTY_CONTACT_MECHANISM.name());
091                    
092                    if(sequenceType != null) {
093                        associatePartyContactMechanismSequence = sequenceControl.getSequenceByName(sequenceType, associatePartyContactMechanismSequenceName);
094                    } else {
095                        addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.ASSOCIATE_PARTY_CONTACT_MECHANISM.name());
096                    }
097                }
098                
099                if(associatePartyContactMechanismSequenceName == null || associatePartyContactMechanismSequence != null) {
100                    var associateReferralSequenceName = form.getAssociateReferralSequenceName();
101                    Sequence associateReferralSequence = null;
102                    
103                    if(associateReferralSequenceName != null) {
104                        var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.ASSOCIATE_REFERRAL.name());
105                        
106                        if(sequenceType != null) {
107                            associateReferralSequence = sequenceControl.getSequenceByName(sequenceType, associateReferralSequenceName);
108                        } else {
109                            addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.ASSOCIATE_REFERRAL.name());
110                        }
111                    }
112                    
113                    if(associateReferralSequenceName == null || associateReferralSequence != null) {
114                        var createdBy = getPartyPK();
115                        var strItemIndirectSalePercent = form.getItemIndirectSalePercent();
116                        var itemIndirectSalePercent = strItemIndirectSalePercent == null? null: Integer.valueOf(strItemIndirectSalePercent);
117                        var strItemDirectSalePercent = form.getItemDirectSalePercent();
118                        var itemDirectSalePercent = strItemDirectSalePercent == null? null: Integer.valueOf(strItemDirectSalePercent);
119                        var isDefault = Boolean.valueOf(form.getIsDefault());
120                        var sortOrder = Integer.valueOf(form.getSortOrder());
121                        var description = form.getDescription();
122                        
123                        associateProgram = associateControl.createAssociateProgram(associateProgramName, associateSequence,
124                                associatePartyContactMechanismSequence, associateReferralSequence, itemIndirectSalePercent,
125                                itemDirectSalePercent, isDefault, sortOrder, createdBy);
126                        
127                        if(description != null) {
128                            associateControl.createAssociateProgramDescription(associateProgram, getPreferredLanguage(), description,
129                                    createdBy);
130                        }
131                    } else {
132                        addExecutionError(ExecutionErrors.UnknownSequenceName.name(), associateReferralSequenceName);
133                    }
134                } else {
135                    addExecutionError(ExecutionErrors.UnknownSequenceName.name(), associatePartyContactMechanismSequenceName);
136                }
137            } else {
138                addExecutionError(ExecutionErrors.UnknownSequenceName.name(), associateSequenceName);
139            }
140        } else {
141            addExecutionError(ExecutionErrors.DuplicateAssociateProgramName.name(), associateProgramName);
142        }
143        
144        return null;
145    }
146    
147}