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.GetGeoCodeDateTimeFormatsForm; 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.GeoCodeDateTimeFormat; 028import com.echothree.model.data.geo.server.factory.GeoCodeCurrencyFactory; 029import com.echothree.model.data.geo.server.factory.GeoCodeDateTimeFormatFactory; 030import com.echothree.model.data.party.server.entity.DateTimeFormat; 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.BasePaginatedMultipleEntitiesCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import java.util.Collection; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class GetGeoCodeDateTimeFormatsCommand 046 extends BasePaginatedMultipleEntitiesCommand<GeoCodeDateTimeFormat, GetGeoCodeDateTimeFormatsForm> { 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.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.GeoCodeDateTimeFormat.name(), SecurityRoles.List.name()) 056 )) 057 )); 058 059 FORM_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("GeoCodeName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("DateTimeFormatName", FieldType.ENTITY_NAME, false, null, null) 062 ); 063 } 064 065 /** Creates a new instance of GetGeoCodeDateTimeFormatsCommand */ 066 public GetGeoCodeDateTimeFormatsCommand() { 067 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 068 } 069 070 @Inject 071 GeoControl geoControl; 072 073 @Inject 074 PartyControl partyControl; 075 076 GeoCode geoCode; 077 DateTimeFormat dateTimeFormat; 078 079 @Override 080 protected void handleForm() { 081 var geoCodeName = form.getGeoCodeName(); 082 var dateTimeFormatName = form.getDateTimeFormatName(); 083 var parameterCount = (geoCodeName != null ? 1 : 0) + (dateTimeFormatName != null ? 1 : 0); 084 085 if(parameterCount == 1) { 086 if(geoCodeName != null) { 087 geoCode = geoControl.getGeoCodeByName(geoCodeName); 088 089 if(geoCode == null) { 090 addExecutionError(ExecutionErrors.UnknownGeoCodeName.name(), geoCodeName); 091 } 092 } else { 093 dateTimeFormat = partyControl.getDateTimeFormatByName(dateTimeFormatName); 094 095 if(dateTimeFormat == null) { 096 addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), dateTimeFormatName); 097 } 098 } 099 } else { 100 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 101 } 102 } 103 104 @Override 105 protected Long getTotalEntities() { 106 Long totalEntities = null; 107 108 if(!hasExecutionErrors()) { 109 if(geoCode != null) { 110 totalEntities = geoControl.countGeoCodeDateTimeFormatsByGeoCode(geoCode); 111 } else { 112 totalEntities = geoControl.countGeoCodeDateTimeFormatsByDateTimeFormat(dateTimeFormat); 113 } 114 } 115 116 return totalEntities; 117 } 118 119 @Override 120 protected Collection<GeoCodeDateTimeFormat> getEntities() { 121 Collection<GeoCodeDateTimeFormat> entities = null; 122 123 if(!hasExecutionErrors()) { 124 if(geoCode != null) { 125 entities = geoControl.getGeoCodeDateTimeFormatsByGeoCode(geoCode); 126 } else { 127 entities = geoControl.getGeoCodeDateTimeFormatsByDateTimeFormat(dateTimeFormat); 128 } 129 } 130 131 return entities; 132 } 133 134 @Override 135 protected BaseResult getResult(Collection<GeoCodeDateTimeFormat> entities) { 136 var result = GeoResultFactory.getGetGeoCodeDateTimeFormatsResult(); 137 138 if(entities != null) { 139 var userVisit = getUserVisit(); 140 141 if(geoCode != null) { 142 result.setGeoCode(geoControl.getGeoCodeTransfer(userVisit, geoCode)); 143 } else { 144 result.setDateTimeFormat(partyControl.getDateTimeFormatTransfer(userVisit, dateTimeFormat)); 145 } 146 147 if(session.hasLimit(GeoCodeDateTimeFormatFactory.class)) { 148 result.setGeoCodeDateTimeFormatCount(getTotalEntities()); 149 } 150 151 result.setGeoCodeDateTimeFormats(geoControl.getGeoCodeDateTimeFormatTransfers(userVisit, entities)); 152 } 153 154 return result; 155 } 156 157}