001 /*
002 * Copyright 2011 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.kfs.gl.batch.dataaccess.impl;
017
018 import java.util.ArrayList;
019 import java.util.List;
020
021 import org.apache.ojb.broker.metadata.ClassDescriptor;
022 import org.apache.ojb.broker.metadata.ClassNotPersistenceCapableException;
023 import org.apache.ojb.broker.metadata.DescriptorRepository;
024 import org.apache.ojb.broker.metadata.FieldDescriptor;
025 import org.apache.ojb.broker.metadata.MetadataManager;
026 import org.kuali.kfs.gl.batch.dataaccess.ReconciliationDao;
027 import org.kuali.kfs.gl.businessobject.OriginEntryFull;
028 import org.kuali.rice.kns.dao.impl.PlatformAwareDaoBaseOjb;
029 import org.kuali.rice.kns.exception.ClassNotPersistableException;
030
031 /**
032 * Uses OJB to determine the column name -> java attribute name mapping
033 */
034 public class ReconciliationDaoOjb extends PlatformAwareDaoBaseOjb implements ReconciliationDao {
035
036 private DescriptorRepository descriptorRepository;
037
038 /**
039 * Constructs a ReconciliationDaoOjb.java.
040 */
041 public ReconciliationDaoOjb() {
042 MetadataManager metadataManager = MetadataManager.getInstance();
043 descriptorRepository = metadataManager.getGlobalRepository();
044 }
045
046 /**
047 * Converts a list of DB column names to a list of java attribute names. The returned list is the same size as arrap parameter
048 *
049 * @param clazz a class for the OriginEntryFull class
050 * @param columnNames an array of database columns
051 * @param caseInsensitive whether to do matching
052 * @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
053 * in the list is the java attribute name
054 * @see org.kuali.kfs.gl.batch.dataaccess.ReconciliationDao#convertDBColumnNamesToJavaName(java.lang.String[])
055 */
056 public List<String> convertDBColumnNamesToJavaName(Class<? extends OriginEntryFull> clazz, String[] columnNames, boolean caseInsensitive) {
057 List<String> results = new ArrayList<String>();
058 ClassDescriptor classDescriptor = getClassDescriptor(clazz);
059 for (int i = 0; i < columnNames.length; i++) {
060 results.add(convertDBColumnNameToJavaName(classDescriptor, columnNames[i], caseInsensitive));
061 }
062 return results;
063 }
064
065 /**
066 * Returns the java attribute name corresponding to the column name
067 *
068 * @param classDescriptor the origin entry class
069 * @param columnName the DB column name
070 * @param caseInsensitive whether to do case insensitive matching
071 * @return the java attribute name
072 */
073 protected String convertDBColumnNameToJavaName(ClassDescriptor classDescriptor, String columnName, boolean caseInsensitive) {
074 FieldDescriptor[] fields = classDescriptor.getFieldDescriptions();
075 for (FieldDescriptor field : fields) {
076 if (caseInsensitive && field.getColumnName().equalsIgnoreCase(columnName)) {
077 return field.getAttributeName();
078 }
079 if (!caseInsensitive && field.getColumnName().equals(columnName)) {
080 return field.getAttributeName();
081 }
082 }
083 return null;
084 }
085
086 /**
087 * Returns the OJB class descriptor
088 *
089 * @param <E> an origin entry class
090 * @param persistableClass the class
091 * @return the class descriptor
092 */
093 protected <E extends OriginEntryFull> ClassDescriptor getClassDescriptor(Class<E> persistableClass) {
094 if (persistableClass == null) {
095 throw new IllegalArgumentException("invalid (null) object");
096 }
097
098 ClassDescriptor classDescriptor = null;
099 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 }