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