View Javadoc

1   /*
2    * Copyright 2005-2008 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.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   * This class is the service implementation for the Lookup structure. It Provides a generic search mechanism against Business
36   * Objects. This is the default implementation, that is delivered with Kuali.
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       * Returns a collection of objects based on the given search parameters.
53       * 
54       * @return Collection returned from the search
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       * Retrieves a Object based on the search criteria, which should uniquely identify a record.
66       * 
67       * @return Object returned from the search
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      * @return Returns the lookupDao.
108      */
109     public LookupDao getLookupDao() {
110         return lookupDao;
111     }
112 
113     /**
114      * @param lookupDao The lookupDao to set.
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 }