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.WorkflowDestinationDescriptionEdit; 020import com.echothree.control.user.workflow.common.edit.WorkflowEditFactory; 021import com.echothree.control.user.workflow.common.form.EditWorkflowDestinationDescriptionForm; 022import com.echothree.control.user.workflow.common.result.EditWorkflowDestinationDescriptionResult; 023import com.echothree.control.user.workflow.common.result.WorkflowResultFactory; 024import com.echothree.control.user.workflow.common.spec.WorkflowDestinationDescriptionSpec; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.control.workflow.server.control.WorkflowControl; 030import com.echothree.model.data.party.server.entity.Language; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.model.data.workflow.server.entity.Workflow; 033import com.echothree.model.data.workflow.server.entity.WorkflowDestination; 034import com.echothree.model.data.workflow.server.entity.WorkflowDestinationDescription; 035import com.echothree.model.data.workflow.server.entity.WorkflowStep; 036import com.echothree.model.data.workflow.server.value.WorkflowDestinationDescriptionValue; 037import com.echothree.util.common.message.ExecutionErrors; 038import com.echothree.util.common.validation.FieldDefinition; 039import com.echothree.util.common.validation.FieldType; 040import com.echothree.util.common.command.BaseResult; 041import com.echothree.util.common.command.EditMode; 042import com.echothree.util.server.control.BaseEditCommand; 043import com.echothree.util.server.control.CommandSecurityDefinition; 044import com.echothree.util.server.control.PartyTypeDefinition; 045import com.echothree.util.server.control.SecurityRoleDefinition; 046import com.echothree.util.server.persistence.Session; 047import java.util.Arrays; 048import java.util.Collections; 049import java.util.List; 050 051public class EditWorkflowDestinationDescriptionCommand 052 extends BaseEditCommand<WorkflowDestinationDescriptionSpec, WorkflowDestinationDescriptionEdit> { 053 054 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 055 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 056 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 057 058 static { 059 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 060 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 061 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 062 new SecurityRoleDefinition(SecurityRoleGroups.WorkflowDestination.name(), SecurityRoles.Description.name()) 063 ))) 064 ))); 065 066 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 067 new FieldDefinition("WorkflowName", FieldType.ENTITY_NAME, true, null, null), 068 new FieldDefinition("WorkflowStepName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("WorkflowDestinationName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 071 )); 072 073 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 074 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 075 )); 076 } 077 078 /** Creates a new instance of EditWorkflowDestinationDescriptionCommand */ 079 public EditWorkflowDestinationDescriptionCommand(UserVisitPK userVisitPK, EditWorkflowDestinationDescriptionForm 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 EditWorkflowDestinationDescriptionResult result = WorkflowResultFactory.getEditWorkflowDestinationDescriptionResult(); 087 String workflowName = spec.getWorkflowName(); 088 var workflow = workflowControl.getWorkflowByName(workflowName); 089 090 if(workflow != null) { 091 String workflowStepName = spec.getWorkflowStepName(); 092 var workflowStep = workflowControl.getWorkflowStepByName(workflow, workflowStepName); 093 094 if(workflowStep != null) { 095 String workflowDestinationName = spec.getWorkflowDestinationName(); 096 WorkflowDestination workflowDestination = workflowControl.getWorkflowDestinationByName(workflowStep, workflowDestinationName); 097 098 if(workflowDestination != null) { 099 var partyControl = Session.getModelController(PartyControl.class); 100 String languageIsoName = spec.getLanguageIsoName(); 101 Language language = partyControl.getLanguageByIsoName(languageIsoName); 102 103 if(language != null) { 104 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 105 WorkflowDestinationDescription workflowDestinationDescription = workflowControl.getWorkflowDestinationDescription(workflowDestination, language); 106 107 if(workflowDestinationDescription != null) { 108 if(editMode.equals(EditMode.LOCK)) { 109 result.setWorkflowDestinationDescription(workflowControl.getWorkflowDestinationDescriptionTransfer(getUserVisit(), workflowDestinationDescription)); 110 111 if(lockEntity(workflowDestination)) { 112 WorkflowDestinationDescriptionEdit edit = WorkflowEditFactory.getWorkflowDestinationDescriptionEdit(); 113 114 result.setEdit(edit); 115 edit.setDescription(workflowDestinationDescription.getDescription()); 116 } else { 117 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 118 } 119 120 result.setEntityLock(getEntityLockTransfer(workflowDestination)); 121 } else { // EditMode.ABANDON 122 unlockEntity(workflowDestination); 123 } 124 } else { 125 addExecutionError(ExecutionErrors.UnknownWorkflowDestinationDescription.name()); 126 } 127 } else if(editMode.equals(EditMode.UPDATE)) { 128 WorkflowDestinationDescriptionValue workflowDestinationDescriptionValue = workflowControl.getWorkflowDestinationDescriptionValueForUpdate(workflowDestination, language); 129 130 if(workflowDestinationDescriptionValue != null) { 131 if(lockEntityForUpdate(workflowDestination)) { 132 try { 133 String description = edit.getDescription(); 134 135 workflowDestinationDescriptionValue.setDescription(description); 136 137 workflowControl.updateWorkflowDestinationDescriptionFromValue(workflowDestinationDescriptionValue, getPartyPK()); 138 } finally { 139 unlockEntity(workflowDestination); 140 } 141 } else { 142 addExecutionError(ExecutionErrors.EntityLockStale.name()); 143 } 144 } else { 145 addExecutionError(ExecutionErrors.UnknownWorkflowDestinationDescription.name()); 146 } 147 } 148 } else { 149 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 150 } 151 } else { 152 addExecutionError(ExecutionErrors.UnknownWorkflowDestinationName.name(), workflowDestinationName); 153 } 154 } else { 155 addExecutionError(ExecutionErrors.UnknownWorkflowStepName.name(), workflowStepName); 156 } 157 } else { 158 addExecutionError(ExecutionErrors.UnknownWorkflowName.name(), workflowName); 159 } 160 161 return result; 162 } 163 164}