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
17
18
19
20
21
22 public class OleUserPreferencesRule
23 extends MaintenanceDocumentRuleBase {
24
25 @Override
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
41
42
43
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
71
72
73
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
88
89
90
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
103
104
105
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