View Javadoc
1   package org.kuali.ole.describe.rule;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.describe.bo.OleEncodingLevel;
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   * OleEncodingLevelRule validates maintenance object for Encoding Level Maintenance Document
14   */
15  public class OleEncodingLevelRule extends MaintenanceDocumentRuleBase {
16  
17  
18      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
19          boolean isValid = true;
20          OleEncodingLevel oleEncodingLevel = (OleEncodingLevel) document.getNewMaintainableObject().getDataObject();
21  
22          isValid &= validateEncodingLevelCode(oleEncodingLevel);
23          return isValid;
24      }
25  
26      /**
27       * This method  validates duplicate encodingLevel Id and return boolean value.
28       *
29       * @param oleEncodingLevel
30       * @return boolean
31       */
32      private boolean validateEncodingLevelCode(OleEncodingLevel oleEncodingLevel) {
33  
34          if (oleEncodingLevel.getEncodingLevelCode() != null) {
35  
36              Map<String, String> criteria = new HashMap<String, String>();
37  
38              criteria.put(OLEConstants.OleEncodingLevel.ENCODING_LEVEL_CD, oleEncodingLevel.getEncodingLevelCode());
39  
40  
41              List<OleEncodingLevel> encodingLevelInDatabase = (List<OleEncodingLevel>) getBoService().findMatching(OleEncodingLevel.class, criteria);
42  
43              if ((encodingLevelInDatabase.size() > 0)) {
44                  for (OleEncodingLevel oleEncodingLevelObj : encodingLevelInDatabase) {
45                      Integer encodingLevelId = oleEncodingLevelObj.getEncodingLevelId();
46                      if (null == oleEncodingLevel.getEncodingLevelId() || oleEncodingLevel.getEncodingLevelId().intValue() != encodingLevelId) {
47                          this.putFieldError(OLEConstants.OleEncodingLevel.ENCODING_LEVEL_CODE, "error.duplicate.code");
48                          return false;
49                      }
50                  }
51              }
52          }
53  
54          return true;
55      }
56  }