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.core.api.criteria.QueryByCriteria;
6 import org.kuali.rice.kns.service.KNSServiceLocator;
7 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
8 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
9 import org.kuali.rice.krad.service.KRADServiceLocator;
10
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14
15
16
17
18 public class OleEncodingLevelRule extends MaintenanceDocumentRuleBase {
19
20
21 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
22 boolean isValid = true;
23 OleEncodingLevel oleEncodingLevel = (OleEncodingLevel) document.getNewMaintainableObject().getDataObject();
24
25 isValid &= validateEncodingLevelCode(oleEncodingLevel);
26 return isValid;
27 }
28
29
30
31
32
33
34
35 private boolean validateEncodingLevelCode(OleEncodingLevel oleEncodingLevel) {
36
37 if (oleEncodingLevel.getEncodingLevelCode() != null) {
38
39 Map<String, String> criteria = new HashMap<String, String>();
40
41 criteria.put(OLEConstants.OleEncodingLevel.ENCODING_LEVEL_CD, oleEncodingLevel.getEncodingLevelCode());
42
43
44 List<OleEncodingLevel> encodingLevelInDatabase = (List<OleEncodingLevel>) KRADServiceLocator.getDataObjectService().findMatching(OleEncodingLevel.class, QueryByCriteria.Builder.andAttributes(criteria).build()).getResults();
45
46 if ((encodingLevelInDatabase.size() > 0)) {
47 for (OleEncodingLevel oleEncodingLevelObj : encodingLevelInDatabase) {
48 Integer encodingLevelId = oleEncodingLevelObj.getEncodingLevelId();
49 if (null == oleEncodingLevel.getEncodingLevelId() || oleEncodingLevel.getEncodingLevelId().intValue() != encodingLevelId) {
50 this.putFieldError(OLEConstants.OleEncodingLevel.ENCODING_LEVEL_CODE, "error.duplicate.code");
51 return false;
52 }
53 }
54 }
55 }
56
57 return true;
58 }
59 }