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