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