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.GetGeoCodeTimeZonesForm; 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.party.server.control.PartyControl; 024import com.echothree.model.control.security.common.SecurityRoleGroups; 025import com.echothree.model.control.security.common.SecurityRoles; 026import com.echothree.model.data.geo.server.entity.GeoCode; 027import com.echothree.model.data.geo.server.entity.GeoCodeTimeZone; 028import com.echothree.model.data.geo.server.factory.GeoCodeTimeZoneFactory; 029import com.echothree.model.data.party.server.entity.TimeZone; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 035import com.echothree.util.server.control.CommandSecurityDefinition; 036import com.echothree.util.server.control.PartyTypeDefinition; 037import com.echothree.util.server.control.SecurityRoleDefinition; 038import java.util.Collection; 039import java.util.List; 040import javax.enterprise.context.Dependent; 041import javax.inject.Inject; 042 043@Dependent 044public class GetGeoCodeTimeZonesCommand 045 extends BasePaginatedMultipleEntitiesCommand<GeoCodeTimeZone, GetGeoCodeTimeZonesForm> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 049 050 static { 051 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 052 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 054 new SecurityRoleDefinition(SecurityRoleGroups.GeoCodeTimeZone.name(), SecurityRoles.List.name()) 055 )) 056 )); 057 058 FORM_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("GeoCodeName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("JavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null) 061 ); 062 } 063 064 /** Creates a new instance of GetGeoCodeTimeZonesCommand */ 065 public GetGeoCodeTimeZonesCommand() { 066 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 067 } 068 069 @Inject 070 GeoControl geoControl; 071 072 @Inject 073 PartyControl partyControl; 074 075 GeoCode geoCode; 076 TimeZone timeZone; 077 078 @Override 079 protected void handleForm() { 080 var geoCodeName = form.getGeoCodeName(); 081 var javaTimeZoneName = form.getJavaTimeZoneName(); 082 var parameterCount = (geoCodeName != null ? 1 : 0) + (javaTimeZoneName != null ? 1 : 0); 083 084 if(parameterCount == 1) { 085 if(geoCodeName != null) { 086 geoCode = geoControl.getGeoCodeByName(geoCodeName); 087 088 if(geoCode == null) { 089 addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), geoCodeName); 090 } 091 } else { 092 timeZone = partyControl.getTimeZoneByJavaName(javaTimeZoneName); 093 094 if(timeZone == null) { 095 addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), javaTimeZoneName); 096 } 097 } 098 } else { 099 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 100 } 101 } 102 103 @Override 104 protected Long getTotalEntities() { 105 Long totalEntities = null; 106 107 if(!hasExecutionErrors()) { 108 if(geoCode != null) { 109 totalEntities = geoControl.countGeoCodeTimeZonesByGeoCode(geoCode); 110 } else { 111 totalEntities = geoControl.countGeoCodeTimeZonesByTimeZone(timeZone); 112 } 113 } 114 115 return totalEntities; 116 } 117 118 @Override 119 protected Collection<GeoCodeTimeZone> getEntities() { 120 Collection<GeoCodeTimeZone> entities = null; 121 122 if(!hasExecutionErrors()) { 123 if(geoCode != null) { 124 entities = geoControl.getGeoCodeTimeZonesByGeoCode(geoCode); 125 } else { 126 entities = geoControl.getGeoCodeTimeZonesByTimeZone(timeZone); 127 } 128 } 129 130 return entities; 131 } 132 133 @Override 134 protected BaseResult getResult(Collection<GeoCodeTimeZone> entities) { 135 var result = GeoResultFactory.getGetGeoCodeTimeZonesResult(); 136 137 if(entities != null) { 138 var userVisit = getUserVisit(); 139 140 if(geoCode != null) { 141 result.setGeoCode(geoControl.getGeoCodeTransfer(userVisit, geoCode)); 142 } else { 143 result.setTimeZone(partyControl.getTimeZoneTransfer(userVisit, timeZone)); 144 } 145 146 if(session.hasLimit(GeoCodeTimeZoneFactory.class)) { 147 result.setGeoCodeTimeZoneCount(getTotalEntities()); 148 } 149 150 result.setGeoCodeTimeZones(geoControl.getGeoCodeTimeZoneTransfers(userVisit, entities)); 151 } 152 153 return result; 154 } 155 156}