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