View Javadoc

1   /*
2    * Copyright 2008 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.sys.dataaccess.impl;
17  
18  import java.sql.DatabaseMetaData;
19  import java.sql.ResultSet;
20  import java.sql.SQLException;
21  
22  import org.apache.commons.beanutils.PropertyUtils;
23  import org.apache.log4j.Logger;
24  import org.apache.ojb.broker.metadata.ClassDescriptor;
25  import org.kuali.ole.sys.dataaccess.FieldMetaData;
26  import org.kuali.rice.core.framework.persistence.ojb.conversion.OjbKualiEncryptDecryptFieldConversion;
27  import org.kuali.rice.krad.bo.BusinessObject;
28  import org.kuali.rice.krad.bo.PersistableBusinessObject;
29  import org.springframework.jdbc.support.DatabaseMetaDataCallback;
30  import org.springframework.jdbc.support.MetaDataAccessException;
31  
32  public class FieldMetaDataImpl implements DatabaseMetaDataCallback, FieldMetaData {
33      private static final Logger LOG = Logger.getLogger(FieldMetaDataImpl.class);
34      
35      private Class businessObjectClass;
36      private String propertyName;
37      
38      private String tableName;
39      private String columnName;
40      private String dataType;
41      private int length;
42      private int decimalPlaces;
43      private boolean encrypted;
44  
45      public FieldMetaDataImpl(Class businessObjectClass, String propertyName) {
46          this.businessObjectClass = businessObjectClass;
47          this.propertyName = propertyName;
48      }
49  
50      public Object processMetaData(DatabaseMetaData databaseMetaData) throws SQLException, MetaDataAccessException {
51          Class workingBusinessObjectClass = businessObjectClass;
52          String workingPropertyName = propertyName;
53          while (workingPropertyName.contains(".")) {
54              try {
55                  workingBusinessObjectClass = org.apache.ojb.broker.metadata.MetadataManager.getInstance().getGlobalRepository().getDescriptorFor(workingBusinessObjectClass).getObjectReferenceDescriptorByName(workingPropertyName.substring(0, workingPropertyName.indexOf("."))).getItemClass();
56              }
57              catch (Exception e1) {
58                  LOG.debug(new StringBuffer("Unable to get property type via reference descriptor for property ").append(workingPropertyName.substring(0, workingPropertyName.indexOf("."))).append(" of BusinessObject class ").append(workingBusinessObjectClass).toString(), e1);
59                  try {
60                      workingBusinessObjectClass = org.apache.ojb.broker.metadata.MetadataManager.getInstance().getGlobalRepository().getDescriptorFor(workingBusinessObjectClass).getCollectionDescriptorByName(workingPropertyName.substring(0, workingPropertyName.indexOf("."))).getItemClass();                        
61                  }
62                  catch (Exception e2) {
63                      LOG.debug(new StringBuffer("Unable to get property type via collection descriptor of property ").append(workingPropertyName.substring(0, workingPropertyName.indexOf("."))).append(" of BusinessObject class ").append(workingBusinessObjectClass).toString(), e2);
64                      BusinessObject businessObject = null;
65                      try {
66                          businessObject = (BusinessObject)workingBusinessObjectClass.newInstance();
67                      }
68                      catch (Exception e3) {
69                          if (LOG.isDebugEnabled()) {
70                              LOG.debug("Unable to instantiate BusinessObject class " + workingBusinessObjectClass, e3);
71                          }
72                          return populateAndReturnNonPersistableInstance();
73                      }
74                      try {
75                          workingBusinessObjectClass = PropertyUtils.getPropertyType(businessObject, workingPropertyName.substring(0, workingPropertyName.indexOf(".")));
76                      }
77                      catch (Exception e4) {
78                          LOG.debug(new StringBuffer("Unable to get type of property ").append(workingPropertyName.substring(0, workingPropertyName.indexOf("."))).append(" for BusinessObject class ").append(workingBusinessObjectClass).toString(), e4);
79                          return populateAndReturnNonPersistableInstance();
80                      }
81                  }
82              }
83              if (workingBusinessObjectClass == null) {
84                  return populateAndReturnNonPersistableInstance();
85              }
86              else {
87                  workingPropertyName = workingPropertyName.substring(workingPropertyName.indexOf(".") + 1);
88              }
89          }
90          if (!PersistableBusinessObject.class.isAssignableFrom(workingBusinessObjectClass)) {
91              return populateAndReturnNonPersistableInstance();
92          }
93          ClassDescriptor classDescriptor = org.apache.ojb.broker.metadata.MetadataManager.getInstance().getGlobalRepository().getDescriptorFor(workingBusinessObjectClass);
94          if (classDescriptor == null) {
95              return populateAndReturnNonPersistableInstance();
96          }
97          tableName = classDescriptor.getFullTableName();
98          if (classDescriptor.getFieldDescriptorByName(workingPropertyName) == null) {
99              return populateAndReturnNonPersistableInstance();
100         }
101         columnName = classDescriptor.getFieldDescriptorByName(workingPropertyName).getColumnName();
102         ResultSet resultSet = databaseMetaData.getColumns(null, null, tableName, columnName);
103         if (resultSet.next()) {
104             dataType = resultSet.getString("TYPE_NAME");
105             length = resultSet.getInt("COLUMN_SIZE");
106             decimalPlaces = resultSet.getInt("DECIMAL_DIGITS");
107             encrypted = classDescriptor.getFieldDescriptorByName(workingPropertyName).getFieldConversion() instanceof OjbKualiEncryptDecryptFieldConversion;
108         }
109         resultSet.close();
110         return this;
111     }
112     
113     protected FieldMetaData populateAndReturnNonPersistableInstance() {
114         tableName = "N/A";
115         columnName = tableName;
116         dataType = tableName;
117         length = 0;
118         decimalPlaces = 0;
119         encrypted = false;
120         return this;
121     }
122 
123     public String getTableName() {
124         return tableName;
125     }
126 
127     public String getColumnName() {
128         return columnName;
129     }
130 
131     public String getDataType() {
132         return dataType;
133     }
134 
135     public int getLength() {
136         return length;
137     }
138 
139     public int getDecimalPlaces() {
140         return decimalPlaces;
141     }
142 
143     public boolean isEncrypted() {
144         return encrypted;
145     }
146 }