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.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 com.echothree.util.server.persistence.Session; 039import java.util.Collection; 040import java.util.List; 041import javax.enterprise.context.RequestScoped; 042 043@RequestScoped 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 /** Creates a new instance of GetWorkflowsCommand */ 064 public GetWorkflowsCommand() { 065 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 066 } 067 068 SelectorKind selectorKind; 069 070 @Override 071 protected void handleForm() { 072 var selectorKindName = form.getSelectorKindName(); 073 074 if(selectorKindName != null) { 075 selectorKind = SelectorKindLogic.getInstance().getSelectorKindByName(this, selectorKindName); 076 } 077 } 078 079 @Override 080 protected Long getTotalEntities() { 081 var workflowControl = Session.getModelController(WorkflowControl.class); 082 083 return hasExecutionErrors() ? null : 084 selectorKind == null ? 085 workflowControl.countWorkflows() : 086 workflowControl.countWorkflowsBySelectorKind(selectorKind); 087 } 088 089 @Override 090 protected Collection<Workflow> getEntities() { 091 var workflowControl = Session.getModelController(WorkflowControl.class); 092 093 return hasExecutionErrors() ? null : 094 selectorKind == null ? 095 workflowControl.getWorkflows() : 096 workflowControl.getWorkflowsBySelectorKind(selectorKind); 097 } 098 099 @Override 100 protected BaseResult getResult(Collection<Workflow> entities) { 101 var result = WorkflowResultFactory.getGetWorkflowsResult(); 102 103 if(entities != null) { 104 var workflowControl = Session.getModelController(WorkflowControl.class); 105 var userVisit = getUserVisit(); 106 107 if(selectorKind != null) { 108 var selectorControl = Session.getModelController(SelectorControl.class); 109 110 result.setSelectorKind(selectorControl.getSelectorKindTransfer(userVisit, selectorKind)); 111 } 112 113 if(session.hasLimit(WorkflowFactory.class)) { 114 result.setWorkflowCount(getTotalEntities()); 115 } 116 117 result.setWorkflows(workflowControl.getWorkflowTransfers(userVisit, entities)); 118 } 119 120 return result; 121 } 122 123}