View Javadoc
1   package org.kuali.ole.describe.rule;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.describe.bo.OleShelvingScheme;
5   import org.kuali.ole.describe.bo.ExternalDataSourceConfig;
6   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
7   import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
8   
9   import java.util.HashMap;
10  import java.util.List;
11  import java.util.Map;
12  
13  /**
14   * Created with IntelliJ IDEA.
15   * User: PJ7789
16   * Date: 7/12/12
17   * Time: 6:40 PM
18   * To change this template use File | Settings | File Templates.
19   */
20  public class OleExternalDatSourceConfigRule
21          extends MaintenanceDocumentRuleBase {
22  
23  
24      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
25          boolean isValid = true;
26          ExternalDataSourceConfig externalDataSourceConfig = (ExternalDataSourceConfig) document
27                  .getNewMaintainableObject().getDataObject();
28  
29          isValid &= validateDuplicateDataSourceName(externalDataSourceConfig);
30          isValid &= validateEmptyDataSourceName(externalDataSourceConfig);
31          return isValid;
32      }
33  
34      /**
35       * This method  validates duplicate external dataSource name, Id and return boolean value.
36       *
37       * @param externalDataSourceConfig
38       * @return boolean
39       */
40      private boolean validateDuplicateDataSourceName(ExternalDataSourceConfig externalDataSourceConfig) {
41  
42          if (externalDataSourceConfig.getName() != null) {
43              Map<String, String> criteria = new HashMap<String, String>();
44              criteria.put(OLEConstants.OleExternalDataSourceConfig.DATA_SOURCE_NM, externalDataSourceConfig.getName());
45              List<ExternalDataSourceConfig> dataSourceNameInDatabase = (List<ExternalDataSourceConfig>) getBoService()
46                      .findMatching(ExternalDataSourceConfig.class, criteria);
47  
48              if ((dataSourceNameInDatabase.size() > 0)) {
49                  for (ExternalDataSourceConfig dataSourceObj : dataSourceNameInDatabase) {
50                      Integer dataSourceId = dataSourceObj.getId();
51                      if (null == externalDataSourceConfig.getId() || (externalDataSourceConfig.getId().intValue()
52                              != dataSourceId.intValue())) {
53                          this.putFieldError(OLEConstants.OleExternalDataSourceConfig.DATA_SOURCE_NAME,
54                                  "error.duplicate.name");
55                          return false;
56                      }
57  
58                  }
59              }
60  
61          }
62          return true;
63      }
64  
65  
66      /**
67       * This method  validates empty external Data Source name and return boolean value.
68       *
69       * @param externalDataSourceConfig
70       * @return boolean
71       */
72      private boolean validateEmptyDataSourceName(ExternalDataSourceConfig externalDataSourceConfig) {
73  
74          if (externalDataSourceConfig.getName() == null || externalDataSourceConfig.getName().equalsIgnoreCase("")
75                  || externalDataSourceConfig.getName().length() == 0) {
76              this.putFieldError(OLEConstants.OleExternalDataSourceConfig.DATA_SOURCE_NAME, "error.empty.name");
77              return false;
78          }
79          return true;
80      }
81  }