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.CreateZipCodeForm; 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 CreateZipCodeCommand 046 extends BaseSimpleCommand<CreateZipCodeForm> { 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.ZipCode.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("ZipCodeName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 063 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 064 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 065 )); 066 } 067 068 /** Creates a new instance of CreateZipCodeCommand */ 069 public CreateZipCodeCommand() { 070 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 071 } 072 073 @Override 074 protected BaseResult execute() { 075 var result = GeoResultFactory.getCreateZipCodeResult(); 076 var geoControl = Session.getModelController(GeoControl.class); 077 GeoCode geoCode = null; 078 079 var countryGeoCodeName = form.getCountryGeoCodeName(); 080 var countryGeoCode = geoControl.getGeoCodeByName(countryGeoCodeName); 081 082 var countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.ISO_2_LETTER.name()); 083 var countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType); 084 var countryIso2Letter = countryGeoCodeAlias.getAlias(); 085 086 var geoCodeScopeName = countryIso2Letter + "_ZIP_CODES"; 087 var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName); 088 089 if(geoCodeScope != null) { 090 var geoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.ZIP_CODE.name()); 091 var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.ZIP_CODE.name()); 092 var zipCodeName = form.getZipCodeName(); 093 var geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, zipCodeName); 094 095 if(geoCodeAlias == null) { 096 BasePK createdBy = getPartyPK(); 097 var geoCodeName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(null, SequenceTypes.GEO_CODE.name()); 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, zipCodeName, createdBy); 105 106 if(description != null) { 107 geoControl.createGeoCodeDescription(geoCode, getPreferredLanguage(), description, createdBy); 108 } 109 } else { 110 geoCode = geoCodeAlias.getGeoCode(); 111 } 112 } // TODO: error, unknown geoCodeScopeName 113 114 if(geoCode != null) { 115 result.setEntityRef(geoCode.getPrimaryKey().getEntityRef()); 116 result.setGeoCodeName(geoCode.getLastDetail().getGeoCodeName()); 117 } 118 119 return result; 120 } 121 122}