001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.sql.columns;
024:
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.ResultSetMetaData;
028: import java.sql.SQLException;
029:
030: import biz.hammurapi.config.Context;
031: import biz.hammurapi.convert.CompositeConverter;
032:
033: /**
034: * @author Pavel Vlasov
035: *
036: * @version $Revision: 1.12 $
037: */
038: public class BooleanColumn extends Column {
039: private boolean value;
040:
041: // Original stuff
042: private boolean originalValue;
043: private boolean isOriginalValueSet;
044:
045: public boolean getOriginalValue() {
046: return isOriginalValueSet ? originalValue : value;
047: }
048:
049: public void parameterizeOriginal(PreparedStatement ps, int idx)
050: throws SQLException {
051: ps.setBoolean(idx, getOriginalValue());
052: }
053:
054: public void setOriginal() {
055: originalValue = value;
056: isOriginalValueSet = true;
057: }
058:
059: // End of original stuff
060:
061: public boolean getValue() {
062: return value;
063: }
064:
065: public void setValue(boolean value) {
066: if (force || this .value != value) {
067: this .value = value;
068: onChange();
069: }
070: }
071:
072: public BooleanColumn(String name, boolean isPrimaryKey) {
073: super (name, isPrimaryKey);
074: }
075:
076: public BooleanColumn(String name, boolean isPrimaryKey, ResultSet rs)
077: throws SQLException {
078: super (name, isPrimaryKey);
079: ResultSetMetaData metaData = rs.getMetaData();
080: for (int i = 1, c = metaData.getColumnCount(); i <= c; i++) {
081: if (name.equals(metaData.getColumnName(i))) {
082: this .value = rs.getBoolean(i);
083: break;
084: }
085: }
086: }
087:
088: public BooleanColumn(String name, boolean isPrimaryKey,
089: boolean value) {
090: super (name, isPrimaryKey);
091: this .value = value;
092: }
093:
094: protected void parameterizeInternal(PreparedStatement ps, int idx)
095: throws SQLException {
096: ps.setBoolean(idx, value);
097: }
098:
099: public Object getObjectValue(boolean ov) {
100: if (ov) {
101: return isOriginalValueSet ? new Boolean(originalValue)
102: : null;
103: }
104: return new Boolean(value);
105: }
106:
107: public String toString() {
108: return getName() + (isModified() ? "*" : "") + "=" + value;
109: }
110:
111: public boolean equals(Object otherColumn) {
112: if (otherColumn instanceof BooleanColumn) {
113: return value == ((BooleanColumn) otherColumn).value;
114: }
115:
116: return false;
117: }
118:
119: public int hashCode() {
120: return getName().hashCode() ^ (value ? 0 : 1);
121: }
122:
123: /**
124: * @param textValue "YES", "yes", "TRUE", "true" yield true. All other values yield false.
125: */
126: public void load(String textValue) {
127: setValue("yes".equalsIgnoreCase(textValue)
128: || "true".equalsIgnoreCase(textValue));
129: }
130:
131: public void clear() {
132: setValue(false);
133: clearModified();
134: }
135:
136: public void configure(Context context, CompositeConverter converter) {
137: Object o = context.get(getName());
138: if (o != null) {
139: setValue(((Boolean) converter.convert(o, boolean.class,
140: false)).booleanValue());
141: }
142:
143: }
144:
145: protected String getType() {
146: return "boolean";
147: }
148:
149: public void set(Column source) {
150: setValue(((BooleanColumn) source).getValue());
151: }
152:
153: /**
154: * Clears modified flag and sets original value to current value.
155: */
156: public void clearModified() {
157: super.clearModified();
158: originalValue = value;
159: }
160:
161: }
|