Package com.centraview.common.source

Source Code of com.centraview.common.source.SourceEJB

/*
* $RCSfile: SourceEJB.java,v $    $Revision: 1.1.1.1 $  $Date: 2005/04/28 20:21:59 $ - $Author: mking_cv $
*
* The contents of this file are subject to the Open Software License
* Version 2.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.centraview.com/opensource/license.html
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is: CentraView Open Source.
*
* The developer of the Original Code is CentraView.  Portions of the
* Original Code created by CentraView are Copyright (c) 2004 CentraView,
* LLC; All Rights Reserved.  The terms "CentraView" and the CentraView
* logos are trademarks and service marks of CentraView, LLC.
*/

package com.centraview.common.source;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;

import com.centraview.common.CVDal;
import com.centraview.common.CVUtility;

/**
* SourceEJB.java
*
* @author
*/
public class SourceEJB implements EntityBean
{
  public EntityContext EJB_Context;
  private SourceVO svo;
  private SourceVO oldSourceVO;
  private boolean isDirty = false;
  private String dataSource = "MySqlDS";

  /**
   * Gets all data from Entity table only
   * see list below:
   * Name,EntityId,ModifiedBy.CreatedBy,Owner,
   * Created/Modified date, Source, ExternalId, DBase, List
   */
  public SourceVO getSourceVOBasic()
  {
    return this.svo;
  }

  /**
  * Finds if a given Entity exits in the database
  * @returns    EntityPK class (EJB clients get Remote)
  * @param  int  Entity ID
  */
  public SourcePK ejbFindByPrimaryKey(SourcePK primaryKey)
    throws FinderException
  {
    HashMap hm = getBasic(primaryKey);

    if (hm == null)
    {
      throw new FinderException("Could not find Source - " + primaryKey);
    }
    else
    {
      return primaryKey;
    }
  }

  /**
  * Sets the this.envo with fresh only basic Source data.
  */
  public void ejbLoad()
  {
    SourcePK spk = (SourcePK) (EJB_Context.getPrimaryKey());
    this.setDataSource(spk.getDataSource());

    HashMap hm = getBasic(spk);

    if (hm == null)
    {
      return;
    }
    else
    {
      setBasicVO(hm);
    }
  }

  /**
   * Creates a new Source.
   *
   * @param  source  SourceVO object to create.
   * @param  ds The datasource of the database.
   *
   * @return The SourcePK of the new Source object.
   */
  public SourcePK ejbCreate(SourceVO source, String ds)
    throws CreateException
  {
    int sourceId = 0;
    CVDal dl = new CVDal(ds);
    try
    {
      dl.setSql("common.addsource");
      dl.setInt(1, source.getSourceId());
      dl.setString(2, source.getName());
 
      dl.executeUpdate();
      sourceId = dl.getAutoGeneratedKey();
    } //end of try block
    catch (Exception e)
    {
      System.out.println("[Exception] SourceEJB.ejbCreate: " + e.toString());
    } //end of catch block (Exception)
    finally
    {
      dl.clearParameters();
      dl.destroy();
    } //end of finally block

    return new SourcePK(sourceId, ds);
  }

  /** EJB Container callback method. */
  public void ejbPostCreate(SourceVO source, String ds)
    throws CreateException
  {
    ejbLoad();
  }

  /** EJB Container callback method.  */
  public void ejbActivate()
  {
    //Not implemented.
  }

  /** EJB Container callback method. */
  public void ejbPassivate()
  {
    //Not Implemented.
  }

  public void ejbStore()
  {
    if (this.isDirty)
    {
      int sourceId = this.svo.getSourceId();

      CVDal dl = new CVDal(dataSource);
      dl.setSql("common.updatesource");

      dl.setString(1, this.svo.getName());
      dl.setInt(2, this.svo.getSourceId());

      dl.executeUpdate();

      dl.clearParameters();
      dl.destroy();
    }
  }

  /**
   * EJB Container callback method
   * Cascade deletes Entity
   *
  */
  public void ejbRemove()
    throws RemoveException
  {
    try
    {
      InitialContext ic = CVUtility.getInitialContext();
      CVDal dl = new CVDal(dataSource);

      dl.setSql("common.deletesource");
      dl.setInt(1, this.svo.getSourceId());

      dl.executeUpdate();
      dl.clearParameters();
      dl.destroy();
      dl = null;
    }
    catch (Exception e)
    {
      System.out.println("[Exception] SourceEJB.ejbRemove: " + e.toString());
      //e.printStackTrace();
    }
  } //end of ejbRemove

  public void setEntityContext(EntityContext ctx)
  {
    EJB_Context = ctx;
  }

  public void unsetEntityContext()
  {
    EJB_Context = null;
  }

  /**
  * Sets the current EntityVo with the new one.
  * The new EntityVO is saved in ejbStore
   * Do not forget to set AddressID and other ID's in their
  * respective VO's.
  *
   * @param  EntityVO  EntityVO object with new data
   *
  */
  public void setSourceVO(SourceVO sourceVO)
  {
    this.oldSourceVO = this.svo;
    this.svo = sourceVO;
    this.isDirty = true;
  }

  //Returns all the fields from the Entity table
  private HashMap getBasic(SourcePK primaryKey)
  {
    CVDal dl = new CVDal(primaryKey.getDataSource());
    dl.setSql("common.getsource");
    dl.setInt(1, primaryKey.getId());

    Collection col = dl.executeQuery();
    dl.destroy();

    Iterator it = col.iterator();

    if (!it.hasNext())
    {
      return null;
    }
    else
    {
      return (HashMap) it.next();
    }
  }

  //Updates the basic Entity data in the VO
  private void setBasicVO(HashMap hm)
  {
    this.svo = new SourceVO();
    Long tmp;
    tmp = (Long) (hm.get("SourceID"));

    if (tmp != null)
    {
      this.svo.setSourceId(tmp.intValue());
    }

    this.svo.setName((String) (hm.get("Name")));
  }

  /**
   * @author Kevin McAllister <kevin@centraview.com>
   * This simply sets the target datasource to be used for DB interaction
   * @param ds A string that contains the cannonical JNDI name of the datasource
   */
  public void setDataSource(String ds)
  {
    this.dataSource = ds;
  }
}
TOP

Related Classes of com.centraview.common.source.SourceEJB

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.