Clover coverage report - Ledge Components - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:13:20 CET
file stats: LOC: 653   Methods: 30
NCLOC: 346   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultOutputRecord.java 78.3% 81.3% 70% 79%
coverage coverage
 1    //
 2    //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 3    //All rights reserved.
 4    //
 5    //Redistribution and use in source and binary forms, with or without modification,
 6    //are permitted provided that the following conditions are met:
 7    //
 8    //* Redistributions of source code must retain the above copyright notice,
 9    //this list of conditions and the following disclaimer.
 10    //* Redistributions in binary form must reproduce the above copyright notice,
 11    //this list of conditions and the following disclaimer in the documentation
 12    //and/or other materials provided with the distribution.
 13    //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 14    //nor the names of its contributors may be used to endorse or promote products
 15    //derived from this software without specific prior written permission.
 16    //
 17    //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 18    //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19    //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 20    //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 21    //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 22    //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 23    //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 24    //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25    //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26    //POSSIBILITY OF SUCH DAMAGE.
 27    //
 28   
 29    package org.objectledge.database.persistence;
 30   
 31    import java.math.BigDecimal;
 32    import java.net.URL;
 33    import java.sql.Array;
 34    import java.sql.Blob;
 35    import java.sql.Clob;
 36    import java.sql.Connection;
 37    import java.sql.PreparedStatement;
 38    import java.sql.Ref;
 39    import java.sql.SQLException;
 40    import java.sql.Time;
 41    import java.sql.Timestamp;
 42    import java.util.Arrays;
 43    import java.util.Date;
 44    import java.util.HashMap;
 45    import java.util.HashSet;
 46    import java.util.Iterator;
 47    import java.util.Set;
 48   
 49    /**
 50    * An implementation of {@link DefaultOutputRecord}.
 51    *
 52    * TODO get rid of sun.misc.Base64Encoder
 53    */
 54    public class DefaultOutputRecord implements OutputRecord
 55    {
 56    /** The persistent object. */
 57    private Persistent object;
 58   
 59    /** The fields. */
 60    private HashMap<String,Object> fields;
 61   
 62    /**
 63    * Constructs an <code>OutputRecordImpl</code>.
 64    *
 65    * @param object a Presistent object.
 66    */
 67  4416 public DefaultOutputRecord(Persistent object)
 68    {
 69  4416 fields = new HashMap<String,Object>();
 70  4416 this.object = object;
 71    }
 72   
 73    /**
 74    * Sets a <code>boolean</code> field value.
 75    *
 76    * @param field the name of the field.
 77    * @param value the value of the field.
 78    * @throws PersistenceException if the field could not be set to the
 79    * specified value.
 80    */
 81  966 public void setBoolean(String field, boolean value)
 82    throws PersistenceException
 83    {
 84  966 fields.put(field, value ? Boolean.TRUE : Boolean.FALSE);
 85    }
 86   
 87    /**
 88    * Sets a <code>byte</code> field value.
 89    *
 90    * @param field the name of the field.
 91    * @param value the value of the field.
 92    * @throws PersistenceException if the field could not be set to the
 93    * specified value.
 94    */
 95  230 public void setByte(String field, byte value)
 96    throws PersistenceException
 97    {
 98  230 fields.put(field, new Byte(value));
 99    }
 100   
 101    /**
 102    * Sets a <code>short</code> field value.
 103    *
 104    * @param field the name of the field.
 105    * @param value the value of the field.
 106    * @throws PersistenceException if the field could not be set to the
 107    * specified value.
 108    */
 109  230 public void setShort(String field, short value)
 110    throws PersistenceException
 111    {
 112  230 fields.put(field, new Short(value));
 113    }
 114   
 115    /**
 116    * Sets an <code>int</code> field value.
 117    *
 118    * @param field the name of the field.
 119    * @param value the value of the field.
 120    * @throws PersistenceException if the field could not be set to the
 121    * specified value.
 122    */
 123  736 public void setInteger(String field, int value)
 124    throws PersistenceException
 125    {
 126  736 fields.put(field, new Integer(value));
 127    }
 128   
 129    /**
 130    * Sets a <code>long</code> field value.
 131    *
 132    * @param field the name of the field.
 133    * @param value the value of the field.
 134    * @throws PersistenceException if the field could not be set to the
 135    * specified value.
 136    */
 137  5566 public void setLong(String field, long value)
 138    throws PersistenceException
 139    {
 140  5566 fields.put(field, new Long(value));
 141    }
 142   
 143    /**
 144    * Sets a <code>BigDecimal</code> field value.
 145    *
 146    * @param field the name of the field.
 147    * @param value the value of the field.
 148    * @throws PersistenceException if the field could not be set to the
 149    * specified value.
 150    */
 151  230 public void setBigDecimal(String field, BigDecimal value)
 152    throws PersistenceException
 153    {
 154  230 if(value == null)
 155    {
 156  230 setNull(field);
 157  230 return;
 158    }
 159  0 fields.put(field, value);
 160    }
 161   
 162    /**
 163    * Sets a <code>float</code> field value.
 164    *
 165    * @param field the name of the field.
 166    * @param value the value of the field.
 167    * @throws PersistenceException if the field could not be set to the
 168    * specified value.
 169    */
 170  230 public void setFloat(String field, float value)
 171    throws PersistenceException
 172    {
 173  230 fields.put(field, new Float(value));
 174    }
 175   
 176    /**
 177    * Sets a <code>double</code> field value.
 178    *
 179    * @param field the name of the field.
 180    * @param value the value of the field.
 181    * @throws PersistenceException if the field could not be set to the
 182    * specified value.
 183    */
 184  0 public void setDouble(String field, double value)
 185    throws PersistenceException
 186    {
 187  0 fields.put(field, new Double(value));
 188    }
 189   
 190    /**
 191    * Sets a <code>String</code> field value.
 192    *
 193    * @param field the name of the field.
 194    * @param value the value of the field.
 195    * @throws PersistenceException if the field could not be set to the
 196    * specified value.
 197    */
 198  8832 public void setString(String field, String value)
 199    throws PersistenceException
 200    {
 201  8832 if(value == null)
 202    {
 203  0 setNull(field);
 204  0 return;
 205    }
 206  8832 fields.put(field, value);
 207    }
 208   
 209    /**
 210    * Sets a <code>byte</code> array field value.
 211    *
 212    * <p>String value read from the database will be BASE64 decoded to obtain
 213    * byte array.</p>
 214    *
 215    * @param field the name of the field.
 216    * @param value the value of the field.
 217    * @throws PersistenceException if the field could not be set to the
 218    * specified value.
 219    */
 220  0 public void setBytes(String field, byte[] value)
 221    throws PersistenceException
 222    {
 223  0 try
 224    {
 225  0 sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
 226  0 String encoded = encoder.encodeBuffer(value);
 227  0 fields.put(field, encoded);
 228    }
 229    catch(Exception e)
 230    {
 231  0 throw new PersistenceException("Failed to encode field "+field, e);
 232    }
 233    }
 234   
 235    /**
 236    * Sets a <code>Date</code> field value.
 237    *
 238    * @param field the name of the field.
 239    * @param value the value of the field.
 240    * @throws PersistenceException if the field could not be set to the
 241    * specified value.
 242    */
 243  230 public void setDate(String field, Date value)
 244    throws PersistenceException
 245    {
 246  230 if(value == null)
 247    {
 248  46 setNull(field);
 249  46 return;
 250    }
 251  184 fields.put(field, new java.sql.Date(value.getTime()));
 252    }
 253   
 254    /**
 255    * Sets a <code>Time</code> field value.
 256    *
 257    * @param field the name of the field.
 258    * @param value the value of the filed.
 259    * @throws PersistenceException if the field could not be set to the
 260    * specified value.
 261    */
 262  230 public void setTime(String field, Date value)
 263    throws PersistenceException
 264    {
 265  230 if(value == null)
 266    {
 267  230 setNull(field);
 268  230 return;
 269    }
 270  0 fields.put(field, new Time(value.getTime()));
 271    }
 272   
 273    /**
 274    * Sets a <code>Timestamp</code> field value.
 275    *
 276    * @param field the name of the field.
 277    * @param value the value of the filed.
 278    * @throws PersistenceException if the field could not be set to the
 279    * specified value.
 280    */
 281  230 public void setTimestamp(String field, Date value)
 282    throws PersistenceException
 283    {
 284  230 if(value == null)
 285    {
 286  230 setNull(field);
 287  230 return;
 288    }
 289  0 fields.put(field, new Timestamp(value.getTime()));
 290    }
 291   
 292    /**
 293    * Sets a <code>Array</code> field value.
 294    *
 295    * @param field the name of the field.
 296    * @param value the value of the filed.
 297    * @throws PersistenceException if the field could not be set to the
 298    * specified value.
 299    */
 300  0 public void setArray(String field, Array value)
 301    throws PersistenceException
 302    {
 303  0 fields.put(field, value);
 304    }
 305   
 306    /**
 307    * Sets a <code>Blob</code> field value.
 308    *
 309    * @param field the name of the field.
 310    * @param value the value of the filed.
 311    * @throws PersistenceException if the field could not be set to the
 312    * specified value.
 313    */
 314  0 public void setBlob(String field, Blob value)
 315    throws PersistenceException
 316    {
 317  0 fields.put(field, value);
 318    }
 319   
 320    /**
 321    * Sets a <code>Clob</code> field value.
 322    *
 323    * @param field the name of the field.
 324    * @param value the value of the filed.
 325    * @throws PersistenceException if the field could not be set to the
 326    * specified value.
 327    */
 328  0 public void setClob(String field, Clob value)
 329    throws PersistenceException
 330    {
 331  0 fields.put(field, value);
 332    }
 333   
 334    /**
 335    * Sets a <code>Ref</code> field value.
 336    *
 337    * @param field the name of the field.
 338    * @param value the value of the filed.
 339    * @throws PersistenceException if the field could not be set to the
 340    * specified value.
 341    */
 342  0 public void setRef(String field, Ref value)
 343    throws PersistenceException
 344    {
 345  0 fields.put(field, value);
 346    }
 347   
 348    /**
 349    * Sets a <code>URL</code> field value.
 350    *
 351    * @param field the name of the field.
 352    * @param value the value of the filed.
 353    * @throws PersistenceException if the field could not be set to the
 354    * specified value.
 355    */
 356  0 public void setURL(String field, URL value)
 357    throws PersistenceException
 358    {
 359  0 fields.put(field, value);
 360    }
 361   
 362    /**
 363    * Sets a <code>Object</code> field value.
 364    *
 365    * @param field the name of the field.
 366    * @param value the value of the filed.
 367    * @throws PersistenceException if the field could not be set to the
 368    * specified value.
 369    */
 370  0 public void setObject(String field, Object value)
 371    throws PersistenceException
 372    {
 373  0 fields.put(field, value);
 374    }
 375   
 376    /**
 377    * Sets a field to <code>SQL NULL</code> value.
 378    *
 379    * @param field the name of the field.
 380    * @throws PersistenceException if the field could not be set to the
 381    * specified value.
 382    */
 383  2208 public void setNull(String field)
 384    throws PersistenceException
 385    {
 386  2208 fields.put(field, null);
 387    }
 388   
 389    // Implementation specific ///////////////////////////////////////////
 390   
 391    /**
 392    * Builds <code>WHERE</code> clause with contained data.
 393    *
 394    * @return the where clause.
 395    * @throws PersistenceException if the clause could not be built.
 396    */
 397  644 public String getWhereClause()
 398    throws PersistenceException
 399    {
 400  644 Set keyFields = getKeyFields();
 401  644 StringBuilder buff = new StringBuilder();
 402  644 for(Iterator i = fields.keySet().iterator(); i.hasNext();)
 403    {
 404  5520 String field = (String)i.next();
 405  5520 if(keyFields.contains(field))
 406    {
 407  1012 buff.append(field);
 408  1012 Object value = fields.get(field);
 409  1012 buff.append(value == null ? " IS " : " = ");
 410  1012 appendValueString(buff, value);
 411  1012 buff.append(" AND ");
 412    }
 413    }
 414    // remove trailing " AND "
 415  644 buff.setLength(buff.length() - 5);
 416  644 return buff.toString();
 417    }
 418   
 419    /**
 420    * Builds an insert statement with contained data.
 421    *
 422    * @param conn database connection.
 423    * @return the statement.
 424    * @throws PersistenceException if the statement could not be built.
 425    * @throws SQLException if the statement could not be created.
 426    */
 427  3772 public PreparedStatement getInsertStatement(Connection conn)
 428    throws PersistenceException, SQLException
 429    {
 430  3772 StringBuilder buff = new StringBuilder();
 431  3772 StringBuilder buff2 = new StringBuilder();
 432  3772 buff.append("INSERT INTO ");
 433  3772 buff.append(object.getTable());
 434  3772 buff.append(" (");
 435  3772 for(Iterator i = fields.keySet().iterator(); i.hasNext();)
 436    {
 437  13018 String field = (String)i.next();
 438  13018 buff.append(field);
 439  13018 Object value = fields.get(field);
 440  13018 appendValueString(buff2, value);
 441  13018 if(i.hasNext())
 442    {
 443  9246 buff.append(", ");
 444  9246 buff2.append(", ");
 445    }
 446    }
 447  3772 buff.append(") VALUES (");
 448  3772 buff.append(buff2.toString());
 449  3772 buff.append(")");
 450  3772 PreparedStatement stmt = conn.prepareStatement(buff.toString());
 451  3772 setValues(stmt, true, true);
 452  3772 return stmt;
 453    }
 454   
 455    /**
 456    * Builds an update statement with contained data.
 457    *
 458    * @param conn database connection.
 459    * @return the statement.
 460    * @throws PersistenceException if the statement could not be built.
 461    * @throws SQLException if the statement could not be created.
 462    */
 463  230 public PreparedStatement getUpdateStatement(Connection conn)
 464    throws PersistenceException, SQLException
 465    {
 466  230 Set keyFields = getKeyFields();
 467  230 StringBuilder buff = new StringBuilder();
 468  230 buff.append("UPDATE ");
 469  230 buff.append(object.getTable());
 470  230 buff.append(" SET ");
 471  230 Iterator i = fields.keySet().iterator();
 472  230 while(i.hasNext())
 473    {
 474  2852 String field = (String)i.next();
 475  2852 if(!keyFields.contains(field))
 476    {
 477  2622 buff.append(field);
 478  2622 buff.append(" = ");
 479  2622 Object value = fields.get(field);
 480  2622 appendValueString(buff, value);
 481  2622 buff.append(", ");
 482    }
 483    }
 484    // remove superfluous ", "
 485  230