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.log4j.Logger;
19  import org.kuali.rice.krad.bo.ExternalizableBusinessObject;
20  import org.kuali.rice.krad.bo.PersistableBusinessObject;
21  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
22  import org.kuali.rice.krad.service.KualiModuleService;
23  import org.kuali.rice.krad.service.ModuleService;
24  import org.kuali.rice.krad.service.PersistenceService;
25  import org.kuali.rice.krad.util.ExternalizableBusinessObjectUtils;
26  import org.springframework.beans.BeanUtils;
27  import org.springframework.transaction.annotation.Transactional;
28  import org.springframework.util.CollectionUtils;
29  
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * This class is the service implementation for the Persistence structure.
36   * OjbRepositoryExplorer provides functions for extracting information from the
37   * OJB repository at runtime. This is the default implementation, that is
38   * delivered with Kuali.
39   */
40  @Transactional
41  public class PersistenceServiceImpl extends PersistenceServiceImplBase implements PersistenceService {
42  
43  	private static Logger LOG = Logger.getLogger(PersistenceServiceImpl.class);
44  
45      private KualiModuleService kualiModuleService;
46  
47  	private PersistenceService persistenceServiceJpa;
48  
49  	private PersistenceService persistenceServiceOjb;
50  
51  	public void setPersistenceServiceJpa(PersistenceService persistenceServiceJpa) {
52  		this.persistenceServiceJpa = persistenceServiceJpa;
53  	}
54  
55  	public void setPersistenceServiceOjb(PersistenceService persistenceServiceOjb) {
56  		this.persistenceServiceOjb = persistenceServiceOjb;
57  	}
58  
59  	private PersistenceService getService(Class clazz) {
60      	return (isJpaEnabledForKradClass(clazz)) ?
61  						persistenceServiceJpa : persistenceServiceOjb;
62  	}
63  
64  	// This method is for OJB specfic features. It is now being called directly where needed.
65  	public void clearCache() {
66  		throw new UnsupportedOperationException("This should be called directly from the OJB Impl if needed.");
67  	}
68  
69  	// This method is for OJB specfic features. It is now being called directly where needed.
70  	public void loadRepositoryDescriptor(String ojbRepositoryFilePath) {
71  		throw new UnsupportedOperationException("This should be called directly from the OJB Impl if needed.");
72  	}
73  
74  	public Object resolveProxy(Object o) {
75  		return getService(o.getClass()).resolveProxy(o);
76  	}
77  
78  	/**
79  	 * @see org.kuali.rice.krad.service.PersistenceService#retrieveNonKeyFields(java.lang.Object)
80  	 */
81  	public void retrieveNonKeyFields(Object persistableObject) {
82          if (persistableObject != null &&
83                  ExternalizableBusinessObjectUtils.isExternalizableBusinessObject(persistableObject.getClass())) {
84              //
85              // special handling for EBOs
86              //
87              Map<String, ?> criteria = KRADServiceLocatorWeb.getDataObjectMetaDataService().getPrimaryKeyFieldValues(persistableObject);
88              if (!CollectionUtils.isEmpty(criteria)) {
89                  ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(persistableObject.getClass());
90                  if (moduleService != null) {
91                      Class<? extends ExternalizableBusinessObject> clazz =
92                              ExternalizableBusinessObjectUtils.determineExternalizableBusinessObjectSubInterface(persistableObject.getClass());
93                      ExternalizableBusinessObject freshEbo = moduleService.getExternalizableBusinessObject(clazz, (Map<String, Object>)criteria);
94                      if (freshEbo != null) {
95                          BeanUtils.copyProperties(freshEbo, persistableObject);
96                      }
97                  }
98              }
99          } else {
100             getService(persistableObject.getClass()).retrieveNonKeyFields(persistableObject);
101         }
102 	}
103 
104 	/**
105 	 * @see org.kuali.rice.krad.service.PersistenceService#retrieveReferenceObject(java.lang.Object,
106 	 *      String referenceObjectName)
107 	 */
108 	public void retrieveReferenceObject(Object persistableObject, String referenceObjectName) {
109 		getService(persistableObject.getClass()).retrieveReferenceObject(persistableObject, referenceObjectName);
110 	}
111 
112 	/**
113 	 * @see org.kuali.rice.krad.service.PersistenceService#retrieveReferenceObject(java.lang.Object,
114 	 *      String referenceObjectName)
115 	 */
116 	public void retrieveReferenceObjects(Object persistableObject, List referenceObjectNames) {
117 		getService(persistableObject.getClass()).retrieveReferenceObjects(persistableObject, referenceObjectNames);
118 	}
119 
120 	/**
121 	 * @see org.kuali.rice.krad.service.PersistenceService#retrieveReferenceObject(java.lang.Object,
122 	 *      String referenceObjectName)
123 	 */
124 	public void retrieveReferenceObjects(List persistableObjects, List referenceObjectNames) {
125 		if (persistableObjects == null) {
126 			throw new IllegalArgumentException("invalid (null) persistableObjects");
127 		}
128 		if (persistableObjects.isEmpty()) {
129 			throw new IllegalArgumentException("invalid (empty) persistableObjects");
130 		}
131 		if (referenceObjectNames == null) {
132 			throw new IllegalArgumentException("invalid (null) referenceObjectNames");
133 		}
134 		if (referenceObjectNames.isEmpty()) {
135 			throw new IllegalArgumentException("invalid (empty) referenceObjectNames");
136 		}
137 
138 		for (Iterator i = persistableObjects.iterator(); i.hasNext();) {
139 			Object persistableObject = i.next();
140 			retrieveReferenceObjects(persistableObject, referenceObjectNames);
141 		}
142 	}
143 
144 	/**
145 	 * @see org.kuali.rice.krad.service.PersistenceService#getFlattenedPrimaryKeyFieldValues(java.lang.Object)
146 	 */
147 	public String getFlattenedPrimaryKeyFieldValues(Object persistableObject) {
148 		return getService(persistableObject.getClass()).getFlattenedPrimaryKeyFieldValues(persistableObject);
149 	}
150 
151 	/**
152 	 * For each reference object to the parent persistableObject, sets the key
153 	 * values for that object. First, if the reference object already has a
154 	 * value for the key, the value is left unchanged. Otherwise, for
155 	 * non-anonymous keys, the value is taken from the parent object. For
156 	 * anonymous keys, all other persistableObjects are checked until a value
157 	 * for the key is found.
158 	 * 
159 	 * @see org.kuali.rice.krad.service.PersistenceService#getReferencedObject(java.lang.Object,
160 	 *      org.apache.ojb.broker.metadata.ObjectReferenceDescriptor)
161 	 */
162 	public void linkObjects(Object persistableObject) {
163 		getService(persistableObject.getClass()).linkObjects(persistableObject);
164 	}
165 
166 	/**
167 	 * 
168 	 * @see org.kuali.rice.krad.service.PersistenceService#allForeignKeyValuesPopulatedForReference(org.kuali.rice.krad.bo.BusinessObject,
169 	 *      java.lang.String)
170 	 */
171 	public boolean allForeignKeyValuesPopulatedForReference(PersistableBusinessObject bo, String referenceName) {
172 		return getService(bo.getClass()).allForeignKeyValuesPopulatedForReference(bo, referenceName);
173 	}
174 
175 	/**
176 	 * 
177 	 * @see org.kuali.rice.krad.service.PersistenceService#refreshAllNonUpdatingReferences(org.kuali.rice.krad.bo.BusinessObject)
178 	 */
179 	public void refreshAllNonUpdatingReferences(PersistableBusinessObject bo) {
180 		getService(bo.getClass()).refreshAllNonUpdatingReferences(bo);
181 	}
182 
183 	/**
184 	 * Defers to the service for the given class
185 	 * 
186 	 * @see org.kuali.rice.krad.service.PersistenceService#isProxied(java.lang.Object)
187 	 */
188 	public boolean isProxied(Object bo) {
189 		return getService(bo.getClass()).isProxied(bo);
190 	}
191 
192     public KualiModuleService getKualiModuleService() {
193         return kualiModuleService;
194     }
195 
196     public void setKualiModuleService(KualiModuleService kualiModuleService) {
197         this.kualiModuleService = kualiModuleService;
198     }
199 }