001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.EntityInstanceControl;
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;
029import javax.enterprise.context.ApplicationScoped;
030import javax.enterprise.inject.spi.CDI;
031
032@ApplicationScoped
033public class AssociateReferralLogic
034        extends BaseLogic {
035
036    protected AssociateReferralLogic() {
037        super();
038    }
039
040    public static AssociateReferralLogic getInstance() {
041        return CDI.current().select(AssociateReferralLogic.class).get();
042    }
043
044    public void handleAssociateReferral(final Session session, final ExecutionErrorAccumulator eea, final AssociatePartyContactMechanismSpec spec,
045            final UserVisit userVisit, final BasePK targetPK, final BasePK partyPK) {
046        var associateName = spec.getAssociateName();
047        AssociateReferral associateReferral;
048
049        if(associateName != null) {
050            var associateControl = Session.getModelController(AssociateControl.class);
051            var associateProgramName = spec.getAssociateProgramName();
052            var associateProgram = associateProgramName == null ? associateControl.getDefaultAssociateProgram() :
053                associateControl.getAssociateProgramByName(associateProgramName);
054
055            if(associateProgram != null) {
056                var associate = associateControl.getAssociateByName(associateProgram, associateName);
057
058                if(associate != null) {
059                    var associatePartyContactMechanismName = spec.getAssociatePartyContactMechanismName();
060                    var associatePartyContactMechanism = associatePartyContactMechanismName == null ?
061                        associateControl.getDefaultAssociatePartyContactMechanism(associate) :
062                        associateControl.getAssociatePartyContactMechanismByName(associate, associatePartyContactMechanismName);
063
064                    if(associatePartyContactMechanismName != null && associatePartyContactMechanism == null) {
065                        eea.addExecutionError(ExecutionErrors.UnknownAssociatePartyContactMechanismName.name(),
066                                associateProgram.getLastDetail().getAssociateProgramName(),
067                                associate.getLastDetail().getAssociateName(), associatePartyContactMechanismName);
068                    } else {
069                        var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
070
071                        associateReferral = associateControl.createAssociateReferral(associate, associatePartyContactMechanism,
072                                entityInstanceControl.getEntityInstanceByBasePK(targetPK), session.START_TIME_LONG, partyPK);
073
074                        userVisit.setAssociateReferral(associateReferral);
075                    }
076                } else {
077                    eea.addExecutionError(ExecutionErrors.UnknownAssociateName.name(),
078                            associateProgram.getLastDetail().getAssociateProgramName(), associateName);
079                }
080            } else {
081                if(associateProgramName != null) {
082                    eea.addExecutionError(ExecutionErrors.UnknownAssociateProgramName.name(), associateProgramName);
083                } else {
084                    eea.addExecutionError(ExecutionErrors.MissingDefaultAssociateProgram.name());
085                }
086            }
087        }
088    }
089
090    public AssociateReferral getAssociateReferral(final Session session, final UserVisit userVisit) {
091        var associateReferral = userVisit == null ? null : userVisit.getAssociateReferral();
092
093        // TODO: Check the time of the referral to see if it is still in effect.
094
095        return associateReferral;
096    }
097
098}