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.CreateCountyForm; 020import com.echothree.control.user.geo.common.result.CreateCountyResult; 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 CreateCountyCommand 053 extends BaseSimpleCommand<CreateCountyForm> { 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.County.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("CountyName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("CountyNumber", FieldType.NUMBER_3, true, null, null), 070 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 071 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 072 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 073 )); 074 } 075 076 /** Creates a new instance of CreateCountyCommand */ 077 public CreateCountyCommand(UserVisitPK userVisitPK, CreateCountyForm form) { 078 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 079 } 080 081 @Override 082 protected BaseResult execute() { 083 CreateCountyResult result = GeoResultFactory.getCreateCountyResult(); 084 var geoControl = Session.getModelController(GeoControl.class); 085 BasePK createdBy = getPartyPK(); 086 GeoCode geoCode; 087 088 String stateGeoCodeName = form.getStateGeoCodeName(); 089 GeoCode stateGeoCode = geoControl.getGeoCodeByName(stateGeoCodeName); 090 091 GeoCodeAliasType stateGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(stateGeoCode.getLastDetail().getGeoCodeType(), GeoConstants.GeoCodeAliasType_POSTAL_2_LETTER); 092 GeoCodeAlias stateGeoCodeAlias = geoControl.getGeoCodeAlias(stateGeoCode, stateGeoCodeAliasType); 093 String statePostal2Letter = stateGeoCodeAlias.getAlias(); 094 095 GeoCodeType countryGeoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_COUNTRY); 096 GeoCode countryGeoCode = null; 097 Collection stateRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCode(stateGeoCode); 098 for(Iterator iter = stateRelationships.iterator(); iter.hasNext();) { 099 GeoCodeRelationship geoCodeRelationship = (GeoCodeRelationship)iter.next(); 100 GeoCode toGeoCode = geoCodeRelationship.getToGeoCode(); 101 if(toGeoCode.getLastDetail().getGeoCodeType().equals(countryGeoCodeType)) { 102 countryGeoCode = toGeoCode; 103 break; 104 } 105 } 106 107 GeoCodeAliasType countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoConstants.GeoCodeAliasType_ISO_2_LETTER); 108 GeoCodeAlias countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType); 109 String countryIso2Letter = countryGeoCodeAlias.getAlias(); 110 111 String geoCodeScopeName = new StringBuilder().append(countryIso2Letter).append("_").append(statePostal2Letter).append("_COUNTIES").toString(); 112 GeoCodeScope geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName); 113 if(geoCodeScope == null) { 114 geoCodeScope = geoControl.createGeoCodeScope(geoCodeScopeName, Boolean.FALSE, 0, getPartyPK()); 115 } 116 117 GeoCodeType geoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_COUNTY); 118 GeoCodeAliasType geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_COUNTY_NAME); 119 String countyName = form.getCountyName(); 120 GeoCodeAlias geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, countyName); 121 122 if(geoCodeAlias == null) { 123 String geoCodeName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(null, SequenceTypes.GEO_CODE.name()); 124 String countyNumber = form.getCountyNumber(); 125 var isDefault = Boolean.valueOf(form.getIsDefault()); 126 var sortOrder = Integer.valueOf(form.getSortOrder()); 127 var description = form.getDescription(); 128 129 geoCode = geoControl.createGeoCode(geoCodeName, geoCodeType, geoCodeScope, isDefault, sortOrder, createdBy); 130 geoControl.createGeoCodeRelationship(geoCode, stateGeoCode, createdBy); 131 geoControl.createGeoCodeAlias(geoCode, geoCodeAliasType, countyName, createdBy); 132 geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_COUNTY_NUMBER); 133 geoControl.createGeoCodeAlias(geoCode, geoCodeAliasType, countyNumber, createdBy); 134 135 if(description != null) { 136 Language language = getPreferredLanguage(); 137 138 geoControl.createGeoCodeDescription(geoCode, language, description, createdBy); 139 } 140 } else { 141 geoCode = geoCodeAlias.getGeoCode(); 142 } 143 144 if(geoCode != null) { 145 result.setEntityRef(geoCode.getPrimaryKey().getEntityRef()); 146 result.setGeoCodeName(geoCode.getLastDetail().getGeoCodeName()); 147 } 148 149 return result; 150 } 151 152}