001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.contact.common.transfer.ContactPostalAddressTransfer;
020import com.echothree.model.control.contact.common.workflow.PostalAddressStatusConstants;
021import com.echothree.model.control.core.server.control.EntityInstanceControl;
022import com.echothree.model.control.geo.server.control.GeoControl;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.control.workflow.server.control.WorkflowControl;
025import com.echothree.model.data.contact.server.entity.ContactPostalAddress;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import com.echothree.util.server.persistence.Session;
028import javax.enterprise.context.RequestScoped;
029
030@RequestScoped
031public class ContactPostalAddressTransferCache
032        extends BaseContactTransferCache<ContactPostalAddress, ContactPostalAddressTransfer> {
033
034    EntityInstanceControl entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
035    WorkflowControl workflowControl = Session.getModelController(WorkflowControl.class);
036    
037    /** Creates a new instance of ContactPostalAddressTransferCache */
038    protected ContactPostalAddressTransferCache() {
039        super();
040    }
041    
042    public ContactPostalAddressTransfer getContactPostalAddressTransfer(UserVisit userVisit, ContactPostalAddress contactPostalAddress) {
043        var contactPostalAddressTransfer = get(contactPostalAddress);
044        
045        if(contactPostalAddressTransfer == null) {
046            var geoControl = Session.getModelController(GeoControl.class);
047            var partyControl = Session.getModelController(PartyControl.class);
048            var personalTitle = contactPostalAddress.getPersonalTitle();
049            var personalTitleTransfer = personalTitle == null? null: partyControl.getPersonalTitleTransfer(userVisit, personalTitle);
050            var firstName = contactPostalAddress.getFirstName();
051            var middleName = contactPostalAddress.getMiddleName();
052            var lastName = contactPostalAddress.getLastName();
053            var nameSuffix = contactPostalAddress.getNameSuffix();
054            var nameSuffixTransfer = nameSuffix == null? null: partyControl.getNameSuffixTransfer(userVisit, nameSuffix);
055            var companyName = contactPostalAddress.getCompanyName();
056            var attention = contactPostalAddress.getAttention();
057            var address1 = contactPostalAddress.getAddress1();
058            var address2 = contactPostalAddress.getAddress2();
059            var address3 = contactPostalAddress.getAddress3();
060            var countyGeoCode = contactPostalAddress.getCountyGeoCode();
061            var countyGeoCodeTransfer = countyGeoCode == null? null: geoControl.getCountyTransfer(userVisit, countyGeoCode);
062            var countryGeoCodeTransfer = geoControl.getCountryTransfer(userVisit, contactPostalAddress.getCountryGeoCode());
063            var isCommercial = contactPostalAddress.getIsCommercial();
064
065            var city = contactPostalAddress.getCity();
066            var cityGeoCode = contactPostalAddress.getCityGeoCode();
067            var cityGeoCodeTransfer = cityGeoCode == null? null: geoControl.getCityTransfer(userVisit, cityGeoCode);
068            
069            if(city == null && cityGeoCode != null) {
070                city = geoControl.getBestGeoCodeDescription(cityGeoCode, getLanguage(userVisit));
071                
072                if(city == null) {
073                    city = geoControl.getAliasForCity(cityGeoCode);
074                }
075            }
076
077            var state = contactPostalAddress.getState();
078            var stateGeoCode = contactPostalAddress.getStateGeoCode();
079            var stateGeoCodeTransfer = stateGeoCode == null? null: geoControl.getStateTransfer(userVisit, stateGeoCode);
080            
081            if(state == null && stateGeoCode != null) {
082                state = geoControl.getBestGeoCodeDescription(stateGeoCode, getLanguage(userVisit));
083                
084                if(state == null) {
085                    state = geoControl.getAliasForState(stateGeoCode);
086                }
087            }
088
089            var postalCode = contactPostalAddress.getPostalCode();
090            var postalCodeGeoCode = contactPostalAddress.getPostalCodeGeoCode();
091            var postalCodeGeoCodeTransfer = postalCodeGeoCode == null? null: geoControl.getPostalCodeTransfer(userVisit, postalCodeGeoCode);
092            
093            if(postalCode == null && postalCodeGeoCode != null) {
094                postalCode = geoControl.getBestGeoCodeDescription(postalCodeGeoCode, getLanguage(userVisit));
095                
096                if(postalCode == null) {
097                    postalCode = geoControl.getAliasForPostalCode(postalCodeGeoCode);
098                }
099            }
100
101            var entityInstance = entityInstanceControl.getEntityInstanceByBasePK(contactPostalAddress.getContactMechanismPK());
102            var postalAddressStatusTransfer = workflowControl.getWorkflowEntityStatusTransferByEntityInstanceUsingNames(userVisit,
103                    PostalAddressStatusConstants.Workflow_POSTAL_ADDRESS_STATUS, entityInstance);
104            
105            contactPostalAddressTransfer = new ContactPostalAddressTransfer(personalTitleTransfer, firstName, middleName, lastName,
106                    nameSuffixTransfer, companyName, attention, address1, address2, address3, city, cityGeoCodeTransfer, countyGeoCodeTransfer,
107                    state, stateGeoCodeTransfer, postalCode, postalCodeGeoCodeTransfer, countryGeoCodeTransfer, isCommercial,
108                    postalAddressStatusTransfer);
109            put(userVisit, contactPostalAddress, contactPostalAddressTransfer);
110        }
111        
112        return contactPostalAddressTransfer;
113    }
114    
115}