View Javadoc

1   package org.kuali.ole.catalog.rule;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.catalog.bo.OleCountryCodes;
5   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
6   import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
7   
8   
9   import java.util.HashMap;
10  import java.util.List;
11  import java.util.Map;
12  
13  /**
14   * OleCountryCodesRule validates maintenance object for Country Codes Maintenance Document
15   */
16  public class OleCountryCodesRule extends MaintenanceDocumentRuleBase {
17  
18  
19      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
20          boolean isValid = true;
21          OleCountryCodes oleCountryCodes = (OleCountryCodes) document.getNewMaintainableObject().getDataObject();
22  
23          isValid &= validateCountryCode(oleCountryCodes);
24          return isValid;
25      }
26  
27      /**
28       *   This method  validates duplicate countryCode Id and return boolean value.
29       * @param oleCountryCodes
30       * @return  boolean
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  }