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    /** Creates a new instance of EditJobCommand */
069    public EditJobCommand() {
070        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
071    }
072    
073    @Inject
074    JobControl jobControl;
075
076    @Override
077    protected EditJobResult getResult() {
078        return JobResultFactory.getEditJobResult();
079    }
080
081    @Override
082    protected JobEdit getEdit() {
083        return JobEditFactory.getJobEdit();
084    }
085
086    @Override
087    protected Job getEntity(EditJobResult result) {
088        Job job;
089        var jobName = spec.getJobName();
090
091        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
092            job = jobControl.getJobByName(jobName);
093        } else { // EditMode.UPDATE
094            job = jobControl.getJobByNameForUpdate(jobName);
095        }
096
097        if(job == null) {
098            addExecutionError(ExecutionErrors.UnknownJobName.name(), jobName);
099        }
100
101        return job;
102    }
103
104    @Override
105    protected Job getLockEntity(Job job) {
106        return job;
107    }
108
109    @Override
110    protected void fillInResult(EditJobResult result, Job job) {
111        result.setJob(jobControl.getJobTransfer(getUserVisit(), job));
112    }
113
114    @Override
115    protected void doLock(JobEdit edit, Job job) {
116        var jobDescription = jobControl.getJobDescription(job, getPreferredLanguage());
117        var jobDetail = job.getLastDetail();
118
119        edit.setJobName(jobDetail.getJobName());
120        edit.setSortOrder(jobDetail.getSortOrder().toString());
121
122        if(jobDescription != null) {
123            edit.setDescription(jobDescription.getDescription());
124        }
125    }
126
127    @Override
128    protected void canUpdate(Job job) {
129        var jobName = edit.getJobName();
130        var duplicateJob = jobControl.getJobByName(jobName);
131
132        if(duplicateJob != null && !job.equals(duplicateJob)) {
133            addExecutionError(ExecutionErrors.DuplicateJobName.name(), jobName);
134        }
135    }
136
137    @Override
138    protected void doUpdate(Job job) {
139        var partyPK = getPartyPK();
140        var jobDetailValue = jobControl.getJobDetailValueForUpdate(job);
141        var jobDescription = jobControl.getJobDescriptionForUpdate(job, getPreferredLanguage());
142        var description = edit.getDescription();
143
144        jobDetailValue.setJobName(edit.getJobName());
145        jobDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
146
147        jobControl.updateJobFromValue(jobDetailValue, partyPK);
148
149        if(jobDescription == null && description != null) {
150            jobControl.createJobDescription(job, getPreferredLanguage(), description, partyPK);
151        } else if(jobDescription != null && description == null) {
152            jobControl.deleteJobDescription(jobDescription, partyPK);
153        } else if(jobDescription != null && description != null) {
154            var jobDescriptionValue = jobControl.getJobDescriptionValue(jobDescription);
155
156            jobDescriptionValue.setDescription(description);
157            jobControl.updateJobDescriptionFromValue(jobDescriptionValue, partyPK);
158        }
159    }
160    
161}