View Javadoc
1   package org.kuali.ole.describe.rule;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.describe.bo.ImportBibUserPreferences;
5   import org.kuali.ole.describe.service.ImportBibService;
6   import org.kuali.rice.ken.service.UserPreferenceService;
7   import org.kuali.rice.ken.service.impl.UserPreferenceServiceImpl;
8   import org.kuali.rice.krad.maintenance.MaintenanceDocument;
9   import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
10  
11  import java.util.HashMap;
12  import java.util.List;
13  import java.util.Map;
14  
15  /**
16   * Created with IntelliJ IDEA.
17   * User: PJ7789
18   * Date: 27/12/12
19   * Time: 6:53 PM
20   * To change this template use File | Settings | File Templates.
21   */
22  public class OleUserPreferencesRule
23          extends MaintenanceDocumentRuleBase {
24  
25  
26      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
27          boolean isValid = true;
28          ImportBibUserPreferences userPreferences = (ImportBibUserPreferences) document.getNewMaintainableObject()
29                  .getDataObject();
30  
31          isValid &= validateDuplicateUserPrefName(userPreferences);
32          isValid &= validateEmptyUserPrefName(userPreferences);
33          isValid &= validateCallNumber(userPreferences);
34          isValid &= validateProtectedNRemovalTags(userPreferences);
35          return isValid;
36      }
37  
38  
39      /**
40       * This method  validates duplicate UserPreference name, Id and return boolean value.
41       *
42       * @param userPreferences
43       * @return boolean
44       */
45      private boolean validateDuplicateUserPrefName(ImportBibUserPreferences userPreferences) {
46  
47          if (userPreferences.getPrefName() != null) {
48              Map<String, String> criteria = new HashMap<String, String>();
49              criteria.put(OLEConstants.OleUserPreferences.USER_PREF_NM, userPreferences.getPrefName());
50              List<ImportBibUserPreferences> dataSourceNameInDatabase = (List<ImportBibUserPreferences>) getBoService()
51                      .findMatching(ImportBibUserPreferences.class, criteria);
52  
53              if ((dataSourceNameInDatabase.size() > 0)) {
54                  for (ImportBibUserPreferences dataSourceObj : dataSourceNameInDatabase) {
55                      Integer dataSourceId = dataSourceObj.getPrefId();
56                      if (null == userPreferences.getPrefId() || (userPreferences.getPrefId().intValue() != dataSourceId
57                              .intValue())) {
58                          this.putFieldError(OLEConstants.OleUserPreferences.USER_PREF_NAME, "error.duplicate.name");
59                          return false;
60                      }
61  
62                  }
63              }
64  
65          }
66          return true;
67      }
68  
69      /**
70       * This method  validates empty UserPreference name and return boolean value.
71       *
72       * @param userPreferences
73       * @return boolean
74       */
75      private boolean validateEmptyUserPrefName(ImportBibUserPreferences userPreferences) {
76  
77          if (userPreferences.getPrefName() == null || userPreferences.getPrefName().equalsIgnoreCase("")
78                  || userPreferences.getPrefName().length() == 0) {
79              this.putFieldError(OLEConstants.OleUserPreferences.USER_PREF_NAME, "error.empty.name");
80              return false;
81          }
82          return true;
83      }
84  
85  
86      /**
87       * This method  validates UserPreference call number and return boolean value.
88       *
89       * @param userPreferences
90       * @return boolean
91       */
92      private boolean validateCallNumber(ImportBibUserPreferences userPreferences) {
93          ImportBibService importBibService = new ImportBibService();
94          if (importBibService.callNumValidation(userPreferences)) {
95              this.putFieldError(OLEConstants.OleUserPreferences.USER_PREF_CALL_NUMBER, "error.call.number");
96              return false;
97          }
98          return true;
99      }
100 
101     /**
102      * This method  validates UserPreference ProtectedTags and Removal tags and return boolean value.
103      *
104      * @param userPreferences
105      * @return boolean
106      */
107 
108     private boolean validateProtectedNRemovalTags(ImportBibUserPreferences userPreferences) {
109         ImportBibService importBibService = new ImportBibService();
110         if (importBibService.proNRemTagValidation(userPreferences)) {
111             this.putFieldError(OLEConstants.OleUserPreferences.USER_PREF_TAGS, "error.pro.rem.tags");
112             return false;
113         }
114         return true;
115     }
116 }
117