1 package org.kuali.ole.describe.rule;
2
3 import org.kuali.ole.OLEConstants;
4 import org.kuali.ole.describe.bo.OleCountryCodes;
5 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
6 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
7
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12
13
14
15 public class OleCountryCodesRule extends MaintenanceDocumentRuleBase {
16
17
18 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
19 boolean isValid = true;
20 OleCountryCodes oleCountryCodes = (OleCountryCodes) document.getNewMaintainableObject().getDataObject();
21
22 isValid &= validateCountryCode(oleCountryCodes);
23 return isValid;
24 }
25
26
27
28
29
30
31
32 private boolean validateCountryCode(OleCountryCodes oleCountryCodes) {
33 if (oleCountryCodes.getCountryCode() != null) {
34 Map<String, String> criteria = new HashMap<String, String>();
35 criteria.put(OLEConstants.OleCountryCodes.COUNTRY_CD, oleCountryCodes.getCountryCode());
36 List<OleCountryCodes> countryCodeInDatabase = (List<OleCountryCodes>) getBoService().findMatching(OleCountryCodes.class, criteria);
37 if ((countryCodeInDatabase.size() > 0)) {
38 for (OleCountryCodes countryCodeObj : countryCodeInDatabase) {
39 Integer countryCodeId = countryCodeObj.getCountryCodeId();
40 if (null == oleCountryCodes.getCountryCodeId() || (oleCountryCodes.getCountryCodeId().intValue() != countryCodeId.intValue())) {
41 this.putFieldError(OLEConstants.OleCountryCodes.COUNTRY_CODE, "error.duplicate.code");
42 return false;
43 }
44 }
45 }
46 }
47 return true;
48 }
49 }