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.edit.WorkflowDestinationEdit; 020import com.echothree.control.user.workflow.common.edit.WorkflowEditFactory; 021import com.echothree.control.user.workflow.common.form.EditWorkflowDestinationForm; 022import com.echothree.control.user.workflow.common.result.EditWorkflowDestinationResult; 023import com.echothree.control.user.workflow.common.result.WorkflowResultFactory; 024import com.echothree.control.user.workflow.common.spec.WorkflowDestinationUniversalSpec; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.control.workflow.server.control.WorkflowControl; 029import com.echothree.model.control.workflow.server.logic.WorkflowDestinationLogic; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.model.data.workflow.server.entity.WorkflowDestination; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseAbstractEditCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import java.util.List; 040import javax.enterprise.context.Dependent; 041import javax.inject.Inject; 042 043@Dependent 044public class EditWorkflowDestinationCommand 045 extends BaseAbstractEditCommand<WorkflowDestinationUniversalSpec, WorkflowDestinationEdit, EditWorkflowDestinationResult, WorkflowDestination, WorkflowDestination> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 049 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.WorkflowDestination.name(), SecurityRoles.Edit.name()) 056 )) 057 )); 058 059 SPEC_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("WorkflowStepName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("WorkflowDestinationName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 064 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 065 ); 066 067 EDIT_FIELD_DEFINITIONS = List.of( 068 new FieldDefinition("WorkflowDestinationName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 070 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 071 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 072 ); 073 } 074 075 @Inject 076 WorkflowControl workflowControl; 077 078 @Inject 079 WorkflowDestinationLogic workflowDestinationLogic; 080 081 082 /** Creates a new instance of EditWorkflowDestinationCommand */ 083 public EditWorkflowDestinationCommand() { 084 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 085 } 086 087 @Override 088 public EditWorkflowDestinationResult getResult() { 089 return WorkflowResultFactory.getEditWorkflowDestinationResult(); 090 } 091 092 @Override 093 public WorkflowDestinationEdit getEdit() { 094 return WorkflowEditFactory.getWorkflowDestinationEdit(); 095 } 096 097 @Override 098 public WorkflowDestination getEntity(EditWorkflowDestinationResult result) { 099 return workflowDestinationLogic.getWorkflowDestinationByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode)); 100 } 101 102 @Override 103 public WorkflowDestination getLockEntity(WorkflowDestination freeOnBoard) { 104 return freeOnBoard; 105 } 106 107 @Override 108 public void fillInResult(EditWorkflowDestinationResult result, WorkflowDestination freeOnBoard) { 109 var workflow = workflowControl; 110 111 result.setWorkflowDestination(workflow.getWorkflowDestinationTransfer(getUserVisit(), freeOnBoard)); 112 } 113 114 @Override 115 public void doLock(WorkflowDestinationEdit edit, WorkflowDestination workflowDestination) { 116 var workflowDestinationDescription = workflowControl.getWorkflowDestinationDescription(workflowDestination, getPreferredLanguage()); 117 var workflowDestinationDetail = workflowDestination.getLastDetail(); 118 119 edit.setWorkflowDestinationName(workflowDestinationDetail.getWorkflowDestinationName()); 120 edit.setIsDefault(workflowDestinationDetail.getIsDefault().toString()); 121 edit.setSortOrder(workflowDestinationDetail.getSortOrder().toString()); 122 123 if(workflowDestinationDescription != null) { 124 edit.setDescription(workflowDestinationDescription.getDescription()); 125 } 126 } 127 128 @Override 129 public void canUpdate(WorkflowDestination workflowDestination) { 130 var workflowStep = workflowDestination.getLastDetail().getWorkflowStep(); 131 var workflowDestinationName = edit.getWorkflowDestinationName(); 132 var duplicateWorkflowDestination = workflowControl.getWorkflowDestinationByName(workflowStep, workflowDestinationName); 133 134 if(duplicateWorkflowDestination != null && !workflowDestination.equals(duplicateWorkflowDestination)) { 135 addExecutionError(ExecutionErrors.DuplicateWorkflowDestinationName.name(), workflowDestinationName); 136 } 137 } 138 139 @Override 140 public void doUpdate(WorkflowDestination workflowDestination) { 141 var partyPK = getPartyPK(); 142 var workflowDestinationDetailValue = workflowControl.getWorkflowDestinationDetailValueForUpdate(workflowDestination); 143 var workflowDestinationDescription = workflowControl.getWorkflowDestinationDescriptionForUpdate(workflowDestination, getPreferredLanguage()); 144 var description = edit.getDescription(); 145 146 workflowDestinationDetailValue.setWorkflowDestinationName(edit.getWorkflowDestinationName()); 147 workflowDestinationDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 148 workflowDestinationDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 149 150 workflowControl.updateWorkflowDestinationFromValue(workflowDestinationDetailValue, partyPK); 151 152 if(workflowDestinationDescription == null && description != null) { 153 workflowControl.createWorkflowDestinationDescription(workflowDestination, getPreferredLanguage(), description, partyPK); 154 } else if(workflowDestinationDescription != null && description == null) { 155 workflowControl.deleteWorkflowDestinationDescription(workflowDestinationDescription, partyPK); 156 } else if(workflowDestinationDescription != null && description != null) { 157 var workflowDestinationDescriptionValue = workflowControl.getWorkflowDestinationDescriptionValue(workflowDestinationDescription); 158 159 workflowDestinationDescriptionValue.setDescription(description); 160 workflowControl.updateWorkflowDestinationDescriptionFromValue(workflowDestinationDescriptionValue, partyPK); 161 } 162 } 163 164}