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