001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.control.user.core.common.result.GetEventsResult; 022import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.security.common.SecurityRoleGroups; 025import com.echothree.model.control.security.common.SecurityRoles; 026import com.echothree.model.data.core.server.entity.EntityInstance; 027import com.echothree.model.data.core.server.entity.Event; 028import com.echothree.model.data.core.server.factory.EventFactory; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.model.data.user.server.entity.UserVisit; 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 java.util.Arrays; 040import java.util.Collection; 041import java.util.Collections; 042import java.util.List; 043 044public class GetEventsCommand 045 extends BaseMultipleEntitiesCommand<Event, GetEventsForm> { 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(Collections.unmodifiableList(Arrays.asList( 052 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 054 new SecurityRoleDefinition(SecurityRoleGroups.Event.name(), SecurityRoles.List.name()) 055 ))) 056 ))); 057 058 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 059 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 060 new FieldDefinition("Key", FieldType.KEY, false, null, null), 061 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 062 new FieldDefinition("Ulid", FieldType.ULID, false, null, null), 063 new FieldDefinition("CreatedByEntityRef", FieldType.ENTITY_REF, false, null, null), 064 new FieldDefinition("CreatedByKey", FieldType.KEY, false, null, null), 065 new FieldDefinition("CreatedByGuid", FieldType.GUID, false, null, null), 066 new FieldDefinition("CreatedByUlid", FieldType.ULID, false, null, null) 067 )); 068 } 069 070 /** Creates a new instance of GetEventsCommand */ 071 public GetEventsCommand(UserVisitPK userVisitPK, GetEventsForm form) { 072 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 073 } 074 075 EntityInstance entityInstance; 076 EntityInstance createdBy; 077 Long eventCount; 078 079 @Override 080 protected Collection<Event> getEntities() { 081 String entityRef = form.getEntityRef(); 082 String key = form.getKey(); 083 String guid = form.getGuid(); 084 String ulid = form.getUlid(); 085 String createdByEntityRef = form.getCreatedByEntityRef(); 086 String createdByKey = form.getCreatedByKey(); 087 String createdByGuid = form.getCreatedByGuid(); 088 String createdByUlid = form.getCreatedByUlid(); 089 var parameterCount = (entityRef == null ? 0 : 1) + (key == null ? 0 : 1) + (guid == null ? 0 : 1) + (ulid == null ? 0 : 1) 090 + (createdByEntityRef == null ? 0 : 1) + (createdByKey == null ? 0 : 1) + (createdByGuid == null ? 0 : 1) + (createdByUlid == null ? 0 : 1); 091 Collection<Event> entities = null; 092 093 if(parameterCount == 1) { 094 var coreControl = getCoreControl(); 095 096 if(entityRef != null || key != null || guid != null || ulid != null) { 097 entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, entityRef, key, guid, ulid, null); 098 099 if(!hasExecutionErrors()) { 100 if(session.hasLimit(EventFactory.class)) { 101 eventCount = coreControl.countEventsByEntityInstance(entityInstance); 102 } 103 104 entities = coreControl.getEventsByEntityInstance(entityInstance); 105 } 106 } else { 107 createdBy = EntityInstanceLogic.getInstance().getEntityInstance(this, createdByEntityRef, createdByKey, createdByGuid, createdByUlid, null); 108 109 if(!hasExecutionErrors()) { 110 if(session.hasLimit(EventFactory.class)) { 111 eventCount = coreControl.countEventsByCreatedBy(createdBy); 112 } 113 114 entities = coreControl.getEventsByCreatedBy(createdBy); 115 } 116 } 117 } else { 118 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 119 } 120 121 return entities; 122 } 123 124 @Override 125 protected BaseResult getResult(Collection<Event> entities) { 126 GetEventsResult result = CoreResultFactory.getGetEventsResult(); 127 128 if(entities != null) { 129 var coreControl = getCoreControl(); 130 UserVisit userVisit = getUserVisit(); 131 132 result.setEventCount(eventCount); 133 134 if(entityInstance != null) { 135 result.setEntityInstance(coreControl.getEntityInstanceTransfer(userVisit, entityInstance, false, false, false, false, false, false)); 136 } 137 138 if(createdBy != null) { 139 result.setCreatedByEntityInstance(coreControl.getEntityInstanceTransfer(userVisit, createdBy, false, false, false, false, false, false)); 140 } 141 142 result.setEvents(coreControl.getEventTransfers(userVisit, entities)); 143 } 144 145 return result; 146 } 147 148}