Java tutorial
/* * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.wantscart.jade.core; import com.wantscart.jade.core.serializer.NullSerializer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.*; import org.springframework.dao.DataRetrievalFailureException; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.support.JdbcUtils; import org.springframework.util.Assert; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; /** * {@link RowMapper} implementation that converts a row into a new instance * of the specified mapped target class. The mapped target class must be a * top-level class and it must have a default or no-arg constructor. * <p/> * <p/> * Column values are mapped based on matching the column name as obtained * from result set metadata to public setters for the corresponding * properties. The names are matched either directly or by transforming a * name separating the parts with underscores to the same name using * "camel" case. * <p/> * <p/> * Mapping is provided for fields in the target class for many common * types, e.g.: String, boolean, Boolean, byte, Byte, short, Short, int, * Integer, long, Long, float, Float, double, Double, BigDecimal, * <code>java.util.Date</code>, etc. * <p/> * <p/> * To facilitate mapping between columns and fields that don't have * matching names, try using column aliases in the SQL statement like * "select fname as first_name from customer". * <p/> * <p/> * Please note that this class is designed to provide convenience rather * than high performance. For best performance consider using a custom * RowMapper. * * @author Thomas Risberg * @author Juergen Hoeller * @author [qieqie.wang@gmail.com] * @since 2.5 */ public class BeanPropertyRowMapper implements RowMapper { /** * Logger available to subclasses */ protected final Log logger = LogFactory.getLog(getClass()); /** * The class we are mapping to */ private final Class<?> mappedClass; /** * Map of the fields we provide mapping for */ private Map<String, TableSchema.Column> mappedFields; private final boolean checkColumns; private final boolean checkProperties; /** * Set of bean properties we provide mapping for */ private Set<String> mappedProperties; /** * Create a new BeanPropertyRowMapper, accepting unpopulated properties * in the target bean. * * mappedClass the class that each row should be mapped to */ public BeanPropertyRowMapper(Class<?> mappedClass, boolean checkColumns, boolean checkProperties) { this.mappedClass = mappedClass; Assert.state(this.mappedClass != null, "Mapped class was not specified"); this.checkProperties = checkProperties; this.checkColumns = checkColumns; initialize(); } /** * Initialize the mapping metadata for the given class. */ protected void initialize() { this.mappedFields = new HashMap<String, TableSchema.Column>(); // PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); TableSchema schema = TableSchema.getSchema(mappedClass); TableSchema.Columns columns = schema.getColumns(); if (checkProperties) { mappedProperties = new HashSet<String>(); } TableSchema.Column pk = schema.getPk(); if (pk != null) { if (checkProperties) { this.mappedProperties.add(pk.getName()); } this.mappedFields.put(pk.getName().toLowerCase(), pk); } for (int i = 0; i < columns.size(); i++) { TableSchema.Column column = columns.get(i); if (column.getSetter() != null) { if (checkProperties) { this.mappedProperties.add(column.getName()); } this.mappedFields.put(column.getName().toLowerCase(), column); // for (String underscoredName : underscoreName(pd.getName())) { // if (underscoredName != null // && !pd.getName().toLowerCase().equals(underscoredName)) { // this.mappedFields.put(underscoredName, pd); // } // } } } } /** * Convert a name in camelCase to an underscored name in lower case. * Any upper case letters are converted to lower case with a preceding * underscore. * * camelCaseName the string containing original name * the converted name */ private String[] underscoreName(String camelCaseName) { StringBuilder result = new StringBuilder(); if (camelCaseName != null && camelCaseName.length() > 0) { result.append(camelCaseName.substring(0, 1).toLowerCase()); for (int i = 1; i < camelCaseName.length(); i++) { char ch = camelCaseName.charAt(i); if (Character.isUpperCase(ch)) { result.append("_"); result.append(Character.toLowerCase(ch)); } else { result.append(ch); } } } String name = result.toString(); // nameuser1_name2name2user_1_name_2 // user_1_name_2user1Name2 String name2 = null; boolean digitFound = false; for (int i = name.length() - 1; i >= 0; i--) { if (Character.isDigit(name.charAt(i))) { // ??continue,???continue digitFound = true; continue; } // ??? if (digitFound && i < name.length() - 1 && i > 0) { if (name2 == null) { name2 = name; } name2 = name2.substring(0, i + 1) + "_" + name2.substring(i + 1); } digitFound = false; } return new String[] { name, name2 }; } /** * Extract the values for all columns in the current row. * <p/> * Utilizes public setters and result set metadata. * * @see java.sql.ResultSetMetaData */ public Object mapRow(ResultSet rs, int rowNumber) throws SQLException { // spring's : Object mappedObject = BeanUtils.instantiateClass(this.mappedClass); // jade's : private Object instantiateClass(this.mappedClass); // why: ??mappedClass.newInstranceBeanUtils.instantiateClass(mappedClass)1? Object mappedObject = instantiateClass(this.mappedClass); BeanWrapper bw = new BeanWrapperImpl(mappedObject); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); boolean warnEnabled = logger.isWarnEnabled(); boolean debugEnabled = logger.isDebugEnabled(); Set<String> populatedProperties = (checkProperties ? new HashSet<String>() : null); for (int index = 1; index <= columnCount; index++) { String columnName = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase(); TableSchema.Column col = this.mappedFields.get(columnName); if (col != null) { try { Object value = JdbcUtils.getResultSetValue(rs, index, (Class<?>) col.getColumnType()); if (debugEnabled && rowNumber == 0) { logger.debug("Mapping column '" + columnName + "' to property '" + col.getName() + "' of type " + col.getType()); } if (col.isSerializable()) { if (col.getSerializer().getClass() != NullSerializer.class) { value = col.getSerializer().deserialize(value, col.getGenericType() != null ? col.getGenericType() : col.getType()); } else { Serializable instance = (Serializable) BeanUtils .instantiateClass((Class) col.getType()); instance.deserialize(value); value = instance; } } bw.setPropertyValue(col.getOriginName(), value); if (populatedProperties != null) { populatedProperties.add(col.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column " + columnName + " to property " + col.getOriginName(), ex); } } else { if (checkColumns) { throw new InvalidDataAccessApiUsageException("Unable to map column '" + columnName + "' to any properties of bean " + this.mappedClass.getName()); } if (warnEnabled && rowNumber == 0) { logger.warn("Unable to map column '" + columnName + "' to any properties of bean " + this.mappedClass.getName()); } } } if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties); } return mappedObject; } /** * clazz * * @throws BeanInstantiationException * @see {@link BeanUtils#instantiateClass(Class)} */ private static Object instantiateClass(Class<?> clazz) throws BeanInstantiationException { /*- spring's BeanUtils.instantiateClass() Assert.notNull(clazz, "Class must not be null"); if (clazz.isInterface()) { throw new BeanInstantiationException(clazz, "Specified class is an interface"); } try { return instantiateClass(clazz.getDeclaredConstructor((Class[]) null), null); } catch (NoSuchMethodException ex) { throw new BeanInstantiationException(clazz, "No default constructor found", ex); }*/ try { return clazz.newInstance(); } catch (Exception ex) { throw new BeanInstantiationException(clazz, ex.getMessage(), ex); } } }