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.contact.server.transfer;
018
019import com.echothree.model.control.comment.common.CommentConstants;
020import com.echothree.model.control.contact.common.ContactMechanismTypes;
021import com.echothree.model.control.contact.common.ContactOptions;
022import com.echothree.model.control.contact.common.transfer.ContactMechanismTransfer;
023import com.echothree.model.control.contact.common.transfer.ContactMechanismTypeTransfer;
024import com.echothree.model.control.contact.server.control.ContactControl;
025import com.echothree.model.data.contact.server.entity.ContactMechanism;
026import com.echothree.model.data.contact.server.entity.ContactMechanismDetail;
027import com.echothree.model.data.user.server.entity.UserVisit;
028import java.util.Set;
029
030public class ContactMechanismTransferCache
031        extends BaseContactTransferCache<ContactMechanism, ContactMechanismTransfer> {
032    
033    boolean includeComments;
034
035    /** Creates a new instance of ContactMechanismTransferCache */
036    public ContactMechanismTransferCache(UserVisit userVisit, ContactControl contactControl) {
037        super(userVisit, contactControl);
038
039        var options = session.getOptions();
040        if(options != null) {
041            setIncludeKey(options.contains(ContactOptions.ContactMechanismIncludeKey));
042            setIncludeGuid(options.contains(ContactOptions.ContactMechanismIncludeGuid));
043            includeComments = options.contains(ContactOptions.ContactMechanismIncludeComments);
044            setIncludeEntityAttributeGroups(options.contains(ContactOptions.ContactMechanismIncludeEntityAttributeGroups));
045            setIncludeTagScopes(options.contains(ContactOptions.ContactMechanismIncludeTagScopes));
046        }
047        
048        setIncludeEntityInstance(true);
049    }
050    
051    public ContactMechanismTransfer getContactMechanismTransfer(ContactMechanism contactMechanism) {
052        ContactMechanismTransfer contactMechanismTransfer = get(contactMechanism);
053        
054        if(contactMechanismTransfer == null) {
055            ContactMechanismDetail contactMechanismDetail = contactMechanism.getLastDetail();
056            String contactMechanismName = contactMechanismDetail.getContactMechanismName();
057            ContactMechanismTypeTransfer contactMechanismType = contactControl.getContactMechanismTypeTransfer(userVisit,
058                    contactMechanismDetail.getContactMechanismType());
059            Boolean allowSolicitation = contactMechanismDetail.getAllowSolicitation();
060
061            contactMechanismTransfer = new ContactMechanismTransfer(contactMechanismName, contactMechanismType, allowSolicitation);
062            put(contactMechanism, contactMechanismTransfer);
063
064            String contactMechanismTypeName = contactMechanismType.getContactMechanismTypeName();
065            
066            if(contactMechanismTypeName.equals(ContactMechanismTypes.POSTAL_ADDRESS.name())) {
067                contactMechanismTransfer.setContactPostalAddress(contactControl.getContactPostalAddressTransfer(userVisit, contactControl.getContactPostalAddress(contactMechanism)) );
068            } else if(contactMechanismTypeName.equals(ContactMechanismTypes.EMAIL_ADDRESS.name())) {
069                contactMechanismTransfer.setContactEmailAddress(contactControl.getContactEmailAddressTransfer(userVisit, contactControl.getContactEmailAddress(contactMechanism)));
070            } else if(contactMechanismTypeName.equals(ContactMechanismTypes.TELECOM_ADDRESS.name())) {
071                contactMechanismTransfer.setContactTelephone(contactControl.getContactTelephoneTransfer(userVisit, contactControl.getContactTelephone(contactMechanism)));
072            } else if(contactMechanismTypeName.equals(ContactMechanismTypes.WEB_ADDRESS.name())) {
073                contactMechanismTransfer.setContactWebAddress(contactControl.getContactWebAddressTransfer(userVisit, contactControl.getContactWebAddress(contactMechanism)));
074            } else if(contactMechanismTypeName.equals(ContactMechanismTypes.INET_4.name())) {
075                contactMechanismTransfer.setContactInet4Address(contactControl.getContactInet4AddressTransfer(userVisit, contactControl.getContactInet4Address(contactMechanism)));
076            }
077            
078            if(includeComments) {
079                setupComments(contactMechanism, null, contactMechanismTransfer, CommentConstants.CommentType_CONTACT_MECHANISM);
080            }
081        }
082        
083        return contactMechanismTransfer;
084    }
085    
086}