001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.gl.dao.ojb;
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.core.dao.ojb.PlatformAwareDaoBaseOjb;
027: import org.kuali.core.exceptions.ClassNotPersistableException;
028: import org.kuali.module.gl.bo.OriginEntryFull;
029: import org.kuali.module.gl.dao.ReconciliationDao;
030:
031: /**
032: * Uses OJB to determine the column name -> java attribute name mapping
033: */
034: public class ReconciliationDaoOjb extends PlatformAwareDaoBaseOjb
035: implements ReconciliationDao {
036:
037: private DescriptorRepository descriptorRepository;
038:
039: /**
040: * Constructs a ReconciliationDaoOjb.java.
041: */
042: public ReconciliationDaoOjb() {
043: MetadataManager metadataManager = MetadataManager.getInstance();
044: descriptorRepository = metadataManager.getGlobalRepository();
045: }
046:
047: /**
048: * Converts a list of DB column names to a list of java attribute names. The returned list is the same size as arrap parameter
049: *
050: * @param clazz a class for the OriginEntryFull class
051: * @param columnNames an array of database columns
052: * @param caseInsensitive whether to do matching
053: * @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
054: * in the list is the java attribute name
055: * @see org.kuali.module.gl.dao.ReconciliationDao#convertDBColumnNamesToJavaName(java.lang.String[])
056: */
057: public List<String> convertDBColumnNamesToJavaName(
058: Class<? extends OriginEntryFull> clazz,
059: String[] columnNames, boolean caseInsensitive) {
060: List<String> results = new ArrayList<String>();
061: ClassDescriptor classDescriptor = getClassDescriptor(clazz);
062: for (int i = 0; i < columnNames.length; i++) {
063: results.add(convertDBColumnNameToJavaName(classDescriptor,
064: columnNames[i], caseInsensitive));
065: }
066: return results;
067: }
068:
069: /**
070: * Returns the java attribute name corresponding to the column name
071: *
072: * @param classDescriptor the origin entry class
073: * @param columnName the DB column name
074: * @param caseInsensitive whether to do case insensitive matching
075: * @return the java attribute name
076: */
077: protected String convertDBColumnNameToJavaName(
078: ClassDescriptor classDescriptor, String columnName,
079: boolean caseInsensitive) {
080: FieldDescriptor[] fields = classDescriptor
081: .getFieldDescriptions();
082: for (FieldDescriptor field : fields) {
083: if (caseInsensitive
084: && field.getColumnName().equalsIgnoreCase(
085: columnName)) {
086: return field.getAttributeName();
087: }
088: if (!caseInsensitive
089: && field.getColumnName().equals(columnName)) {
090: return field.getAttributeName();
091: }
092: }
093: return null;
094: }
095:
096: /**
097: * Returns the OJB class descriptor
098: *
099: * @param <E> an origin entry class
100: * @param persistableClass the class
101: * @return the class descriptor
102: */
103: protected <E extends OriginEntryFull> ClassDescriptor getClassDescriptor(
104: Class<E> persistableClass) {
105: if (persistableClass == null) {
106: throw new IllegalArgumentException("invalid (null) object");
107: }
108:
109: ClassDescriptor classDescriptor = null;
110: DescriptorRepository globalRepository = getDescriptorRepository();
111: try {
112: classDescriptor = globalRepository
113: .getDescriptorFor(persistableClass);
114: } catch (ClassNotPersistenceCapableException e) {
115: throw new ClassNotPersistableException("class '"
116: + persistableClass.getName()
117: + "' is not persistable", e);
118: }
119:
120: return classDescriptor;
121: }
122:
123: /**
124: * Gets the descriptorRepository attribute.
125: *
126: * @return Returns the descriptorRepository.
127: */
128: protected DescriptorRepository getDescriptorRepository() {
129: return descriptorRepository;
130: }
131:
132: /**
133: * Sets the descriptorRepository attribute value.
134: *
135: * @param descriptorRepository The descriptorRepository to set.
136: */
137: public void setDescriptorRepository(
138: DescriptorRepository descriptorRepository) {
139: this.descriptorRepository = descriptorRepository;
140: }
141: }
|