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.control.user.geo.server.command; 018 019import com.echothree.control.user.geo.common.form.CreateCityForm; 020import com.echothree.control.user.geo.common.result.GeoResultFactory; 021import com.echothree.model.control.geo.common.GeoCodeAliasTypes; 022import com.echothree.model.control.geo.common.GeoCodeTypes; 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.control.sequence.common.SequenceTypes; 028import com.echothree.model.control.sequence.server.logic.SequenceGeneratorLogic; 029import com.echothree.model.data.geo.server.entity.GeoCode; 030import com.echothree.model.data.geo.server.entity.GeoCodeRelationship; 031import com.echothree.util.common.command.BaseResult; 032import com.echothree.util.common.persistence.BasePK; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseSimpleCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.echothree.util.server.persistence.Session; 040import java.util.Arrays; 041import java.util.Collection; 042import java.util.Collections; 043import java.util.List; 044import javax.enterprise.context.RequestScoped; 045 046@RequestScoped 047public class CreateCityCommand 048 extends BaseSimpleCommand<CreateCityForm> { 049 050 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 051 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 057 new SecurityRoleDefinition(SecurityRoleGroups.City.name(), SecurityRoles.Create.name()) 058 ))) 059 ))); 060 061 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 062 new FieldDefinition("StateGeoCodeName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("CityName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 065 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 066 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 067 )); 068 } 069 070 /** Creates a new instance of CreateCityCommand */ 071 public CreateCityCommand() { 072 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 073 } 074 075 @Override 076 protected BaseResult execute() { 077 var result = GeoResultFactory.getCreateCityResult(); 078 var geoControl = Session.getModelController(GeoControl.class); 079 BasePK createdBy = getPartyPK(); 080 GeoCode geoCode; 081 082 var stateGeoCodeName = form.getStateGeoCodeName(); 083 var stateGeoCode = geoControl.getGeoCodeByName(stateGeoCodeName); 084 085 var stateGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(stateGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.POSTAL_2_LETTER.name()); 086 var stateGeoCodeAlias = geoControl.getGeoCodeAlias(stateGeoCode, stateGeoCodeAliasType); 087 var statePostal2Letter = stateGeoCodeAlias.getAlias(); 088 089 var countryGeoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.COUNTRY.name()); 090 GeoCode countryGeoCode = null; 091 Collection stateRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCode(stateGeoCode); 092 for(var iter = stateRelationships.iterator(); iter.hasNext();) { 093 var geoCodeRelationship = (GeoCodeRelationship)iter.next(); 094 var toGeoCode = geoCodeRelationship.getToGeoCode(); 095 if(toGeoCode.getLastDetail().getGeoCodeType().equals(countryGeoCodeType)) { 096 countryGeoCode = toGeoCode; 097 break; 098 } 099 } 100 101 var countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.ISO_2_LETTER.name()); 102 var countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType); 103 var countryIso2Letter = countryGeoCodeAlias.getAlias(); 104 105 var geoCodeScopeName = countryIso2Letter + "_" + statePostal2Letter + "_CITIES"; 106 var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName); 107 if(geoCodeScope == null) { 108 geoCodeScope = geoControl.createGeoCodeScope(geoCodeScopeName, false, 0, getPartyPK()); 109 } 110 111 var geoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.CITY.name()); 112 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.CITY_NAME.name()); 113 var cityName = form.getCityName(); 114 var geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, cityName); 115 116 if(geoCodeAlias == null) { 117 var geoCodeName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(null, SequenceTypes.GEO_CODE.name()); 118 var isDefault = Boolean.valueOf(form.getIsDefault()); 119 var sortOrder = Integer.valueOf(form.getSortOrder()); 120 var description = form.getDescription(); 121 122 geoCode = geoControl.createGeoCode(geoCodeName, geoCodeType, geoCodeScope, isDefault, sortOrder, createdBy); 123 geoControl.createGeoCodeRelationship(geoCode, stateGeoCode, createdBy); 124 geoControl.createGeoCodeAlias(geoCode, geoCodeAliasType, cityName, createdBy); 125 126 if(description != null) { 127 var language = getPreferredLanguage(); 128 129 geoControl.createGeoCodeDescription(geoCode, language, description, createdBy); 130 } 131 } else { 132 geoCode = geoCodeAlias.getGeoCode(); 133 } 134 135 if(geoCode != null) { 136 result.setEntityRef(geoCode.getPrimaryKey().getEntityRef()); 137 result.setGeoCodeName(geoCode.getLastDetail().getGeoCodeName()); 138 } 139 140 return result; 141 } 142 143}