001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.GeoCodeTypes; 020import com.echothree.model.control.geo.common.GeoOptions; 021import com.echothree.model.control.geo.common.transfer.StateTransfer; 022import com.echothree.model.data.geo.server.entity.GeoCode; 023import com.echothree.model.data.user.server.entity.UserVisit; 024import javax.enterprise.context.RequestScoped; 025 026@RequestScoped 027public class StateTransferCache 028 extends BaseGeoCodeTransferCache<GeoCode, StateTransfer> { 029 030 boolean includeAliases; 031 032 /** Creates a new instance of StateTransferCache */ 033 protected StateTransferCache() { 034 super(); 035 036 var options = session.getOptions(); 037 if(options != null) { 038 includeAliases = options.contains(GeoOptions.StateIncludeAliases); 039 } 040 041 setIncludeEntityInstance(true); 042 } 043 044 public StateTransfer getStateTransfer(UserVisit userVisit, GeoCode geoCode) { 045 var stateTransfer = get(geoCode); 046 047 if(stateTransfer == null) { 048 var geoCodeDetail = geoCode.getLastDetail(); 049 var geoCodeName = geoCodeDetail.getGeoCodeName(); 050 var geoCodeType = geoControl.getGeoCodeTypeTransfer(userVisit, geoCodeDetail.getGeoCodeType()); 051 var geoCodeScope = geoControl.getGeoCodeScopeTransfer(userVisit, geoCodeDetail.getGeoCodeScope()); 052 var isDefault = geoCodeDetail.getIsDefault(); 053 var sortOrder = geoCodeDetail.getSortOrder(); 054 var description = geoControl.getBestGeoCodeDescription(geoCode, getLanguage(userVisit)); 055 056 var countryGeoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.COUNTRY.name()); 057 var geoCodeRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCodeAndGeoCodeType(geoCode, countryGeoCodeType); 058 if(geoCodeRelationships.size() != 1) { 059 getLog().error("non-1 geoCodeRelationships.size()"); 060 } 061 var country = geoControl.getCountryTransfer(userVisit, geoCodeRelationships.getFirst().getToGeoCode()); 062 063 stateTransfer = new StateTransfer(country, geoCodeName, geoCodeType, geoCodeScope, isDefault, sortOrder, description); 064 put(userVisit, geoCode, stateTransfer); 065 066 if(includeAliases) { 067 setupGeoCodeAliasTransfers(userVisit, geoCode, stateTransfer); 068 } 069 } 070 071 return stateTransfer; 072 } 073 074}