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.GetStatesForm; 020import com.echothree.control.user.geo.common.result.GeoResultFactory; 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.GeoCodeScope; 027import com.echothree.model.data.geo.server.factory.GeoCodeFactory; 028import com.echothree.util.common.command.BaseResult; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 033import com.echothree.util.server.control.CommandSecurityDefinition; 034import com.echothree.util.server.control.PartyTypeDefinition; 035import com.echothree.util.server.control.SecurityRoleDefinition; 036import java.util.Collection; 037import java.util.List; 038import javax.enterprise.context.Dependent; 039import javax.inject.Inject; 040 041@Dependent 042public class GetStatesCommand 043 extends BasePaginatedMultipleEntitiesCommand<GeoCode, GetStatesForm> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 047 048 static { 049 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 050 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 051 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 052 new PartyTypeDefinition(PartyTypes.VENDOR.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 054 new SecurityRoleDefinition(SecurityRoleGroups.State.name(), SecurityRoles.List.name()) 055 )) 056 )); 057 058 FORM_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("CountryName", FieldType.ENTITY_NAME, true, null, null) 060 ); 061 } 062 063 /** Creates a new instance of GetStatesCommand */ 064 public GetStatesCommand() { 065 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 066 } 067 068 @Inject 069 GeoControl geoControl; 070 071 GeoCode countryGeoCode; 072 GeoCodeScope statesGeoCodeScope; 073 074 @Override 075 protected void handleForm() { 076 var countryName = form.getCountryName(); 077 078 countryGeoCode = geoControl.getCountryByAlias(countryName); 079 080 if(countryGeoCode != null) { 081 statesGeoCodeScope = geoControl.getStatesGeoCodeScope(countryGeoCode); 082 } else { 083 addExecutionError(ExecutionErrors.UnknownCountryName.name(), countryName); 084 } 085 } 086 087 @Override 088 protected Long getTotalEntities() { 089 return statesGeoCodeScope == null ? null : geoControl.countGeoCodesByGeoCodeScope(statesGeoCodeScope); 090 } 091 092 @Override 093 protected Collection<GeoCode> getEntities() { 094 return statesGeoCodeScope == null ? null : geoControl.getStatesByCountry(statesGeoCodeScope); 095 } 096 097 @Override 098 protected BaseResult getResult(Collection<GeoCode> entities) { 099 var result = GeoResultFactory.getGetStatesResult(); 100 101 if(entities != null) { 102 var userVisit = getUserVisit(); 103 104 result.setCountry(geoControl.getCountryTransfer(userVisit, countryGeoCode)); 105 106 if(session.hasLimit(GeoCodeFactory.class)) { 107 result.setStateCount(getTotalEntities()); 108 } 109 110 result.setStates(geoControl.getStateTransfers(userVisit, entities)); 111 } 112 113 return result; 114 } 115 116}