/* Copyright (c) 2005 Ricebridge. All Rights Reserved.
 *
 * This file is available under the terms and conditions of the
 * Ricebridge "Open Source API" policy; Ricebridge grants use of this
 * copyrighted work under the terms of a BSD-style license only. See
 * http://www.opensource.org/licenses/bsd-license.php for more
 * information.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *  - Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above
 *  copyright notice, this list of conditions and the following
 *  disclaimer in the documentation and/or other materials provided
 *  with the distribution.
 *
 *  - Neither the name of the Ricebridge nor the names of its
 *  contributors may be used to endorse or promote products derived
 *  from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.  
 */

package com.ricebridge.xmlman;


import com.ricebridge.data.BeanSpec;
import com.ricebridge.data.DataException;

import org.jostraca.util.Internal;

import java.lang.reflect.Method;

import java.util.*;


/** Simple implementation of {@link RecordProvider} that provides data records for saving
 *  a <code>List</code> of <a href="http://java.sun.com/products/javabeans">Java Beans</a>.
 *    <p>This class is used to implement the {@link XmlManager#saveBeans(File,RecordSpec,BeanSpec,List) saveBeans(*,RecordSpec,BeanSpec)} 
 *  methods in {@link XmlManager}.
 *  Data records are created from Java Bean objects by calling the property <i>get</i> methods
 *  of the objects in accordance with the Java Beans specification. Ordinary, boolean and indexed properties are supported.
 *  See the {@link BeanSpec} documentation for more information.</p>
 *    <p><code>BeanRecordProvider</code> is designed to be subclassed. You can change the default implementation by calling the 
 *  {@link com.ricebridge.xmlman.in.XmlManagerStoreBase#setBeanRecordProviderClass setBeanRecordProviderClass} method of 
 *  {@link com.ricebridge.xmlman.in.XmlManagerStore}, and providing a subclass of <code>BeanRecordProvider</code>. 
 *  <code>XmlManagerStore</code> can be accessed using {@link XmlManager#getXmlManagerStore}.</p>
 *    <p>The <b><a href="BeanRecordProvider.java.html">Source Code</a></b> of this Java class 
 *    is available under a <a href="http://www.opensource.org/licenses/bsd-license.php">BSD-style license</a>.</p>
 */
public class BeanRecordProvider extends RecordProviderSupport {

  /** Specify that invalid data fields have the default value for their data type
   *  (name for {@link XmlSpec#setProperty XmlSpec.setProperty}: <code>Bean.useDefault</code>). */
  public static final String PROP_Bean_useDefault = "Bean.useDefault";


  // protected instance

  /** <code>List</code> of Beans. */
  protected List iBeans = null;

  /** Index of current bean in <code>List</code>. */
  protected int iBeanIndex = 0;

  /** {@link BeanSpec} specification object defining the properties of the beans to save. */
  protected BeanSpec iBeanSpec = null;

  /** If a data field is invalid, use the default value of the property for that field data type. */
  protected boolean iUseDefault = false;


  
  // constructors

  /** Create uninitialised for use with {@link #setBeans}. */
  public BeanRecordProvider() {
    // init with setData
  }


  /** Create using a <code>List</code> of <a href="http://java.sun.com/products/javabeans">Java Beans</a> to save. */
  public BeanRecordProvider( List pData ) {
    setBeans( iBeans );
  }



  // public methods

  /** Set the <a href="http://java.sun.com/products/javabeans">Java Beans</a> to save. */
  public void setBeans( List pBeans ) {
    iBeans = (List) Internal.null_list( pBeans );
  }

  
  /** Set the {@link BeanSpec} object that defines the beans to save. 
   *    <p>The <code>BeanSpec</code> object contains the definitions of the 
   *  bean properties and performs the conversion from internal Java data types to <code>String</code> values.</p>
   *  @param pBeanSpec {@link BeanSpec} definition of beans to save
   */
  public void setBeanSpec( BeanSpec pBeanSpec ) {
    iBeanSpec = (BeanSpec) Internal.null_arg( pBeanSpec );
  }



  // RecordProviderSupport methods

  /** Handle property settings for saving <a href="http://java.sun.com/products/javabeans">Java Beans</a>. 
   *    <p>You can change these using {@link XmlSpec}.</p>
   *  @see RecordListener#setXmlSpec
   *  @see #PROP_Bean_useDefault
   */
  protected void setXmlSpecImpl( XmlSpec pXmlSpec ) {
    iUseDefault = pXmlSpec.getBooleanProperty( PROP_Bean_useDefault );
  }


  /** Start the load process.
   *   <p>Performs some sanity checks on the saving setup.</p>
   */
  public void startProcessImpl() {
    Internal.null_state( iFieldNames );
    Internal.null_array( iFieldNames );
    Internal.null_state( iBeanSpec );
    iBeanIndex = 0;
  }


  /** Check if there are any beans left. */
  public boolean hasNextRecordImpl() {
    return iBeanIndex < iBeans.size();
  }


  /** Return data fields of next bean as a <code>String[]</code> array. */
  public String[] nextRecordImpl() {
    Object   bean   = iBeans.get( iBeanIndex );
    Class    bc     = bean.getClass();
    String[] record = new String[iFieldNames.length];
    for( int fI = 0; fI < iFieldNames.length; fI++ ) {
      try {
        record[fI] = iBeanSpec.getStringValue( bean, iFieldNames[fI], iUseDefault );
      }
      catch( XmlManagerException xme ) { throw xme; }
      catch( DataException de ) {
        throw new XmlManagerException( XmlManagerException.CODE_getbeanfield_msg, 
                                       new String[] {"bean-class", null==bc?"[unknown]":bc.getName(),
                                                     "field-name",iFieldNames[fI],
                                                     "datamsg", de.getUserMessage() } );
      }
      catch( Exception e ) {
        throw new XmlManagerException( XmlManagerException.CODE_getbeanfield, 
                                       new String[] {"bean-class", null==bc?"[unknown]":bc.getName(),
                                                     "field-name",iFieldNames[fI] }, e );
      }
    }

    iBeanIndex++;
    return record;
  }


}
Syntax Highlighting created using the com.Ostermiller.Syntax package.
Thursday, February 23 2006 at 16:47