001    /**
002     * Copyright 2004-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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    package org.kuali.hr.job.validation;
017    
018    import java.util.List;
019    
020    import org.kuali.hr.job.Job;
021    import org.kuali.hr.time.assignment.Assignment;
022    import org.kuali.hr.time.service.base.TkServiceLocator;
023    import org.kuali.hr.time.util.TKUtils;
024    import org.kuali.hr.time.util.ValidationUtils;
025    import org.kuali.rice.kns.document.MaintenanceDocument;
026    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
027    import org.kuali.rice.krad.bo.PersistableBusinessObject;
028    
029    public class JobValidation extends MaintenanceDocumentRuleBase {
030    
031            private boolean validatePrincipalId(Job job) {
032                    if (job.getPrincipalId() != null
033                                    && !ValidationUtils.validatePrincipalId(job.getPrincipalId())) {
034                            this.putFieldError("principalId", "error.existence",
035                                            "principalId '" + job.getPrincipalId() + "'");
036                            return false;
037                    } else {
038                            return true;
039                    }
040            }
041    
042            private boolean validateJobNumber(Job job) {
043                    if (job.getJobNumber() != null) {
044                            Job jobObj = TkServiceLocator.getJobService().getJob(
045                                            job.getPrincipalId(), job.getJobNumber(),
046                                            job.getEffectiveDate(), false);
047                            if (jobObj != null) {
048                                    String[] parameters = new String[2];
049                                    parameters[0] = job.getJobNumber().toString();
050                                    parameters[1] = job.getPrincipalId();
051                                    this.putFieldError("jobNumber",
052                                                    "principal.jobnumber.already.exist", parameters);
053                                    return false;
054                            }
055                    }
056                    return true;
057            }
058    
059            protected boolean validateDepartment(Job job) {
060                    if (job.getDept() != null
061                                    && !ValidationUtils.validateDepartment(job.getDept(), job
062                                                    .getEffectiveDate())) {
063                            this.putFieldError("dept", "error.existence", "department '"
064                                            + job.getDept() + "'");
065                            return false;
066                    } else {
067                            return true;
068                    }
069            }
070    
071            boolean validateSalGroup(Job job) {
072                    if (!ValidationUtils.validateSalGroup(job.getHrSalGroup(), job
073                                    .getEffectiveDate())) {
074                            this.putFieldError("hrSalGroup", "error.existence", "Salgroup '"
075                                            + job.getHrSalGroup() + "'");
076                            return false;
077                    } else {
078                            return true;
079                    }
080            }
081    
082            boolean validateLocation(Job job) {
083                    if (job.getLocation() != null
084                                    && !ValidationUtils.validateLocation(job.getLocation(), null)) {
085                            this.putFieldError("location", "error.existence", "location '"
086                                            + job.getLocation() + "'");
087                            return false;
088                    } else {
089                            return true;
090                    }
091            }
092    
093            boolean validatePayType(Job job) {
094                    if (job.getHrPayType() != null
095                                    && !ValidationUtils.validatePayType(job.getHrPayType(), job
096                                                    .getEffectiveDate())) {
097                            this.putFieldError("hrPayType", "error.existence", "pay type '"
098                                            + job.getHrPayType() + "'");
099                            return false;
100                    } else {
101                            return true;
102                    }
103            }
104    
105            boolean validatePayGrade(Job job) {
106                    if (job.getPayGrade() != null
107                                    && !ValidationUtils.validatePayGrade(job.getPayGrade(), job
108                                                    .getEffectiveDate())) {
109                            this.putFieldError("payGrade", "error.existence", "pay grade '"
110                                            + job.getPayGrade() + "'");
111                            return false;
112                    } else {
113                            return true;
114                    }
115            }
116            
117            boolean validatePrimaryIndicator(Job job, Job oldJob) {
118                    boolean valid = true;
119                    if (job.getPrimaryIndicator()) {
120                            //do not block editing of previous primary job
121                            if(oldJob!=null && oldJob.getPrimaryIndicator()!=null && oldJob.getPrimaryIndicator()){
122                                    return valid;
123                            }
124                            Job existingJob = TkServiceLocator.getJobService().getPrimaryJob(job.getPrincipalId(), TKUtils.getCurrentDate());
125                            if (existingJob != null && existingJob.getPrimaryIndicator()) {
126                                    this.putFieldError("primaryIndicator", "error.primary.job.already.exist", job.getPrincipalId());
127                                    valid = false;
128                            }
129                    }
130                    return valid;
131            }
132            
133            // KPME-1129 Kagata
134            // This method determines if the job can be inactivated or not
135            boolean validateInactivation(Job job){
136                    
137                    // Get a list of active assignments based on principalId, jobNumber and current date.
138                    // If the list is not null, there are active assignments and the job can't be inactivated, so return false, otherwise true
139                    if(!job.isActive()) {
140                            //this has to use the effective date of the job passed in
141                            List<Assignment> aList = TkServiceLocator.getAssignmentService().getActiveAssignmentsForJob(job.getPrincipalId(), job.getJobNumber(), job.getEffectiveDate());
142                            if (aList != null && aList.size() > 0) {
143                                    // error.job.inactivate=Can not inactivate job number {0}.  It is used in active assignments.
144                                    this.putFieldError("active", "error.job.inactivate", job.getJobNumber().toString());
145                                    return false;
146                            } 
147                    }
148    
149                    return true;
150            }
151    
152            @Override
153            protected boolean processCustomRouteDocumentBusinessRules(
154                            MaintenanceDocument document) {
155                    boolean valid = false;
156                    LOG.debug("entering custom validation for Job");
157                    PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
158                    if (pbo instanceof Job) {
159                            Job job = (Job) pbo;
160                            Job oldJob = (Job) this.getOldBo();
161                            if (job != null) {
162                                    valid = true;
163                                    valid &= this.validatePrincipalId(job);
164                                    if(!document.isOldBusinessObjectInDocument()){
165                                            valid &= this.validateJobNumber(job);
166                                    }
167                                    valid &= this.validateDepartment(job);
168                                    valid &= this.validateSalGroup(job);
169                                    valid &= this.validateLocation(job);
170                                    valid &= this.validatePayType(job);
171                                    valid &= this.validatePayGrade(job);
172                                    valid &= this.validatePrimaryIndicator(job, oldJob);
173                                    // KPME-1129 Kagata
174                                    valid &= this.validateInactivation(job);
175                            }
176                    }
177                    return valid;
178            }
179    }