PageRenderTime 43ms CodeModel.GetById 33ms app.highlight 8ms 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 */
 22package org.jboss.as.cmp.jdbc;
 23
 24import java.util.HashMap;
 25import javax.ejb.EJBException;
 26import org.jboss.as.cmp.CmpMessages;
 27import static org.jboss.as.cmp.CmpMessages.MESSAGES;
 28
 29/**
 30 * JDBCTypeComplex provides the mapping between a Java Bean (not an EJB)
 31 * and a set of columns. This class has a flattened view of the Java Bean,
 32 * which may contain other Java Beans.  This class simply treats the bean
 33 * as a set of properties, which may be in the a.b.c style. The details
 34 * of how this mapping is performed can be found in JDBCTypeFactory.
 35 * <p/>
 36 * This class holds a description of the columns
 37 * and the properties that map to the columns. Additionally, this class
 38 * knows how to extract a column value from the Java Bean and how to set
 39 * a column value info the Java Bean. See JDBCTypeComplexProperty for
 40 * details on how this is done.
 41 *
 42 * @author <a href="mailto:dain@daingroup.com">Dain Sundstrom</a>
 43 * @version $Revision: 81030 $
 44 */
 45public final class JDBCTypeComplex implements JDBCType {
 46    private final JDBCTypeComplexProperty[] properties;
 47    private final String[] columnNames;
 48    private final Class[] javaTypes;
 49    private final int[] jdbcTypes;
 50    private final String[] sqlTypes;
 51    private final boolean[] notNull;
 52    private final JDBCResultSetReader[] resultSetReaders;
 53    private final JDBCParameterSetter[] paramSetters;
 54    private final Class fieldType;
 55    private final HashMap propertiesByName = new HashMap();
 56
 57    public JDBCTypeComplex(
 58            JDBCTypeComplexProperty[] properties,
 59            Class fieldType) {
 60
 61        this.properties = properties;
 62        this.fieldType = fieldType;
 63
 64        int propNum = properties.length;
 65        columnNames = new String[propNum];
 66        javaTypes = new Class[propNum];
 67        jdbcTypes = new int[propNum];
 68        sqlTypes = new String[propNum];
 69        notNull = new boolean[propNum];
 70        resultSetReaders = new JDBCResultSetReader[propNum];
 71        paramSetters = new JDBCParameterSetter[propNum];
 72        for (int i = 0; i < properties.length; i++) {
 73            JDBCTypeComplexProperty property = properties[i];
 74            columnNames[i] = property.getColumnName();
 75            javaTypes[i] = property.getJavaType();
 76            jdbcTypes[i] = property.getJDBCType();
 77            sqlTypes[i] = property.getSQLType();
 78            notNull[i] = property.isNotNull();
 79            resultSetReaders[i] = property.getResultSetReader();
 80            paramSetters[i] = property.getParameterSetter();
 81            propertiesByName.put(property.getPropertyName(), property);
 82        }
 83    }
 84
 85    public String[] getColumnNames() {
 86        return columnNames;
 87    }
 88
 89    public Class[] getJavaTypes() {
 90        return javaTypes;
 91    }
 92
 93    public int[] getJDBCTypes() {
 94        return jdbcTypes;
 95    }
 96
 97    public String[] getSQLTypes() {
 98        return sqlTypes;
 99    }
100
101    public boolean[] getNotNull() {
102        return notNull;
103    }
104
105    public boolean[] getAutoIncrement() {
106        return new boolean[]{false};
107    }
108
109    public Object getColumnValue(int index, Object value) {
110        return getColumnValue(properties[index], value);
111    }
112
113    public Object setColumnValue(int index, Object value, Object columnValue) {
114        return setColumnValue(properties[index], value, columnValue);
115    }
116
117    public boolean hasMapper() {
118        return false;
119    }
120
121    public boolean isSearchable() {
122        return false;
123    }
124
125    public JDBCResultSetReader[] getResultSetReaders() {
126        return resultSetReaders;
127    }
128
129    public JDBCParameterSetter[] getParameterSetter() {
130        return paramSetters;
131    }
132
133    public JDBCTypeComplexProperty[] getProperties() {
134        return properties;
135    }
136
137    public JDBCTypeComplexProperty getProperty(String propertyName) {
138        JDBCTypeComplexProperty prop = (JDBCTypeComplexProperty) propertiesByName.get(propertyName);
139        if (prop == null) {
140            throw CmpMessages.MESSAGES.fieldDoesNotHaveProperty(fieldType.getName(), propertyName);
141        }
142        return prop;
143    }
144
145    private static Object getColumnValue(JDBCTypeComplexProperty property, Object value) {
146        try {
147            return property.getColumnValue(value);
148        } catch (EJBException e) {
149            throw e;
150        } catch (Exception e) {
151            throw MESSAGES.errorGettingColumnValue(e);
152        }
153    }
154
155    private Object setColumnValue(
156            JDBCTypeComplexProperty property,
157            Object value,
158            Object columnValue) {
159
160        if (value == null && columnValue == null) {
161            // nothing to do
162            return null;
163        }
164
165        try {
166            if (value == null) {
167                value = fieldType.newInstance();
168            }
169            return property.setColumnValue(value, columnValue);
170        } catch (Exception e) {
171            throw MESSAGES.errorSettingColumnValue(e);
172        }
173    }
174}