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.GetCountryForm;
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.GeoCodeScopes;
025import com.echothree.model.control.geo.common.GeoCodeTypes;
026import com.echothree.model.control.geo.server.control.GeoControl;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.data.geo.server.entity.GeoCode;
031import com.echothree.model.data.geo.server.entity.GeoCodeAlias;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseSingleEntityCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class GetCountryCommand
046        extends BaseSingleEntityCommand<GeoCode, GetCountryForm> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null),
055                new PartyTypeDefinition(PartyTypes.VENDOR.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
057                        new SecurityRoleDefinition(SecurityRoleGroups.Country.name(), SecurityRoles.Review.name())
058                ))
059        ));
060        
061        FORM_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("GeoCodeName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("CountryName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("Iso3Number", FieldType.NUMBER_3, false, null, null),
065                new FieldDefinition("Iso3Letter", FieldType.UPPER_LETTER_3, false, null, null),
066                new FieldDefinition("Iso2Letter", FieldType.UPPER_LETTER_2, false, null, null),
067                new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null)
068        );
069    }
070
071    @Inject
072    GeoControl geoControl;
073
074    
075    /** Creates a new instance of GetCountryCommand */
076    public GetCountryCommand() {
077        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
078    }
079
080    @Override
081    protected GeoCode getEntity() {
082        GeoCode geoCode = null;
083        var geoCodeName = form.getGeoCodeName();
084        var countryName = form.getCountryName();
085        var iso3Number = form.getIso3Number();
086        var iso3Letter = form.getIso3Letter();
087        var iso2Letter = form.getIso2Letter();
088        var alias = form.getAlias();
089        var parameterCount = (geoCodeName == null ? 0 : 1) + (countryName == null ? 0 : 1) + (iso3Number == null ? 0 : 1) + (iso3Letter == null ? 0 : 1)
090                + (iso2Letter == null ? 0 : 1) + (alias == null ? 0 : 1);
091
092        if(GeoDebugFlags.GetCountryCommand) {
093            log.info("parameterCount = " + parameterCount);
094        }
095
096        if(parameterCount < 2) {
097            var geoCodeScope = geoControl.getGeoCodeScopeByName(GeoCodeScopes.COUNTRIES.name());
098
099            if(parameterCount == 0) {
100                geoCode = geoControl.getDefaultGeoCode(geoCodeScope);
101            } else {
102                var geoCodeType = geoControl.getGeoCodeTypeByName(GeoCodeTypes.COUNTRY.name());
103
104                if(geoCodeName != null) {
105                    if(GeoDebugFlags.GetCountryCommand) {
106                        log.info("lookup will be by geoCodeName");
107                    }
108
109                    geoCode = geoControl.getGeoCodeByName(geoCodeName);
110
111                    if(geoCode != null) {
112                        var geoCodeDetail = geoCode.getLastDetail();
113
114                        if(!geoCodeDetail.getGeoCodeType().equals(geoCodeType)) {
115                            addExecutionError(ExecutionErrors.InvalidGeoCodeType.name(), geoCodeDetail.getGeoCodeType().getLastDetail().getGeoCodeTypeName());
116                        } else if(!geoCodeDetail.getGeoCodeScope().equals(geoCodeScope)) {
117                            addExecutionError(ExecutionErrors.InvalidGeoCodeScope.name(), geoCodeDetail.getGeoCodeScope().getLastDetail().getGeoCodeScopeName());
118                        }
119
120                        if(hasExecutionErrors()) {
121                            geoCode = null;
122                        }
123                    } else {
124                        addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), geoCodeName);
125                    }
126                } else if(alias != null) {
127                    if(GeoDebugFlags.GetCountryCommand) {
128                        log.info("lookup will be by alias");
129                    }
130
131                    geoCode = geoControl.getCountryByAlias(alias);
132
133                    if(geoCode == null) {
134                        addExecutionError(ExecutionErrors.UnknownGeoCodeAlias.name(), alias);
135                    }
136                } else {
137                    GeoCodeAlias geoCodeAlias = null;
138
139                    if(countryName != null) {
140                        if(GeoDebugFlags.GetCountryCommand) {
141                            log.info("lookup will be by countryName");
142                        }
143
144                        var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.COUNTRY_NAME.name());
145                        geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, countryName);
146
147                        if(geoCodeAlias == null) {
148                            addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName);
149                        }
150                    } else if(iso3Number != null) {
151                        if(GeoDebugFlags.GetCountryCommand) {
152                            log.info("lookup will be by iso3Number");
153                        }
154
155                        var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.ISO_3_NUMBER.name());
156                        geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, iso3Number);
157
158                        if(geoCodeAlias == null) {
159                            addExecutionError(ExecutionErrors.UnknownCountryIso3Number.name(), iso3Number);
160                        }
161                    } else if(iso3Letter != null) {
162                        if(GeoDebugFlags.GetCountryCommand) {
163                            log.info("lookup will be by iso3Letter");
164                        }
165
166                        var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.ISO_3_LETTER.name());
167                        geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, iso3Letter);
168
169                        if(geoCodeAlias == null) {
170                            addExecutionError(ExecutionErrors.UnknownCountryIso3Letter.name(), iso3Letter);
171                        }
172                    } else if(iso2Letter != null) {
173                        if(GeoDebugFlags.GetCountryCommand) {
174                            log.info("lookup will be by iso2Letter");
175                        }
176
177                        var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoCodeAliasTypes.ISO_2_LETTER.name());
178                        geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, iso2Letter);
179
180                        if(geoCodeAlias == null) {
181                            addExecutionError(ExecutionErrors.UnknownCountryIso2Letter.name(), iso2Letter);
182                        }
183                    }
184
185                    if(geoCodeAlias != null) {
186                        geoCode = geoCodeAlias.getGeoCode();
187                    }
188                }
189            }
190
191            if(geoCode != null) {
192                sendEvent(geoCode.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
193            }
194        } else {
195            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
196        }
197
198        return geoCode;
199    }
200
201    @Override
202    protected BaseResult getResult(GeoCode entity) {
203        var result = GeoResultFactory.getGetCountryResult();
204
205        if(entity != null) {
206            result.setCountry(geoControl.getCountryTransfer(getUserVisit(), entity));
207        }
208
209        return result;
210    }
211
212}