Package com.centraview.account.item

Source Code of com.centraview.account.item.ItemEJB

/*
* $RCSfile: ItemEJB.java,v $    $Revision: 1.1.1.1 $  $Date: 2005/04/28 20:21:28 $ - $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.account.item;

import java.sql.Timestamp;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;

import com.centraview.administration.authorization.AuthorizationLocal;
import com.centraview.administration.authorization.AuthorizationLocalHome;
import com.centraview.common.AuthorizationFailedException;
import com.centraview.common.CVDal;
import com.centraview.common.CVUtility;
import com.centraview.common.DDNameValue;
import com.centraview.contact.entity.EntityVO;

/**
*
*/
public class ItemEJB implements SessionBean
{
  protected SessionContext ctx;
  private String dataSource = "MySqlDS";

  public void setSessionContext(SessionContext ctx)
  {
    this.ctx = ctx;
  }

  /**
   *
   */
  public void ejbCreate()
  {
  }

  /**
   *
   */
  public void ejbRemove()
  {
  }

  /**
   *
   */
  public void ejbActivate()
  {
  }

  /**
   *
   */
  public void ejbPassivate()
  {
  }

  /**
   * Adds a new item.
   *
   * @param  individualID  the individualID of the logged-in user attempting to create this Item.
   * @param  itemVO  the VO object defining the object to be created
   * @throws ItemException
   * @return int - the created ItemID or 0 for failure
   */
  public int addItem(int individualID, ItemVO itemVO) throws ItemException,AuthorizationFailedException
  {
    if (! CVUtility.isModuleVisible("Item", individualID, this.dataSource))
    {
      throw new AuthorizationFailedException("ItemEJB.addItem() for individualID = " + individualID);
    }

    int itemID = 0;

    CVDal cvdl = new CVDal(dataSource);

    try
    {
      if (itemVO == null)
      {
        throw new ItemException(ItemException.INVALID_DATA, "Cannot add item. ItemVO is empty.");
      } //end of if statement (itemVO == null)
    String linkToInventry = itemVO.getLinkToInventory();//cw
    linkToInventry = linkToInventry== null?"NO":linkToInventry;

      cvdl.setSql("item.insertitem");

      cvdl.setInt(1, itemVO.getItemTypeId());
      cvdl.setString(2, itemVO.getItemDesc());
      cvdl.setInt(3, itemVO.getTaxClassId());
      cvdl.setInt(4, itemVO.getSubItemOfId());
      cvdl.setDouble(5, itemVO.getPrice());
      cvdl.setDouble(6, itemVO.getCost());
      cvdl.setInt(7, itemVO.getCreatedBy());
      cvdl.setString(8, itemVO.getSku());
      cvdl.setInt(9, itemVO.getGlAccountId());
      cvdl.setInt(10, itemVO.getManufacturerid());
      cvdl.setInt(11, itemVO.getVendorid());
      cvdl.setString(12, itemVO.getItemName());
      cvdl.setString(13, itemVO.getExternalID());
      cvdl.setString(14, linkToInventry);

       cvdl.setInt(15, itemVO.getQtyOnBackOrder());
       cvdl.setInt(16, itemVO.getQtyOnOrder());

      cvdl.executeUpdate();

      itemID = cvdl.getAutoGeneratedKey();

      InitialContext ic = CVUtility.getInitialContext();
      AuthorizationLocalHome authorizationHome = (AuthorizationLocalHome)ic.lookup("local/Authorization");
      AuthorizationLocal authorizationLocal = authorizationHome.create();
      authorizationLocal.setDataSource(dataSource);
      authorizationLocal.saveCurrentDefaultPermission("Item", itemID, individualID);

    }catch(Exception e){
      System.out.println("[Exception][ItemEJB.addItem] Exception Thrown: " + e);
      //e.printStackTrace();
      throw new ItemException(ItemException.INSERT_FAILED, "Failed in item ejb while adding item");
    }finally{
      cvdl.destroy();
      cvdl = null;
    }   // end try block

    if (itemID == 0)
    {
      throw new ItemException(ItemException.INVALID_DATA, "Cannot add item. itemID is 0 after y.");
    }   // end if (itemId == 0)

    return itemID;
  }   // end addItem() method

  /**
   * This method gets the details of an Item from the database.
   * @param userId The individualID of the user requesting the information.
   * @param itemId The itemID of the item we are asking about.
   */
  public ItemVO getItem(int userId, int itemId) throws ItemException,AuthorizationFailedException
  {
    if (! CVUtility.isModuleVisible("Item",userId, this.dataSource))
    {
      throw new AuthorizationFailedException("Item- getItem");
    }

    ItemVO itemVO;
    CVDal cvdl = new CVDal(dataSource);

    try
    {
      cvdl.setSql("item.getitem");
      cvdl.setInt(1, itemId);
      Collection col = cvdl.executeQuery();

      if (col == null)
      {
        throw new ItemException(ItemException.GET_FAILED, "Could not find Item : " + itemId);
      }

      Iterator it = col.iterator();
      if (! it.hasNext())
      {
        throw new ItemException(ItemException.GET_FAILED, "Could not find Item : " + itemId);
      }

      HashMap hm = (HashMap) it.next();

      itemVO = new ItemVO();
      itemVO.setItemId(((Long)hm.get("itemid")).intValue());
      itemVO.setItemDesc((String)hm.get("description"));
      itemVO.setItemTypeId(((Long)hm.get("type")).intValue());
      itemVO.setTaxClassId(((Long)hm.get("taxclass")).intValue());
      itemVO.setSubItemOfId(((Long)hm.get("parent")).intValue());
      itemVO.setPrice(Double.parseDouble(hm.get("listprice").toString()));
      itemVO.setCost(Double.parseDouble(hm.get("cost").toString()));
      itemVO.setSku((String) hm.get("sku"));
      itemVO.setGlAccountId(((Long)hm.get("glaccountid")).intValue());

      itemVO.setManufacturerid(((Long)hm.get("manufacturerid")).intValue());
      EntityVO entityVO = new EntityVO();
      entityVO.setName((String)hm.get("manufacturername"));
      itemVO.setManufacturerVO(entityVO);

      itemVO.setVendorid(((Long)hm.get("vendorid")).intValue());
      entityVO = new EntityVO();
      entityVO.setName((String)hm.get("vendorname"));
      itemVO.setVendorVO(entityVO);

      itemVO.setQtyOnBackOrder(((Long)hm.get("qtyonbackorder")).intValue());
      itemVO.setQtyOnOrder(((Long)hm.get("qtyonorder")).intValue());
      itemVO.setQtyOnHand(((Integer)hm.get("qtyonhand")).intValue());


      itemVO.setItemName((String)hm.get("title"));
      itemVO.setCreatedOn((Timestamp)hm.get("createddate"));
      itemVO.setModifiedOn((Timestamp)hm.get("modifieddate"));
      itemVO.setExternalID((String)hm.get("externalid"));

      String linkToInventry = (String)hm.get("LinkToInventory");//cw
      linkToInventry = linkToInventry == null ? "NO" : linkToInventry;
      itemVO.setLinkToInventory(linkToInventry);
    }catch(Exception e){
      System.out.println("[Exception][ItemEJB.getItem] Exception Thrown: "+e);
      e.printStackTrace();
      throw new ItemException(ItemException.GET_FAILED, "Failed in file ejb while Getting Item");
    }finally{
      cvdl.destroy();
      cvdl = null;
    }
    return itemVO;
  }   // end getItem() method

  /**
   * updated a item using ItemVO
   *
   * @param userId
   * @param itemVO
   * @exception ItemException
   */
  public void updateItem(int userId, ItemVO itemVO) throws ItemException,AuthorizationFailedException
  {
    if(!CVUtility.isModuleVisible("Item",userId, this.dataSource))
      throw new AuthorizationFailedException("Item- updateItem");
    CVDal cvdl = new CVDal(dataSource);
    try
    {
      // begin :: adding for fieldlevel rights...
      ItemVO itemDBVO = getItem(userId,itemVO.getItemId());
      itemVO = (ItemVO)CVUtility.replaceVO(itemDBVO, itemVO, "Item", userId, this.dataSource);
      // end :: adding for fieldlevel rights...
      if (itemVO == null)
      {
        throw new ItemException(ItemException.INVALID_DATA,
            "Cannot update folder. ItemVO is empty.");
      } //end of if statement (itemVO == null)
    String linkToInventry = itemVO.getLinkToInventory();//cw
    linkToInventry = linkToInventry== null?"NO":linkToInventry;
      cvdl.setSql("item.updateitem");
      cvdl.setString(1, itemVO.getItemDesc());
      cvdl.setInt(2, itemVO.getItemTypeId());
      cvdl.setInt(3, itemVO.getTaxClassId());
      cvdl.setInt(4, itemVO.getSubItemOfId());
      cvdl.setDouble(5, itemVO.getPrice());
      cvdl.setDouble(6, itemVO.getCost());
      cvdl.setInt(7, userId);
      cvdl.setInt(8, itemVO.getGlAccountId());
      cvdl.setInt(9, itemVO.getManufacturerid());
      cvdl.setInt(10, itemVO.getVendorid());
      cvdl.setString(11, itemVO.getItemName());
      cvdl.setString(12, itemVO.getSku());
      cvdl.setString(13,linkToInventry);
      cvdl.setInt   (14, itemVO.getQtyOnBackOrder());
      cvdl.setInt   (15, itemVO.getQtyOnOrder());
      cvdl.setInt   (16, itemVO.getItemId());

      cvdl.executeUpdate();
    } //end of try block
    catch (Exception e)
    {
      System.out.println("[Exception][ItemEJB.updateItem] Exception Thrown: "+e);
      e.printStackTrace();
      throw new ItemException(ItemException.INSERT_FAILED,
          "Failed in item ejb while updating item");
    } //end of catch block (Exception)
    finally
    {
      cvdl.destroy();
      cvdl = null;
    } //end of finally block
  } //end of updateItem method

  /**
   * This method deletes the item
   *
   * @param userId
   * @param itemId
   */
  public void deleteItem(int userId,int itemId)
  {
    CVDal cvdl = new CVDal(dataSource);
    try
    {
      cvdl.setSql("item.deleteitem");
      cvdl.setInt(1, itemId);
      cvdl.executeUpdate();
    } //end of try block
    catch (Exception e)
    {
      System.out.println("[Exception][ItemEJB.deleteItem] Exception Thrown: "+e);
      e.printStackTrace();
    } //end of catch block (Exception)
    finally
    {
      cvdl.destroy();
      cvdl = null;
    } //end of finally block
  } //end of deleteItem method

  /*
   * Returns vector containing item item
   */
  public Vector getItemType()
  {
    Vector vec = new Vector();
    CVDal dl = new CVDal(dataSource);
    try
    {
      dl.setSql("item.getitemtype");
      Collection itemTypeCollection = dl.executeQuery();
      Iterator itemTypeIterator = itemTypeCollection.iterator();
      while (itemTypeIterator.hasNext())
      {
        HashMap itemTypeHM = (HashMap) itemTypeIterator.next();
        DDNameValue itemTypeDD =
          new DDNameValue(((Long) itemTypeHM.get("itemtypeid")).intValue(), (String) itemTypeHM.get("title"));
        vec.add(itemTypeDD);
      } //end of while loop (itemTypeIterator.hasNext())
    } //end of try block
    catch (Exception e)
    {
      System.out.println("[Exception][ItemEJB.getItemType] Exception Thrown: "+e);
      e.printStackTrace();
    } //end of catch block (Exception)
    finally
    {
      dl.destroy();
      dl = null;
    } //end of finally block
    return vec;
  } //end of getItemType method

  /*
   * Returns vector containing gl account
   */
  public Vector getGLAccountType()
  {
    Vector vec = new Vector();
    CVDal dl = new CVDal(dataSource);
    try
    {
      dl.setSql("item.getglaccounttype");
      Collection glAccountCollection = dl.executeQuery();
      Iterator glAccountIterator = glAccountCollection.iterator();
      while (glAccountIterator.hasNext())
      {
        HashMap glAccountHM = (HashMap) glAccountIterator.next();
        DDNameValue glAccountDD =
          new DDNameValue(((Long) glAccountHM.get("GLAccountsID")).intValue(), (String) glAccountHM.get("Title"));
        vec.add(glAccountDD);
      } //end of while loop (glAccountIterator.hasNext())
    } //end of try block
    catch (Exception e)
    {
      System.out.println("[Exception][ItemEJB.getGLAccountType] Exception Thrown: "+e);
      e.printStackTrace();
    } //end of catch block (Exception)
    finally
    {
      dl.destroy();
      dl = null;
    } //end of finally block
    return vec;
  } //end of getGLAccountType method

  /*
   * Returns vector containing tax class
   */
  public Vector getTaxClassType()
  {
    Vector vec = new Vector();
    CVDal dl = new CVDal(dataSource);
    try
    {
      dl.setSql("item.gettaxclasstype");
      Collection taxClassCollection = dl.executeQuery();
      Iterator taxClassIterator = taxClassCollection.iterator();
      while (taxClassIterator.hasNext())
      {
        HashMap taxClassHM = (HashMap) taxClassIterator.next();
        DDNameValue taxClassDD =
          new DDNameValue(((Long) taxClassHM.get("taxclassid")).intValue(), (String) taxClassHM.get("title"));
        vec.add(taxClassDD);
      } //end of while loop (taxClassIterator.hasNext())
    } //end of try block
    catch (Exception e)
    {
      System.out.println("[Exception][ItemEJB.getTaxClassType] Exception Thrown: "+e);
      e.printStackTrace();
    } //end of catch block (Exception)
    finally
    {
      dl.destroy();
      dl = null;
    } //end of finally block
    return vec;
  } //end of getTaxClassType method

  /*
   * Returns sku code if present
   */
  public ItemVO checkSKUCode(String sku)
  {
    ItemVO itemVO = new ItemVO();
    itemVO.setItemId(0);
    CVDal dl = new CVDal(dataSource);
    try
    {
      dl.setSql("item.getiteminfo");
      dl.setString(1, sku);
      Collection col = (Collection) dl.executeQuery();
      Iterator it = col.iterator();
      while (it.hasNext())
      {
        HashMap hm = (HashMap) it.next();
        itemVO.setItemId(((Long) hm.get("itemid")).intValue());
      } //end of while loop (it.hasNext())
    } //end of try block
    catch (Exception e)
    {
      System.out.println("[Exception][ItemEJB.checkSKUCode] Exception Thrown: "+e);
      e.printStackTrace();
    } //end of catch block (Exception)
    finally
    {
      dl.destroy();
      dl = null;
    } //end of finally block
    return itemVO;
  } //end of checkSKUCode method

  /**
   * @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 name of the datasource.
   */
  public void setDataSource(String ds) {
    this.dataSource = ds;
  }

} //end of ItemEJB class
TOP

Related Classes of com.centraview.account.item.ItemEJB

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.