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.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.GeoConstants;
024import com.echothree.model.control.geo.server.control.GeoControl;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.geo.server.entity.GeoCode;
029import com.echothree.model.data.geo.server.entity.GeoCodeAlias;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
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;
044
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(Collections.unmodifiableList(Arrays.asList(
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(), Collections.unmodifiableList(Arrays.asList(
057                        new SecurityRoleDefinition(SecurityRoleGroups.Country.name(), SecurityRoles.Review.name())
058                        )))
059                )));
060        
061        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
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    Log log = null;
072    
073    /** Creates a new instance of GetCountryCommand */
074    public GetCountryCommand(UserVisitPK userVisitPK, GetCountryForm form) {
075        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
076        
077        if(GeoDebugFlags.GetCountryCommand) {
078            log = getLog();
079        }
080    }
081
082    @Override
083    protected GeoCode getEntity() {
084        GeoCode geoCode = null;
085        var geoCodeName = form.getGeoCodeName();
086        var countryName = form.getCountryName();
087        var iso3Number = form.getIso3Number();
088        var iso3Letter = form.getIso3Letter();
089        var iso2Letter = form.getIso2Letter();
090        var alias = form.getAlias();
091        var parameterCount = (geoCodeName == null ? 0 : 1) + (countryName == null ? 0 : 1) + (iso3Number == null ? 0 : 1) + (iso3Letter == null ? 0 : 1)
092                + (iso2Letter == null ? 0 : 1) + (alias == null ? 0 : 1);
093
094        if(GeoDebugFlags.GetCountryCommand) {
095            log.info("parameterCount = " + parameterCount);
096        }
097
098        if(parameterCount < 2) {
099            var geoControl = Session.getModelController(GeoControl.class);
100            var geoCodeScope = geoControl.getGeoCodeScopeByName(GeoConstants.GeoCodeScope_COUNTRIES);
101
102            if(parameterCount == 0) {
103                geoCode = geoControl.getDefaultGeoCode(geoCodeScope);
104            } else {
105                var geoCodeType = geoControl.getGeoCodeTypeByName(GeoConstants.GeoCodeType_COUNTRY);
106
107                if(geoCodeName != null) {
108                    if(GeoDebugFlags.GetCountryCommand) {
109                        log.info("lookup will be by geoCodeName");
110                    }
111
112                    geoCode = geoControl.getGeoCodeByName(geoCodeName);
113
114                    if(geoCode != null) {
115                        var geoCodeDetail = geoCode.getLastDetail();
116
117                        if(!geoCodeDetail.getGeoCodeType().equals(geoCodeType)) {
118                            addExecutionError(ExecutionErrors.InvalidGeoCodeType.name(), geoCodeDetail.getGeoCodeType().getLastDetail().getGeoCodeTypeName());
119                        } else if(!geoCodeDetail.getGeoCodeScope().equals(geoCodeScope)) {
120                            addExecutionError(ExecutionErrors.InvalidGeoCodeScope.name(), geoCodeDetail.getGeoCodeScope().getLastDetail().getGeoCodeScopeName());
121                        }
122
123                        if(hasExecutionErrors()) {
124                            geoCode = null;
125                        }
126                    } else {
127                        addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), geoCodeName);
128                    }
129                } else if(alias != null) {
130                    if(GeoDebugFlags.GetCountryCommand) {
131                        log.info("lookup will be by alias");
132                    }
133
134                    geoCode = geoControl.getCountryByAlias(alias);
135
136                    if(geoCode == null) {
137                        addExecutionError(ExecutionErrors.UnknownGeoCodeAlias.name(), alias);
138                    }
139                } else {
140                    GeoCodeAlias geoCodeAlias = null;
141
142                    if(countryName != null) {
143                        if(GeoDebugFlags.GetCountryCommand) {
144                            log.info("lookup will be by countryName");
145                        }
146
147                        var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_COUNTRY_NAME);
148                        geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, countryName);
149
150                        if(geoCodeAlias == null) {
151                            addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName);
152                        }
153                    } else if(iso3Number != null) {
154                        if(GeoDebugFlags.GetCountryCommand) {
155                            log.info("lookup will be by iso3Number");
156                        }
157
158                        var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_ISO_3_NUMBER);
159                        geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, iso3Number);
160
161                        if(geoCodeAlias == null) {
162                            addExecutionError(ExecutionErrors.UnknownCountryIso3Number.name(), iso3Number);
163                        }
164                    } else if(iso3Letter != null) {
165                        if(GeoDebugFlags.GetCountryCommand) {
166                            log.info("lookup will be by iso3Letter");
167                        }
168
169                        var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_ISO_3_LETTER);
170                        geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, iso3Letter);
171
172                        if(geoCodeAlias == null) {
173                            addExecutionError(ExecutionErrors.UnknownCountryIso3Letter.name(), iso3Letter);
174                        }
175                    } else if(iso2Letter != null) {
176                        if(GeoDebugFlags.GetCountryCommand) {
177                            log.info("lookup will be by iso2Letter");
178                        }
179
180                        var geoCodeAliasType = geoControl.getGeoCodeAliasTypeByName(geoCodeType, GeoConstants.GeoCodeAliasType_ISO_2_LETTER);
181                        geoCodeAlias = geoControl.getGeoCodeAliasByAliasWithinScope(geoCodeScope, geoCodeAliasType, iso2Letter);
182
183                        if(geoCodeAlias == null) {
184                            addExecutionError(ExecutionErrors.UnknownCountryIso2Letter.name(), iso2Letter);
185                        }
186                    }
187
188                    if(geoCodeAlias != null) {
189                        geoCode = geoCodeAlias.getGeoCode();
190                    }
191                }
192            }
193
194            if(geoCode != null) {
195                sendEvent(geoCode.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
196            }
197        } else {
198            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
199        }
200
201        return geoCode;
202    }
203
204    @Override
205    protected BaseResult getResult(GeoCode entity) {
206        var result = GeoResultFactory.getGetCountryResult();
207
208        if(entity != null) {
209            var geoControl = Session.getModelController(GeoControl.class);
210
211            result.setCountry(geoControl.getCountryTransfer(getUserVisit(), entity));
212        }
213
214        return result;
215    }
216
217}