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.contactlist.server.transfer;
018
019import com.echothree.model.control.comment.common.CommentConstants;
020import com.echothree.model.control.contactlist.common.ContactListOptions;
021import com.echothree.model.control.contactlist.common.transfer.ContactListContactMechanismPurposeTransfer;
022import com.echothree.model.control.contactlist.common.transfer.ContactListTransfer;
023import com.echothree.model.control.contactlist.common.transfer.PartyContactListTransfer;
024import com.echothree.model.control.contactlist.server.ContactListControl;
025import com.echothree.model.control.core.server.control.CoreControl;
026import com.echothree.model.control.party.common.transfer.PartyTransfer;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.control.workflow.server.control.WorkflowControl;
029import com.echothree.model.data.contactlist.server.entity.ContactList;
030import com.echothree.model.data.contactlist.server.entity.ContactListContactMechanismPurpose;
031import com.echothree.model.data.contactlist.server.entity.PartyContactList;
032import com.echothree.model.data.contactlist.server.entity.PartyContactListDetail;
033import com.echothree.model.data.core.server.entity.EntityInstance;
034import com.echothree.model.data.user.server.entity.UserVisit;
035import com.echothree.model.data.workflow.server.entity.Workflow;
036import com.echothree.util.server.persistence.Session;
037import java.util.Set;
038
039public class PartyContactListTransferCache
040        extends BaseContactListTransferCache<PartyContactList, PartyContactListTransfer> {
041    
042    CoreControl coreControl = Session.getModelController(CoreControl.class);
043    PartyControl partyControl = Session.getModelController(PartyControl.class);
044    WorkflowControl workflowControl = Session.getModelController(WorkflowControl.class);
045    
046    boolean includeStatus;
047    boolean includeComments;
048    
049    /** Creates a new instance of PartyContactListTransferCache */
050    public PartyContactListTransferCache(UserVisit userVisit, ContactListControl contactListControl) {
051        super(userVisit, contactListControl);
052        
053        var options = session.getOptions();
054        if(options != null) {
055            setIncludeKey(options.contains(ContactListOptions.PartyContactListIncludeKey));
056            setIncludeGuid(options.contains(ContactListOptions.PartyContactListIncludeGuid));
057            includeStatus = options.contains(ContactListOptions.PartyContactListIncludeStatus);
058            includeComments = options.contains(ContactListOptions.PartyContactListIncludeComments);
059        }
060        
061        setIncludeEntityInstance(true);
062    }
063    
064    public PartyContactListTransfer getPartyContactListTransfer(PartyContactList partyContactList) {
065        PartyContactListTransfer partyContactListTransfer = get(partyContactList);
066        
067        if(partyContactListTransfer == null) {
068            PartyContactListDetail partyContactListDetail = partyContactList.getLastDetail();
069            PartyTransfer partyTransfer = partyControl.getPartyTransfer(userVisit, partyContactListDetail.getParty());
070            ContactList contactList = partyContactListDetail.getContactList();
071            ContactListTransfer contactListTransfer = contactListControl.getContactListTransfer(userVisit, contactList);
072            ContactListContactMechanismPurpose preferredContactListContactMechanismPurpose = partyContactListDetail.getPreferredContactListContactMechanismPurpose();
073            ContactListContactMechanismPurposeTransfer preferredContactListContactMechanismPurposeTransfer = preferredContactListContactMechanismPurpose == null ? null : contactListControl.getContactListContactMechanismPurposeTransfer(userVisit, preferredContactListContactMechanismPurpose);
074            EntityInstance entityInstance = coreControl.getEntityInstanceByBasePK(partyContactList.getPrimaryKey());
075            
076            partyContactListTransfer = new PartyContactListTransfer(partyTransfer, contactListTransfer, preferredContactListContactMechanismPurposeTransfer);
077            put(partyContactList, partyContactListTransfer, entityInstance);
078            
079            if(includeStatus) {
080                Workflow workflow = contactList.getLastDetail().getDefaultPartyContactListStatus().getLastDetail().getWorkflow();
081                
082                if(workflow != null) {
083                    partyContactListTransfer.setPartyContactListStatus(workflowControl.getWorkflowEntityStatusTransferByEntityInstance(userVisit, workflow,
084                            entityInstance));
085                }
086            }
087
088            if(includeComments) {
089                setupComments(partyContactList, entityInstance, partyContactListTransfer, CommentConstants.CommentType_PARTY_CONTACT_LIST);
090            }
091        }
092        
093        return partyContactListTransfer;
094    }
095    
096}