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