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.core.server.command; 018 019import com.echothree.control.user.core.common.form.GetEventsForm; 020import com.echothree.control.user.core.common.result.CoreResultFactory; 021import com.echothree.model.control.core.server.control.EntityInstanceControl; 022import com.echothree.model.control.core.server.control.EventControl; 023import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.data.core.server.entity.EntityInstance; 028import com.echothree.model.data.core.server.entity.Event; 029import com.echothree.model.data.core.server.factory.EventFactory; 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.BaseMultipleEntitiesCommand; 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.Collection; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043 044@Dependent 045public class GetEventsCommand 046 extends BaseMultipleEntitiesCommand<Event, GetEventsForm> { 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.Event.name(), SecurityRoles.List.name()) 056 )) 057 )); 058 059 FORM_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 061 new FieldDefinition("Uuid", FieldType.UUID, false, null, null), 062 new FieldDefinition("CreatedByEntityRef", FieldType.ENTITY_REF, false, null, null), 063 new FieldDefinition("CreatedByUuid", FieldType.UUID, false, null, null) 064 ); 065 } 066 067 /** Creates a new instance of GetEventsCommand */ 068 public GetEventsCommand() { 069 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 070 } 071 072 EntityInstance entityInstance; 073 EntityInstance createdBy; 074 Long eventCount; 075 076 @Override 077 protected Collection<Event> getEntities() { 078 var eventControl = Session.getModelController(EventControl.class); 079 var entityRef = form.getEntityRef(); 080 var uuid = form.getUuid(); 081 var createdByEntityRef = form.getCreatedByEntityRef(); 082 var createdByUuid = form.getCreatedByUuid(); 083 var parameterCount = (entityRef == null ? 0 : 1) + (uuid == null ? 0 : 1) 084 + (createdByEntityRef == null ? 0 : 1) + (createdByUuid == null ? 0 : 1); 085 Collection<Event> entities = null; 086 087 if(parameterCount == 1) { 088 if(entityRef != null || uuid != null) { 089 entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, entityRef, uuid, null); 090 091 if(!hasExecutionErrors()) { 092 if(session.hasLimit(EventFactory.class)) { 093 eventCount = eventControl.countEventsByEntityInstance(entityInstance); 094 } 095 096 entities = eventControl.getEventsByEntityInstance(entityInstance); 097 } 098 } else { 099 createdBy = EntityInstanceLogic.getInstance().getEntityInstance(this, createdByEntityRef, createdByUuid, null); 100 101 if(!hasExecutionErrors()) { 102 if(session.hasLimit(EventFactory.class)) { 103 eventCount = eventControl.countEventsByCreatedBy(createdBy); 104 } 105 106 entities = eventControl.getEventsByCreatedBy(createdBy); 107 } 108 } 109 } else { 110 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 111 } 112 113 return entities; 114 } 115 116 @Override 117 protected BaseResult getResult(Collection<Event> entities) { 118 var result = CoreResultFactory.getGetEventsResult(); 119 120 if(entities != null) { 121 var eventControl = Session.getModelController(EventControl.class); 122 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 123 var userVisit = getUserVisit(); 124 125 result.setEventCount(eventCount); 126 127 if(entityInstance != null) { 128 result.setEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, entityInstance, false, false, false, false)); 129 } 130 131 if(createdBy != null) { 132 result.setCreatedByEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, createdBy, false, false, false, false)); 133 } 134 135 result.setEvents(eventControl.getEventTransfers(userVisit, entities)); 136 } 137 138 return result; 139 } 140 141}