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.GetStateForm;
020import com.echothree.control.user.geo.common.result.GeoResultFactory;
021import com.echothree.control.user.geo.server.GeoDebugFlags;
022import com.echothree.model.control.core.common.EventTypes;
023import com.echothree.model.control.geo.common.GeoCodeAliasTypes;
024import com.echothree.model.control.geo.common.GeoCodeTypes;
025import com.echothree.model.control.geo.server.control.GeoControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.geo.server.entity.GeoCode;
030import com.echothree.model.data.geo.server.entity.GeoCodeAlias;
031import com.echothree.util.common.command.BaseResult;
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.server.control.BaseSingleEntityCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import com.echothree.util.server.persistence.Session;
040import java.util.Arrays;
041import java.util.Collections;
042import java.util.List;
043import org.apache.commons.logging.Log;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class GetStateCommand
048        extends BaseSingleEntityCommand<GeoCode, GetStateForm> {
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.CUSTOMER.name(), null),
057                new PartyTypeDefinition(PartyTypes.VENDOR.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
059                        new SecurityRoleDefinition(SecurityRoleGroups.State.name(), SecurityRoles.Review.name())
060                        )))
061                )));
062        
063        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
064                new FieldDefinition("CountryGeoCodeName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("StateName", FieldType.ENTITY_NAME, false, null, null),
066                new FieldDefinition("Postal2Letter", FieldType.UPPER_LETTER_2, false, null, null)
067                ));
068    }
069    
070    Log log = null;
071    
072    /** Creates a new instance of GetStateCommand */
073    public GetStateCommand() {
074        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
075        
076        if(GeoDebugFlags.GetStateCommand) {
077            log = getLog();
078        }
079    }
080
081    @Override
082    protected GeoCode getEntity() {
083        GeoCode geoCode = null;
084        var stateName = form.getStateName();
085        var postal2Letter = form.getPostal2Letter();
086        var parameterCount = (stateName == null ? 0 : 1) + (postal2Letter == null ? 0 : 1);
087
088        if(GeoDebugFlags.GetStateCommand) {
089            log.info("parameterCount = " + parameterCount);
090        }
091
092        if(parameterCount == 1) {
093            var geoControl = Session.getModelController(GeoControl.class);
094
095            var countryGeoCodeName = form.getCountryGeoCodeName();
096            var countryGeoCode = geoControl.getGeoCodeByName(countryGeoCodeName);
097
098            var countryGeoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(countryGeoCode.getLastDetail().getGeoCodeType(), GeoCodeAliasTypes.ISO_2_LETTER.name());
099            var countryGeoCodeAlias = geoControl.getGeoCodeAlias(countryGeoCode, countryGeoCodeAliasType);
100            var countryIso2Letter = countryGeoCodeAlias.getAlias();
101
102            var geoCodeScopeName = countryIso2Letter + "_STATES";
103            var geoCodeScope = geoControl.getGeoCodeScopeByName(geoCodeScopeName);
104
105            if(geoCodeScope != null) {
106                var geoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.STATE.name());
107                GeoCodeAlias geoCodeAlias;
108
109                if(stateName != null) {
110                    if(GeoDebugFlags.GetStateCommand) {
111                        log.info("lookup will be by stateName");
112                    }
113
114                    var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.STATE_NAME.name());
115                    geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, stateName);
116
117                    if(geoCodeAlias == null) {
118                        addExecutionError(ExecutionErrors.UnknownStateName.name(), stateName);
119                    }
120                } else {
121                    if(GeoDebugFlags.GetStateCommand) {
122                        log.info("lookup will be by postal2Letter");
123                    }
124
125                    var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.POSTAL_2_LETTER.name());
126                    geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, postal2Letter);
127
128                    if(geoCodeAlias == null) {
129                        addExecutionError(ExecutionErrors.UnknownStatePostal2Letter.name());
130                    }
131                }
132
133                if(geoCodeAlias != null) {
134                    geoCode = geoCodeAlias.getGeoCode();
135
136                    sendEvent(geoCode.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
137                }
138            } else {
139                addExecutionError(ExecutionErrors.UnknownGeoCodeScopeName.name(), geoCodeScopeName);
140            }
141        } else {
142            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
143        }
144
145        return geoCode;
146    }
147
148    @Override
149    protected BaseResult getResult(GeoCode entity) {
150        var result = GeoResultFactory.getGetStateResult();
151
152        if(entity != null) {
153            var geoControl = Session.getModelController(GeoControl.class);
154
155            result.setState(geoControl.getStateTransfer(getUserVisit(), entity));
156        }
157
158        return result;
159    }
160
161}