View Javadoc

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