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 BooleanColumn extends Column { 021 private boolean value; 022 023 // Original stuff 024 private boolean originalValue; 025 private boolean isOriginalValueSet; 026 027 public boolean getOriginalValue() { 028 return isOriginalValueSet ? originalValue : value; 029 } 030 031 public void parameterizeOriginal(PreparedStatement ps, int idx) throws SQLException { 032 ps.setBoolean(idx, getOriginalValue()); 033 } 034 035 public void setOriginal() { 036 originalValue=value; 037 isOriginalValueSet=true; 038 } 039 // End of original stuff 040 041 public boolean getValue() { 042 return value; 043 } 044 045 public void setValue(boolean value) { 046 if (force || this.value!=value) { 047 this.value = value; 048 onChange(); 049 } 050 } 051 052 public BooleanColumn(String name, boolean isPrimaryKey) { 053 super(name, isPrimaryKey); 054 } 055 056 public BooleanColumn(String name, boolean isPrimaryKey, ResultSet rs) throws SQLException { 057 super(name, isPrimaryKey); 058 ResultSetMetaData metaData = rs.getMetaData(); 059 for (int i=1, c=metaData.getColumnCount(); i<=c; i++) { 060 if (name.equals(metaData.getColumnName(i))) { 061 this.value=rs.getBoolean(i); 062 break; 063 } 064 } 065 } 066 067 public BooleanColumn(String name, boolean isPrimaryKey, boolean value) { 068 super(name, isPrimaryKey); 069 this.value=value; 070 } 071 072 protected void parameterizeInternal(PreparedStatement ps, int idx) throws SQLException { 073 ps.setBoolean(idx, value); 074 } 075 076 public Object getObjectValue(boolean ov) { 077 if (ov) { 078 return isOriginalValueSet ? new Boolean(originalValue) : null; 079 } 080 return new Boolean(value); 081 } 082 083 public String toString() { 084 return getName()+(isModified() ? "*" : "")+"="+value; 085 } 086 087 public boolean equals(Object otherColumn) { 088 if (otherColumn instanceof BooleanColumn) { 089 return value==((BooleanColumn) otherColumn).value; 090 } 091 092 return false; 093 } 094 095 public int hashCode() { 096 return getName().hashCode() ^ (value ? 0 : 1); 097 } 098 099 /** 100 * @param textValue "YES", "yes", "TRUE", "true" yield true. All other values yield false. 101 */ 102 public void load(String textValue) { 103 setValue("yes".equalsIgnoreCase(textValue) || "true".equalsIgnoreCase(textValue)); 104 } 105 106 public void clear() { 107 setValue(false); 108 clearModified(); 109 } 110 111 public void configure(Context context, CompositeConverter converter) { 112 Object o=context.get(getName()); 113 if (o!=null) { 114 setValue(((Boolean) converter.convert(o, boolean.class, false)).booleanValue()); 115 } 116 117 } 118 119 protected String getType() { 120 return "boolean"; 121 } 122 123 public void set(Column source) { 124 setValue(((BooleanColumn) 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 135 }