View Javadoc
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   * OleCountryCodesRule validates maintenance object for Country Codes Maintenance Document
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       * This method  validates duplicate countryCode Id and return boolean value.
28       *
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  }