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.CreateStateForm; 020import com.echothree.control.user.geo.common.result.CreateStateResult; 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.GeoCodeScope; 033import com.echothree.model.data.geo.server.entity.GeoCodeType; 034import com.echothree.model.data.party.server.entity.Language; 035import com.echothree.model.data.user.common.pk.UserVisitPK; 036import com.echothree.util.common.command.BaseResult; 037import com.echothree.util.common.persistence.BasePK; 038import com.echothree.util.common.validation.FieldDefinition; 039import com.echothree.util.common.validation.FieldType; 040import com.echothree.util.server.control.BaseSimpleCommand; 041import com.echothree.util.server.control.CommandSecurityDefinition; 042import com.echothree.util.server.control.PartyTypeDefinition; 043import com.echothree.util.server.control.SecurityRoleDefinition; 044import com.echothree.util.server.persistence.Session; 045import java.util.Arrays; 046import java.util.Collections; 047import java.util.List; 048 049public class CreateStateCommand 050 extends BaseSimpleCommand<CreateStateForm> { 051 052 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 053 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 054 055 static { 056 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 057 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 058 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 059 new SecurityRoleDefinition(SecurityRoleGroups.State.name(), SecurityRoles.Create.name()) 060 ))) 061 ))); 062 063 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 064 new FieldDefinition("CountryGeoCodeName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("StateName", FieldType.ENTITY_NAME, true, null, null), 066 new FieldDefinition("Postal2Letter", FieldType.UPPER_LETTER_2, true, null, null), 067 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 068 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 069 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 070 )); 071 } 072 073 /** Creates a new instance of CreateStateCommand */ 074 public CreateStateCommand(UserVisitPK userVisitPK, CreateStateForm form) { 075 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 076 } 077 078 @Override 079 protected BaseResult execute() { 080 CreateStateResult result = GeoResultFactory.getCreateStateResult(); 081 var geoControl = Session.getModelController(GeoControl.class); 082 GeoCode geoCode = null; 083 084 String countryGeoCodeName = form.getCountryGeoCodeName(); 085 GeoCode countryGeoCode = geoControl.getGeoCodeByName(countryGeoCodeName); 086 087 GeoCodeAliasType countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoConstants.GeoCodeAliasType_ISO_2_LETTER); 088 GeoCodeAlias countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType); 089 String countryIso2Letter = countryGeoCodeAlias.getAlias(); 090 091 String geoCodeScopeName = new StringBuilder().append(countryIso2Letter).append("_STATES").toString(); 092 GeoCodeScope geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName); 093 094 if(geoCodeScope != null) { 095 GeoCodeType geoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_STATE); 096 GeoCodeAliasType geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_POSTAL_2_LETTER); 097 String postal2Letter = form.getPostal2Letter(); 098 GeoCodeAlias geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, postal2Letter); 099 100 if(geoCodeAlias == null) { 101 BasePK createdBy = getPartyPK(); 102 String geoCodeName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(null, SequenceTypes.GEO_CODE.name()); 103 String stateName = form.getStateName(); 104 var isDefault = Boolean.valueOf(form.getIsDefault()); 105 var sortOrder = Integer.valueOf(form.getSortOrder()); 106 var description = form.getDescription(); 107 108 geoCode = geoControl.createGeoCode(geoCodeName, geoCodeType, geoCodeScope, isDefault, sortOrder, createdBy); 109 geoControl.createGeoCodeRelationship(geoCode, countryGeoCode, createdBy); 110 geoControl.createGeoCodeAlias(geoCode, geoCodeAliasType, postal2Letter, createdBy); 111 geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_STATE_NAME); 112 geoControl.createGeoCodeAlias(geoCode, geoCodeAliasType, stateName, createdBy); 113 114 if(description != null) { 115 Language language = getPreferredLanguage(); 116 117 geoControl.createGeoCodeDescription(geoCode, language, description, createdBy); 118 } 119 } else { 120 geoCode = geoCodeAlias.getGeoCode(); 121 } 122 } // TODO: error, unknown geoCodeScopeName 123 124 if(geoCode != null) { 125 result.setEntityRef(geoCode.getPrimaryKey().getEntityRef()); 126 result.setGeoCodeName(geoCode.getLastDetail().getGeoCodeName()); 127 } 128 129 return result; 130 } 131 132}