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.job.server.command; 018 019import com.echothree.control.user.job.common.edit.JobEdit; 020import com.echothree.control.user.job.common.edit.JobEditFactory; 021import com.echothree.control.user.job.common.form.EditJobForm; 022import com.echothree.control.user.job.common.result.JobResultFactory; 023import com.echothree.control.user.job.common.spec.JobSpec; 024import com.echothree.model.control.job.server.control.JobControl; 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.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.command.EditMode; 034import com.echothree.util.server.control.BaseEditCommand; 035import com.echothree.util.server.control.CommandSecurityDefinition; 036import com.echothree.util.server.control.PartyTypeDefinition; 037import com.echothree.util.server.control.SecurityRoleDefinition; 038import com.echothree.util.server.persistence.Session; 039import java.util.Arrays; 040import java.util.Collections; 041import java.util.List; 042import javax.enterprise.context.RequestScoped; 043 044@RequestScoped 045public class EditJobCommand 046 extends BaseEditCommand<JobSpec, JobEdit> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 050 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 056 new SecurityRoleDefinition(SecurityRoleGroups.Job.name(), SecurityRoles.Edit.name()) 057 ))) 058 ))); 059 060 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 061 new FieldDefinition("JobName", FieldType.ENTITY_NAME, true, null, null) 062 )); 063 064 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 065 new FieldDefinition("JobName", FieldType.ENTITY_NAME, true, null, null), 066 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 067 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 068 )); 069 } 070 071 /** Creates a new instance of EditJobCommand */ 072 public EditJobCommand() { 073 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 074 } 075 076 @Override 077 protected BaseResult execute() { 078 var jobControl = Session.getModelController(JobControl.class); 079 var result = JobResultFactory.getEditJobResult(); 080 081 if(editMode.equals(EditMode.LOCK)) { 082 var jobName = spec.getJobName(); 083 var job = jobControl.getJobByName(jobName); 084 085 if(job != null) { 086 result.setJob(jobControl.getJobTransfer(getUserVisit(), job)); 087 088 if(lockEntity(job)) { 089 var jobDescription = jobControl.getJobDescription(job, getPreferredLanguage()); 090 var edit = JobEditFactory.getJobEdit(); 091 var jobDetail = job.getLastDetail(); 092 093 result.setEdit(edit); 094 edit.setJobName(jobDetail.getJobName()); 095 edit.setSortOrder(jobDetail.getSortOrder().toString()); 096 097 if(jobDescription != null) 098 edit.setDescription(jobDescription.getDescription()); 099 } else { 100 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 101 } 102 103 result.setEntityLock(getEntityLockTransfer(job)); 104 } else { 105 addExecutionError(ExecutionErrors.UnknownJobName.name(), jobName); 106 } 107 } else if(editMode.equals(EditMode.UPDATE)) { 108 var jobName = spec.getJobName(); 109 var job = jobControl.getJobByNameForUpdate(jobName); 110 111 if(job != null) { 112 jobName = edit.getJobName(); 113 var duplicateJob = jobControl.getJobByName(jobName); 114 115 if(duplicateJob == null || job.equals(duplicateJob)) { 116 if(lockEntityForUpdate(job)) { 117 try { 118 var partyPK = getPartyPK(); 119 var jobDetailValue = jobControl.getJobDetailValueForUpdate(job); 120 var jobDescription = jobControl.getJobDescriptionForUpdate(job, getPreferredLanguage()); 121 var description = edit.getDescription(); 122 123 jobDetailValue.setJobName(edit.getJobName()); 124 jobDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 125 126 jobControl.updateJobFromValue(jobDetailValue, partyPK); 127 128 if(jobDescription == null && description != null) { 129 jobControl.createJobDescription(job, getPreferredLanguage(), description, partyPK); 130 } else if(jobDescription != null && description == null) { 131 jobControl.deleteJobDescription(jobDescription, partyPK); 132 } else if(jobDescription != null && description != null) { 133 var jobDescriptionValue = jobControl.getJobDescriptionValue(jobDescription); 134 135 jobDescriptionValue.setDescription(description); 136 jobControl.updateJobDescriptionFromValue(jobDescriptionValue, partyPK); 137 } 138 } finally { 139 unlockEntity(job); 140 } 141 } else { 142 addExecutionError(ExecutionErrors.EntityLockStale.name()); 143 } 144 } else { 145 addExecutionError(ExecutionErrors.DuplicateJobName.name(), jobName); 146 } 147 } else { 148 addExecutionError(ExecutionErrors.UnknownJobName.name(), jobName); 149 } 150 151 if(hasExecutionErrors()) { 152 result.setJob(jobControl.getJobTransfer(getUserVisit(), job)); 153 result.setEntityLock(getEntityLockTransfer(job)); 154 } 155 } 156 157 return result; 158 } 159 160}