View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.service.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.config.property.ConfigurationService;
20  import org.kuali.rice.core.api.search.SearchOperator;
21  import org.kuali.rice.kns.service.KNSServiceLocator;
22  import org.kuali.rice.krad.dao.LookupDao;
23  import org.kuali.rice.krad.service.LookupService;
24  
25  import java.util.Collection;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  
30  /**
31   * Service implementation for the Lookup structure. It Provides a generic search
32   * mechanism against Business Objects. This is the default implementation, that
33   * is delivered with Kuali.
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public class LookupServiceImpl implements LookupService {
38      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LookupServiceImpl.class);
39  
40      private LookupDao lookupDao;
41      private ConfigurationService kualiConfigurationService;
42  
43      public <T extends Object> Collection<T> findCollectionBySearchUnbounded(Class<T> example,
44              Map<String, String> formProps) {
45          return findCollectionBySearchHelper(example, formProps, true);
46      }
47  
48      /**
49       * Returns a collection of objects based on the given search parameters.
50       * 
51       * @return Collection returned from the search
52       */
53      public <T extends Object> Collection<T> findCollectionBySearch(Class<T> example, Map<String, String> formProps) {
54          return findCollectionBySearchHelper(example, formProps, false);
55      }
56  
57      /**
58       * Since 2.3
59       * This version of findCollectionBySearchHelper is needed for version compatibility.   It allows executeSearch
60       * to behave the same way as it did prior to 2.3. In the LookupDao, the value for searchResultsLimit will be
61       * retrieved from the KNS version of LookupUtils in the LookupDao.
62       */
63      public <T extends Object> Collection<T> findCollectionBySearchHelper(Class<T> example,
64              Map<String, String> formProps, boolean unbounded) {
65          return lookupDao.findCollectionBySearchHelper(example, formProps, unbounded,
66                  allPrimaryKeyValuesPresentAndNotWildcard(example, formProps));
67      }
68  
69      public <T extends Object> Collection<T> findCollectionBySearchHelper(Class<T> example,
70              Map<String, String> formProps, boolean unbounded, Integer searchResultsLimit) {
71          return lookupDao.findCollectionBySearchHelper(example, formProps, unbounded,
72                  allPrimaryKeyValuesPresentAndNotWildcard(example, formProps), searchResultsLimit);
73      }
74  
75      /**
76       * Retrieves a Object based on the search criteria, which should uniquely
77       * identify a record.
78       * 
79       * @return Object returned from the search
80       */
81      public <T extends Object> T findObjectBySearch(Class<T> example, Map<String, String> formProps) {
82          if (example == null || formProps == null) {
83              throw new IllegalArgumentException("Object and Map must not be null");
84          }
85  
86          T obj = null;
87          try {
88              obj = example.newInstance();
89          } catch (IllegalAccessException e) {
90              throw new RuntimeException("Cannot get new instance of " + example.getName(), e);
91          } catch (InstantiationException e) {
92              throw new RuntimeException("Cannot instantiate " + example.getName(), e);
93          }
94  
95          return lookupDao.findObjectByMap(obj, formProps);
96      }
97  
98      public boolean allPrimaryKeyValuesPresentAndNotWildcard(Class<?> boClass, Map<String, String> formProps) {
99          List<String> pkFields = KNSServiceLocator.getBusinessObjectMetaDataService().listPrimaryKeyFieldNames(
100                 boClass);
101         Iterator<String> pkIter = pkFields.iterator();
102         boolean returnVal = true;
103         while (returnVal && pkIter.hasNext()) {
104             String pkName = pkIter.next();
105             String pkValue = formProps.get(pkName);
106 
107             if (StringUtils.isBlank(pkValue)) {
108                 returnVal = false;
109             } else {
110                 for (SearchOperator op : SearchOperator.QUERY_CHARACTERS) {
111                     if (pkValue.contains(op.op())) {
112                         returnVal = false;
113                         break;
114                     }
115                 }
116             }
117         }
118         return returnVal;
119     }
120 
121     /**
122      * @return Returns the lookupDao.
123      */
124     public LookupDao getLookupDao() {
125         return lookupDao;
126     }
127 
128     /**
129      * @param lookupDao The lookupDao to set.
130      */
131     public void setLookupDao(LookupDao lookupDao) {
132         this.lookupDao = lookupDao;
133     }
134 
135     public ConfigurationService getKualiConfigurationService() {
136         return kualiConfigurationService;
137     }
138 
139     public void setKualiConfigurationService(ConfigurationService kualiConfigurationService) {
140         this.kualiConfigurationService = kualiConfigurationService;
141     }
142 }