View Javadoc

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