001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.Arrays;
041import java.util.Collection;
042import java.util.Collections;
043import java.util.List;
044import javax.enterprise.context.RequestScoped;
045
046@RequestScoped
047public class GetEventsCommand
048        extends BaseMultipleEntitiesCommand<Event, GetEventsForm> {
049    
050    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
051    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
057                    new SecurityRoleDefinition(SecurityRoleGroups.Event.name(), SecurityRoles.List.name())
058                    )))
059                )));
060        
061        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
062                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
063                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
064                new FieldDefinition("CreatedByEntityRef", FieldType.ENTITY_REF, false, null, null),
065                new FieldDefinition("CreatedByUuid", FieldType.UUID, false, null, null)
066                ));
067    }
068    
069    /** Creates a new instance of GetEventsCommand */
070    public GetEventsCommand() {
071        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
072    }
073
074    EntityInstance entityInstance;
075    EntityInstance createdBy;
076    Long eventCount;
077
078    @Override
079    protected Collection<Event> getEntities() {
080        var eventControl = Session.getModelController(EventControl.class);
081        var entityRef = form.getEntityRef();
082        var uuid = form.getUuid();
083        var createdByEntityRef = form.getCreatedByEntityRef();
084        var createdByUuid = form.getCreatedByUuid();
085        var parameterCount = (entityRef == null ? 0 : 1) + (uuid == null ? 0 : 1)
086                + (createdByEntityRef == null ? 0 : 1) + (createdByUuid == null ? 0 : 1);
087        Collection<Event> entities = null;
088
089        if(parameterCount == 1) {
090            if(entityRef != null || uuid != null) {
091                entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, entityRef, uuid, null);
092
093                if(!hasExecutionErrors()) {
094                    if(session.hasLimit(EventFactory.class)) {
095                        eventCount = eventControl.countEventsByEntityInstance(entityInstance);
096                    }
097
098                    entities = eventControl.getEventsByEntityInstance(entityInstance);
099                }
100            } else {
101                createdBy = EntityInstanceLogic.getInstance().getEntityInstance(this, createdByEntityRef, createdByUuid, null);
102
103                if(!hasExecutionErrors()) {
104                    if(session.hasLimit(EventFactory.class)) {
105                        eventCount = eventControl.countEventsByCreatedBy(createdBy);
106                    }
107
108                    entities = eventControl.getEventsByCreatedBy(createdBy);
109                }
110            }
111        } else {
112            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
113        }
114
115        return entities;
116    }
117
118    @Override
119    protected BaseResult getResult(Collection<Event> entities) {
120        var result = CoreResultFactory.getGetEventsResult();
121
122        if(entities != null) {
123            var eventControl = Session.getModelController(EventControl.class);
124            var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
125            var userVisit = getUserVisit();
126
127            result.setEventCount(eventCount);
128
129            if(entityInstance != null) {
130                result.setEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, entityInstance, false, false, false, false));
131            }
132
133            if(createdBy != null) {
134                result.setCreatedByEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, createdBy, false, false, false, false));
135            }
136
137            result.setEvents(eventControl.getEventTransfers(userVisit, entities));
138        }
139
140        return result;
141    }
142    
143}