View Javadoc

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