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.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 com.echothree.util.server.persistence.Session; 040import java.util.List; 041import javax.enterprise.context.RequestScoped; 042 043@RequestScoped 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 /** Creates a new instance of EditWorkflowEntranceCommand */ 075 public EditWorkflowEntranceCommand() { 076 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 077 } 078 079 @Override 080 public EditWorkflowEntranceResult getResult() { 081 return WorkflowResultFactory.getEditWorkflowEntranceResult(); 082 } 083 084 @Override 085 public WorkflowEntranceEdit getEdit() { 086 return WorkflowEditFactory.getWorkflowEntranceEdit(); 087 } 088 089 @Override 090 public WorkflowEntrance getEntity(EditWorkflowEntranceResult result) { 091 return WorkflowEntranceLogic.getInstance().getWorkflowEntranceByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode)); 092 } 093 094 @Override 095 public WorkflowEntrance getLockEntity(WorkflowEntrance freeOnBoard) { 096 return freeOnBoard; 097 } 098 099 @Override 100 public void fillInResult(EditWorkflowEntranceResult result, WorkflowEntrance freeOnBoard) { 101 var workflow = Session.getModelController(WorkflowControl.class); 102 103 result.setWorkflowEntrance(workflow.getWorkflowEntranceTransfer(getUserVisit(), freeOnBoard)); 104 } 105 106 @Override 107 public void doLock(WorkflowEntranceEdit edit, WorkflowEntrance workflowEntrance) { 108 var workflowControl = Session.getModelController(WorkflowControl.class); 109 var workflowEntranceDescription = workflowControl.getWorkflowEntranceDescription(workflowEntrance, getPreferredLanguage()); 110 var workflowEntranceDetail = workflowEntrance.getLastDetail(); 111 112 edit.setWorkflowEntranceName(workflowEntranceDetail.getWorkflowEntranceName()); 113 edit.setIsDefault(workflowEntranceDetail.getIsDefault().toString()); 114 edit.setSortOrder(workflowEntranceDetail.getSortOrder().toString()); 115 116 if(workflowEntranceDescription != null) { 117 edit.setDescription(workflowEntranceDescription.getDescription()); 118 } 119 } 120 121 @Override 122 public void canUpdate(WorkflowEntrance workflowEntrance) { 123 var workflowControl = Session.getModelController(WorkflowControl.class); 124 var workflow = workflowEntrance.getLastDetail().getWorkflow(); 125 var workflowEntranceName = edit.getWorkflowEntranceName(); 126 var duplicateWorkflowEntrance = workflowControl.getWorkflowEntranceByName(workflow, workflowEntranceName); 127 128 if(duplicateWorkflowEntrance != null && !workflowEntrance.equals(duplicateWorkflowEntrance)) { 129 addExecutionError(ExecutionErrors.DuplicateWorkflowEntranceName.name(), workflowEntranceName); 130 } 131 } 132 133 @Override 134 public void doUpdate(WorkflowEntrance workflowEntrance) { 135 var workflowControl = Session.getModelController(WorkflowControl.class); 136 var partyPK = getPartyPK(); 137 var workflowEntranceDetailValue = workflowControl.getWorkflowEntranceDetailValueForUpdate(workflowEntrance); 138 var workflowEntranceDescription = workflowControl.getWorkflowEntranceDescriptionForUpdate(workflowEntrance, getPreferredLanguage()); 139 var description = edit.getDescription(); 140 141 workflowEntranceDetailValue.setWorkflowEntranceName(edit.getWorkflowEntranceName()); 142 workflowEntranceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 143 workflowEntranceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 144 145 workflowControl.updateWorkflowEntranceFromValue(workflowEntranceDetailValue, partyPK); 146 147 if(workflowEntranceDescription == null && description != null) { 148 workflowControl.createWorkflowEntranceDescription(workflowEntrance, getPreferredLanguage(), description, partyPK); 149 } else if(workflowEntranceDescription != null && description == null) { 150 workflowControl.deleteWorkflowEntranceDescription(workflowEntranceDescription, partyPK); 151 } else if(workflowEntranceDescription != null && description != null) { 152 var workflowEntranceDescriptionValue = workflowControl.getWorkflowEntranceDescriptionValue(workflowEntranceDescription); 153 154 workflowEntranceDescriptionValue.setDescription(description); 155 workflowControl.updateWorkflowEntranceDescriptionFromValue(workflowEntranceDescriptionValue, partyPK); 156 } 157 } 158 159}