PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/cmp/src/main/java/org/jboss/as/cmp/jdbc/JDBCTypeComplex.java

#
Java | 174 lines | 113 code | 23 blank | 38 comment | 9 complexity | d65f58035a0022d4aa196157524a1417 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.cmp.jdbc;
  23. import java.util.HashMap;
  24. import javax.ejb.EJBException;
  25. import org.jboss.as.cmp.CmpMessages;
  26. import static org.jboss.as.cmp.CmpMessages.MESSAGES;
  27. /**
  28. * JDBCTypeComplex provides the mapping between a Java Bean (not an EJB)
  29. * and a set of columns. This class has a flattened view of the Java Bean,
  30. * which may contain other Java Beans. This class simply treats the bean
  31. * as a set of properties, which may be in the a.b.c style. The details
  32. * of how this mapping is performed can be found in JDBCTypeFactory.
  33. * <p/>
  34. * This class holds a description of the columns
  35. * and the properties that map to the columns. Additionally, this class
  36. * knows how to extract a column value from the Java Bean and how to set
  37. * a column value info the Java Bean. See JDBCTypeComplexProperty for
  38. * details on how this is done.
  39. *
  40. * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
  41. * @version $Revision: 81030 $
  42. */
  43. public final class JDBCTypeComplex implements JDBCType {
  44. private final JDBCTypeComplexProperty[] properties;
  45. private final String[] columnNames;
  46. private final Class[] javaTypes;
  47. private final int[] jdbcTypes;
  48. private final String[] sqlTypes;
  49. private final boolean[] notNull;
  50. private final JDBCResultSetReader[] resultSetReaders;
  51. private final JDBCParameterSetter[] paramSetters;
  52. private final Class fieldType;
  53. private final HashMap propertiesByName = new HashMap();
  54. public JDBCTypeComplex(
  55. JDBCTypeComplexProperty[] properties,
  56. Class fieldType) {
  57. this.properties = properties;
  58. this.fieldType = fieldType;
  59. int propNum = properties.length;
  60. columnNames = new String[propNum];
  61. javaTypes = new Class[propNum];
  62. jdbcTypes = new int[propNum];
  63. sqlTypes = new String[propNum];
  64. notNull = new boolean[propNum];
  65. resultSetReaders = new JDBCResultSetReader[propNum];
  66. paramSetters = new JDBCParameterSetter[propNum];
  67. for (int i = 0; i < properties.length; i++) {
  68. JDBCTypeComplexProperty property = properties[i];
  69. columnNames[i] = property.getColumnName();
  70. javaTypes[i] = property.getJavaType();
  71. jdbcTypes[i] = property.getJDBCType();
  72. sqlTypes[i] = property.getSQLType();
  73. notNull[i] = property.isNotNull();
  74. resultSetReaders[i] = property.getResultSetReader();
  75. paramSetters[i] = property.getParameterSetter();
  76. propertiesByName.put(property.getPropertyName(), property);
  77. }
  78. }
  79. public String[] getColumnNames() {
  80. return columnNames;
  81. }
  82. public Class[] getJavaTypes() {
  83. return javaTypes;
  84. }
  85. public int[] getJDBCTypes() {
  86. return jdbcTypes;
  87. }
  88. public String[] getSQLTypes() {
  89. return sqlTypes;
  90. }
  91. public boolean[] getNotNull() {
  92. return notNull;
  93. }
  94. public boolean[] getAutoIncrement() {
  95. return new boolean[]{false};
  96. }
  97. public Object getColumnValue(int index, Object value) {
  98. return getColumnValue(properties[index], value);
  99. }
  100. public Object setColumnValue(int index, Object value, Object columnValue) {
  101. return setColumnValue(properties[index], value, columnValue);
  102. }
  103. public boolean hasMapper() {
  104. return false;
  105. }
  106. public boolean isSearchable() {
  107. return false;
  108. }
  109. public JDBCResultSetReader[] getResultSetReaders() {
  110. return resultSetReaders;
  111. }
  112. public JDBCParameterSetter[] getParameterSetter() {
  113. return paramSetters;
  114. }
  115. public JDBCTypeComplexProperty[] getProperties() {
  116. return properties;
  117. }
  118. public JDBCTypeComplexProperty getProperty(String propertyName) {
  119. JDBCTypeComplexProperty prop = (JDBCTypeComplexProperty) propertiesByName.get(propertyName);
  120. if (prop == null) {
  121. throw CmpMessages.MESSAGES.fieldDoesNotHaveProperty(fieldType.getName(), propertyName);
  122. }
  123. return prop;
  124. }
  125. private static Object getColumnValue(JDBCTypeComplexProperty property, Object value) {
  126. try {
  127. return property.getColumnValue(value);
  128. } catch (EJBException e) {
  129. throw e;
  130. } catch (Exception e) {
  131. throw MESSAGES.errorGettingColumnValue(e);
  132. }
  133. }
  134. private Object setColumnValue(
  135. JDBCTypeComplexProperty property,
  136. Object value,
  137. Object columnValue) {
  138. if (value == null && columnValue == null) {
  139. // nothing to do
  140. return null;
  141. }
  142. try {
  143. if (value == null) {
  144. value = fieldType.newInstance();
  145. }
  146. return property.setColumnValue(value, columnValue);
  147. } catch (Exception e) {
  148. throw MESSAGES.errorSettingColumnValue(e);
  149. }
  150. }
  151. }