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.GeoOptions;
020import com.echothree.model.control.geo.common.transfer.GeoCodeScopeTransfer;
021import com.echothree.model.control.geo.common.transfer.GeoCodeTransfer;
022import com.echothree.model.control.geo.common.transfer.GeoCodeTypeTransfer;
023import com.echothree.model.control.geo.server.control.GeoControl;
024import com.echothree.model.data.geo.server.entity.GeoCode;
025import com.echothree.model.data.geo.server.entity.GeoCodeDetail;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import java.util.Set;
028
029public class GeoCodeTransferCache
030        extends BaseGeoCodeTransferCache<GeoCode, GeoCodeTransfer> {
031    
032    boolean includeAliases;
033    
034    /** Creates a new instance of GeoCodeTransferCache */
035    public GeoCodeTransferCache(UserVisit userVisit, GeoControl geoControl) {
036        super(userVisit, geoControl);
037        
038        var options = session.getOptions();
039        if(options != null) {
040            includeAliases = options.contains(GeoOptions.GeoCodeIncludeAliases);
041        }
042        
043        setIncludeEntityInstance(true);
044    }
045    
046    public GeoCodeTransfer getGeoCodeTransfer(GeoCode geoCode) {
047        GeoCodeTransfer geoCodeTransfer = get(geoCode);
048        
049        if(geoCodeTransfer == null) {
050            GeoCodeDetail geoCodeDetail = geoCode.getLastDetail();
051            String geoCodeName = geoCodeDetail.getGeoCodeName();
052            GeoCodeTypeTransfer geoCodeType = geoControl.getGeoCodeTypeTransfer(userVisit, geoCodeDetail.getGeoCodeType());
053            GeoCodeScopeTransfer geoCodeScope = geoControl.getGeoCodeScopeTransfer(userVisit, geoCodeDetail.getGeoCodeScope());
054            Boolean isDefault = geoCodeDetail.getIsDefault();
055            Integer sortOrder = geoCodeDetail.getSortOrder();
056            String description = geoControl.getBestGeoCodeDescription(geoCode, getLanguage());
057            
058            geoCodeTransfer = new GeoCodeTransfer(geoCodeName, geoCodeType, geoCodeScope, isDefault, sortOrder, description);
059            put(geoCode, geoCodeTransfer);
060            
061            if(includeAliases) {
062                setupGeoCodeAliasTransfers(geoCode, geoCodeTransfer);
063            }
064        }
065        
066        return geoCodeTransfer;
067    }
068    
069}