001    /*
002     @license.text@
003      */
004    package biz.hammurapi.sql.columns;
005    
006    import java.sql.PreparedStatement;
007    import java.sql.ResultSet;
008    import java.sql.ResultSetMetaData;
009    import java.sql.SQLException;
010    
011    import biz.hammurapi.config.Context;
012    import biz.hammurapi.convert.CompositeConverter;
013    
014    
015    /**
016     * @author Pavel Vlasov
017     *
018     * @version $Revision: 1.12 $
019     */
020    public class DoubleColumn extends Column {
021            private double value;
022            
023            // Original stuff       
024            private double originalValue;
025            private boolean isOriginalValueSet;
026            
027            public double getOriginalValue() {
028                    return isOriginalValueSet ? originalValue : value;
029            }
030            
031            public void parameterizeOriginal(PreparedStatement ps, int idx) throws SQLException {
032                    ps.setDouble(idx, getOriginalValue());
033            }
034            
035            public void setOriginal() {
036                    originalValue=value;
037                    isOriginalValueSet=true;
038            }
039            // End of original stuff
040            
041            public double getValue() {
042                    return value;
043            }
044            
045            public void setValue(double value) {
046                    if (force || this.value!=value) {
047                            this.value = value;
048                            onChange();
049                    }
050            }
051            
052            public DoubleColumn(String name, boolean isPrimaryKey) {
053                    super(name, isPrimaryKey);
054            }
055    
056            public DoubleColumn(String name, boolean isPrimaryKey, double value) {
057                    super(name, isPrimaryKey);
058                    this.value=value;
059            }
060            
061            public DoubleColumn(String name, boolean isPrimaryKey, ResultSet rs) throws SQLException {
062                    super(name, isPrimaryKey);
063                    ResultSetMetaData metaData = rs.getMetaData();
064                    for (int i=1, c=metaData.getColumnCount(); i<=c; i++) {
065                            if (name.equals(metaData.getColumnName(i))) {
066                                    this.value=rs.getDouble(i);
067                                    break;
068                            }
069                    }
070            }       
071            
072            protected void parameterizeInternal(PreparedStatement ps, int idx) throws SQLException {
073                    ps.setDouble(idx, value);
074            }
075    
076            public Object getObjectValue(boolean ov) {
077                    if (ov) {
078                            return isOriginalValueSet ? new Double(originalValue) : null;
079                    }
080                    return new Double(value);
081            }
082            
083            public String toString() {
084                    return getName()+(isModified() ? "*" : "")+"="+value;
085            }
086            
087        public boolean equals(Object otherColumn) {
088            if (otherColumn instanceof DoubleColumn) {
089                    return value==((DoubleColumn) otherColumn).value;
090            }
091            
092                    return false;
093        }           
094    
095            public int hashCode() {
096                    return getName().hashCode() ^ new Double(value).hashCode();
097            }
098            
099            /* (non-Javadoc)
100             * @see biz.hammurapi.sql.columns.Column#load(java.lang.String)
101             */
102            public void load(String textValue) {
103                    setValue(Double.parseDouble(textValue));
104            }       
105            
106            public void clear() {
107                    setValue(0);
108                    clearModified();
109            }
110            
111            public void configure(Context context, CompositeConverter converter) {
112                    Object o=context.get(getName());
113                    if (o!=null) {
114                            setValue(((Number) converter.convert(o, double.class, false)).doubleValue());
115                    }               
116            }
117            
118            protected String getAlignment() {
119                    return "right";
120            }       
121    
122            protected String getType() {
123                    return "double";
124            }       
125    
126            public void set(Column source) {
127                    setValue(((DoubleColumn) source).getValue());
128            }               
129            
130        /**
131         * Clears modified flag and sets original value to current value.
132         */
133        public void clearModified() {
134            super.clearModified();
135            originalValue=value;
136        }
137    }