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 com.echothree.util.server.persistence.Session;
039import java.util.List;
040import javax.enterprise.context.Dependent;
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    /** Creates a new instance of CreateZipCodeCommand */
067    public CreateZipCodeCommand() {
068        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
069    }
070    
071    @Override
072    protected BaseResult execute() {
073        var result = GeoResultFactory.getCreateZipCodeResult();
074        var geoControl = Session.getModelController(GeoControl.class);
075        GeoCode geoCode = null;
076
077        var countryGeoCodeName = form.getCountryGeoCodeName();
078        var countryGeoCode = geoControl.getGeoCodeByName(countryGeoCodeName);
079
080        var countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.ISO_2_LETTER.name());
081        var countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType);
082        var countryIso2Letter = countryGeoCodeAlias.getAlias();
083
084        var geoCodeScopeName = countryIso2Letter + "_ZIP_CODES";
085        var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName);
086        
087        if(geoCodeScope != null) {
088            var geoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.ZIP_CODE.name());
089            var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.ZIP_CODE.name());
090            var zipCodeName = form.getZipCodeName();
091            var geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, zipCodeName);
092            
093            if(geoCodeAlias == null) {
094                BasePK createdBy = getPartyPK();
095                var geoCodeName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(null, SequenceTypes.GEO_CODE.name());
096                var isDefault = Boolean.valueOf(form.getIsDefault());
097                var sortOrder = Integer.valueOf(form.getSortOrder());
098                var description = form.getDescription();
099                
100                geoCode = geoControl.createGeoCode(geoCodeName, geoCodeType, geoCodeScope, isDefault, sortOrder, createdBy);
101                geoControl.createGeoCodeRelationship(geoCode, countryGeoCode, createdBy);
102                geoControl.createGeoCodeAlias(geoCode, geoCodeAliasType, zipCodeName, createdBy);
103                
104                if(description != null) {
105                    geoControl.createGeoCodeDescription(geoCode, getPreferredLanguage(), description, createdBy);
106                }
107            } else {
108                geoCode = geoCodeAlias.getGeoCode();
109            }
110        } // TODO: error, unknown geoCodeScopeName
111        
112        if(geoCode != null) {
113            result.setEntityRef(geoCode.getPrimaryKey().getEntityRef());
114            result.setGeoCodeName(geoCode.getLastDetail().getGeoCodeName());
115        }
116        
117        return result;
118    }
119    
120}