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.party.server.logic;
018
019import com.echothree.model.control.chain.common.ChainConstants;
020import com.echothree.model.control.chain.server.control.ChainControl;
021import com.echothree.model.control.chain.server.logic.BaseChainLogic;
022import com.echothree.model.control.core.server.control.EntityInstanceControl;
023import com.echothree.model.data.chain.server.entity.ChainInstance;
024import com.echothree.model.data.party.server.entity.Party;
025import com.echothree.util.common.persistence.BasePK;
026import com.echothree.util.server.message.ExecutionErrorAccumulator;
027import com.echothree.util.server.persistence.Session;
028import javax.enterprise.context.ApplicationScoped;
029import javax.enterprise.inject.spi.CDI;
030
031@ApplicationScoped
032public class PartyChainLogic
033        extends BaseChainLogic {
034
035    protected PartyChainLogic() {
036        super();
037    }
038
039    public static PartyChainLogic getInstance() {
040        return CDI.current().select(PartyChainLogic.class).get();
041    }
042    
043    protected ChainInstance createPartyChainInstance(final ExecutionErrorAccumulator eea, final String chainTypeName, final Party party,
044            final boolean resetChainIfRunning, final BasePK createdBy) {
045        var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName(); // Used as ChainTypeName & ChainEntityRoleTypeName
046        var chainType = getChainTypeByName(eea, partyTypeName, chainTypeName);
047        ChainInstance chainInstance = null;
048        
049        if(!hasExecutionErrors(eea)) {
050            var chainEntityRoleType = getChainEntityRoleTypeByName(eea, chainType, partyTypeName);
051            
052            if(!hasExecutionErrors(eea)) {
053                var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
054                var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(party.getPrimaryKey());
055                
056                if(resetChainIfRunning) {
057                    deleteChainInstancedByChainEntityRoleTypeAndEntityInstance(chainEntityRoleType, entityInstance, createdBy);
058                }
059                
060                chainInstance = createChainInstance(eea, chainType, party, createdBy);
061
062                if(!hasExecutionErrors(eea) && chainInstance != null) {
063                    var chainControl = Session.getModelController(ChainControl.class);
064
065                    chainControl.createChainInstanceEntityRole(chainInstance, chainEntityRoleType, entityInstance, createdBy);
066                }
067            }
068        }
069
070        return chainInstance;
071    }
072    
073    public ChainInstance createPartyWelcomeChainInstance(final ExecutionErrorAccumulator eea, final Party party, final BasePK createdBy) {
074        return createPartyChainInstance(eea, ChainConstants.ChainType_WELCOME, party, false, createdBy);
075    }
076    
077    public ChainInstance createPartyPasswordRecoveryChainInstance(final ExecutionErrorAccumulator eea, final Party party, final BasePK createdBy) {
078        return createPartyChainInstance(eea, ChainConstants.ChainType_PASSWORD_RECOVERY, party, true, createdBy);
079    }
080    
081    public ChainInstance createPartyTermChangedChainInstance(final ExecutionErrorAccumulator eea, final Party party, final BasePK createdBy) {
082        return createPartyChainInstance(eea, ChainConstants.ChainType_PARTY_TERM_CHANGED, party, false, createdBy);
083    }
084    
085    public ChainInstance createPartyCreditLimitChangedChainInstance(final ExecutionErrorAccumulator eea, final Party party, final BasePK createdBy) {
086        return createPartyChainInstance(eea, ChainConstants.ChainType_PARTY_CREDIT_LIMIT_CHANGED, party, false, createdBy);
087    }
088    
089    public ChainInstance createPartyCreditStatusChangedChainInstance(final ExecutionErrorAccumulator eea, final Party party, final BasePK createdBy) {
090        return createPartyChainInstance(eea, ChainConstants.ChainType_PARTY_CREDIT_STATUS_CHANGED, party, false, createdBy);
091    }
092    
093}