View Javadoc

1   /*
2    * Copyright 2006-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.krad.service.impl;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.kuali.rice.krad.bo.BusinessObjectRelationship;
23  import org.kuali.rice.krad.bo.PersistableBusinessObject;
24  import org.kuali.rice.krad.service.PersistenceStructureService;
25  import org.kuali.rice.krad.util.ForeignKeyFieldsPopulationState;
26  import org.kuali.rice.krad.util.spring.CacheNoCopy;
27  
28  /**
29   * This class is now a proxy, which uses the @Entity annotation to decide whether to use the JPA or OJB underlying services to perform an action.
30   * 
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   *
33   */
34  public class PersistenceStructureServiceImpl extends PersistenceServiceImplBase implements PersistenceStructureService {
35  
36  	/**
37  	 * 
38  	 * special case when the attributeClass passed in doesnt match the class of
39  	 * the reference-descriptor as defined in ojb-repository. Currently the only
40  	 * case of this happening is ObjectCode vs. ObjectCodeCurrent.
41  	 * 
42  	 * NOTE: This method makes no real sense and is a product of a hack
43  	 * introduced by KFS for an unknown reason. If you find yourself using this
44  	 * map stop and go do something else.
45  	 * 
46  	 * @param from
47  	 *            the class in the code
48  	 * @param to
49  	 *            the class in the repository
50  	 */
51  	public static Map<Class, Class> referenceConversionMap = new HashMap<Class, Class>();
52  	
53  	private PersistenceStructureService persistenceStructureServiceJpa;
54  	private PersistenceStructureService persistenceStructureServiceOjb;
55  
56  	public void setPersistenceStructureServiceJpa(PersistenceStructureService persistenceStructureServiceJpa) {
57  		this.persistenceStructureServiceJpa = persistenceStructureServiceJpa;
58  	}
59  
60  	public void setPersistenceStructureServiceOjb(PersistenceStructureService persistenceStructureServiceOjb) {
61  		this.persistenceStructureServiceOjb = persistenceStructureServiceOjb;
62  	}
63  	
64  	private PersistenceStructureService getService(Class clazz) {
65  		return (isJpaEnabledForKradClass(clazz)) ?
66  						persistenceStructureServiceJpa : persistenceStructureServiceOjb;
67  	}
68  	
69  	/**
70  	 * @see org.kuali.rice.krad.service.PersistenceService#isPersistable(java.lang.Class)
71  	 */
72  	@CacheNoCopy
73  	public boolean isPersistable(Class clazz) {
74  		return getService(clazz).isPersistable(clazz);
75  	}
76  
77  	/**
78  	 * @see org.kuali.rice.krad.service.PersistenceService#getPrimaryKeys(java.lang.Class)
79  	 */
80  	@CacheNoCopy
81  	public List getPrimaryKeys(Class clazz) {
82  		return getService(clazz).getPrimaryKeys(clazz);
83  	}
84  
85  	/**
86  	 * @see org.kuali.rice.krad.service.PersistenceMetadataExplorerService#listFieldNames(java.lang.Class)
87  	 */
88  	@CacheNoCopy
89  	public List listFieldNames(Class clazz) {
90  		return getService(clazz).listFieldNames(clazz);
91  	}
92  
93  	/**
94  	 * @see org.kuali.rice.krad.service.PersistenceMetadataService#clearPrimaryKeyFields(java.lang.Object)
95  	 */
96  	// Unit tests only
97  	public Object clearPrimaryKeyFields(Object persistableObject) {
98  		return getService(persistableObject.getClass()).clearPrimaryKeyFields(persistableObject);
99  	}
100 
101 	/**
102 	 * @see org.kuali.rice.krad.service.PersistenceMetadataExplorerService#listPersistableSubclasses(java.lang.Class)
103 	 */
104 	@CacheNoCopy
105 	// Unit tests only
106 	public List listPersistableSubclasses(Class superclazz) {
107 		return getService(superclazz).listPersistableSubclasses(superclazz);
108 	}
109 
110 	/**
111 	 * @see org.kuali.rice.krad.service.PersistenceService#getRelationshipMetadata(java.lang.Class,
112 	 *      java.lang.String)
113 	 */
114 	@CacheNoCopy
115 	public Map<String, BusinessObjectRelationship> getRelationshipMetadata(Class persistableClass, String attributeName, String attributePrefix) {
116 		return getService(persistableClass).getRelationshipMetadata(persistableClass, attributeName, attributePrefix);
117 	}
118 
119 	@CacheNoCopy
120 	// Unit tests only
121 	public Map<String, BusinessObjectRelationship> getRelationshipMetadata(Class persistableClass, String attributeName) {
122 		return getService(persistableClass).getRelationshipMetadata(persistableClass, attributeName);
123 	}
124 
125 	/**
126 	 * @see org.kuali.rice.krad.service.PersistenceService#getForeignKeyFieldName(java.lang.Object,
127 	 *      java.lang.String, java.lang.String)
128 	 */
129 	@CacheNoCopy
130 	public String getForeignKeyFieldName(Class persistableObjectClass, String attributeName, String pkName) {
131 		return getService(persistableObjectClass).getForeignKeyFieldName(persistableObjectClass, attributeName, pkName);
132 	}
133 
134 	/**
135 	 * @see org.kuali.rice.krad.service.PersistenceService#getReferencesForForeignKey(java.lang.Class,
136 	 *      java.lang.String)
137 	 */
138 	@CacheNoCopy
139 	public Map getReferencesForForeignKey(Class persistableObjectClass, String attributeName) {
140 		return getService(persistableObjectClass).getReferencesForForeignKey(persistableObjectClass, attributeName);
141 	}
142 
143 	/**
144 	 * @see org.kuali.rice.krad.service.PersistenceService#getForeignKeysForReference(java.lang.Class, java.lang.String)
145 	 */
146 	@CacheNoCopy
147 	public Map getForeignKeysForReference(Class clazz, String attributeName) {
148 		return getService(clazz).getForeignKeysForReference(clazz, attributeName);
149 	}
150 
151 	@CacheNoCopy
152 	public Map<String, String> getInverseForeignKeysForCollection(Class boClass, String collectionName) {
153 		return getService(boClass).getInverseForeignKeysForCollection(boClass, collectionName);
154 	}
155 
156 	/**
157 	 * @see org.kuali.rice.krad.service.PersistenceService#getNestedForeignKeyMap(java.lang.Class)
158 	 */
159 	@CacheNoCopy
160 	public Map getNestedForeignKeyMap(Class persistableObjectClass) {
161 		return getService(persistableObjectClass).getNestedForeignKeyMap(persistableObjectClass);
162 	}
163 
164 	/**
165 	 * @see org.kuali.rice.krad.service.PersistenceMetadataService#hasPrimaryKeyFieldValues(java.lang.Object)
166 	 */
167 	public boolean hasPrimaryKeyFieldValues(Object persistableObject) {
168 		return getService(persistableObject.getClass()).hasPrimaryKeyFieldValues(persistableObject);
169 	}
170 
171 	/**
172 	 * @see org.kuali.rice.krad.service.PersistenceService#getForeignKeyFieldsPopulationState(org.kuali.rice.krad.bo.BusinessObject,
173 	 *      java.lang.String)
174 	 */
175 	public ForeignKeyFieldsPopulationState getForeignKeyFieldsPopulationState(PersistableBusinessObject bo, String referenceName) {
176 		return getService(bo.getClass()).getForeignKeyFieldsPopulationState(bo, referenceName);
177 	}
178 
179 	/**
180 	 * @see org.kuali.rice.krad.service.PersistenceStructureService#listReferenceObjectFieldNames(java.lang.Class)
181 	 */
182 	@CacheNoCopy
183 	public Map<String, Class> listReferenceObjectFields(Class boClass) {
184 		return getService(boClass).listReferenceObjectFields(boClass);
185 	}
186 
187 	@CacheNoCopy
188 	public Map<String, Class> listCollectionObjectTypes(Class boClass) {
189 		return getService(boClass).listCollectionObjectTypes(boClass);
190 	}
191 
192 	public Map<String, Class> listCollectionObjectTypes(PersistableBusinessObject bo) {
193 		return getService(bo.getClass()).listCollectionObjectTypes(bo);
194 	}
195 
196 	/**
197 	 * @see org.kuali.rice.krad.service.PersistenceStructureService#listReferenceObjectFieldNames(org.kuali.rice.krad.bo.BusinessObject)
198 	 */
199 	public Map<String, Class> listReferenceObjectFields(PersistableBusinessObject bo) {
200 		return getService(bo.getClass()).listReferenceObjectFields(bo);
201 	}
202 
203 	@CacheNoCopy
204 	public boolean isReferenceUpdatable(Class boClass, String referenceName) {
205 		return getService(boClass).isReferenceUpdatable(boClass, referenceName);
206 	}
207 
208 	@CacheNoCopy
209 	public boolean isCollectionUpdatable(Class boClass, String collectionName) {
210 		return getService(boClass).isCollectionUpdatable(boClass, collectionName);
211 	}
212 
213 	@CacheNoCopy
214 	public boolean hasCollection(Class boClass, String collectionName) {
215 		return getService(boClass).hasCollection(boClass, collectionName);
216 	}
217 
218 	@CacheNoCopy
219 	public boolean hasReference(Class boClass, String referenceName) {
220 		return getService(boClass).hasReference(boClass, referenceName);
221 	}
222 
223 	/**
224 	 * This overridden method ...
225 	 * 
226 	 * @see org.kuali.rice.krad.service.PersistenceStructureService#getTableName(java.lang.Class)
227 	 */
228 	public String getTableName(
229 			Class<? extends PersistableBusinessObject> boClass) {
230 		return getService(boClass).getTableName(boClass);
231 	}
232 	
233 	
234 }