001    /*
002    @license.text@
003     */
004    package biz.hammurapi.sql;
005    
006    import java.sql.ResultSet;
007    import java.sql.ResultSetMetaData;
008    import java.sql.SQLException;
009    import java.util.HashMap;
010    import java.util.Map;
011    
012    import biz.hammurapi.config.ConfigurationException;
013    import biz.hammurapi.config.DomConfigFactory;
014    import biz.hammurapi.config.MapContext;
015    
016    /**
017     * Projects fields from result set to object properties (fields or setters)
018     * @author Pavel Vlasov
019     * @version $Revision: 1.3 $
020     */
021    public class PropertyProjector extends BaseReflectionProjector implements Projector {
022            private Class objectClass;
023            private Map fieldMap;
024            
025            /**
026             * Constructor
027             * @param objectClass Class to instantiate. Must have no-argument public constructor
028             * @param typeMap {@link java.sql.ResultSet#getObject(java.lang.String, java.util.Map)}
029             * @param fieldMap Field map. Can be null. If value of some mapping is null then that field
030             * is suppressed and is not set.
031             * @param lenient If it is set to false than exception will be thrown if property 
032             * to set does not exist.
033             * @param toLowerCase If set to true then database field names will be converted to lower case
034             * for field/setters discovery.
035             */
036            public PropertyProjector(Class objectClass, Map typeMap, Map fieldMap) {
037                    super(typeMap);
038                    this.objectClass=objectClass;
039                    this.fieldMap=fieldMap;
040            }               
041    
042            /**
043             * Instantiates object using default constructors and then sets properties.
044             */
045            public Object project(ResultSet rs) throws SQLException {               
046                    try {
047                            Object instance = objectClass.newInstance();
048                            ResultSetMetaData metaData = rs.getMetaData();
049                            int cc=metaData.getColumnCount();
050                            Map contextMap=new HashMap();
051                            for (int i=1; i<=cc; i++) {
052                                    String colName=metaData.getColumnName(i);
053                                    String propertyName=propertyName(colName);
054                                    
055                                    if (fieldMap!=null && fieldMap.containsKey(propertyName)) {
056                                            propertyName=(String) fieldMap.get(propertyName);
057                                    }
058                                    
059                                    if (propertyName!=null) {
060                                            contextMap.put(propertyName, getColumn(rs, colName));
061                                    }
062                            }
063                            DomConfigFactory.inject(instance, new MapContext(contextMap));
064                            return instance;
065                    } catch (InstantiationException e) {
066                            throw new SQLExceptionEx(e);
067                    } catch (IllegalAccessException e) {
068                            throw new SQLExceptionEx(e);
069                    } catch (ConfigurationException e) {
070                            throw new SQLExceptionEx(e);
071                    }
072            }
073    
074    }