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.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.Collection;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class GetEventsCommand
045        extends BasePaginatedMultipleEntitiesCommand<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(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                    new SecurityRoleDefinition(SecurityRoleGroups.Event.name(), SecurityRoles.List.name())
055                ))
056        ));
057        
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
060                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
061                new FieldDefinition("CreatedByEntityRef", FieldType.ENTITY_REF, false, null, null),
062                new FieldDefinition("CreatedByUuid", FieldType.UUID, false, null, null)
063        );
064    }
065    
066    @Inject
067    EntityInstanceControl entityInstanceControl;
068
069    @Inject
070    EntityInstanceLogic entityInstanceLogic;
071
072    /** Creates a new instance of GetEventsCommand */
073    public GetEventsCommand() {
074        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
075    }
076    
077    private EntityInstance entityInstance;
078    private EntityInstance createdBy;
079
080    @Override
081    protected void handleForm() {
082        var entityRef = form.getEntityRef();
083        var uuid = form.getUuid();
084        var createdByEntityRef = form.getCreatedByEntityRef();
085        var createdByUuid = form.getCreatedByUuid();
086        var parameterCount = (entityRef == null ? 0 : 1) + (uuid == null ? 0 : 1)
087                + (createdByEntityRef == null ? 0 : 1) + (createdByUuid == null ? 0 : 1);
088
089        if(parameterCount == 1) {
090            if(entityRef != null || uuid != null) {
091                entityInstance = entityInstanceLogic.getEntityInstance(this, entityRef, uuid, null);
092            } else {
093                createdBy = entityInstanceLogic.getEntityInstance(this, createdByEntityRef, createdByUuid, null);
094            }
095        } else {
096            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
097        }
098    }
099
100    @Override
101    protected Long getTotalEntities() {
102        Long total = null;
103
104        if(!hasExecutionErrors()) {
105            if(entityInstance != null) {
106                total = eventControl.countEventsByEntityInstance(entityInstance);
107            } else if(createdBy != null) {
108                total = eventControl.countEventsByCreatedBy(createdBy);
109            }
110        }
111
112        return total;
113    }
114
115    @Override
116    protected Collection<Event> getEntities() {
117        Collection<Event> entities = null;
118
119        if(!hasExecutionErrors()) {
120            if(entityInstance != null) {
121                entities = eventControl.getEventsByEntityInstance(entityInstance);
122            } else if(createdBy != null) {
123                entities = eventControl.getEventsByCreatedBy(createdBy);
124            }
125        }
126
127        return entities;
128    }
129
130    @Override
131    protected BaseResult getResult(Collection<Event> entities) {
132        var result = CoreResultFactory.getGetEventsResult();
133
134        if(entities != null) {
135            var userVisit = getUserVisit();
136
137            if(session.hasLimit(EventFactory.class)) {
138                result.setEventCount(getTotalEntities());
139            }
140
141            if(entityInstance != null) {
142                result.setEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, entityInstance, false, false, false, false));
143            }
144
145            if(createdBy != null) {
146                result.setCreatedByEntityInstance(entityInstanceControl.getEntityInstanceTransfer(userVisit, createdBy, false, false, false, false));
147            }
148
149            result.setEvents(eventControl.getEventTransfers(userVisit, entities));
150        }
151
152        return result;
153    }
154    
155}