com.opensource.dbhelp.dbutils.CamelBeanProcessor.java Source code

Java tutorial

Introduction

Here is the source code for com.opensource.dbhelp.dbutils.CamelBeanProcessor.java

Source

package com.opensource.dbhelp.dbutils;

import java.beans.PropertyDescriptor;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Arrays;

import org.apache.commons.lang3.text.WordUtils;

/**
 * ?bean?
 * <p/>
 * ?dbutilsbeanProcessor?"mapColumnsToProperties" ??user_iduserIduser_iduser_id? ????user_iduserId?
 * <p/>
 * Copyright: Copyright (c) Feb 6, 2009 3:39:21 PM
 * <p/>
 * Company: GNU General Public License
 * <p/>
 * Author: GNU General Public License
 * <p/>
 * Version: 1.1
 * <p/>
 */
public class CamelBeanProcessor extends BeanProcessor {

    /**
     * 
     *
     * @param rsmd
     *            
     * @param props
     *            
     * @return ??
     */
    @Override
    protected int[] mapColumnsToProperties(ResultSetMetaData rsmd, PropertyDescriptor[] props) throws SQLException {

        int cols = rsmd.getColumnCount();
        int columnToProperty[] = new int[cols + 1];
        Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND);

        for (int col = 1; col <= cols; col++) {
            String columnName = rsmd.getColumnLabel(col);
            if (null == columnName || 0 == columnName.length()) {
                columnName = rsmd.getColumnName(col);
            }

            for (int i = 0; i < props.length; i++) {
                if (formatColName(columnName).equalsIgnoreCase(props[i].getName())) {
                    columnToProperty[col] = i;
                    break;
                }
                if (columnName.equalsIgnoreCase(props[i].getName())) {
                    columnToProperty[col] = i;
                    break;
                }
            }
        }

        return columnToProperty;
    }

    /**
     * ?user_idUSER_ID?userId
     *
     * @param name
     *            ??
     * @return ???
     */
    private String formatColName(String name) {
        if (name == null || "".equals(name)) {
            return "";
        }
        String rstr = name.toLowerCase();
        rstr = WordUtils.uncapitalize(WordUtils.capitalize(rstr, "_".toCharArray()));
        rstr = rstr.replaceAll("_", "");
        return rstr;
    }

}