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