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.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.WorkflowEntranceSpec; 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.data.user.common.pk.UserVisitPK; 030import com.echothree.model.data.workflow.server.entity.Workflow; 031import com.echothree.model.data.workflow.server.entity.WorkflowEntrance; 032import com.echothree.model.data.workflow.server.entity.WorkflowEntranceDescription; 033import com.echothree.model.data.workflow.server.entity.WorkflowEntranceDetail; 034import com.echothree.model.data.workflow.server.value.WorkflowEntranceDescriptionValue; 035import com.echothree.model.data.workflow.server.value.WorkflowEntranceDetailValue; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.common.validation.FieldDefinition; 038import com.echothree.util.common.validation.FieldType; 039import com.echothree.util.common.command.BaseResult; 040import com.echothree.util.common.command.EditMode; 041import com.echothree.util.server.control.BaseEditCommand; 042import com.echothree.util.server.control.CommandSecurityDefinition; 043import com.echothree.util.server.control.PartyTypeDefinition; 044import com.echothree.util.server.control.SecurityRoleDefinition; 045import com.echothree.util.server.persistence.Session; 046import java.util.Arrays; 047import java.util.Collections; 048import java.util.List; 049 050public class EditWorkflowEntranceCommand 051 extends BaseEditCommand<WorkflowEntranceSpec, WorkflowEntranceEdit> { 052 053 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 054 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 055 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 056 057 static { 058 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 059 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 060 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 061 new SecurityRoleDefinition(SecurityRoleGroups.WorkflowEntrance.name(), SecurityRoles.Edit.name()) 062 ))) 063 ))); 064 065 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 066 new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("WorkflowEntranceName", FieldType.ENTITY_NAME, true, null, null) 068 )); 069 070 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 071 new FieldDefinition("WorkflowEntranceName", FieldType.ENTITY_NAME, true, null, null), 072 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 073 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 074 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 075 )); 076 } 077 078 /** Creates a new instance of EditWorkflowEntranceCommand */ 079 public EditWorkflowEntranceCommand(UserVisitPK userVisitPK, EditWorkflowEntranceForm form) { 080 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 081 } 082 083 @Override 084 protected BaseResult execute() { 085 var workflowControl = Session.getModelController(WorkflowControl.class); 086 EditWorkflowEntranceResult result = WorkflowResultFactory.getEditWorkflowEntranceResult(); 087 String workflowName = spec.getWorkflowName(); 088 var workflow = workflowControl.getWorkflowByName(workflowName); 089 090 if(workflow != null) { 091 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 092 String workflowEntranceName = spec.getWorkflowEntranceName(); 093 WorkflowEntrance workflowEntrance = workflowControl.getWorkflowEntranceByName(workflow, workflowEntranceName); 094 095 if(workflowEntrance != null) { 096 if(editMode.equals(EditMode.LOCK)) { 097 if(lockEntity(workflowEntrance)) { 098 WorkflowEntranceDescription workflowEntranceDescription = workflowControl.getWorkflowEntranceDescription(workflowEntrance, getPreferredLanguage()); 099 WorkflowEntranceEdit edit = WorkflowEditFactory.getWorkflowEntranceEdit(); 100 WorkflowEntranceDetail workflowEntranceDetail = workflowEntrance.getLastDetail(); 101 102 result.setWorkflowEntrance(workflowControl.getWorkflowEntranceTransfer(getUserVisit(), workflowEntrance)); 103 104 result.setEdit(edit); 105 edit.setWorkflowEntranceName(workflowEntranceDetail.getWorkflowEntranceName()); 106 edit.setIsDefault(workflowEntranceDetail.getIsDefault().toString()); 107 edit.setSortOrder(workflowEntranceDetail.getSortOrder().toString()); 108 109 if(workflowEntranceDescription != null) { 110 edit.setDescription(workflowEntranceDescription.getDescription()); 111 } 112 } else { 113 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 114 } 115 116 result.setEntityLock(getEntityLockTransfer(workflowEntrance)); 117 } else { // EditMode.ABANDON 118 unlockEntity(workflowEntrance); 119 } 120 } else { 121 addExecutionError(ExecutionErrors.UnknownWorkflowEntranceName.name(), workflowName, workflowEntranceName); 122 } 123 } else if(editMode.equals(EditMode.UPDATE)) { 124 String workflowEntranceName = spec.getWorkflowEntranceName(); 125 WorkflowEntrance workflowEntrance = workflowControl.getWorkflowEntranceByNameForUpdate(workflow, workflowEntranceName); 126 127 if(workflowEntrance != null) { 128 workflowEntranceName = edit.getWorkflowEntranceName(); 129 WorkflowEntrance duplicateWorkflowEntrance = workflowControl.getWorkflowEntranceByName(workflow, workflowEntranceName); 130 131 if(duplicateWorkflowEntrance == null || workflowEntrance.equals(duplicateWorkflowEntrance)) { 132 if(lockEntityForUpdate(workflowEntrance)) { 133 try { 134 var partyPK = getPartyPK(); 135 WorkflowEntranceDetailValue workflowEntranceDetailValue = workflowControl.getWorkflowEntranceDetailValueForUpdate(workflowEntrance); 136 WorkflowEntranceDescription workflowEntranceDescription = workflowControl.getWorkflowEntranceDescriptionForUpdate(workflowEntrance, getPreferredLanguage()); 137 String description = edit.getDescription(); 138 139 workflowEntranceDetailValue.setWorkflowEntranceName(workflowEntranceName); 140 workflowEntranceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 141 workflowEntranceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 142 143 workflowControl.updateWorkflowEntranceFromValue(workflowEntranceDetailValue, partyPK); 144 145 if(workflowEntranceDescription == null && description != null) { 146 workflowControl.createWorkflowEntranceDescription(workflowEntrance, getPreferredLanguage(), description, partyPK); 147 } else if(workflowEntranceDescription != null && description == null) { 148 workflowControl.deleteWorkflowEntranceDescription(workflowEntranceDescription, partyPK); 149 } else if(workflowEntranceDescription != null && description != null) { 150 WorkflowEntranceDescriptionValue workflowEntranceDescriptionValue = workflowControl.getWorkflowEntranceDescriptionValue(workflowEntranceDescription); 151 152 workflowEntranceDescriptionValue.setDescription(description); 153 workflowControl.updateWorkflowEntranceDescriptionFromValue(workflowEntranceDescriptionValue, partyPK); 154 } 155 } finally { 156 unlockEntity(workflowEntrance); 157 } 158 } else { 159 addExecutionError(ExecutionErrors.EntityLockStale.name()); 160 } 161 } else { 162 addExecutionError(ExecutionErrors.DuplicateWorkflowEntranceName.name(), workflowName, workflowEntranceName); 163 } 164 } else { 165 addExecutionError(ExecutionErrors.UnknownWorkflowEntranceName.name(), workflowName, workflowEntranceName); 166 } 167 168 if(hasExecutionErrors()) { 169 result.setWorkflowEntrance(workflowControl.getWorkflowEntranceTransfer(getUserVisit(), workflowEntrance)); 170 result.setEntityLock(getEntityLockTransfer(workflowEntrance)); 171 } 172 } 173 } else { 174 addExecutionError(ExecutionErrors.UnknownWorkflowName.name(), workflowName); 175 } 176 177 return result; 178 } 179 180}