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 com.echothree.util.server.persistence.Session; 031import java.util.List; 032import javax.enterprise.context.Dependent; 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 /** Creates a new instance of CreateAssociateProgramCommand */ 055 public CreateAssociateProgramCommand() { 056 super(null, FORM_FIELD_DEFINITIONS, false); 057 } 058 059 @Override 060 protected BaseResult execute() { 061 var associateControl = Session.getModelController(AssociateControl.class); 062 var associateProgramName = form.getAssociateProgramName(); 063 var associateProgram = associateControl.getAssociateProgramByName(associateProgramName); 064 065 if(associateProgram == null) { 066 var sequenceControl = Session.getModelController(SequenceControl.class); 067 var associateSequenceName = form.getAssociateSequenceName(); 068 Sequence associateSequence = null; 069 070 if(associateSequenceName != null) { 071 var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.ASSOCIATE.name()); 072 073 if(sequenceType != null) { 074 associateSequence = sequenceControl.getSequenceByName(sequenceType, associateSequenceName); 075 } else { 076 addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.ASSOCIATE.name()); 077 } 078 } 079 080 if(associateSequenceName == null || associateSequence != null) { 081 var associatePartyContactMechanismSequenceName = form.getAssociatePartyContactMechanismSequenceName(); 082 Sequence associatePartyContactMechanismSequence = null; 083 084 if(associatePartyContactMechanismSequenceName != null) { 085 var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.ASSOCIATE_PARTY_CONTACT_MECHANISM.name()); 086 087 if(sequenceType != null) { 088 associatePartyContactMechanismSequence = sequenceControl.getSequenceByName(sequenceType, associatePartyContactMechanismSequenceName); 089 } else { 090 addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.ASSOCIATE_PARTY_CONTACT_MECHANISM.name()); 091 } 092 } 093 094 if(associatePartyContactMechanismSequenceName == null || associatePartyContactMechanismSequence != null) { 095 var associateReferralSequenceName = form.getAssociateReferralSequenceName(); 096 Sequence associateReferralSequence = null; 097 098 if(associateReferralSequenceName != null) { 099 var sequenceType = sequenceControl.getSequenceTypeByName(SequenceTypes.ASSOCIATE_REFERRAL.name()); 100 101 if(sequenceType != null) { 102 associateReferralSequence = sequenceControl.getSequenceByName(sequenceType, associateReferralSequenceName); 103 } else { 104 addExecutionError(ExecutionErrors.UnknownSequenceTypeName.name(), SequenceTypes.ASSOCIATE_REFERRAL.name()); 105 } 106 } 107 108 if(associateReferralSequenceName == null || associateReferralSequence != null) { 109 var createdBy = getPartyPK(); 110 var strItemIndirectSalePercent = form.getItemIndirectSalePercent(); 111 var itemIndirectSalePercent = strItemIndirectSalePercent == null? null: Integer.valueOf(strItemIndirectSalePercent); 112 var strItemDirectSalePercent = form.getItemDirectSalePercent(); 113 var itemDirectSalePercent = strItemDirectSalePercent == null? null: Integer.valueOf(strItemDirectSalePercent); 114 var isDefault = Boolean.valueOf(form.getIsDefault()); 115 var sortOrder = Integer.valueOf(form.getSortOrder()); 116 var description = form.getDescription(); 117 118 associateProgram = associateControl.createAssociateProgram(associateProgramName, associateSequence, 119 associatePartyContactMechanismSequence, associateReferralSequence, itemIndirectSalePercent, 120 itemDirectSalePercent, isDefault, sortOrder, createdBy); 121 122 if(description != null) { 123 associateControl.createAssociateProgramDescription(associateProgram, getPreferredLanguage(), description, 124 createdBy); 125 } 126 } else { 127 addExecutionError(ExecutionErrors.UnknownSequenceName.name(), associateReferralSequenceName); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownSequenceName.name(), associatePartyContactMechanismSequenceName); 131 } 132 } else { 133 addExecutionError(ExecutionErrors.UnknownSequenceName.name(), associateSequenceName); 134 } 135 } else { 136 addExecutionError(ExecutionErrors.DuplicateAssociateProgramName.name(), associateProgramName); 137 } 138 139 return null; 140 } 141 142}