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