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.AddZipCodeToCityForm;
020import com.echothree.model.control.geo.common.GeoConstants;
021import com.echothree.model.control.geo.server.control.GeoControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.security.common.SecurityRoleGroups;
024import com.echothree.model.control.security.common.SecurityRoles;
025import com.echothree.model.data.geo.server.entity.GeoCode;
026import com.echothree.model.data.geo.server.entity.GeoCodeAlias;
027import com.echothree.model.data.geo.server.entity.GeoCodeAliasType;
028import com.echothree.model.data.geo.server.entity.GeoCodeRelationship;
029import com.echothree.model.data.geo.server.entity.GeoCodeScope;
030import com.echothree.model.data.geo.server.entity.GeoCodeType;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.common.command.BaseResult;
036import com.echothree.util.server.control.BaseSimpleCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.Arrays;
042import java.util.Collection;
043import java.util.Collections;
044import java.util.Iterator;
045import java.util.List;
046
047public class AddZipCodeToCityCommand
048        extends BaseSimpleCommand<AddZipCodeToCityForm> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                    new SecurityRoleDefinition(SecurityRoleGroups.City.name(), SecurityRoles.Edit.name())
058                    )))
059                )));
060
061        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("CityGeoCodeName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("ZipCodeGeoCodeName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("ZipCodeName", FieldType.ENTITY_NAME, false, null, null)
065                ));
066    }
067    
068    /** Creates a new instance of AddZipCodeToCityCommand */
069    public AddZipCodeToCityCommand(UserVisitPK userVisitPK, AddZipCodeToCityForm form) {
070        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
071    }
072    
073    @Override
074    protected BaseResult execute() {
075        String zipCodeGeoCodeName = form.getZipCodeGeoCodeName();
076        String zipCodeName = form.getZipCodeName();
077        var parameterCount = (zipCodeGeoCodeName == null ? 0 : 1) + (zipCodeName == null ? 0 : 1);
078        
079        if(parameterCount == 1) {
080            var geoControl = Session.getModelController(GeoControl.class);
081            GeoCodeType geoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_ZIP_CODE);
082            String cityGeoCodeName = form.getCityGeoCodeName();
083            GeoCode cityGeoCode = geoControl.getGeoCodeByName(cityGeoCodeName);
084            GeoCode zipCodeGeoCode = null;
085            
086            if(zipCodeGeoCodeName != null) {
087                zipCodeGeoCode = geoControl.getGeoCodeByName(zipCodeGeoCodeName);
088                
089                if(!zipCodeGeoCode.getLastDetail().getGeoCodeType().equals(geoCodeType)) {
090                    zipCodeGeoCode = null;
091                }
092                
093                if(zipCodeGeoCode == null) {
094                    addExecutionError(ExecutionErrors.UnknownZipCodeGeoCodeName.name(), zipCodeGeoCodeName);
095                }
096            } else if(zipCodeName != null) {
097                GeoCodeType stateGeoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_STATE);
098                GeoCode stateGeoCode = null;
099                Collection cityRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCode(cityGeoCode);
100                for(Iterator iter = cityRelationships.iterator(); iter.hasNext();) {
101                    GeoCodeRelationship geoCodeRelationship = (GeoCodeRelationship)iter.next();
102                    GeoCode toGeoCode = geoCodeRelationship.getToGeoCode();
103                    if(toGeoCode.getLastDetail().getGeoCodeType().equals(stateGeoCodeType)) {
104                        stateGeoCode = toGeoCode;
105                        break;
106                    }
107                }
108                
109                GeoCodeType countryGeoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_COUNTRY);
110                GeoCode countryGeoCode = null;
111                Collection stateRelationships = geoControl.getGeoCodeRelationshipsByFromGeoCode(stateGeoCode);
112                for(Iterator iter = stateRelationships.iterator(); iter.hasNext();) {
113                    GeoCodeRelationship geoCodeRelationship = (GeoCodeRelationship)iter.next();
114                    GeoCode toGeoCode = geoCodeRelationship.getToGeoCode();
115                    if(toGeoCode.getLastDetail().getGeoCodeType().equals(countryGeoCodeType)) {
116                        countryGeoCode = toGeoCode;
117                        break;
118                    }
119                }
120                
121                GeoCodeAliasType countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoConstants.GeoCodeAliasType_ISO_2_LETTER);
122                GeoCodeAlias countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType);
123                String countryIso2Letter = countryGeoCodeAlias.getAlias();
124                
125                String geoCodeScopeName = new StringBuilder().append(countryIso2Letter).append("_ZIP_CODES").toString();
126                GeoCodeScope geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName);
127                
128                GeoCodeAliasType geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_ZIP_CODE);
129                GeoCodeAlias geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, zipCodeName);
130                zipCodeGeoCode = geoCodeAlias.getGeoCode();
131                
132                if(zipCodeGeoCode == null) {
133                    addExecutionError(ExecutionErrors.UnknownZipCodeName.name(), zipCodeName);
134                }
135            }
136            
137            if(zipCodeGeoCode != null) {
138                GeoCodeRelationship geoCodeRelationship = geoControl.getGeoCodeRelationship(zipCodeGeoCode, cityGeoCode);
139                
140                if(geoCodeRelationship == null) {
141                    geoControl.createGeoCodeRelationship(zipCodeGeoCode, cityGeoCode, getPartyPK());
142                } else {
143                    addExecutionError(ExecutionErrors.DuplicateGeoCodeRelationship.name());
144                }
145            }
146        } else {
147            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
148        }
149        
150        return null;
151    }
152    
153}