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.CreateWorkflowForm; 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.security.server.control.SecurityControl; 025import com.echothree.model.control.selector.server.control.SelectorControl; 026import com.echothree.model.control.workflow.server.control.WorkflowControl; 027import com.echothree.model.data.user.common.pk.UserVisitPK; 028import com.echothree.util.common.command.BaseResult; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BaseSimpleCommand; 033import com.echothree.util.server.control.CommandSecurityDefinition; 034import com.echothree.util.server.control.PartyTypeDefinition; 035import com.echothree.util.server.control.SecurityRoleDefinition; 036import java.util.List; 037import javax.enterprise.context.Dependent; 038import javax.inject.Inject; 039 040@Dependent 041public class CreateWorkflowCommand 042 extends BaseSimpleCommand<CreateWorkflowForm> { 043 044 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 049 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 050 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 051 new SecurityRoleDefinition(SecurityRoleGroups.Workflow.name(), SecurityRoles.Create.name()) 052 )) 053 )); 054 055 FORM_FIELD_DEFINITIONS = List.of( 056 new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, true, null, null), 057 new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, false, null, null), 058 new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, false, null, null), 059 new FieldDefinition("SecurityRoleGroupName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 061 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 062 ); 063 } 064 065 @Inject 066 SecurityControl securityControl; 067 068 @Inject 069 SelectorControl selectorControl; 070 071 @Inject 072 WorkflowControl workflowControl; 073 074 075 /** Creates a new instance of CreateWorkflowCommand */ 076 public CreateWorkflowCommand() { 077 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 078 } 079 080 @Override 081 protected BaseResult execute() { 082 var result = WorkflowResultFactory.getCreateWorkflowResult(); 083 var workflowName = form.getWorkflowName(); 084 var workflow = workflowControl.getWorkflowByName(workflowName); 085 086 if(workflow == null) { 087 var selectorKindName = form.getSelectorKindName(); 088 var selectorTypeName = form.getSelectorTypeName(); 089 var parameterCount = (selectorKindName == null ? 0 : 1) + (selectorTypeName == null ? 0 : 1); 090 091 if(parameterCount == 0 || parameterCount == 2) { 092 var selectorKind = selectorKindName == null? null: selectorControl.getSelectorKindByName(selectorKindName); 093 094 if(selectorKindName == null || selectorKind != null) { 095 var selectorType = selectorTypeName == null? null: selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName); 096 097 if(selectorTypeName == null || selectorType != null) { 098 var securityRoleGroupName = form.getSecurityRoleGroupName(); 099 var securityRoleGroup = securityRoleGroupName == null? null: securityControl.getSecurityRoleGroupByName(securityRoleGroupName); 100 101 if(securityRoleGroupName == null || securityRoleGroup != null) { 102 var partyPK = getPartyPK(); 103 var sortOrder = Integer.valueOf(form.getSortOrder()); 104 var description = form.getDescription(); 105 106 workflow = workflowControl.createWorkflow(workflowName, selectorType, securityRoleGroup, sortOrder, partyPK); 107 108 if(description != null) { 109 workflowControl.createWorkflowDescription(workflow, getPreferredLanguage(), description, partyPK); 110 } 111 } else { 112 addExecutionError(ExecutionErrors.UnknownSecurityRoleGroupName.name(), securityRoleGroupName); 113 } 114 } else { 115 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorKindName, selectorTypeName); 116 } 117 } else { 118 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName); 119 } 120 } else { 121 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.DuplicateWorkflowName.name(), workflowName); 125 } 126 127 if(workflow != null) { 128 var basePK = workflow.getPrimaryKey(); 129 130 result.setWorkflowName(workflow.getLastDetail().getWorkflowName()); 131 result.setEntityRef(basePK.getEntityRef()); 132 } 133 134 return result; 135 } 136 137}