001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.krad.service.impl;
017    
018    import java.util.HashMap;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.kuali.rice.krad.bo.DataObjectRelationship;
023    import org.kuali.rice.krad.bo.PersistableBusinessObject;
024    import org.kuali.rice.krad.service.PersistenceStructureService;
025    import org.kuali.rice.krad.util.ForeignKeyFieldsPopulationState;
026    
027    /**
028     * 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.
029     * 
030     * @author Kuali Rice Team (rice.collab@kuali.org)
031     *
032     */
033    public class PersistenceStructureServiceImpl extends PersistenceServiceImplBase implements PersistenceStructureService {
034    
035            /**
036             * 
037             * special case when the attributeClass passed in doesnt match the class of
038             * the reference-descriptor as defined in ojb-repository. Currently the only
039             * case of this happening is ObjectCode vs. ObjectCodeCurrent.
040             * 
041             * NOTE: This method makes no real sense and is a product of a hack
042             * introduced by KFS for an unknown reason. If you find yourself using this
043             * map stop and go do something else.
044             * 
045             * @param from
046             *            the class in the code
047             * @param to
048             *            the class in the repository
049             */
050            public static Map<Class, Class> referenceConversionMap = new HashMap<Class, Class>();
051            
052            private PersistenceStructureService persistenceStructureServiceJpa;
053            private PersistenceStructureService persistenceStructureServiceOjb;
054    
055            public void setPersistenceStructureServiceJpa(PersistenceStructureService persistenceStructureServiceJpa) {
056                    this.persistenceStructureServiceJpa = persistenceStructureServiceJpa;
057            }
058    
059            public void setPersistenceStructureServiceOjb(PersistenceStructureService persistenceStructureServiceOjb) {
060                    this.persistenceStructureServiceOjb = persistenceStructureServiceOjb;
061            }
062            
063            private PersistenceStructureService getService(Class clazz) {
064                    return (isJpaEnabledForKradClass(clazz)) ?
065                                                    persistenceStructureServiceJpa : persistenceStructureServiceOjb;
066            }
067            
068            /**
069             * @see org.kuali.rice.krad.service.PersistenceService#isPersistable(java.lang.Class)
070             */
071            
072            public boolean isPersistable(Class clazz) {
073                    return getService(clazz).isPersistable(clazz);
074            }
075    
076            /**
077             * @see org.kuali.rice.krad.service.PersistenceService#getPrimaryKeys(java.lang.Class)
078             */
079            
080            public List getPrimaryKeys(Class clazz) {
081                    return getService(clazz).getPrimaryKeys(clazz);
082            }
083    
084            /**
085             * @see org.kuali.rice.krad.service.PersistenceMetadataExplorerService#listFieldNames(java.lang.Class)
086             */
087            
088            public List listFieldNames(Class clazz) {
089                    return getService(clazz).listFieldNames(clazz);
090            }
091    
092            /**
093             * @see org.kuali.rice.krad.service.PersistenceMetadataService#clearPrimaryKeyFields(java.lang.Object)
094             */
095            // Unit tests only
096            public Object clearPrimaryKeyFields(Object persistableObject) {
097                    return getService(persistableObject.getClass()).clearPrimaryKeyFields(persistableObject);
098            }
099    
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    }