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.time.dept.lunch.validation;
017    
018    import java.math.BigDecimal;
019    
020    import org.kuali.hr.time.dept.lunch.DeptLunchRule;
021    import org.kuali.hr.time.service.base.TkServiceLocator;
022    import org.kuali.hr.time.util.TkConstants;
023    import org.kuali.hr.time.util.ValidationUtils;
024    import org.kuali.rice.kns.document.MaintenanceDocument;
025    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
026    import org.kuali.rice.krad.bo.PersistableBusinessObject;
027    import org.kuali.rice.krad.util.GlobalVariables;
028    
029    public class DeptLunchRuleRule extends MaintenanceDocumentRuleBase {
030    
031            boolean validateWorkArea(DeptLunchRule ruleObj) {
032                    boolean valid = true;
033                    if (ruleObj.getWorkArea() != null
034                                    && !ValidationUtils.validateWorkArea(ruleObj.getWorkArea(), ruleObj
035                                                    .getEffectiveDate())) {
036                            this.putFieldError("workArea", "error.existence", "workArea '"
037                                            + ruleObj.getWorkArea() + "'");
038                            valid = false;
039                    } else if (ruleObj.getWorkArea() != null
040                                    && !ruleObj.getWorkArea().equals(TkConstants.WILDCARD_LONG)) {
041                            int count = TkServiceLocator.getWorkAreaService().getWorkAreaCount(ruleObj.getDept(), ruleObj.getWorkArea());
042                            valid = (count > 0);
043                            if (!valid) {
044                                    this.putFieldError("workArea", "dept.workarea.invalid.sync",
045                                                    ruleObj.getWorkArea() + "");
046                            }
047                    }
048                    return valid;
049            }
050    
051            boolean validateDepartment(DeptLunchRule ruleObj) {
052                    if (!ValidationUtils.validateDepartment(ruleObj.getDept(), ruleObj.getEffectiveDate())) {
053                            this.putFieldError("dept", "error.existence", "department '" + ruleObj.getDept() + "'");
054                            return false;
055                    } else {
056                            return true;
057                    }
058            }
059    
060            boolean validateJobNumber(DeptLunchRule ruleObj) {
061                    boolean valid = true;
062                    if (ruleObj.getJobNumber() == null) {
063                            valid = false;
064                    } else if (!ruleObj.getJobNumber().equals(TkConstants.WILDCARD_LONG)) {
065                            int count = TkServiceLocator.getJobService().getJobCount(ruleObj.getPrincipalId(), ruleObj.getJobNumber(), null);
066                            valid = (count > 0);
067                            if (!valid) {
068                                    this.putFieldError("jobNumber", "principalid.job.invalid.sync",
069                                                    ruleObj.getJobNumber() + "");
070                            }
071                    }
072                    return valid;
073            }
074    
075            boolean validatePrincipalId(DeptLunchRule ruleObj) {
076                    if (!ruleObj.getPrincipalId().equals(TkConstants.WILDCARD_CHARACTER)
077                                    &&!ValidationUtils.validatePrincipalId(ruleObj.getPrincipalId())) {
078                            this.putFieldError("principalId", "error.existence", "Principal Id '" + ruleObj.getPrincipalId() + "'");
079                            return false;
080                    } else {
081                            return true;
082                    }
083            }
084    
085            boolean validateShiftHour(DeptLunchRule ruleObj) {
086                    BigDecimal shiftHour = ruleObj.getShiftHours();
087                    BigDecimal maxHour = new BigDecimal(24);
088                    if(shiftHour.compareTo(maxHour) == 1) {
089                            this.putFieldError("shiftHours", "dept.shifthour.exceedsMax");
090                            return false;
091                    }
092                    return true;
093            }
094    
095    
096    
097            /**
098             * It looks like the method that calls this class doesn't actually care
099             * about the return type.
100             */
101            @Override
102            protected boolean processCustomRouteDocumentBusinessRules(
103                            MaintenanceDocument document) {
104                    boolean valid = false;
105    
106                    LOG.debug("entering custom validation for DeptLunchRule");
107                    PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
108                    if (pbo instanceof DeptLunchRule) {
109                            DeptLunchRule deptLunchRule = (DeptLunchRule) pbo;
110                            deptLunchRule.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
111                            if (deptLunchRule != null) {
112                                    valid = true;
113                                    valid &= this.validateDepartment(deptLunchRule);
114                                    valid &= this.validateWorkArea(deptLunchRule);
115                                    valid &= this.validatePrincipalId(deptLunchRule);
116                                    valid &= this.validateJobNumber(deptLunchRule);
117                                    valid &= this.validateShiftHour(deptLunchRule);
118                            }
119                    }
120    
121                    return valid;
122            }
123    
124            @Override
125            protected boolean processCustomApproveDocumentBusinessRules(
126                            MaintenanceDocument document) {
127                    return super.processCustomApproveDocumentBusinessRules(document);
128            }
129    }