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