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