View Javadoc

1   /**
2    * Copyright 2005-2013 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 java.util.Collection;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.kuali.rice.core.api.CoreConstants;
24  import org.kuali.rice.core.api.mo.common.active.Inactivatable;
25  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
26  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
27  import org.kuali.rice.krad.service.KeyValuesService;
28  import org.kuali.rice.krad.service.ModuleService;
29  
30  /**
31   * This class provides collection retrievals to populate key value pairs of business objects.
32   */
33  @Deprecated
34  public class KeyValuesServiceImpl implements KeyValuesService {
35      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KeyValuesServiceImpl.class);
36  
37      /**
38       * @see org.kuali.rice.krad.service.KeyValuesService#findAll(java.lang.Class)
39       */
40      @Override
41  	public <T> Collection<T> findAll(Class<T> clazz) {
42      	ModuleService responsibleModuleService = KRADServiceLocatorWeb.getKualiModuleService().getResponsibleModuleService(clazz);
43  		if(responsibleModuleService!=null && responsibleModuleService.isExternalizable(clazz)){
44  			return (Collection<T>) responsibleModuleService.getExternalizableBusinessObjectsList((Class<ExternalizableBusinessObject>) clazz, Collections.<String, Object>emptyMap());
45  		}
46          if (containsActiveIndicator(clazz)) {
47          	return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(clazz, Collections.singletonMap(CoreConstants.CommonElements.ACTIVE, true));
48          }
49          if (LOG.isDebugEnabled()) {
50  			LOG.debug("Active indicator not found for class " + clazz.getName());
51  		}
52          return KRADServiceLocatorWeb.getLegacyDataAdapter().findAll(clazz);
53      }
54  
55      /**
56       * @see org.kuali.rice.krad.service.KeyValuesService#findAllOrderBy(java.lang.Class, java.lang.String, boolean)
57       */
58      @Override
59  	public <T> Collection<T> findAllOrderBy(Class<T> clazz, String sortField, boolean sortAscending) {
60          if (containsActiveIndicator(clazz)) {
61              return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatchingOrderBy(clazz, Collections.singletonMap(CoreConstants.CommonElements.ACTIVE, true), sortField, sortAscending);
62          }
63          if (LOG.isDebugEnabled()) {
64  			LOG.debug("Active indicator not found for class " + clazz.getName());
65  		}
66          return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatchingOrderBy(clazz, new HashMap<String,Object>(), sortField, sortAscending);
67      }
68  
69      /**
70       * @see org.kuali.rice.krad.service.BusinessObjectService#findMatching(java.lang.Class, java.util.Map)
71       */
72      @Override
73  	public <T> Collection<T> findMatching(Class<T> clazz, Map<String, Object> fieldValues) {
74          if (containsActiveIndicator(clazz)) {
75          	// copying the map since we need to change it and don't know if it is unmodifiable
76          	Map<String,Object> criteria = new HashMap<String, Object>( fieldValues );
77          	criteria.put(CoreConstants.CommonElements.ACTIVE, true);
78              return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(clazz, criteria);
79          }
80          if (LOG.isDebugEnabled()) {
81  			LOG.debug("Active indicator not found for class " + clazz.getName());
82  		}
83          return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(clazz, fieldValues);
84      }
85  
86      /**
87       * Checks whether the class implements the Inactivatable interface.
88       *
89       * NOTE: This is different than earlier checks, as it assumes that the active flag
90       * is persistent.
91       *
92       * @param clazz
93       * @return boolean if active column is mapped for Class
94       */
95      private <T> boolean containsActiveIndicator(Class<T> clazz) {
96      	return Inactivatable.class.isAssignableFrom(clazz);
97      }
98  
99      @Override
100 	public <T> Collection<T> findAllInactive(Class<T> clazz) {
101         if (containsActiveIndicator(clazz)) {
102         	return KRADServiceLocatorWeb.getLegacyDataAdapter().findMatching(clazz, Collections.singletonMap(CoreConstants.CommonElements.ACTIVE, false));
103         }
104 		LOG.warn("Active indicator not found for class.  Assuming all are active. " + clazz.getName());
105         return Collections.emptyList();
106     }
107 
108 }