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