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.WorkflowEditFactory; 020import com.echothree.control.user.workflow.common.edit.WorkflowEntranceEdit; 021import com.echothree.control.user.workflow.common.form.EditWorkflowEntranceForm; 022import com.echothree.control.user.workflow.common.result.EditWorkflowEntranceResult; 023import com.echothree.control.user.workflow.common.result.WorkflowResultFactory; 024import com.echothree.control.user.workflow.common.spec.WorkflowEntranceUniversalSpec; 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.WorkflowEntranceLogic; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.model.data.workflow.server.entity.WorkflowEntrance; 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 EditWorkflowEntranceCommand 045 extends BaseAbstractEditCommand<WorkflowEntranceUniversalSpec, WorkflowEntranceEdit, EditWorkflowEntranceResult, WorkflowEntrance, WorkflowEntrance> { 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.WorkflowEntrance.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("WorkflowEntranceName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 063 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 064 ); 065 066 EDIT_FIELD_DEFINITIONS = List.of( 067 new FieldDefinition("WorkflowEntranceName", FieldType.ENTITY_NAME, true, null, null), 068 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 069 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 070 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 071 ); 072 } 073 074 @Inject 075 WorkflowControl workflowControl; 076 077 @Inject 078 WorkflowEntranceLogic workflowEntranceLogic; 079 080 081 /** Creates a new instance of EditWorkflowEntranceCommand */ 082 public EditWorkflowEntranceCommand() { 083 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 084 } 085 086 @Override 087 public EditWorkflowEntranceResult getResult() { 088 return WorkflowResultFactory.getEditWorkflowEntranceResult(); 089 } 090 091 @Override 092 public WorkflowEntranceEdit getEdit() { 093 return WorkflowEditFactory.getWorkflowEntranceEdit(); 094 } 095 096 @Override 097 public WorkflowEntrance getEntity(EditWorkflowEntranceResult result) { 098 return workflowEntranceLogic.getWorkflowEntranceByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode)); 099 } 100 101 @Override 102 public WorkflowEntrance getLockEntity(WorkflowEntrance freeOnBoard) { 103 return freeOnBoard; 104 } 105 106 @Override 107 public void fillInResult(EditWorkflowEntranceResult result, WorkflowEntrance freeOnBoard) { 108 var workflow = workflowControl; 109 110 result.setWorkflowEntrance(workflow.getWorkflowEntranceTransfer(getUserVisit(), freeOnBoard)); 111 } 112 113 @Override 114 public void doLock(WorkflowEntranceEdit edit, WorkflowEntrance workflowEntrance) { 115 var workflowEntranceDescription = workflowControl.getWorkflowEntranceDescription(workflowEntrance, getPreferredLanguage()); 116 var workflowEntranceDetail = workflowEntrance.getLastDetail(); 117 118 edit.setWorkflowEntranceName(workflowEntranceDetail.getWorkflowEntranceName()); 119 edit.setIsDefault(workflowEntranceDetail.getIsDefault().toString()); 120 edit.setSortOrder(workflowEntranceDetail.getSortOrder().toString()); 121 122 if(workflowEntranceDescription != null) { 123 edit.setDescription(workflowEntranceDescription.getDescription()); 124 } 125 } 126 127 @Override 128 public void canUpdate(WorkflowEntrance workflowEntrance) { 129 var workflow = workflowEntrance.getLastDetail().getWorkflow(); 130 var workflowEntranceName = edit.getWorkflowEntranceName(); 131 var duplicateWorkflowEntrance = workflowControl.getWorkflowEntranceByName(workflow, workflowEntranceName); 132 133 if(duplicateWorkflowEntrance != null && !workflowEntrance.equals(duplicateWorkflowEntrance)) { 134 addExecutionError(ExecutionErrors.DuplicateWorkflowEntranceName.name(), workflowEntranceName); 135 } 136 } 137 138 @Override 139 public void doUpdate(WorkflowEntrance workflowEntrance) { 140 var partyPK = getPartyPK(); 141 var workflowEntranceDetailValue = workflowControl.getWorkflowEntranceDetailValueForUpdate(workflowEntrance); 142 var workflowEntranceDescription = workflowControl.getWorkflowEntranceDescriptionForUpdate(workflowEntrance, getPreferredLanguage()); 143 var description = edit.getDescription(); 144 145 workflowEntranceDetailValue.setWorkflowEntranceName(edit.getWorkflowEntranceName()); 146 workflowEntranceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 147 workflowEntranceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 148 149 workflowControl.updateWorkflowEntranceFromValue(workflowEntranceDetailValue, partyPK); 150 151 if(workflowEntranceDescription == null && description != null) { 152 workflowControl.createWorkflowEntranceDescription(workflowEntrance, getPreferredLanguage(), description, partyPK); 153 } else if(workflowEntranceDescription != null && description == null) { 154 workflowControl.deleteWorkflowEntranceDescription(workflowEntranceDescription, partyPK); 155 } else if(workflowEntranceDescription != null && description != null) { 156 var workflowEntranceDescriptionValue = workflowControl.getWorkflowEntranceDescriptionValue(workflowEntranceDescription); 157 158 workflowEntranceDescriptionValue.setDescription(description); 159 workflowControl.updateWorkflowEntranceDescriptionFromValue(workflowEntranceDescriptionValue, partyPK); 160 } 161 } 162 163}