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.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 java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class CreateZipCodeCommand
044        extends BaseSimpleCommand<CreateZipCodeForm> {
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.ZipCode.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("ZipCodeName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
061                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
062                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
063        );
064    }
065
066    @Inject
067    GeoControl geoControl;
068
069    @Inject
070    SequenceGeneratorLogic sequenceGeneratorLogic;
071
072    
073    /** Creates a new instance of CreateZipCodeCommand */
074    public CreateZipCodeCommand() {
075        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
076    }
077    
078    @Override
079    protected BaseResult execute() {
080        var result = GeoResultFactory.getCreateZipCodeResult();
081        GeoCode geoCode = null;
082
083        var countryGeoCodeName = form.getCountryGeoCodeName();
084        var countryGeoCode = geoControl.getGeoCodeByName(countryGeoCodeName);
085
086        var countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.ISO_2_LETTER.name());
087        var countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType);
088        var countryIso2Letter = countryGeoCodeAlias.getAlias();
089
090        var geoCodeScopeName = countryIso2Letter + "_ZIP_CODES";
091        var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName);
092        
093        if(geoCodeScope != null) {
094            var geoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.ZIP_CODE.name());
095            var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.ZIP_CODE.name());
096            var zipCodeName = form.getZipCodeName();
097            var geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, zipCodeName);
098            
099            if(geoCodeAlias == null) {
100                BasePK createdBy = getPartyPK();
101                var geoCodeName = sequenceGeneratorLogic.getNextSequenceValue(null, SequenceTypes.GEO_CODE.name());
102                var isDefault = Boolean.valueOf(form.getIsDefault());
103                var sortOrder = Integer.valueOf(form.getSortOrder());
104                var description = form.getDescription();
105                
106                geoCode = geoControl.createGeoCode(geoCodeName, geoCodeType, geoCodeScope, isDefault, sortOrder, createdBy);
107                geoControl.createGeoCodeRelationship(geoCode, countryGeoCode, createdBy);
108                geoControl.createGeoCodeAlias(geoCode, geoCodeAliasType, zipCodeName, createdBy);
109                
110                if(description != null) {
111                    geoControl.createGeoCodeDescription(geoCode, getPreferredLanguage(), 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}