/*
* $RCSfile: InventoryEJB.java,v $ $Revision: 1.1.1.1 $ $Date: 2005/04/28 20:21:26 $ - $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.inventory;
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.account.item.ItemVO;
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.contact.entity.EntityVO;
import com.centraview.contact.helper.CustomFieldVO;
import com.centraview.customfield.CustomFieldLocal;
import com.centraview.customfield.CustomFieldLocalHome;
public class InventoryEJB 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 to inventory
*
* @param individualID
* @param inventoryVO
* @exception InventoryException
*/
public InventoryVO insertInventory (int individualID, InventoryVO inventoryVO) throws InventoryException,AuthorizationFailedException
{
if(!CVUtility.isModuleVisible("Inventory",individualID, this.dataSource))
throw new AuthorizationFailedException("Inventory- insertInventory");
int pKey = 0 ;
CVDal cvdl = new CVDal(dataSource);
try
{
if (inventoryVO == null)
throw new InventoryException(InventoryException.INVALID_DATA,"Cannot add inventory");
cvdl.setSql("account.inventory.insertinventory");
cvdl.setString(1, inventoryVO.getStrIdentifier());
cvdl.setString(2, inventoryVO.getStrDescription());
cvdl.setInt(3, inventoryVO.getQty());
cvdl.setInt(4, inventoryVO.getIntLocationID());
ItemVO itemVO = inventoryVO.getItemVO();
cvdl.setInt(5,itemVO.getItemId());
cvdl.setInt(6, inventoryVO.getIntStatusID());
EntityVO soldToVO = inventoryVO.getSoldToVO();
cvdl.setInt(7,soldToVO.getContactID());
EntityVO vendorVO = inventoryVO.getVendorVO();
cvdl.setInt(8,vendorVO.getContactID());
cvdl.setInt(9,individualID);
cvdl.executeUpdate();
pKey = cvdl.getAutoGeneratedKey();
inventoryVO.setInventoryID(pKey);
// Save CustomFields
Vector custfieldVec = inventoryVO.getCustomFieldsVec();
InitialContext ic = CVUtility.getInitialContext();
if(custfieldVec != null && custfieldVec.size() != 0){
CustomFieldLocalHome custHome = (CustomFieldLocalHome)ic.lookup("local/CustomField");
CustomFieldLocal custRemote = custHome.create();
custRemote.setDataSource(this.dataSource);
for (int i =0;i<custfieldVec.size();i++)
{
CustomFieldVO custFieldVO = (CustomFieldVO)custfieldVec.get(i);
custFieldVO.setRecordID(pKey);
custRemote.addCustomField(custFieldVO);
}
}
// Save CustomFields ends here
AuthorizationLocalHome authorizationHome = (AuthorizationLocalHome)ic.lookup("local/Authorization");
AuthorizationLocal authorizationLocal = authorizationHome.create();
authorizationLocal.setDataSource(dataSource);
authorizationLocal.saveCurrentDefaultPermission("Inventory", pKey, individualID);
} catch(Exception e)
{
System.out.println("[Exception][InventoryEJB.insertInventory] Exception Thrown: "+e);
throw new InventoryException(InventoryException.INSERT_FAILED,"Failed in inventory ejb while adding inventory");
} finally {
cvdl.destroy();
cvdl = null;
}
inventoryVO = null;
inventoryVO = getInventory(individualID,pKey);
inventoryVO.setInventoryID(pKey);
return inventoryVO;
}
/**
* This method gets the details of the inventory
*
* @param individualID
* @param inventoryID
* @return
* @exception InventoryException
*/
public InventoryVO getInventory(int individualID, int inventoryID) throws InventoryException,AuthorizationFailedException
{
if(!CVUtility.isModuleVisible("Inventory",individualID, this.dataSource))
throw new AuthorizationFailedException("Inventory- getInventory");
InventoryVO inventoryVO;
CVDal cvdl = new CVDal(dataSource);
try
{
cvdl.setSql("account.inventory.getinventory");
cvdl.setInt(1,inventoryID);
Collection col = cvdl.executeQuery();
cvdl.clearParameters();
if (col == null)
throw new InventoryException(InventoryException.GET_FAILED,"Could not find Inventory : " + inventoryID);
Iterator it = col.iterator();
if (!it.hasNext())
throw new InventoryException(InventoryException.GET_FAILED,"Could not find Inventory : " + inventoryID);
HashMap hm = (HashMap)it.next();
inventoryVO = new InventoryVO();
if (hm != null)
{
if (hm.get("itemname") != null)
{
ItemVO itemVO = new ItemVO();
itemVO.setItemName((String)hm.get("itemname"));
if (hm.get("itemid") != null)
itemVO.setItemId(((Long)hm.get("itemid")).intValue());
inventoryVO.setItemVO(itemVO);
}
if(hm.get("qty") != null)
inventoryVO.setQty(((Long)hm.get("qty")).intValue());
if(hm.get("identifier") != null)
inventoryVO.setStrIdentifier((String)hm.get("identifier"));
if(hm.get("location") != null)
inventoryVO.setStrLocation((String)hm.get("location"));
if(hm.get("locationid") != null)
inventoryVO.setIntLocationID(((Long)hm.get("locationid")).intValue());
if (hm.get("manufacturer") != null)
{
EntityVO entityVO = new EntityVO();
entityVO.setName((String)hm.get("manufacturer"));
entityVO.setContactID(((Long)hm.get("manid")).intValue());
inventoryVO.setManufacturerVO(entityVO);
}
if (hm.get("vendor") != null)
{
EntityVO entityVO = new EntityVO();
entityVO.setName((String)hm.get("vendor"));
if (hm.get("vendorid") != null)
entityVO.setContactID(((Long)hm.get("vendorid")).intValue());
inventoryVO.setVendorVO(entityVO);
}
if(hm.get("description") != null)
inventoryVO.setStrDescription((String)hm.get("description"));
if(hm.get("created") != null)
inventoryVO.setCreated((java.sql.Date)hm.get("created"));
if(hm.get("modified") != null)
inventoryVO.setModified((java.sql.Timestamp)hm.get("modified"));
if(hm.get("status") != null)
inventoryVO.setStrStatus((String)hm.get("status"));
if(hm.get("statusid") != null)
inventoryVO.setIntStatusID(((Long)hm.get("statusid")).intValue());
if (hm.get("soldto") != null)
{
EntityVO entityVO = new EntityVO();
entityVO.setName((String)hm.get("soldto"));
if (hm.get("soldtoid") != null)
entityVO.setContactID(((Long)hm.get("soldtoid")).intValue());
inventoryVO.setSoldToVO(entityVO);
}
}
} catch(Exception e)
{
System.out.println("[Exception][InventoryEJB.getInventory] Exception Thrown: "+e);
e.printStackTrace();
throw new InventoryException(InventoryException.GET_FAILED,"Failed in ejb while Getting Inventory");
} finally {
cvdl.destroy();
cvdl = null;
}
return inventoryVO;
}
/**
* updated a inventory using InventoryVO
*
* @param individualID
* @param inventoryVO
* @exception InventoryException
*/
public void updateInventory(int individualID, InventoryVO inventoryVO) throws InventoryException,AuthorizationFailedException
{
if(!CVUtility.isModuleVisible("Inventory",individualID, this.dataSource))
throw new AuthorizationFailedException("Inventory- updateInventory");
CVDal cvdl = new CVDal(dataSource);
try
{
// begin :: adding for fieldlevel rights...
InventoryVO inventoryDBVO = getInventory(individualID,inventoryVO.getInventoryID());
inventoryVO = (InventoryVO)CVUtility.replaceVO(inventoryDBVO, inventoryVO, "Inventory", individualID, this.dataSource);
// end :: adding for fieldlevel rights...
if (inventoryVO == null)
throw new InventoryException(InventoryException.INVALID_DATA,"Cannot update InventoryVO.");
cvdl.setSql("account.inventory.updateinventory");
cvdl.setString(1, inventoryVO.getStrIdentifier());
cvdl.setString(2, inventoryVO.getStrDescription());
cvdl.setInt(3, inventoryVO.getQty());
cvdl.setInt(4, inventoryVO.getIntLocationID());
ItemVO itemVO = inventoryVO.getItemVO();
cvdl.setInt(5,itemVO.getItemId());
cvdl.setInt(6, inventoryVO.getIntStatusID());
EntityVO soldToVO = inventoryVO.getSoldToVO();
cvdl.setInt(7,soldToVO.getContactID());
cvdl.setInt(9,inventoryVO.getInventoryID());
EntityVO vendorVO = inventoryVO.getVendorVO();
cvdl.setInt(8,vendorVO.getContactID());
cvdl.executeUpdate();
int pKey = inventoryVO.getInventoryID();
// Save CustomFields
Vector custfieldVec = inventoryVO.getCustomFieldsVec();
InitialContext ic = CVUtility.getInitialContext();
CustomFieldLocalHome custHome = (CustomFieldLocalHome)ic.lookup("local/CustomField");
CustomFieldLocal custRemote = custHome.create();
custRemote.setDataSource(this.dataSource);
for (int i =0;i<custfieldVec.size();i++)
{
CustomFieldVO custFieldVO = (CustomFieldVO)custfieldVec.get(i);
custFieldVO.setRecordID(pKey);
custRemote.updateCustomField(custFieldVO);
}
// Save CustomFields ends here
cvdl.clearParameters();
} catch(Exception e)
{
System.out.println("[Exception][InventoryEJB.updateInventory] Exception Thrown: "+e);
//e.printStackTrace();
throw new InventoryException(InventoryException.INSERT_FAILED,"Failed in inventory ejb while updating inventory");
} finally {
cvdl.destroy();
cvdl = null;
}
}
/**
* This method deletes the inventory
*
* @param individualID
* @param inventoryID
*/
public void deleteInventory(int individualID, int inventoryID) throws InventoryException,AuthorizationFailedException
{
if(!CVUtility.isModuleVisible("Inventory",individualID, this.dataSource))
throw new AuthorizationFailedException("Inventory- deleteInventory");
CVDal cvdl = new CVDal(dataSource);
try
{
cvdl.setSql("account.inventory.deleteinventory");
cvdl.setInt(1,inventoryID);
cvdl.executeUpdate();
cvdl.clearParameters();
//Delete Custom Field
cvdl.setSql("contact.deletecustomfieldscalar");
cvdl.setString(1,"inventory");
cvdl.setInt(2,inventoryID);
cvdl.executeUpdate();
cvdl.clearParameters();
cvdl.setSql("contact.deletecustomfieldmultiple");
cvdl.setString(1,"inventory");
cvdl.setInt(2,inventoryID);
cvdl.executeUpdate();
cvdl.clearParameters();
}
catch(Exception e)
{
System.out.println("[Exception][InventoryEJB.deleteInventory] Exception Thrown: "+e);
e.printStackTrace();
throw new InventoryException(InventoryException.INSERT_FAILED,"Failed in inventory ejb while deleting inventory");
} finally {
cvdl.destroy();
cvdl = null;
}
}
/**
* @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;
}
}