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.geo.server.transfer;
018
019import com.echothree.model.control.contact.common.transfer.PostalAddressFormatTransfer;
020import com.echothree.model.control.contact.server.control.ContactControl;
021import com.echothree.model.control.geo.common.GeoOptions;
022import com.echothree.model.control.geo.common.transfer.CountryTransfer;
023import com.echothree.model.control.geo.common.transfer.GeoCodeScopeTransfer;
024import com.echothree.model.control.geo.common.transfer.GeoCodeTypeTransfer;
025import com.echothree.model.control.geo.server.control.GeoControl;
026import com.echothree.model.data.geo.server.entity.GeoCode;
027import com.echothree.model.data.geo.server.entity.GeoCodeCountry;
028import com.echothree.model.data.geo.server.entity.GeoCodeDetail;
029import com.echothree.model.data.user.server.entity.UserVisit;
030import com.echothree.util.server.persistence.Session;
031import java.util.Set;
032
033public class CountryTransferCache
034        extends BaseGeoCodeTransferCache<GeoCode, CountryTransfer> {
035    
036    ContactControl contactControl = Session.getModelController(ContactControl.class);
037    boolean includeAliases;
038    
039    /** Creates a new instance of CountryTransferCache */
040    public CountryTransferCache(UserVisit userVisit, GeoControl geoControl) {
041        super(userVisit, geoControl);
042        
043        var options = session.getOptions();
044        if(options != null) {
045            includeAliases = options.contains(GeoOptions.CountryIncludeAliases);
046        }
047        
048        setIncludeEntityInstance(true);
049    }
050    
051    public CountryTransfer getCountryTransfer(GeoCode geoCode) {
052        CountryTransfer countryTransfer = get(geoCode);
053        
054        if(countryTransfer == null) {
055            GeoCodeDetail geoCodeDetail = geoCode.getLastDetail();
056            GeoCodeCountry geoCodeCountry = geoControl.getGeoCodeCountry(geoCode);
057            String geoCodeName = geoCodeDetail.getGeoCodeName();
058            GeoCodeTypeTransfer geoCodeType = geoControl.getGeoCodeTypeTransfer(userVisit, geoCodeDetail.getGeoCodeType());
059            GeoCodeScopeTransfer geoCodeScope = geoControl.getGeoCodeScopeTransfer(userVisit, geoCodeDetail.getGeoCodeScope());
060            Boolean isDefault = geoCodeDetail.getIsDefault();
061            Integer sortOrder = geoCodeDetail.getSortOrder();
062            String telephoneCode = geoCodeCountry.getTelephoneCode();
063            String areaCodePattern = geoCodeCountry.getAreaCodePattern();
064            Boolean areaCodeRequired = geoCodeCountry.getAreaCodeRequired();
065            String areaCodeExample = geoCodeCountry.getAreaCodeExample();
066            String telephoneNumberPattern = geoCodeCountry.getTelephoneNumberPattern();
067            String telephoneNumberExample = geoCodeCountry.getTelephoneNumberExample();
068            PostalAddressFormatTransfer postalAddressFormat = contactControl.getPostalAddressFormatTransfer(userVisit, geoCodeCountry.getPostalAddressFormat());
069            Boolean cityRequired = geoCodeCountry.getCityRequired();
070            Boolean cityGeoCodeRequired = geoCodeCountry.getCityGeoCodeRequired();
071            Boolean stateRequired = geoCodeCountry.getStateRequired();
072            Boolean stateGeoCodeRequired = geoCodeCountry.getCityGeoCodeRequired();
073            String postalCodePattern = geoCodeCountry.getPostalCodePattern();
074            Boolean postalCodeRequired = geoCodeCountry.getPostalCodeRequired();
075            Boolean postalCodeGeoCodeRequired = geoCodeCountry.getPostalCodeGeoCodeRequired();
076            Integer postalCodeLength = geoCodeCountry.getPostalCodeLength();
077            Integer postalCodeGeoCodeLength = geoCodeCountry.getPostalCodeGeoCodeLength();
078            String postalCodeExample = geoCodeCountry.getPostalCodeExample();
079            String description = geoControl.getBestGeoCodeDescription(geoCode, getLanguage());
080            
081            countryTransfer = new CountryTransfer(geoCodeName, geoCodeType, geoCodeScope, isDefault, sortOrder, telephoneCode,
082                    areaCodePattern, areaCodeRequired, areaCodeExample, telephoneNumberPattern, telephoneNumberExample,
083                    postalAddressFormat, cityRequired, cityGeoCodeRequired, stateRequired, stateGeoCodeRequired, postalCodePattern,
084                    postalCodeRequired, postalCodeGeoCodeRequired, postalCodeLength, postalCodeGeoCodeLength, postalCodeExample,
085                    description);
086            put(geoCode, countryTransfer);
087            
088            if(includeAliases) {
089                setupGeoCodeAliasTransfers(geoCode, countryTransfer);
090            }
091        }
092        
093        return countryTransfer;
094    }
095    
096}