View Javadoc
1   /**
2    * Copyright 2004-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.pm.positionreportsubcat.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.kpme.core.util.ValidationUtils;
20  import org.kuali.kpme.pm.positionreportcat.PositionReportCategory;
21  import org.kuali.kpme.pm.positionreportsubcat.PositionReportSubCategory;
22  import org.kuali.kpme.pm.service.base.PmServiceLocator;
23  import org.kuali.rice.kns.document.MaintenanceDocument;
24  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
25  
26  public class PositionReportSubCatValidation extends MaintenanceDocumentRuleBase {
27  	@Override
28  	protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
29  		boolean valid = false;
30  		LOG.debug("entering custom validation for Position Report Sub Category");
31  		PositionReportSubCategory prsc = (PositionReportSubCategory) this.getNewBo();
32  		
33  		if (prsc != null) {
34  			valid = true;
35  			valid &= this.validatePositionReportCategory(prsc);
36  			valid &= this.validateInstitution(prsc);
37  			valid &= this.validateLocation(prsc);
38  		}
39  		return valid;
40  	}
41  	
42  	private boolean validatePositionReportCategory(PositionReportSubCategory prsc) {
43  		PositionReportCategory aCat = PmServiceLocator.getPositionReportCatService().getPositionReportCat(prsc.getPositionReportCat(), prsc.getEffectiveLocalDate());
44  		
45  		String errorMes = "PositionReportCategory '" + prsc.getPositionReportCat() + "'";
46  		if(aCat == null) {
47  			this.putFieldError("positionReportCat", "error.existence", errorMes);
48  			return false;
49  		} else {
50  			if(!ValidationUtils.wildCardMatch(aCat.getInstitution(), prsc.getInstitution())) {
51  				String[] params = new String[3];
52  				params[0] = prsc.getInstitution();
53  				params[1] = aCat.getInstitution();
54  				params[2] = errorMes;
55  				this.putFieldError("institution", "institution.inconsistent", params);
56  				return false;
57  			}
58  			if(!ValidationUtils.wildCardMatch(aCat.getLocation(), prsc.getLocation())) {
59  				String[] params = new String[3];
60  				params[0] = prsc.getLocation();
61  				params[1] = aCat.getLocation();
62  				params[2] = errorMes;
63  				this.putFieldError("location", "location.inconsistent", params);
64  				return false;
65  			}
66  		}
67  		return true;
68  	}	
69  
70  	private boolean validateInstitution(PositionReportSubCategory prsc) {
71  		if (StringUtils.isNotEmpty(prsc.getInstitution())) {
72  			if(!ValidationUtils.validateInstitution(prsc.getInstitution(), prsc.getEffectiveLocalDate())) {
73  				this.putFieldError("institution", "error.existence", "Instituion '"
74  						+ prsc.getInstitution() + "'");
75  				return false;
76  			}
77  		}
78  		return true;
79  	}
80  	
81  	private boolean validateLocation(PositionReportSubCategory prsc) {
82  		if (StringUtils.isNotEmpty(prsc.getLocation())) {
83  			if(!ValidationUtils.validateLocation(prsc.getLocation(), prsc.getEffectiveLocalDate())) {
84  				this.putFieldError("location", "error.existence", "Location '"
85  						+ prsc.getLocation() + "'");
86  				return false;
87  			}
88  		}
89  		return true;
90  	}
91  }