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 IntColumn extends Column {
021            private int value;
022            
023            // Original stuff       
024            private int originalValue;
025            private boolean isOriginalValueSet;
026            
027            public int getOriginalValue() {
028                    return isOriginalValueSet ? originalValue : value;
029            }
030            
031            public void parameterizeOriginal(PreparedStatement ps, int idx) throws SQLException {
032                    ps.setInt(idx, getOriginalValue());
033            }
034            
035            public void setOriginal() {
036                    originalValue=value;
037                    isOriginalValueSet=true;
038            }
039            // End of original stuff
040            
041            public int getValue() {
042                    return value;
043            }
044            
045            public void setValue(int value) {
046                    if (force || this.value!=value) {
047                            this.value = value;
048                            onChange();
049                    }
050            }
051            
052            public IntColumn(String name, boolean isPrimaryKey) {
053                    super(name, isPrimaryKey);
054            }
055    
056            public IntColumn(String name, boolean isPrimaryKey, int value) {
057                    super(name, isPrimaryKey);
058                    this.value=value;
059            }       
060            
061            public IntColumn(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.getInt(i);
067                                    break;
068                            }
069                    }
070            }       
071            
072            protected void parameterizeInternal(PreparedStatement ps, int idx) throws SQLException {
073                    ps.setInt(idx, value);
074            }
075    
076            public Object getObjectValue(boolean ov) {
077                    if (ov) {
078                            return isOriginalValueSet ? new Integer(originalValue) : null;
079                    }
080                    return new Integer(value);
081            }
082            
083            public String toString() {
084                    return getName()+(isModified() ? "*" : "")+"="+value;
085            }
086            
087        public boolean equals(Object otherColumn) {
088            if (otherColumn instanceof IntColumn) {
089                    return value==((IntColumn) otherColumn).value;
090            }
091            
092                    return false;
093        }   
094    
095            public int hashCode() {
096                    return getName().hashCode() ^ value;
097            }
098    
099            public void load(String textValue) {
100                    setValue(Integer.parseInt(textValue));          
101            }
102            
103            public void clear() {
104                    setValue(0);
105                    clearModified();
106            }       
107            
108            public void configure(Context context, CompositeConverter converter) {
109                    Object o=context.get(getName());
110                    if (o!=null) {
111                            setValue(((Number) converter.convert(o, int.class, false)).intValue());
112                    }               
113            }               
114            
115            protected String getAlignment() {
116                    return "right";
117            }       
118    
119            protected String getType() {
120                    return "int";
121            }       
122    
123            public void set(Column source) {
124                    setValue(((IntColumn) source).getValue());
125            }               
126            
127        /**
128         * Clears modified flag and sets original value to current value.
129         */
130        public void clearModified() {
131            super.clearModified();
132            originalValue=value;
133        }
134    }