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