001package com.hfg.sql.table.field;
002
003import com.hfg.sql.jdbc.JDBCException;
004import com.hfg.sql.table.DatabaseCol;
005import com.hfg.sql.table.DatabaseTable;
006import com.hfg.util.BooleanUtil;
007import com.hfg.xml.XMLTag;
008
009import java.sql.PreparedStatement;
010import java.sql.ResultSet;
011import java.sql.SQLException;
012import java.sql.Types;
013
014//------------------------------------------------------------------------------
015/**
016 Database field that manages a boolean value. Values stored in the database as 'Y'/'N' char.
017 <div>
018 @author J. Alex Taylor, hairyfatguy.com
019 </div>
020 */
021//------------------------------------------------------------------------------
022// com.hfg XML/HTML Coding Library
023//
024// This library is free software; you can redistribute it and/or
025// modify it under the terms of the GNU Lesser General Public
026// License as published by the Free Software Foundation; either
027// version 2.1 of the License, or (at your option) any later version.
028//
029// This library is distributed in the hope that it will be useful,
030// but WITHOUT ANY WARRANTY; without even the implied warranty of
031// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
032// Lesser General Public License for more details.
033//
034// You should have received a copy of the GNU Lesser General Public
035// License along with this library; if not, write to the Free Software
036// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
037//
038// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
039// jataylor@hairyfatguy.com
040//------------------------------------------------------------------------------
041
042public class DatabaseBooleanField extends DatabaseField<Boolean>
043{
044   //###########################################################################
045   // CONSTRUCTORS
046   //###########################################################################
047
048   //---------------------------------------------------------------------------
049   public DatabaseBooleanField(DatabaseCol inCol)
050   {
051      this(inCol, (Object) null);
052   }
053   
054   //---------------------------------------------------------------------------
055   public DatabaseBooleanField(DatabaseCol inCol, Boolean inValue)
056   {
057      super(inCol, inValue);
058   }
059
060   //---------------------------------------------------------------------------
061   public DatabaseBooleanField(DatabaseCol inCol, Object inValue)
062   {
063      super(inCol, convertToBoolean(inValue));
064   }
065
066   //---------------------------------------------------------------------------
067   public DatabaseBooleanField(DatabaseCol inCol, ResultSet inResultSet)
068   {
069      super(inCol, inResultSet);
070   }
071
072   //---------------------------------------------------------------------------
073   public DatabaseBooleanField(XMLTag inXMLTag, DatabaseTable inTable)
074   {
075      super(inXMLTag, inTable);
076   }
077
078   //###########################################################################
079   // PUBLIC METHODS
080   //###########################################################################
081
082   //---------------------------------------------------------------------------
083   protected void setValueFromResultSet(ResultSet inResultSet)
084   {
085      // Retrieve the index for the ResultSet column with the matching name.
086      // If no column with the proper name is present in the ResultSet, don't do anything.
087      Integer index = getColIndex(inResultSet);
088      if (index != null)
089      {
090         try
091         {
092            String stringValue = inResultSet.getString(index);
093            setInitialValue(BooleanUtil.valueOf(stringValue));
094         }
095         catch (SQLException e)
096         {
097            try
098            {
099               throw new JDBCException("Problem mapping " + getCol().name() + " value: " + inResultSet.getString(getCol().name()) + "!", e);
100            }
101            catch (SQLException e2)
102            {
103               throw new JDBCException("Problem mapping " + getCol().name() + " value!", e);
104            }
105         }
106      }
107   }
108
109   //---------------------------------------------------------------------------
110   public void setValueInPreparedStatement(PreparedStatement inPreparedStatement, int inIndex)
111   {
112      try
113      {
114         if (isNull())
115         {
116            inPreparedStatement.setNull(inIndex, Types.CHAR);
117         }
118         else
119         {
120            inPreparedStatement.setString(inIndex, getSQLValue());
121         }
122      }
123      catch (SQLException e)
124      {
125         throw new JDBCException("Problem setting column " + getCol().name() + " value into PreparedStatement!", e);
126      }
127   }
128
129   //---------------------------------------------------------------------------
130   public void setValueFromString(String inValue)
131   {
132      setValue(convertToBoolean(inValue));
133   }
134
135   //---------------------------------------------------------------------------
136   @Override
137   public String getSQLValue()
138   {
139      return getValue() != null ? "'" + (getValue() ? 'Y' : 'N') + "'" : "null";
140   }
141
142   //###########################################################################
143   // PRIVATE METHODS
144   //###########################################################################
145
146   //---------------------------------------------------------------------------
147   private static Boolean convertToBoolean(Object inValue)
148   {
149      Boolean booleanValue = null;
150      if (inValue != null)
151      {
152         if (inValue instanceof String)
153         {
154            booleanValue = BooleanUtil.valueOf((String)inValue);
155         }
156         else if (inValue instanceof Character)
157         {
158            booleanValue = BooleanUtil.valueOf((Character)inValue);
159         }
160         else if (inValue instanceof Integer)
161         {
162            booleanValue = BooleanUtil.valueOf((Integer)inValue);
163         }
164      }
165
166      return booleanValue;
167   }
168}