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.model.control.contactlist.server.logic;
018
019import com.echothree.model.control.chain.common.ChainKinds;
020import com.echothree.model.control.chain.common.ChainTypes;
021import com.echothree.model.control.chain.common.ChainEntityRoleTypes;
022import com.echothree.model.control.chain.server.control.ChainControl;
023import com.echothree.model.control.chain.server.logic.ChainInstanceLogic;
024import com.echothree.model.control.chain.server.logic.ChainLogic;
025import com.echothree.model.control.chain.server.logic.ChainTypeLogic;
026import com.echothree.model.data.chain.server.entity.Chain;
027import com.echothree.model.data.chain.server.entity.ChainInstance;
028import com.echothree.model.data.contactlist.server.entity.PartyContactList;
029import com.echothree.model.data.party.server.entity.Party;
030import com.echothree.util.common.persistence.BasePK;
031import com.echothree.util.server.control.BaseLogic;
032import com.echothree.util.server.message.ExecutionErrorAccumulator;
033import com.echothree.util.server.persistence.Session;
034import javax.enterprise.context.ApplicationScoped;
035import javax.enterprise.inject.spi.CDI;
036import javax.inject.Inject;
037
038@ApplicationScoped
039public class ContactListChainLogic
040        extends BaseLogic {
041
042    @Inject
043    protected ChainLogic chainLogic;
044
045    @Inject
046    protected ChainTypeLogic chainTypeLogic;
047
048    @Inject
049    protected ChainInstanceLogic chainInstanceLogic;
050
051    protected ContactListChainLogic() {
052        super();
053    }
054
055    public static ContactListChainLogic getInstance() {
056        return CDI.current().select(ContactListChainLogic.class).get();
057    }
058    
059    /** In looking for the Chain, the following are checked:
060     * 1) Check for PartyTypeContactList, if exists, use it; skip 2-3.
061     * 2) Check for PartyTypeContactListGroup, if exists, use it; skip 3.
062     * 3) Check for default for the particular type of chain needed.
063     */
064    protected ChainInstance createChainInstance(final ExecutionErrorAccumulator eea, final String chainKindName, final String chainTypeName,
065            final PartyContactList partyContactList, final BasePK createdBy) {
066        var partyContactListDetail = partyContactList.getLastDetail();
067        var party = partyContactListDetail.getParty();
068        var contactListTypeDetail = partyContactListDetail.getContactList().getLastDetail().getContactListType().getLastDetail();
069        Chain chain = null;
070        ChainInstance chainInstance = null;
071        
072        if(chainTypeName.equals(ChainTypes.CONFIRMATION_REQUEST.name())) {
073            chain = contactListTypeDetail.getConfirmationRequestChain();
074        } else if(chainTypeName.equals(ChainTypes.SUBSCRIBE.name())) {
075            chain = contactListTypeDetail.getSubscribeChain();
076        } else if(chainTypeName.equals(ChainTypes.UNSUBSCRIBE.name())) {
077            chain = contactListTypeDetail.getUnsubscribeChain();
078        }
079        
080        if(chain == null) {
081            var chainType = chainTypeLogic.getChainTypeByName(eea, chainKindName, chainTypeName);
082
083            if(chainType != null) {
084                chain = chainLogic.getChain(eea, chainType, party);
085            }
086        }
087        
088        if(chain != null) {
089            chainInstance = chainInstanceLogic.createChainInstance(eea, chain, createdBy);
090        }
091        
092        return chainInstance;
093    }
094    
095    protected ChainInstance createContactListChainInstance(final ExecutionErrorAccumulator eea, final String chainTypeName,
096            final PartyContactList partyContactList, final BasePK createdBy) {
097        var chainInstance = createChainInstance(eea, ChainKinds.CONTACT_LIST.name(), chainTypeName, partyContactList, createdBy);
098        
099        if(chainInstance != null) {
100            var chainControl = Session.getModelController(ChainControl.class);
101            var chainType = chainInstance.getLastDetail().getChain().getLastDetail().getChainType();
102            
103            chainControl.createChainInstanceEntityRole(chainInstance, chainControl.getChainEntityRoleTypeByName(chainType,
104                    ChainEntityRoleTypes.PARTY_CONTACT_LIST.name()), partyContactList.getPrimaryKey(), createdBy);
105        }
106        
107        return chainInstance;
108    }
109    
110    public ChainInstance createContactListConfirmationChainInstance(final ExecutionErrorAccumulator eea, final Party party, final PartyContactList partyContactList,
111            final BasePK createdBy) {
112        return createContactListChainInstance(eea, ChainTypes.CONFIRMATION_REQUEST.name(), partyContactList, createdBy);
113    }
114    
115    public ChainInstance createContactListSubscribeChainInstance(final ExecutionErrorAccumulator eea, final Party party, final PartyContactList partyContactList,
116            final BasePK createdBy) {
117        return createContactListChainInstance(eea, ChainTypes.SUBSCRIBE.name(), partyContactList, createdBy);
118    }
119    
120    public ChainInstance createContactListUnsubscribeChainInstance(final ExecutionErrorAccumulator eea, final Party party, final PartyContactList partyContactList,
121            final BasePK createdBy) {
122        return createContactListChainInstance(eea, ChainTypes.UNSUBSCRIBE.name(), partyContactList, createdBy);
123    }
124    
125}