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.control.user.geo.server.command; 018 019import com.echothree.control.user.geo.common.form.GetCityForm; 020import com.echothree.control.user.geo.common.result.GeoResultFactory; 021import com.echothree.model.control.core.common.EventTypes; 022import com.echothree.model.control.geo.common.GeoConstants; 023import com.echothree.model.control.geo.server.control.GeoControl; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.data.geo.server.entity.GeoCode; 028import com.echothree.model.data.geo.server.entity.GeoCodeRelationship; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.server.control.BaseSingleEntityCommand; 035import com.echothree.util.server.control.CommandSecurityDefinition; 036import com.echothree.util.server.control.PartyTypeDefinition; 037import com.echothree.util.server.control.SecurityRoleDefinition; 038import com.echothree.util.server.persistence.Session; 039import java.util.Arrays; 040import java.util.Collections; 041import java.util.List; 042 043public class GetCityCommand 044 extends BaseSingleEntityCommand<GeoCode, GetCityForm> { 045 046 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 047 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 048 049 static { 050 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 051 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 052 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 053 new PartyTypeDefinition(PartyTypes.VENDOR.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 055 new SecurityRoleDefinition(SecurityRoleGroups.City.name(), SecurityRoles.Review.name()) 056 ))) 057 ))); 058 059 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 060 new FieldDefinition("StateGeoCodeName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("CityName", FieldType.ENTITY_NAME, true, null, null) 062 )); 063 } 064 065 /** Creates a new instance of GetCityCommand */ 066 public GetCityCommand(UserVisitPK userVisitPK, GetCityForm form) { 067 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 068 } 069 070 @Override 071 protected GeoCode getEntity() { 072 var geoControl = Session.getModelController(GeoControl.class); 073 var createdBy = getPartyPK(); 074 GeoCode geoCode = null; 075 076 var stateGeoCodeName = form.getStateGeoCodeName(); 077 var stateGeoCode = geoControl.getGeoCodeByName(stateGeoCodeName); 078 079 var stateGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(stateGeoCode.getLastDetail().getGeoCodeType(), GeoConstants.GeoCodeAliasType_POSTAL_2_LETTER); 080 var stateGeoCodeAlias = geoControl.getGeoCodeAlias(stateGeoCode, stateGeoCodeAliasType); 081 var statePostal2Letter = stateGeoCodeAlias.getAlias(); 082 083 var countryGeoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_COUNTRY); 084 GeoCode countryGeoCode = null; 085 var stateRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCode(stateGeoCode); 086 for(var geoCodeRelationship : stateRelationships) { 087 GeoCode toGeoCode = geoCodeRelationship.getToGeoCode(); 088 if(toGeoCode.getLastDetail().getGeoCodeType().equals(countryGeoCodeType)) { 089 countryGeoCode = toGeoCode; 090 break; 091 } 092 } 093 094 var countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoConstants.GeoCodeAliasType_ISO_2_LETTER); 095 var countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType); 096 var countryIso2Letter = countryGeoCodeAlias.getAlias(); 097 098 var geoCodeScopeName = countryIso2Letter + "_" + statePostal2Letter + "_CITIES"; 099 var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName); 100 if(geoCodeScope == null) { 101 geoCodeScope = geoControl.createGeoCodeScope(geoCodeScopeName, Boolean.FALSE, 0, createdBy); 102 } 103 104 var geoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_CITY); 105 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_CITY_NAME); 106 var cityName = form.getCityName(); 107 var geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, cityName); 108 109 if(geoCodeAlias != null) { 110 geoCode = geoCodeAlias.getGeoCode(); 111 112 sendEvent(geoCode.getPrimaryKey(), EventTypes.READ, null, null, createdBy); 113 } else { 114 addExecutionError(ExecutionErrors.UnknownCityName.name()); 115 } 116 117 return geoCode; 118 } 119 120 @Override 121 protected BaseResult getResult(GeoCode entity) { 122 var result = GeoResultFactory.getGetCityResult(); 123 124 if(entity != null) { 125 var geoControl = Session.getModelController(GeoControl.class); 126 127 result.setCity(geoControl.getCityTransfer(getUserVisit(), entity)); 128 } 129 130 return result; 131 } 132 133}