Package com.centraview.account.expense

Source Code of com.centraview.account.expense.ExpenseEJB

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

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;

/*
*  This Ejb is Entity Bean
* @author  Parshuram Walunjkar
* @version 1.0
*/
public class ExpenseEJB implements EntityBean
{

  protected javax.ejb.EntityContext ctx;
  protected Context environment;
  private ExpenseVO expenseVO;
  private boolean isDirty = false//update data if isDirty is true (ejbStore)
  private String dataSource = "MySqlDS";

  /*
  *  Create Expense record in Database
  */
  public ExpensePK ejbCreate(ExpenseVO expenseVO,int userID, String ds)
  {
    int expenseID = 0;
    this.dataSource = ds;
    CVDal cvdal = new CVDal(ds);
    try
    {
      ItemLines itemLines =  expenseVO.getItemLines();

      itemLines.calculate();
      cvdal.setSql("account.expense.addexpense");
      cvdal.setInt(1,expenseVO.getGlAccountIDValue());
      cvdal.setFloat(2,expenseVO.getAmount());
      cvdal.setString(3,expenseVO.getTitle());
      cvdal.setString(4,expenseVO.getExpenseDescription());
      cvdal.setInt(5,expenseVO.getEntityIDValue());
      cvdal.setInt(6,expenseVO.getStatusIDValue());
      cvdal.setInt(7,userID);
      cvdal.setInt(8,expenseVO.getProjectIDValue());
      cvdal.setInt(9,expenseVO.getOpportunityIDValue());
      cvdal.setInt(10,expenseVO.getSupportTicketIDValue());
      cvdal.setString(11,expenseVO.getNotes());
      cvdal.setString(12,expenseVO.getExternalID());

      cvdal.executeUpdate();
      this.expenseVO = expenseVO;
      expenseID  = cvdal.getAutoGeneratedKey();
      this.expenseVO.setExpenseID(expenseID);

      cvdal.clearParameters();

      Set listkey = itemLines.keySet();
      Iterator it =  listkey.iterator();
      while (it.hasNext())
      {
        Object str = ( Object )it.next();
        ItemElement ele  = ( ItemElement)itemLines.get( str );
        this.addExpenseItem(ele,expenseID);
      }// end of while

      InitialContext ic = CVUtility.getInitialContext();
      AuthorizationLocalHome authorizationHome = (AuthorizationLocalHome)ic.lookup("local/Authorization");
      AuthorizationLocal authorizationLocal = authorizationHome.create();
      authorizationLocal.setDataSource(ds);
      authorizationLocal.saveCurrentDefaultPermission("Expense", expenseID, userID);

    }
    catch(Exception e)
    {
      System.out.println("[Exception][ExpenseEJB.ejbCreate] Exception Thrown: "+e);
      e.printStackTrace();
    }
    finally
    {
      cvdal.destroy();
      cvdal = null;
    }
    return new ExpensePK(expenseID, ds);
  }// end of ejbCreate

     /*
     *  Add ExpenseItem record in Database
     */
   private void addExpenseItem(ItemElement ele ,int expenseID)
   {
    CVDal cvdal = 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();

      cvdal.setSql("account.expense.addexpenseitem");
      cvdal.setInt(1,expenseID);
      cvdal.setInt(2,itemID );
      cvdal.setString(3,(String)sku.getMemberValue());
      cvdal.setString(4,(String)desc.getMemberValue());
      cvdal.setFloat(5,((Float)priceEach.getMemberValue()).floatValue());
      cvdal.setInt(6,quantity);
      cvdal.executeUpdate();
      cvdal.clearParameters();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      cvdal.destroy();
      cvdal = null;
    }
   }// end of addExpenseItem

   /*
   *  Update ExpenseItem record in Database
   */
   private void updateExpenseItem(ItemElement ele ,int expenseID)
   {
    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("account.expense.updateexpenseitem");
      dl.setInt(1, itemID);
      dl.setString(2,(String)sku.getMemberValue());
      dl.setString(3,(String)desc.getMemberValue());
      dl.setFloat(4,price);
      dl.setInt(5,quantity);
      dl.setString(6,ele.getLineStatus());
      dl.setInt(7,expenseID);
      dl.setInt(8,((Integer)lineid.getMemberValue()).intValue() );
      dl.executeUpdate();
      dl.clearParameters();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      dl.destroy();
      dl = null;
    }
   }// end of updateExpenseItem

   /*
   *  Update ExpenseItem record in Database
  *  Mark as delete
   */
   private void markDeletedExpenseItem(ItemElement ele ,int expenseID)
   {
    CVDal dl = new CVDal(this.dataSource);
     try
     {
       IntMember lineid = ( IntMember )ele.get("LineId");
       String status    = ele.getLineStatus();
      IntMember itemid = ( IntMember )ele.get("ItemId");

       dl.setSql("account.expense.markdeletedexpenseitem");
       dl.setString(1,status);
       dl.setInt(2,expenseID);
      dl.setInt(3,((Integer)itemid.getMemberValue()).intValue() );
       dl.setInt(4,((Integer)lineid.getMemberValue()).intValue());
       dl.executeUpdate();
       dl.clearParameters();
     }
     catch(Exception e)
     {
       e.printStackTrace();
     }
     finally
     {
       dl.destroy();
       dl = null;
     }
   }// end of markDeleatedExpenseItem


  /**
  * EJB Container callback method
  */
  public void ejbPostCreate (ExpenseVO expenseVO,int userID, String ds) throws CreateException
  {
  }

  /**
  * Finds if a given Expense exits in the database
  * @returns    ExpensePK class (EJB clients get Remote)
  * @param  int  ExpenseID
  */
  public ExpensePK ejbFindByPrimaryKey(ExpensePK primaryKey) throws FinderException
  {
    HashMap hm = getBasic(primaryKey);
    if (hm == null)
    {
      throw new FinderException("Could not find Expense: " + primaryKey);
    }
    else
    {
      return primaryKey;
    }
  }// end of ejbFindByPrimaryKey

  /*
  *  get Basic Expense
  */
  private HashMap getBasic(ExpensePK primaryKey)
  {
    CVDal dl = new CVDal(primaryKey.getDataSource());
    dl.setSql("account.expense.getexpense");
    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.expenseVO = new ExpenseVO();

    if(hm.get("expenseid")!=null)
      this.expenseVO.setExpenseID(Integer.parseInt(hm.get("expenseid").toString()));

    if(hm.get("GLAccountsID")!=null)
      this.expenseVO.setGlAccountIDValue(Integer.parseInt(hm.get("GLAccountsID").toString()));

    if(hm.get("Amount")!=null)
      this.expenseVO.setAmount(Float.parseFloat(hm.get("Amount").toString()));


    if(hm.get("Title") != null)
      this.expenseVO.setTitle((String)hm.get("Title"));

    if(hm.get("Description") != null)
      this.expenseVO.setExpenseDescription((String)hm.get("Description"));

    if(hm.get("notes") != null)
      this.expenseVO.setNotes((String)hm.get("notes"));

    if(hm.get("EntityName") != null)
      this.expenseVO.setEntity((String)hm.get("EntityName"));

    if(hm.get("EntityID") != null)
      this.expenseVO.setEntityIDValue(Integer.parseInt(hm.get("EntityID").toString()));

    if (hm.get("Created") != null)
    {
      java.sql.Timestamp ts = (java.sql.Timestamp)hm.get("Created");
      java.sql.Date date = new java.sql.Date(ts.getYear(),ts.getMonth(),ts.getDate());
      this.expenseVO.setDateEntered(date);
      this.expenseVO.setCreatedOn(ts);
    }

    if(hm.get("Modified") != null)
      this.expenseVO.setModifiedOn((java.sql.Timestamp)hm.get("Modified"));

    if(hm.get("Status") != null)
      this.expenseVO.setStatus((String)hm.get("Status"));

    if(hm.get("EmployeeName") != null)
      this.expenseVO.setEmployee((String)hm.get("EmployeeName"));

    if(hm.get("IndividualID") != null)
      this.expenseVO.setEmployeeIDValue(Integer.parseInt(hm.get("IndividualID").toString()));


    if(hm.get("ProjectTitle") != null)
      this.expenseVO.setProject((String)hm.get("ProjectTitle"));

    if(hm.get("ProjectID") != null)
      this.expenseVO.setProjectIDValue(Integer.parseInt(hm.get("ProjectID").toString()));

    if(hm.get("OpportunityTitle") != null)
      this.expenseVO.setOpportunity((String)hm.get("OpportunityTitle"));

    if(hm.get("OpportunityID") != null)
      this.expenseVO.setOpportunityIDValue(Integer.parseInt(hm.get("OpportunityID").toString()));

    if(hm.get("SupportTicket") != null)
      this.expenseVO.setSupportTicket((String)hm.get("SupportTicket"));

    if(hm.get("TicketID") != null)
      this.expenseVO.setSupportTicketIDValue(Integer.parseInt(hm.get("TicketID").toString()));

    if(hm.get("externalid") != null)
      this.expenseVO.setExternalID((String)hm.get("externalid"));

  }//end of setBasicForm

  /*
  *  Set the ExpenseItem Data in ExpenseVO Object
  */
  private void setExpenseItemData()
  {
    CVDal dl = new CVDal(this.dataSource);
    try
    {
      dl.setSqlQuery("select * from `expenseitem` where LineStatus != 'Deleted' and ExpenseID = "+this.expenseVO.getExpenseID()+";");
      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("LineID")).intValue();
          int itemID = ((Number)hm.get("ExpenseItemID")).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("LineStatus");
          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.expenseVO.setItemLines(itemLines);
      }// end of if
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      dl.destroy();
      dl = null;
    }
  }// end of setExpenseItemData

  /**
  * 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("account.expense.deleteexpenseitem");
      dl.setInt(1,this.expenseVO.getExpenseID());
      dl.executeUpdate();
      dl.clearParameters();

      dl.setSql("account.expense.deleteexpense");
      dl.setInt(1,this.expenseVO.getExpenseID());
      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()
  {
    ExpensePK expensePK = (ExpensePK)(ctx.getPrimaryKey());
    this.setDataSource(expensePK.getDataSource());
    HashMap hm = getBasic(expensePK);

    if (hm == null)
      return;
    else
      setBasicForm(hm);
  }

  public void ejbStore()
  {
    if (isDirty)
    {
      CVDal dl = new CVDal(dataSource);
      try
      {
        ItemLines itemLines =  expenseVO.getItemLines();
        itemLines.calculate();

        dl.setSql("account.expense.updateexpense");
        //ALLSQL.put("account.expense.updateexpense","update expense set glaccountsid = ?,amount = ?,title = ?,description = ?,entityid = ?,status = ?,created = CONCAT(CURRENT_DATE),owner = ?,project = ?,opportunity = ?,ticket = ? where expenseid = ?");
        dl.setInt(1,expenseVO.getGlAccountIDValue());
        dl.setFloat(2,expenseVO.getAmount());
        dl.setString(3,expenseVO.getTitle());
        dl.setString(4,expenseVO.getExpenseDescription());
        dl.setInt(5,expenseVO.getEntityIDValue());
        dl.setInt(6,expenseVO.getStatusIDValue());
        dl.setInt(7,expenseVO.getEmployeeIDValue());
        dl.setInt(8,expenseVO.getProjectIDValue());
        dl.setInt(9,expenseVO.getOpportunityIDValue());
        dl.setInt(10,expenseVO.getSupportTicketIDValue());
        dl.setString(11,expenseVO.getNotes());
        dl.setInt(12,this.expenseVO.getExpenseID());

        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"))
          {
            updateExpenseItem(ele,this.expenseVO.getExpenseID());
          }
          else if (status.equals("Deleted"))
          {
            markDeletedExpenseItem(ele,this.expenseVO.getExpenseID());
          }
          else if (status.equals("New") || status.equals(""))
          {
            addExpenseItem(ele,this.expenseVO.getExpenseID());
          }
        }// end of while

      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
      finally
      {
        dl.destroy();
        dl = null;
        this.isDirty = false;
      }
    }// end of if
  }// end of ejbStore

  public ExpenseVO getExpenseVO()
  {
    ejbLoad();
    setExpenseItemData();
    return this.expenseVO;
  }// end of getExpenseForm

  /*
  *  set the Expense Form
  */
  public void setExpenseVO(ExpenseVO expenseVO,int userID)
  {
    ExpenseVO expenseDBVO = getExpenseVO();
    expenseDBVO = (ExpenseVO)CVUtility.replaceVO(expenseDBVO, expenseVO, "Expense", userID, this.dataSource);
    this.expenseVO = expenseVO ;
    this.isDirty = true;
  }// end of setExpenseForm

  /**
  * @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.account.expense.ExpenseEJB

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.