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.workflow.server.command; 018 019import com.echothree.control.user.workflow.common.form.GetWorkflowsForm; 020import com.echothree.control.user.workflow.common.result.WorkflowResultFactory; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.security.common.SecurityRoleGroups; 023import com.echothree.model.control.security.common.SecurityRoles; 024import com.echothree.model.control.selector.server.control.SelectorControl; 025import com.echothree.model.control.selector.server.logic.SelectorKindLogic; 026import com.echothree.model.control.workflow.server.control.WorkflowControl; 027import com.echothree.model.data.selector.server.entity.SelectorKind; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.model.data.workflow.server.entity.Workflow; 030import com.echothree.model.data.workflow.server.factory.WorkflowFactory; 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.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 GetWorkflowsCommand 045 extends BasePaginatedMultipleEntitiesCommand<Workflow, GetWorkflowsForm> { 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.Workflow.name(), SecurityRoles.List.name()) 055 )) 056 )); 057 058 FORM_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, false, null, null) 060 ); 061 } 062 063 @Inject 064 SelectorControl selectorControl; 065 066 @Inject 067 WorkflowControl workflowControl; 068 069 @Inject 070 SelectorKindLogic selectorKindLogic; 071 072 073 /** Creates a new instance of GetWorkflowsCommand */ 074 public GetWorkflowsCommand() { 075 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 076 } 077 078 SelectorKind selectorKind; 079 080 @Override 081 protected void handleForm() { 082 var selectorKindName = form.getSelectorKindName(); 083 084 if(selectorKindName != null) { 085 selectorKind = selectorKindLogic.getSelectorKindByName(this, selectorKindName); 086 } 087 } 088 089 @Override 090 protected Long getTotalEntities() { 091 return hasExecutionErrors() ? null : 092 selectorKind == null ? 093 workflowControl.countWorkflows() : 094 workflowControl.countWorkflowsBySelectorKind(selectorKind); 095 } 096 097 @Override 098 protected Collection<Workflow> getEntities() { 099 return hasExecutionErrors() ? null : 100 selectorKind == null ? 101 workflowControl.getWorkflows() : 102 workflowControl.getWorkflowsBySelectorKind(selectorKind); 103 } 104 105 @Override 106 protected BaseResult getResult(Collection<Workflow> entities) { 107 var result = WorkflowResultFactory.getGetWorkflowsResult(); 108 109 if(entities != null) { 110 var userVisit = getUserVisit(); 111 112 if(selectorKind != null) { 113 result.setSelectorKind(selectorControl.getSelectorKindTransfer(userVisit, selectorKind)); 114 } 115 116 if(session.hasLimit(WorkflowFactory.class)) { 117 result.setWorkflowCount(getTotalEntities()); 118 } 119 120 result.setWorkflows(workflowControl.getWorkflowTransfers(userVisit, entities)); 121 } 122 123 return result; 124 } 125 126}