BusinessObjectList.java :  » Web-Framework » JAT » com » jat » business » Java Open Source

Java Open Source » Web Framework » JAT 
JAT » com » jat » business » BusinessObjectList.java
package com.jat.business;

import java.io.IOException;
import java.util.Enumeration;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import com.jat.core.config.Config;
import com.jat.core.log.LogManager;
import com.jat.integration.DataSource;
import com.jat.integration.DataSourceFactory;
import com.jat.integration.IntegrationException;
import com.jat.util.OrderVector;
import com.jat.util.xml.XMLUtil;
import com.jat.util.xml.XMLable;
import java.io.Serializable;

/**
 * <p>Title: JAT</p>
 * <p>Description: This class is a list of Business Object ({@link com.jat.business.BusinessObject}).
 * <br>Is possible to sort the Business Objects by {@link orderBy} method
 * or get the XML format of an instance of this class ({@link toXML} and {@link getXmlDocument} methods).
 * </p>
 * <p>
 * If you want to build your own Business Object, you must create:
 * <ul>
 * <li>The Business Object extending {@link com.jat.business.BusinessObject} class</li>
 * <li>The Business Object Factory extending {@link com.jat.business.BusinessObjectFactory} class</li>
 * <li>The Business Object List object extending {@link com.jat.business.BusinessObjectList} class</li>
 * </ul>
 * </p>
 * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
 * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
 * @author stf
 * @version 1.2
 * @since 1.0
 * @see com.com.jat.business.BusinessObject
 * @see com.com.jat.business.BusinessObjectFactory
 */

public abstract class BusinessObjectList implements XMLable, Serializable {

  public BusinessObjectList(String dataSourceName) {
  this.dataSourceName = dataSourceName;
  this.events = new OrderVector();
  }

  public BusinessObjectList(String dataSourceName, BusinessObjectPropertyList props) {
  this(dataSourceName);
  for(Enumeration e = props.elements(); e.hasMoreElements(); ) {
    BusinessObjectProperties prop = (BusinessObjectProperties)e.nextElement();
    BusinessObject item = this.getBusinessObject(prop);
    this.addElement(item);
  }
  }

  public BusinessObject firstElement() {
  return(BusinessObject)this.events.firstElement();
  }

  public BusinessObject elementAt(int index) {
  return(BusinessObject)this.events.elementAt(index);
  }

  public Enumeration elements() {
  return this.events.elements();
  }

  public int size() {
  return this.events.size();
  }

  public void orderBy(String field, boolean ascendent) throws Exception {
  if(ascendent)
    this.events = OrderVector.orderAscendent(this.events, field);
  else
    this.events = OrderVector.orderDescendent(this.events, field);
  }

  public String getDataSourceName() {
  return this.dataSourceName;
  }

  /**
   * Execute the configured operation (execName) for all Business Object in the list
   * and return the result list of each pperation
   * @param execName the name of an operation configured
   * @return the list of {@link  com.jat.business.BusinessObjectProperties}
   * containing the exection result for each operation
   * @throws BusinessException if an exception occours
   */
  public BusinessObjectPropertyList persist(String execName) throws BusinessException {
  try {
    MultiBusinessObjectProperties multi = new MultiBusinessObjectProperties();
    for(Enumeration e = this.elements(); e.hasMoreElements(); )
    multi.put(execName, (BusinessObject)e.nextElement());
    return DataSourceFactory.getFactory().getDataSource(this.getDataSourceName()).execute(multi);
  } catch(IntegrationException ex) {
    throw new BusinessException(ex);
  }
  }

  /**
   * Refresh the list of {@link com.jat.business.BusinessObject} reloading from the same data source
   * and using the same parameters (query name and properties) of the current instance was loaded.
   * Before use this method be sure that this object has been instanced throw a {@link com.jat.business.BusinessObjectFactory} object,
   * otherwise a {@link com.jat.business.BusinessException} will be thrown.
   * @return the {@link com.jat.business.BusinessObjectList} reloaded from the data source
   * @throws BusinessException if this class has not been instanced throw a {@link com.jat.business.BusinessObjectFactory} object
   * @throws IntegrationException whenever an Integration error occours
   */
  public BusinessObjectList refresh() throws BusinessException, IntegrationException {
  if(this.requestName==null)
    throw new BusinessException(this.getClass().getName()+"::refresh: Operation name is not defined: impossible to refresh the list");
  BusinessObjectList bol = this.getBusinessObjectList(this.factory, this.getDataSourceName(), this.requestName, this.requestProperties);
  this.events = bol.events;
  return bol;
  }

  /**
   * Return a list of business object loaded from a data source by an operation using the specific properties
   * @param factory the {@link com.jat.business.BusinessObjectFactory} instance
   * @param dataSourceName the configured data source (see {@link com.jat.integration.DataSource} configuration)
   * @param name the operation name of the data source (see {@link com.jat.integration.DataSource} configuration)
   * @param properties the list of property used by the specified operation. It can be null.
   * @return the list of Business Object matching with properties in the operation of the data source
   * @throws java.lang.Exception if an error occours
   */
  public static BusinessObjectList getBusinessObjectList(
    BusinessObjectFactory factory,
    String dataSourceName,
    String name,
    BusinessObjectProperties properties) throws BusinessException, IntegrationException {
  if(factory==null)
    throw new BusinessException("Business Factory cannot be null");
  DataSource ds = DataSourceFactory.getFactory().getDataSource(dataSourceName);
  if(ds==null)
    throw new BusinessException("Data Source '"+dataSourceName+"' not found");
  BusinessObjectPropertyList props = ds.getData(name, properties);
  BusinessObjectList bol = factory.getBusinessObjectList(dataSourceName, props);
  bol.setRequestProperties(factory, name, properties);
  return bol;
  }

  public void addElement(BusinessObject item) {
  //this.remove(item);
  this.events.addAscElement(item);
  LogManager.sendDebug(this.getClass().getName()+":: addElement: event: "+item.toString());
  }

  public String toString() {
  String ret = getClass().getName()+": ";
  for(Enumeration e = this.elements(); e.hasMoreElements(); ) {
    ret += "{"+e.nextElement().toString()+"}";
  }
  return ret;
  }

  public boolean remove(BusinessObject item) {
  if(item==null)
    return false;
  return this.events.removeElement(item);
  }

  public void add(BusinessObjectList list) {
  for(Enumeration e = list.elements(); e.hasMoreElements(); )
    this.events.add(e.nextElement());
  this.events = OrderVector.orderAscendent(this.events);
  }

  protected void setRequestProperties(BusinessObjectFactory factory, String requestName, BusinessObjectProperties requestProperties) {
  this.factory = factory;
  this.requestName = requestName;
  this.requestProperties = requestProperties;
  }

  /**
   * return a Business Object from a set of properties.
   * <br/>Implement this method to build a new instance of your Business Object
   * using the constructor with data source name and {@link com.jat.businessBusinessObjectProperties}
   * @param props the list of properties
   * @return a new instance of a Business Object implementation
   */
  protected abstract BusinessObject getBusinessObject(BusinessObjectProperties props);

  /* From XMLable interface */
  public Document getXmlDocument(boolean validate) throws SAXException, ParserConfigurationException, IOException {
  String xml = this.toXML();
  return XMLUtil.getXMLDocument(xml, validate, false).getDocument();
  }

  public String toXML() {
  initXml();
  String xml = "";
  if(schema!=null&&!schema.equals(""))
    xml = "<"+list+" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\""+schema+"\">";
  else
    xml = "<"+list+">";
  for(Enumeration e = this.elements(); e.hasMoreElements(); ) {
    BusinessObject obj = (BusinessObject)e.nextElement();
    xml += obj.toXML();
  }
  xml += "</"+list+">";
  return xml;
  }

  private void initXml() {
  try {
    list = Config.getCurrent().getValue("xml_object", "list");
  } catch(Exception ex) {
    list = "list";
    LogManager.sendWarning(this.getClass().getName()+"::toXML: exception: "+ex);
  }
  try {
    schema = Config.getCurrent().getValue("xml_object", "schema");
  } catch(Exception ex) {
    schema = "";
    LogManager.sendWarning(this.getClass().getName()+"::toXML: exception: "+ex);
  }
  }

  static String list = null;
  static String schema = null;

  private OrderVector events;
  private String dataSourceName;
  private BusinessObjectProperties requestProperties = null;
  private BusinessObjectFactory factory = null;
  private String requestName = null;

  /** @link dependency
   * @stereotype instantiate
   * @label objects*/
  /*# BusinessObject lnkBusinessObject; */
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.