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.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; 041 042public class GetWorkflowsCommand 043 extends BasePaginatedMultipleEntitiesCommand<Workflow, GetWorkflowsForm> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 047 048 static { 049 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 050 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 051 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 052 new SecurityRoleDefinition(SecurityRoleGroups.Workflow.name(), SecurityRoles.List.name()) 053 )) 054 )); 055 056 FORM_FIELD_DEFINITIONS = List.of( 057 new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, false, null, null) 058 ); 059 } 060 061 /** Creates a new instance of GetWorkflowsCommand */ 062 public GetWorkflowsCommand(UserVisitPK userVisitPK, GetWorkflowsForm form) { 063 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 064 } 065 066 SelectorKind selectorKind; 067 068 @Override 069 protected void handleForm() { 070 var selectorKindName = form.getSelectorKindName(); 071 072 if(selectorKindName != null) { 073 selectorKind = SelectorKindLogic.getInstance().getSelectorKindByName(this, selectorKindName); 074 } 075 } 076 077 @Override 078 protected Long getTotalEntities() { 079 var workflowControl = Session.getModelController(WorkflowControl.class); 080 081 return hasExecutionErrors() ? null : 082 selectorKind == null ? 083 workflowControl.countWorkflows() : 084 workflowControl.countWorkflowsBySelectorKind(selectorKind); 085 } 086 087 @Override 088 protected Collection<Workflow> getEntities() { 089 var workflowControl = Session.getModelController(WorkflowControl.class); 090 091 return hasExecutionErrors() ? null : 092 selectorKind == null ? 093 workflowControl.getWorkflows() : 094 workflowControl.getWorkflowsBySelectorKind(selectorKind); 095 } 096 097 @Override 098 protected BaseResult getResult(Collection<Workflow> entities) { 099 var result = WorkflowResultFactory.getGetWorkflowsResult(); 100 101 if(entities != null) { 102 var workflowControl = Session.getModelController(WorkflowControl.class); 103 var userVisit = getUserVisit(); 104 105 if(selectorKind != null) { 106 var selectorControl = Session.getModelController(SelectorControl.class); 107 108 result.setSelectorKind(selectorControl.getSelectorKindTransfer(userVisit, selectorKind)); 109 } 110 111 if(session.hasLimit(WorkflowFactory.class)) { 112 result.setWorkflowCount(getTotalEntities()); 113 } 114 115 result.setWorkflows(workflowControl.getWorkflowTransfers(userVisit, entities)); 116 } 117 118 return result; 119 } 120 121}