1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.service.impl;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.kuali.rice.kns.bo.PersistableBusinessObject;
26 import org.kuali.rice.kns.dao.LookupDao;
27 import org.kuali.rice.kns.service.DataDictionaryService;
28 import org.kuali.rice.kns.service.KNSServiceLocator;
29 import org.kuali.rice.kns.service.KualiConfigurationService;
30 import org.kuali.rice.kns.service.LookupService;
31 import org.kuali.rice.kns.service.PersistenceStructureService;
32 import org.kuali.rice.kns.util.KNSConstants;
33
34
35
36
37
38 public class LookupServiceImpl implements LookupService {
39 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupServiceImpl.class);
40 private static final Collection EMPTY_COLLECTION = new ArrayList(0);
41
42 private LookupDao lookupDao;
43 private KualiConfigurationService kualiConfigurationService;
44 private DataDictionaryService dataDictionaryService;
45 private PersistenceStructureService persistenceStructureService;
46
47 public Collection findCollectionBySearchUnbounded(Class example, Map formProps) {
48 return findCollectionBySearchHelper(example, formProps, true);
49 }
50
51
52
53
54
55
56 public Collection findCollectionBySearch(Class example, Map formProps) {
57 return findCollectionBySearchHelper(example, formProps, false);
58 }
59
60 public Collection findCollectionBySearchHelper(Class example, Map formProps, boolean unbounded) {
61 return lookupDao.findCollectionBySearchHelper(example, formProps, unbounded, allPrimaryKeyValuesPresentAndNotWildcard(example, formProps));
62 }
63
64
65
66
67
68
69 public Object findObjectBySearch(Class example, Map formProps) {
70 if (example == null || formProps == null) {
71 throw new IllegalArgumentException("Object and Map must not be null");
72 }
73
74 PersistableBusinessObject obj = null;
75 try {
76 obj = (PersistableBusinessObject) example.newInstance();
77 }
78 catch (IllegalAccessException e) {
79 throw new RuntimeException("Cannot get new instance of " + example.getName(), e);
80 }
81 catch (InstantiationException e) {
82 throw new RuntimeException("Cannot instantiate " + example.getName(), e);
83 }
84
85 return lookupDao.findObjectByMap(obj, formProps);
86 }
87
88 public boolean allPrimaryKeyValuesPresentAndNotWildcard(Class boClass, Map formProps) {
89 List pkFields = KNSServiceLocator.getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(boClass);
90 Iterator pkIter = pkFields.iterator();
91 boolean returnVal = true;
92 while (returnVal && pkIter.hasNext()) {
93 String pkName = (String) pkIter.next();
94 String pkValue = (String) formProps.get(pkName);
95
96 if (StringUtils.isBlank(pkValue)) {
97 returnVal = false;
98 }
99 else if (StringUtils.indexOfAny(pkValue, KNSConstants.QUERY_CHARACTERS) != -1) {
100 returnVal = false;
101 }
102 }
103 return returnVal;
104 }
105
106
107
108
109 public LookupDao getLookupDao() {
110 return lookupDao;
111 }
112
113
114
115
116 public void setLookupDao(LookupDao lookupDao) {
117 this.lookupDao = lookupDao;
118 }
119
120 public KualiConfigurationService getKualiConfigurationService() {
121 return kualiConfigurationService;
122 }
123
124 public void setKualiConfigurationService(KualiConfigurationService kualiConfigurationService) {
125 this.kualiConfigurationService = kualiConfigurationService;
126 }
127
128 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
129 this.dataDictionaryService = dataDictionaryService;
130 }
131
132 public void setPersistenceStructureService(PersistenceStructureService persistenceStructureService) {
133 this.persistenceStructureService = persistenceStructureService;
134 }
135 }