001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.CreateZipCodeResult;
021import com.echothree.control.user.geo.common.result.GeoResultFactory;
022import com.echothree.model.control.geo.common.GeoConstants;
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.model.data.geo.server.entity.GeoCodeAlias;
031import com.echothree.model.data.geo.server.entity.GeoCodeAliasType;
032import com.echothree.model.data.geo.server.entity.GeoCodeScope;
033import com.echothree.model.data.geo.server.entity.GeoCodeType;
034import com.echothree.model.data.user.common.pk.UserVisitPK;
035import com.echothree.util.common.command.BaseResult;
036import com.echothree.util.common.persistence.BasePK;
037import com.echothree.util.common.validation.FieldDefinition;
038import com.echothree.util.common.validation.FieldType;
039import com.echothree.util.server.control.BaseSimpleCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import com.echothree.util.server.persistence.Session;
044import java.util.Arrays;
045import java.util.Collections;
046import java.util.List;
047
048public class CreateZipCodeCommand
049        extends BaseSimpleCommand<CreateZipCodeForm> {
050    
051    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
052    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
053    
054    static {
055        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
056                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
057                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
058                        new SecurityRoleDefinition(SecurityRoleGroups.ZipCode.name(), SecurityRoles.Create.name())
059                        )))
060                )));
061        
062        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
063                new FieldDefinition("CountryGeoCodeName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("ZipCodeName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
066                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
067                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
068                ));
069    }
070    
071    /** Creates a new instance of CreateZipCodeCommand */
072    public CreateZipCodeCommand(UserVisitPK userVisitPK, CreateZipCodeForm form) {
073        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
074    }
075    
076    @Override
077    protected BaseResult execute() {
078        CreateZipCodeResult result = GeoResultFactory.getCreateZipCodeResult();
079        var geoControl = Session.getModelController(GeoControl.class);
080        GeoCode geoCode = null;
081        
082        String countryGeoCodeName = form.getCountryGeoCodeName();
083        GeoCode countryGeoCode = geoControl.getGeoCodeByName(countryGeoCodeName);
084        
085        GeoCodeAliasType countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoConstants.GeoCodeAliasType_ISO_2_LETTER);
086        GeoCodeAlias countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType);
087        String countryIso2Letter = countryGeoCodeAlias.getAlias();
088        
089        String geoCodeScopeName = new StringBuilder().append(countryIso2Letter).append("_ZIP_CODES").toString();
090        GeoCodeScope geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName);
091        
092        if(geoCodeScope != null) {
093            GeoCodeType geoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_ZIP_CODE);
094            GeoCodeAliasType geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_ZIP_CODE);
095            String zipCodeName = form.getZipCodeName();
096            GeoCodeAlias geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, zipCodeName);
097            
098            if(geoCodeAlias == null) {
099                BasePK createdBy = getPartyPK();
100                String geoCodeName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(null, SequenceTypes.GEO_CODE.name());
101                var isDefault = Boolean.valueOf(form.getIsDefault());
102                var sortOrder = Integer.valueOf(form.getSortOrder());
103                var description = form.getDescription();
104                
105                geoCode = geoControl.createGeoCode(geoCodeName, geoCodeType, geoCodeScope, isDefault, sortOrder, createdBy);
106                geoControl.createGeoCodeRelationship(geoCode, countryGeoCode, createdBy);
107                geoControl.createGeoCodeAlias(geoCode, geoCodeAliasType, zipCodeName, createdBy);
108                
109                if(description != null) {
110                    geoControl.createGeoCodeDescription(geoCode, getPreferredLanguage(), description, createdBy);
111                }
112            } else {
113                geoCode = geoCodeAlias.getGeoCode();
114            }
115        } // TODO: error, unknown geoCodeScopeName
116        
117        if(geoCode != null) {
118            result.setEntityRef(geoCode.getPrimaryKey().getEntityRef());
119            result.setGeoCodeName(geoCode.getLastDetail().getGeoCodeName());
120        }
121        
122        return result;
123    }
124    
125}