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.training.server.command; 018 019import com.echothree.control.user.training.common.form.GetPartyTrainingClassesForm; 020import com.echothree.control.user.training.common.result.TrainingResultFactory; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.control.party.server.logic.PartyLogic; 024import com.echothree.model.control.security.common.SecurityRoleGroups; 025import com.echothree.model.control.security.common.SecurityRoles; 026import com.echothree.model.control.training.server.control.TrainingControl; 027import com.echothree.model.control.training.server.logic.TrainingClassLogic; 028import com.echothree.model.data.party.server.entity.Party; 029import com.echothree.model.data.training.server.entity.PartyTrainingClass; 030import com.echothree.model.data.training.server.entity.TrainingClass; 031import com.echothree.model.data.training.server.factory.PartyTrainingClassFactory; 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.BasePaginatedMultipleEntitiesCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.Collection; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043import javax.inject.Inject; 044 045@Dependent 046public class GetPartyTrainingClassesCommand 047 extends BasePaginatedMultipleEntitiesCommand<PartyTrainingClass, GetPartyTrainingClassesForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.PartyTrainingClass.name(), SecurityRoles.List.name()) 057 )) 058 )); 059 060 FORM_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("TrainingClassName", FieldType.ENTITY_NAME, false, null, null) 063 ); 064 } 065 066 @Inject 067 PartyControl partyControl; 068 069 @Inject 070 TrainingControl trainingControl; 071 072 @Inject 073 PartyLogic partyLogic; 074 075 @Inject 076 TrainingClassLogic trainingClassLogic; 077 078 /** Creates a new instance of GetPartyTrainingClassesCommand */ 079 public GetPartyTrainingClassesCommand() { 080 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 081 } 082 083 private Party party; 084 private TrainingClass trainingClass; 085 086 @Override 087 protected void handleForm() { 088 var partyName = form.getPartyName(); 089 var trainingClassName = form.getTrainingClassName(); 090 var parameterCount = (partyName == null ? 0 : 1) + (trainingClassName == null ? 0 : 1); 091 092 if(parameterCount == 1) { 093 if(partyName != null) { 094 party = partyLogic.getPartyByName(this, partyName); 095 } else { 096 trainingClass = trainingClassLogic.getTrainingClassByName(this, trainingClassName, false); 097 } 098 } else { 099 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 100 } 101 } 102 103 @Override 104 protected Long getTotalEntities() { 105 Long total = null; 106 107 if(!hasExecutionErrors()) { 108 total = party != null ? trainingControl.countPartyTrainingClassesByParty(party) : 109 trainingControl.countPartyTrainingClassesByTrainingClass(trainingClass); 110 } 111 112 return total; 113 } 114 115 @Override 116 protected Collection<PartyTrainingClass> getEntities() { 117 Collection<PartyTrainingClass> entities = null; 118 119 if(!hasExecutionErrors()) { 120 entities = party != null ? trainingControl.getPartyTrainingClassesByParty(party) : 121 trainingControl.getPartyTrainingClassesByTrainingClass(trainingClass); 122 } 123 124 return entities; 125 } 126 127 @Override 128 protected BaseResult getResult(Collection<PartyTrainingClass> entities) { 129 var result = TrainingResultFactory.getGetPartyTrainingClassesResult(); 130 131 if(entities != null) { 132 var userVisit = getUserVisit(); 133 134 if(party != null) { 135 result.setParty(partyControl.getPartyTransfer(userVisit, party)); 136 } else { 137 result.setTrainingClass(trainingControl.getTrainingClassTransfer(userVisit, trainingClass)); 138 } 139 140 if(session.hasLimit(PartyTrainingClassFactory.class)) { 141 result.setPartyTrainingClassCount(getTotalEntities()); 142 } 143 144 result.setPartyTrainingClasses(trainingControl.getPartyTrainingClassTransfers(userVisit, entities)); 145 } 146 147 return result; 148 } 149 150}