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