/*
* $RCSfile: PurchaseOrderEJB.java,v $ $Revision: 1.1.1.1 $ $Date: 2005/04/28 20:21:31 $ - $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.purchaseorder;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.ejb.CreateException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.centraview.account.common.ItemElement;
import com.centraview.account.common.ItemLines;
import com.centraview.administration.authorization.AuthorizationLocal;
import com.centraview.administration.authorization.AuthorizationLocalHome;
import com.centraview.common.CVDal;
import com.centraview.common.CVUtility;
import com.centraview.common.FloatMember;
import com.centraview.common.IntMember;
import com.centraview.common.StringMember;
public class PurchaseOrderEJB implements EntityBean
{
protected javax.ejb.EntityContext ctx;
protected Context environment;
private PurchaseOrderVO purchaseorderVO;
private boolean isDirty = false; //update data if isDirty is true (ejbStore)
private String dataSource = "MySqlDS";
/*
* Create PurchaseOrder record in Database
*/
public PurchaseOrderPK ejbCreate(PurchaseOrderVO purchaseorderVO,int userID, String ds)
{
int purchaseorderID = 0;
this.dataSource = ds;
CVDal dl = new CVDal(ds);
try
{
ItemLines itemLines = purchaseorderVO.getItemLines();
itemLines.calculate();
dl.setSql("purchaseorder.addpurchaseorder");
dl.setInt(1,purchaseorderVO.getBillToId());//billaddress
dl.setInt(2,purchaseorderVO.getShipToId());//shipaddress
dl.setInt(3,purchaseorderVO.getStatusId()); // statusid
dl.setInt(4,purchaseorderVO.getTermId());// termid
dl.setInt(5,purchaseorderVO.getAccountManagerId()); //accmanager
dl.setInt(6,userID);
dl.setDate(7,purchaseorderVO.getPurchaseOrderDate()); // purchaseorderDate DateObject
dl.setFloat(8,itemLines.getSubtotal()); // sub total
dl.setFloat(9,itemLines.getTax()); //tax
dl.setFloat(10,itemLines.getTotal()); // total
dl.setFloat(11,purchaseorderVO.getVendorId()); // vendor id
dl.setString(12,purchaseorderVO.getNotes()); //description
if (purchaseorderVO.getExternalID() != null)
dl.setString(13,purchaseorderVO.getExternalID());
else
dl.setString(13,"0");
dl.setInt(14,userID); //Owner
dl.executeUpdate();
purchaseorderID = dl.getAutoGeneratedKey();
this.purchaseorderVO =purchaseorderVO;
purchaseorderVO.setPurchaseOrderId(purchaseorderID);
dl.clearParameters();
Set listkey = itemLines.keySet();
Iterator it = listkey.iterator();
while (it.hasNext())
{
Object str = ( Object )it.next();
ItemElement ele = ( ItemElement)itemLines.get( str );
this.addPurchaseOrderItem(ele,purchaseorderID);
}// end of while
InitialContext ic = CVUtility.getInitialContext();
AuthorizationLocalHome authorizationHome = (AuthorizationLocalHome)ic.lookup("local/Authorization");
AuthorizationLocal authorizationLocal = authorizationHome.create();
authorizationLocal.setDataSource(ds);
authorizationLocal.saveCurrentDefaultPermission("PurchaseOrder", purchaseorderID, userID);
}
catch(Exception e)
{
System.out.println("[Exception][PurchaseOrderEJB.ejbCreate] Exception Thrown: "+e);
e.printStackTrace();
}
finally
{
dl.destroy();
dl = null;
}
return new PurchaseOrderPK(purchaseorderID, ds);
}// end of ejbCreate
/*
* Add PurchaseOrderItem record in Database
*/
private void addPurchaseOrderItem(ItemElement ele ,int purchaseorderID)
{
CVDal dl = new CVDal(this.dataSource);
try
{
IntMember lineid = ( IntMember )ele.get("LineId");
IntMember itemid = ( IntMember )ele.get("ItemId");
StringMember sku = ( StringMember )ele.get("SKU");
IntMember qty = ( IntMember )ele.get("Quantity");
FloatMember priceEach = ( FloatMember )ele.get("Price");
StringMember desc = ( StringMember )ele.get("Description");
String status = ele.getLineStatus();
int quantity = ((Number) qty.getMemberValue()).intValue();
float price = ((Number) priceEach.getMemberValue()).floatValue();
int itemID = ((Number)itemid.getMemberValue()).intValue();
dl.setSql("purchaseorder.addpurchaseorderitem");
dl.setInt(1,purchaseorderID);
dl.setInt(2,itemID );
dl.setInt(3,quantity);
dl.setFloat(4,((Float)priceEach.getMemberValue()).floatValue());
dl.setString(5,(String)sku.getMemberValue());
dl.setString(6,(String)desc.getMemberValue());
dl.executeUpdate();
dl.clearParameters();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
dl.destroy();
dl = null;
}
}// end of addPurchaseOrderItem
/*
* Update PurchaseOrderItem record in Database
*/
private void updatePurchaseOrderItem(ItemElement ele ,int purchaseorderID)
{
CVDal dl = new CVDal(this.dataSource);
try
{
IntMember lineid = ( IntMember )ele.get("LineId");
IntMember itemid = ( IntMember )ele.get("ItemId");
StringMember sku = ( StringMember )ele.get("SKU");
IntMember qty = ( IntMember )ele.get("Quantity");
FloatMember priceEach = ( FloatMember )ele.get("Price");
StringMember desc = ( StringMember )ele.get("Description");
String status = ele.getLineStatus();
int quantity = ((Number) qty.getMemberValue()).intValue();
float price = ((Number) priceEach.getMemberValue()).floatValue();
int itemID = ((Number)itemid.getMemberValue()).intValue();
dl.setSql("purchaseorder.updatepurchaseorderitem");
dl.setInt(1, itemID);
dl.setInt(2,quantity);
dl.setFloat(3,price);
dl.setString(4,(String)sku.getMemberValue());
dl.setString(5,(String)desc.getMemberValue());
dl.setInt(6,((Integer)lineid.getMemberValue()).intValue() );
dl.setInt(7,purchaseorderID);
dl.executeUpdate();
dl.clearParameters();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
dl.destroy();
dl = null;
}
}// end of updatePurchaseOrderItem
/*
* Update PurchaseOrderItem record in Database
* Mark as delete
*/
private void markDeleatedPurchaseOrderItem(ItemElement ele ,int purchaseorderID)
{
CVDal dl = new CVDal(this.dataSource);
try
{
IntMember lineid = ( IntMember )ele.get("LineId");
String status = ele.getLineStatus();
dl.setSql("purchaseorder.markdeletedpurchaseorderitem");
dl.setString(1,status);
dl.setInt(2,((Integer)lineid.getMemberValue()).intValue() );
dl.setInt(3,purchaseorderID);
dl.executeUpdate();
dl.clearParameters();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
dl.destroy();
dl = null;
}
}// end of markDeleatedPurchaseOrderItem
/**
* EJB Container callback method
*/
public void ejbPostCreate (PurchaseOrderVO purchaseorderVO,int userID, String ds) throws CreateException
{
}
/**
* Finds if a given PurchaseOrder exits in the database
* @returns PurchaseOrderPK class (EJB clients get Remote)
* @param int PurchaseOrderID
*/
public PurchaseOrderPK ejbFindByPrimaryKey(PurchaseOrderPK primaryKey) throws FinderException
{
HashMap hm = getBasic(primaryKey);
if (hm == null)
{
throw new FinderException("Could not find PurchaseOrder: " + primaryKey);
}
else
{
return primaryKey;
}
}// end of ejbFindByPrimaryKey
/*
* get Basic PurchaseOrder
*/
private HashMap getBasic(PurchaseOrderPK primaryKey)
{
CVDal dl = new CVDal(primaryKey.getDataSource());
dl.setSql("purchaseorder.getpurchaseorder");
dl.setInt(1,primaryKey.getId());
Collection col = dl.executeQuery();
dl.destroy();
dl = null;
Iterator it = col.iterator();
if (!it.hasNext())
return null;
else
return (HashMap)it.next();
}// end of getBasic
/*
* This method set the basicform for
*
*/
private void setBasicForm(HashMap hm)
{
this.purchaseorderVO = new PurchaseOrderVO();
Long tmp;
tmp = (Long)(hm.get("purchaseorderid"));
if(tmp!=null)
this.purchaseorderVO.setPurchaseOrderId(Integer.parseInt((hm.get("purchaseorderid")).toString()));
tmp = (Long)(hm.get("entity"));
if(tmp!=null)
this.purchaseorderVO.setVendorId(Integer.parseInt((hm.get("entity")).toString()));
this.purchaseorderVO.setVendorName((String)(hm.get("entityname")));
tmp = (Long)(hm.get("billaddress"));
if(tmp!=null)
this.purchaseorderVO.setBillToId(Integer.parseInt((hm.get("billaddress")).toString()));
this.purchaseorderVO.setBillToAddress((String)(hm.get("billaddressdesc")));
tmp = (Long)(hm.get("shipaddress"));
if(tmp!=null)
this.purchaseorderVO.setShipToId(Integer.parseInt((hm.get("shipaddress")).toString()));
this.purchaseorderVO.setShipToAddress((String)(hm.get("shipaddressdesc")));
tmp = (Long)(hm.get("status"));
if(tmp!=null)
this.purchaseorderVO.setStatusId(Integer.parseInt((hm.get("status")).toString()));
this.purchaseorderVO.setStatusName((String)(hm.get("statusdesc")));
this.purchaseorderVO.setPurchaseOrderDate((java.sql.Date)(hm.get("purchaseorderdate")));
tmp = (Long)(hm.get("terms"));
if(tmp!=null)
this.purchaseorderVO.setTermId(Integer.parseInt((hm.get("terms")).toString()));
this.purchaseorderVO.setTerm((String)(hm.get("termsdesc")));
tmp = (Long)(hm.get("accountmgr"));
if(tmp!=null)
this.purchaseorderVO.setAccountManagerId(Integer.parseInt((hm.get("accountmgr")).toString()));
this.purchaseorderVO.setAccountManagerName((String)(hm.get("accountmgrdesc")));
this.purchaseorderVO.setNotes((String)(hm.get("description")));
if(hm.get("externalid") != null)
this.purchaseorderVO.setExternalID((hm.get("externalid")).toString());
}//end of setBasicForm
/*
* Set the PurchaseOrderItem Data in PurchaseOrderVO Object
*/
private void setPurchaseOrderItemData()
{
CVDal dl = new CVDal(this.dataSource);
try
{
dl.setSqlQuery("select * from `purchaseorderitem` where status != 'Deleted' and purchaseorderid = "+this.purchaseorderVO.getPurchaseOrderId()+";");
Collection col = dl.executeQuery();
if (col != null)
{
ItemLines itemLines= new ItemLines();
Iterator it = col.iterator();
int count = 1;
while (it.hasNext())
{
HashMap hm =(HashMap)it.next();
int lineID = ((Number)hm.get("purchaseorderlineid")).intValue();
int itemID = ((Number)hm.get("itemid")).intValue();
int quantity = ((Number)hm.get("quantity")).intValue();
float price = ((Number)hm.get("price")).floatValue();
String sku = (String) hm.get("sku");
String description = (String) hm.get("description");
String status = (String) hm.get("status");
float priceExtended = quantity * price;
IntMember LineId = new IntMember("LineId",lineID,'D',"",'T',false,20);
IntMember ItemId = new IntMember("ItemId",itemID,'D',"",'T',false,20);
IntMember Quantity = new IntMember("Quantity",quantity,'D',"",'T',false,20);
FloatMember PriceEach = new FloatMember("Price",new Float(price),'D',"",'T',false,20);
StringMember SKU = new StringMember("SKU",sku,'D',"",'T',false);
StringMember Description = new StringMember("Description",description,'D',"",'T',false);
FloatMember PriceExtended = new FloatMember("PriceExtended",new Float(priceExtended),'D',"",'T',false,20);
ItemElement ie = new ItemElement(11);
ie.put ("LineId",LineId);
ie.put ("ItemId",ItemId);
ie.put ("Quantity",Quantity);
ie.put ("Price",PriceEach);
ie.put ("SKU",SKU);
ie.put ("Description",Description);
ie.put ("PriceExtended",PriceExtended);
ie.setLineStatus(status);
itemLines.put(""+count,ie);
count ++;
}
this.purchaseorderVO.setItemLines(itemLines);
}// end of if
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
dl.destroy();
dl = null;
}
}// end of setOrderItemData
/**
* A container invokes this method before it ends the life of the entity object. This
* happens as a result of a client's invoking a remove operation, or when a container
* decides to terminate the entity object after a timeout. This method is called with
* no transaction context.
*/
public void ejbRemove ()
{
CVDal dl = new CVDal(this.dataSource);
try
{
dl.setSql("purchaseorder.deletepurchaseorderitem");
dl.setInt(1,this.purchaseorderVO.getPurchaseOrderId());
dl.executeUpdate();
dl.clearParameters();
dl.setSql("purchaseorder.deletepurchaseorder");
dl.setInt(1,this.purchaseorderVO.getPurchaseOrderId());
dl.executeUpdate();
dl.clearParameters();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
dl.destroy();
dl = null;
}
}
/**
* The activate method is called when the instance is activated from its 'passive' state.
* The instance should acquire any resource that it has released earlier in the ejbPassivate()
* method. This method is called with no transaction context.
*/
public void ejbActivate ()
{
}
/**
* The passivate method is called before the instance enters the 'passive' state. The
* instance should release any resources that it can re-acquire later in the ejbActivate()
* method. After the passivate method completes, the instance must be in a state that
* allows the container to use the Java Serialization protocol to externalize and store
* away the instance's state. This method is called with no transaction context.
*/
public void ejbPassivate ()
{
}
/*
* Set the associated entity context. The container calls this method after the instance
* creation. The enterprise Bean instance should store the reference to the context
* object in an instance variable. This method is called with no transaction context.
*/
public void setEntityContext(EntityContext ctx)
{
this.ctx = ctx;
}
public void unsetEntityContext()
{
this.ctx = null;
}
public void ejbLoad()
{
PurchaseOrderPK purchaseorderPK = (PurchaseOrderPK)(ctx.getPrimaryKey());
this.setDataSource(purchaseorderPK.getDataSource());
HashMap hm = getBasic(purchaseorderPK);
if (hm == null)
return;
else
setBasicForm(hm);
}
public void ejbStore()
{
if (isDirty)
{
CVDal dl = new CVDal(this.dataSource);
try
{
ItemLines itemLines = purchaseorderVO.getItemLines();
itemLines.calculate();
dl.setSql("purchaseorder.updatepurchaseorder");
dl.setInt(1,this.purchaseorderVO.getBillToId());//bill address
dl.setInt(2,this.purchaseorderVO.getShipToId()); //ship address
dl.setInt(3,this.purchaseorderVO.getStatusId());// status
dl.setInt(4,this.purchaseorderVO.getTermId()); //terms
dl.setInt(5,this.purchaseorderVO.getAccountManagerId()); //accountmgr
dl.setFloat(6,itemLines.getSubtotal()); // sub total
dl.setFloat(7,itemLines.getTax()); //tax
dl.setFloat(8,itemLines.getTotal()); // total
dl.setString(9,this.purchaseorderVO.getNotes()); //notes
dl.setInt(10,purchaseorderVO.getModifiedBy()); //modified By
dl.setInt(11,this.purchaseorderVO.getPurchaseOrderId()); //purchase order id
dl.executeUpdate();
dl.clearParameters();
//Insert,Update Order Lines. as per status.
Set listkey = itemLines.keySet();
Iterator it = listkey.iterator();
while (it.hasNext())
{
Object str = ( Object)it.next();
ItemElement ele = ( ItemElement)itemLines.get( str );
String status = ele.getLineStatus();
if(status == null)
status = "";
status = status.trim();
if (status.equals("Active"))
{
updatePurchaseOrderItem(ele,this.purchaseorderVO.getPurchaseOrderId());
}
else if (status.equals("Deleted"))
{
markDeleatedPurchaseOrderItem(ele,this.purchaseorderVO.getPurchaseOrderId());
}
else if (status.equals("New") || status.equals(""))
{
addPurchaseOrderItem(ele,this.purchaseorderVO.getPurchaseOrderId());
}
}// end of while
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
dl.destroy();
dl = null;
}
}// end of if
}// end of ejbStore
public PurchaseOrderVO getPurchaseOrderVO()
{
ejbLoad();
setPurchaseOrderItemData();
return this.purchaseorderVO;
}// end of getPurchaseOrderForm
/*
* set the PurchaseOrder Form
*/
public void setPurchaseOrderVO(PurchaseOrderVO purchaseorderVO,int userID)
{
PurchaseOrderVO purchaseorderDBVO = getPurchaseOrderVO();
purchaseorderVO = (PurchaseOrderVO)CVUtility.replaceVO(purchaseorderDBVO, purchaseorderVO , "PurchaseOrder", userID, this.dataSource);
this.purchaseorderVO = purchaseorderVO ;
this.isDirty = true;
}// end of setPurchaseOrderForm
/**
* @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;
}
}