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.workflow.server.command;
018
019import com.echothree.control.user.workflow.common.form.GetWorkflowEntityStatusesForm;
020import com.echothree.control.user.workflow.common.form.GetWorkflowEntityTypesForm;
021import com.echothree.control.user.workflow.common.result.WorkflowResultFactory;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.security.common.SecurityRoleGroups;
024import com.echothree.model.control.security.common.SecurityRoles;
025import com.echothree.model.control.workflow.server.control.WorkflowControl;
026import com.echothree.model.control.workflow.server.logic.WorkflowLogic;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.model.data.workflow.server.entity.Workflow;
029import com.echothree.model.data.workflow.server.entity.WorkflowEntityStatus;
030import com.echothree.model.data.workflow.server.entity.WorkflowEntityType;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseMultipleEntitiesCommand;
035import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
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;
044
045public class GetWorkflowEntityStatusesCommand
046        extends BasePaginatedMultipleEntitiesCommand<WorkflowEntityStatus, GetWorkflowEntityStatusesForm> {
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.WorkflowEntityStatus.name(), SecurityRoles.List.name())
056                ))
057        ));
058
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, true, null, null)
061        );
062    }
063    
064    /** Creates a new instance of GetWorkflowEntityStatusesCommand */
065    public GetWorkflowEntityStatusesCommand(UserVisitPK userVisitPK, GetWorkflowEntityStatusesForm form) {
066        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
067    }
068
069    Workflow workflow;
070
071    @Override
072    protected void handleForm() {
073        var workflowName = form.getWorkflowName();
074
075        workflow = WorkflowLogic.getInstance().getWorkflowByName(this, workflowName);
076    }
077
078    @Override
079    protected Long getTotalEntities() {
080        var workflowControl = Session.getModelController(WorkflowControl.class);
081
082        return hasExecutionErrors() ? null :
083                workflowControl.countWorkflowEntityStatusesByWorkflow(workflow);
084    }
085
086    @Override
087    protected Collection<WorkflowEntityStatus> getEntities() {
088        Collection<WorkflowEntityStatus> workflowEntityStatuses = null;
089
090        if(!hasExecutionErrors()) {
091            var workflowControl = Session.getModelController(WorkflowControl.class);
092
093            workflowEntityStatuses = workflowControl.getWorkflowEntityStatusesByWorkflow(workflow);
094        }
095
096        return workflowEntityStatuses;
097    }
098
099    @Override
100    protected BaseResult getResult(Collection<WorkflowEntityStatus> entities) {
101        var result = WorkflowResultFactory.getGetWorkflowEntityStatusesResult();
102
103        if(entities != null) {
104            var workflowControl = Session.getModelController(WorkflowControl.class);
105            var userVisit = getUserVisit();
106
107            result.setWorkflow(workflowControl.getWorkflowTransfer(userVisit, workflow));
108            result.setWorkflowEntityStatuses(workflowControl.getWorkflowEntityStatusTransfers(getUserVisit(), entities));
109        }
110
111        return result;
112    }
113
114}