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 com.echothree.util.server.persistence.Session; 037import java.util.List; 038import javax.enterprise.context.Dependent; 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 /** Creates a new instance of CreateWorkflowCommand */ 066 public CreateWorkflowCommand() { 067 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 068 } 069 070 @Override 071 protected BaseResult execute() { 072 var result = WorkflowResultFactory.getCreateWorkflowResult(); 073 var workflowControl = Session.getModelController(WorkflowControl.class); 074 var workflowName = form.getWorkflowName(); 075 var workflow = workflowControl.getWorkflowByName(workflowName); 076 077 if(workflow == null) { 078 var selectorControl = Session.getModelController(SelectorControl.class); 079 var selectorKindName = form.getSelectorKindName(); 080 var selectorTypeName = form.getSelectorTypeName(); 081 var parameterCount = (selectorKindName == null ? 0 : 1) + (selectorTypeName == null ? 0 : 1); 082 083 if(parameterCount == 0 || parameterCount == 2) { 084 var selectorKind = selectorKindName == null? null: selectorControl.getSelectorKindByName(selectorKindName); 085 086 if(selectorKindName == null || selectorKind != null) { 087 var selectorType = selectorTypeName == null? null: selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName); 088 089 if(selectorTypeName == null || selectorType != null) { 090 var securityControl = Session.getModelController(SecurityControl.class); 091 var securityRoleGroupName = form.getSecurityRoleGroupName(); 092 var securityRoleGroup = securityRoleGroupName == null? null: securityControl.getSecurityRoleGroupByName(securityRoleGroupName); 093 094 if(securityRoleGroupName == null || securityRoleGroup != null) { 095 var partyPK = getPartyPK(); 096 var sortOrder = Integer.valueOf(form.getSortOrder()); 097 var description = form.getDescription(); 098 099 workflow = workflowControl.createWorkflow(workflowName, selectorType, securityRoleGroup, sortOrder, partyPK); 100 101 if(description != null) { 102 workflowControl.createWorkflowDescription(workflow, getPreferredLanguage(), description, partyPK); 103 } 104 } else { 105 addExecutionError(ExecutionErrors.UnknownSecurityRoleGroupName.name(), securityRoleGroupName); 106 } 107 } else { 108 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorKindName, selectorTypeName); 109 } 110 } else { 111 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName); 112 } 113 } else { 114 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 115 } 116 } else { 117 addExecutionError(ExecutionErrors.DuplicateWorkflowName.name(), workflowName); 118 } 119 120 if(workflow != null) { 121 var basePK = workflow.getPrimaryKey(); 122 123 result.setWorkflowName(workflow.getLastDetail().getWorkflowName()); 124 result.setEntityRef(basePK.getEntityRef()); 125 } 126 127 return result; 128 } 129 130}