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