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.geo.common.GeoConstants;
020import com.echothree.model.control.geo.common.GeoOptions;
021import com.echothree.model.control.geo.common.transfer.CountyTransfer;
022import com.echothree.model.control.geo.common.transfer.GeoCodeScopeTransfer;
023import com.echothree.model.control.geo.common.transfer.GeoCodeTypeTransfer;
024import com.echothree.model.control.geo.common.transfer.StateTransfer;
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.GeoCodeDetail;
028import com.echothree.model.data.geo.server.entity.GeoCodeRelationship;
029import com.echothree.model.data.geo.server.entity.GeoCodeType;
030import com.echothree.model.data.user.server.entity.UserVisit;
031import java.util.List;
032import java.util.Set;
033
034public class CountyTransferCache
035        extends BaseGeoCodeTransferCache<GeoCode, CountyTransfer> {
036    
037    boolean includeAliases;
038    
039    /** Creates a new instance of CountyTransferCache */
040    public CountyTransferCache(UserVisit userVisit, GeoControl geoControl) {
041        super(userVisit, geoControl);
042        
043        var options = session.getOptions();
044        if(options != null) {
045            includeAliases = options.contains(GeoOptions.CountyIncludeAliases);
046        }
047        
048        setIncludeEntityInstance(true);
049    }
050    
051    public CountyTransfer getCountyTransfer(GeoCode geoCode) {
052        CountyTransfer countyTransfer = get(geoCode);
053        
054        if(countyTransfer == null) {
055            GeoCodeDetail geoCodeDetail = geoCode.getLastDetail();
056            String geoCodeName = geoCodeDetail.getGeoCodeName();
057            GeoCodeTypeTransfer geoCodeType = geoControl.getGeoCodeTypeTransfer(userVisit, geoCodeDetail.getGeoCodeType());
058            GeoCodeScopeTransfer geoCodeScope = geoControl.getGeoCodeScopeTransfer(userVisit, geoCodeDetail.getGeoCodeScope());
059            Boolean isDefault = geoCodeDetail.getIsDefault();
060            Integer sortOrder = geoCodeDetail.getSortOrder();
061            String description = geoControl.getBestGeoCodeDescription(geoCode, getLanguage());
062            
063            GeoCodeType stateGeoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_STATE);
064            List<GeoCodeRelationship> geoCodeRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCodeAndGeoCodeType(geoCode, stateGeoCodeType);
065            if(geoCodeRelationships.size() != 1) {
066                getLog().error("non-1 geoCodeRelationships.size()");
067            }
068            StateTransfer state = geoControl.getStateTransfer(userVisit, geoCodeRelationships.iterator().next().getToGeoCode());
069            
070            countyTransfer = new CountyTransfer(state, geoCodeName, geoCodeType, geoCodeScope, isDefault, sortOrder, description);
071            put(geoCode, countyTransfer);
072            
073            if(includeAliases) {
074                setupGeoCodeAliasTransfers(geoCode, countyTransfer);
075            }
076        }
077        
078        return countyTransfer;
079    }
080    
081}