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.model.control.associate.server.logic; 018 019import com.echothree.control.user.associate.common.spec.AssociatePartyContactMechanismSpec; 020import com.echothree.model.control.associate.server.control.AssociateControl; 021import com.echothree.model.control.core.server.control.CoreControl; 022import com.echothree.model.data.associate.server.entity.AssociateReferral; 023import com.echothree.model.data.user.server.entity.UserVisit; 024import com.echothree.util.common.message.ExecutionErrors; 025import com.echothree.util.common.persistence.BasePK; 026import com.echothree.util.server.control.BaseLogic; 027import com.echothree.util.server.message.ExecutionErrorAccumulator; 028import com.echothree.util.server.persistence.Session; 029 030public class AssociateReferralLogic 031 extends BaseLogic { 032 033 private AssociateReferralLogic() { 034 super(); 035 } 036 037 private static class LogicHolder { 038 static AssociateReferralLogic instance = new AssociateReferralLogic(); 039 } 040 041 public static AssociateReferralLogic getInstance() { 042 return LogicHolder.instance; 043 } 044 045 public void handleAssociateReferral(final Session session, final ExecutionErrorAccumulator eea, final AssociatePartyContactMechanismSpec spec, 046 final UserVisit userVisit, final BasePK targetPK, final BasePK partyPK) { 047 var associateName = spec.getAssociateName(); 048 AssociateReferral associateReferral; 049 050 if(associateName != null) { 051 var associateControl = Session.getModelController(AssociateControl.class); 052 var associateProgramName = spec.getAssociateProgramName(); 053 var associateProgram = associateProgramName == null ? associateControl.getDefaultAssociateProgram() : 054 associateControl.getAssociateProgramByName(associateProgramName); 055 056 if(associateProgram != null) { 057 var associate = associateControl.getAssociateByName(associateProgram, associateName); 058 059 if(associate != null) { 060 var associatePartyContactMechanismName = spec.getAssociatePartyContactMechanismName(); 061 var associatePartyContactMechanism = associatePartyContactMechanismName == null ? 062 associateControl.getDefaultAssociatePartyContactMechanism(associate) : 063 associateControl.getAssociatePartyContactMechanismByName(associate, associatePartyContactMechanismName); 064 065 if(associatePartyContactMechanismName != null && associatePartyContactMechanism == null) { 066 eea.addExecutionError(ExecutionErrors.UnknownAssociatePartyContactMechanismName.name(), 067 associateProgram.getLastDetail().getAssociateProgramName(), 068 associate.getLastDetail().getAssociateName(), associatePartyContactMechanismName); 069 } else { 070 var coreControl = Session.getModelController(CoreControl.class); 071 072 associateReferral = associateControl.createAssociateReferral(associate, associatePartyContactMechanism, 073 coreControl.getEntityInstanceByBasePK(targetPK), session.START_TIME_LONG, partyPK); 074 075 userVisit.setAssociateReferral(associateReferral); 076 } 077 } else { 078 eea.addExecutionError(ExecutionErrors.UnknownAssociateName.name(), 079 associateProgram.getLastDetail().getAssociateProgramName(), associateName); 080 } 081 } else { 082 if(associateProgramName != null) { 083 eea.addExecutionError(ExecutionErrors.UnknownAssociateProgramName.name(), associateProgramName); 084 } else { 085 eea.addExecutionError(ExecutionErrors.MissingDefaultAssociateProgram.name()); 086 } 087 } 088 } 089 } 090 091 public AssociateReferral getAssociateReferral(final Session session, final UserVisit userVisit) { 092 var associateReferral = userVisit == null ? null : userVisit.getAssociateReferral(); 093 094 // TODO: Check the time of the referral to see if it is still in effect. 095 096 return associateReferral; 097 } 098 099}