View Javadoc
1   /*
2    * Copyright 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.ole.gl.batch.dataaccess.impl;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.ojb.broker.metadata.ClassDescriptor;
22  import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
23  import org.apache.ojb.broker.metadata.DescriptorRepository;
24  import org.apache.ojb.broker.metadata.FieldDescriptor;
25  import org.apache.ojb.broker.metadata.MetadataManager;
26  import org.kuali.ole.gl.batch.dataaccess.ReconciliationDao;
27  import org.kuali.ole.gl.businessobject.OriginEntryFull;
28  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
29  import org.kuali.rice.krad.exception.ClassNotPersistableException;
30  
31  /**
32   * Uses OJB to determine the column name -> java attribute name mapping
33   */
34  public class ReconciliationDaoOjb extends PlatformAwareDaoBaseOjb implements ReconciliationDao {
35  
36      private DescriptorRepository descriptorRepository;
37  
38      /**
39       * Constructs a ReconciliationDaoOjb.java.
40       */
41      public ReconciliationDaoOjb() {
42          MetadataManager metadataManager = MetadataManager.getInstance();
43          descriptorRepository = metadataManager.getGlobalRepository();
44      }
45  
46      /**
47       * Converts a list of DB column names to a list of java attribute names. The returned list is the same size as arrap parameter
48       * 
49       * @param clazz a class for the OriginEntryFull class
50       * @param columnNames an array of database columns
51       * @param caseInsensitive whether to do matching
52       * @return for every valid index in the return value and the array, the value in the array is the db column name, and the value
53       *         in the list is the java attribute name
54       * @see org.kuali.ole.gl.batch.dataaccess.ReconciliationDao#convertDBColumnNamesToJavaName(java.lang.String[])
55       */
56      public List<String> convertDBColumnNamesToJavaName(Class<? extends OriginEntryFull> clazz, String[] columnNames, boolean caseInsensitive) {
57          List<String> results = new ArrayList<String>();
58          ClassDescriptor classDescriptor = getClassDescriptor(clazz);
59          for (int i = 0; i < columnNames.length; i++) {
60              results.add(convertDBColumnNameToJavaName(classDescriptor, columnNames[i], caseInsensitive));
61          }
62          return results;
63      }
64  
65      /**
66       * Returns the java attribute name corresponding to the column name
67       * 
68       * @param classDescriptor the origin entry class
69       * @param columnName the DB column name
70       * @param caseInsensitive whether to do case insensitive matching
71       * @return the java attribute name
72       */
73      protected String convertDBColumnNameToJavaName(ClassDescriptor classDescriptor, String columnName, boolean caseInsensitive) {
74          FieldDescriptor[] fields = classDescriptor.getFieldDescriptions();
75          for (FieldDescriptor field : fields) {
76              if (caseInsensitive && field.getColumnName().equalsIgnoreCase(columnName)) {
77                  return field.getAttributeName();
78              }
79              if (!caseInsensitive && field.getColumnName().equals(columnName)) {
80                  return field.getAttributeName();
81              }
82          }
83          return null;
84      }
85  
86      /**
87       * Returns the OJB class descriptor
88       * 
89       * @param <E> an origin entry class
90       * @param persistableClass the class
91       * @return the class descriptor
92       */
93      protected <E extends OriginEntryFull> ClassDescriptor getClassDescriptor(Class<E> persistableClass) {
94          if (persistableClass == null) {
95              throw new IllegalArgumentException("invalid (null) object");
96          }
97  
98          ClassDescriptor classDescriptor = null;
99          DescriptorRepository globalRepository = getDescriptorRepository();
100         try {
101             classDescriptor = globalRepository.getDescriptorFor(persistableClass);
102         }
103         catch (ClassNotPersistenceCapableException e) {
104             throw new ClassNotPersistableException("class '" + persistableClass.getName() + "' is not persistable", e);
105         }
106 
107         return classDescriptor;
108     }
109 
110     /**
111      * Gets the descriptorRepository attribute.
112      * 
113      * @return Returns the descriptorRepository.
114      */
115     protected DescriptorRepository getDescriptorRepository() {
116         return descriptorRepository;
117     }
118 
119     /**
120      * Sets the descriptorRepository attribute value.
121      * 
122      * @param descriptorRepository The descriptorRepository to set.
123      */
124     public void setDescriptorRepository(DescriptorRepository descriptorRepository) {
125         this.descriptorRepository = descriptorRepository;
126     }
127 }