package com.lotus.mst.entity.c_order;

import javax.ejb.*;
import java.rmi.RemoteException;
import java.util.*;
import java.sql.*;
import java.io.Serializable;

import com.lotus.common.*;
import com.lotus.exception.LotusException;

/**
 * @stereotype EntityBean
 */
public class C_OrderBean implements EntityBean {
	final static boolean VERBOSE = false;
	final static boolean DEBUG = false;

	// use for method update -- system variable
	private transient boolean isDirty;
	private EntityContext ctx;
	private C_OrderData cd = new C_OrderData();

	public boolean isModified() {
		return isDirty;
	}

  	/**
   	* Sets the EJBean as modified.
   	*
   	* @param flag              boolean Flag
   	*/
  	public void setModified(boolean flag) {
    	isDirty = flag;
 	}

	public void ejbActivate() throws RemoteException {
	}

	public void setEntityContext(EntityContext ctx) throws RemoteException {
		this.ctx = ctx;
	}

	public void unsetEntityContext() {
		this.ctx = null;
	}

	public void ejbPassivate() throws RemoteException {
	}

	public void ejbLoad() throws RemoteException {
		try {
			refresh((C_OrderPK) ctx.getPrimaryKey());
    	} catch (FinderException fe) {
      		fe.printStackTrace();
    	} finally {
    		setModified(false);
    	}
	}

	public C_OrderPK ejbFindByPrimaryKey(C_OrderPK pk)
    			throws FinderException, RemoteException {
		if (pk == null) {
			throw new FinderException ("primary key cannot be null");
		}

		try {
			log("C_OrderBean: FindByPrimaryKey To Refresh:" + pk.getC_Order_ID());
			refresh(pk);
		} catch (FinderException fe) {
			fe.printStackTrace();
			throw fe;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			setModified(false);
		}

		return pk;
  	}

  	public Enumeration ejbFindAllRecord() throws FinderException, RemoteException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
  			con = Env.getConnection();
  			ps = con.prepareStatement("SELECT C_Order_ID FROM C_Order ORDER BY C_Order_ID");
  			ps.executeQuery();
  			rs = ps.getResultSet();
  			Vector v = new Vector();
  			C_OrderPK pk;
  			while (rs.next()) {
    			pk = new C_OrderPK(rs.getString(1));
    		    v.addElement(pk);
     		}

  			rs.close();
  			return v.elements();
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			throw new FinderException (sqe.getMessage());
		} finally {
  			try {
    			setModified(false);
    			if (rs != null) rs.close();
    			if (ps != null) ps.close();
    			if (con!= null) con.close();
  			} catch (Exception e) {
				e.printStackTrace();
  			}
		}
  	}

  	public Enumeration ejbFindActiveRecord() throws FinderException, RemoteException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
  			con = Env.getConnection();

  			ps = con.prepareStatement("SELECT C_Order_ID FROM C_Order WHERE IsActive=? ORDER BY C_Order_ID");
  			ps.setString(1, Var.CONST_ACTIVE);
  			ps.executeQuery();
  			rs = ps.getResultSet();

  			Vector v = new Vector();
  			C_OrderPK pk = null;
  			while (rs.next()) {
    			pk = new C_OrderPK(rs.getString(1));
    		    v.addElement(pk);
     		}

  			rs.close();
  			return v.elements();
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			throw new FinderException (sqe.getMessage());
		} finally {
  			try {
    			setModified(false);
    			if (rs != null) rs.close();
    			if (ps != null) ps.close();
    			if (con!= null) con.close();
  			} catch (Exception e) {
				e.printStackTrace();
  			}
		}
  	}

  	public Enumeration ejbFindByCondition(C_OrderSearchData sd) throws FinderException, RemoteException {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sSql = "";
		boolean bWhere = false;

		try {
  			con = Env.getConnection();

  			sSql = "SELECT C_Order_ID FROM C_Order ";

  			if(sd.getValue().length() > 0) {
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

  				sSql += " [Value] = '" + Env.fmtSqlStr(sd.getValue()) + "'";
  			}

			if(sd.getC_BPartner_ID().length() > 0) {
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

  				sSql += " [C_BPartner_ID] = '" + Env.fmtSqlStr(sd.getC_BPartner_ID()) + "'";
  			}

  			if(sd.getCreatedBy().length() > 0) {
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

  				sSql += " CreatedBy = '" + Env.fmtSqlStr(sd.getCreatedBy()) + "'";
  			}

  			if(sd.getCreatedFrom().length() > 0) {
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

  				sSql += " CONVERT(CHAR(10), Created, 21) >= '" + Env.convDate(sd.getCreatedFrom()) + "'";
  			}

  			if(sd.getCreatedTo().length() > 0) {
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

  				sSql += " CONVERT(CHAR(10), Created, 21) <= '" + Env.convDate(sd.getCreatedTo()) + "'";
  			}

  			if(sd.getUpdatedBy().length() > 0) {
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

  				sSql += " UpdatedBy = '" + Env.fmtSqlStr(sd.getUpdatedBy()) + "'";
  			}

  			if(sd.getUpdatedFrom().length() > 0) {
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

  				sSql += " CONVERT(CHAR(10), Updated, 21) >= '" + Env.convDate(sd.getUpdatedFrom()) + "'";
  			}

  			if(sd.getUpdatedTo().length() > 0) {
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

  				sSql += " CONVERT(CHAR(10), Updated, 21) <= '" + Env.convDate(sd.getUpdatedTo()) + "'";
  			}
 			//-----------------------

   			if(sd.getSales().length() > 0) {
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

  				sSql += " SalesRep_ID = '" + Env.fmtSqlStr(sd.getSales()) + "'";
  			}

   			if(sd.getIsActive().length() > 0) {
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

  				sSql += " IsActive = '" + Env.fmtSqlStr(sd.getIsActive()) + "'";
  			}

			log("==================================================");
			log(sd.getProcessed());
			log("==================================================");

   			if(sd.getProcessed().equalsIgnoreCase(Var.CONST_CONFIRM_FIRST)) {		//一级确认
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

				sSql += " (";
  				sSql += " IsSelected = '" + Var.CONST_YES + "'";
				sSql += " AND Processing = '" + Var.CONST_NO + "'";
				sSql += " AND Processed = '" + Var.CONST_NO + "'" ;
				sSql += " )";
  			} else if(sd.getProcessed().equalsIgnoreCase(Var.CONST_CONFIRM_SECOND)) {		//二级确认
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

				sSql += " (";
  				sSql += " IsSelected = '" + Var.CONST_YES + "'";
				sSql += " AND Processing = '" + Var.CONST_YES + "'";
				sSql += " AND Processed = '" + Var.CONST_NO + "'" ;
				sSql += " )";
  			} else if(sd.getProcessed().equalsIgnoreCase(Var.CONST_CONFIRM_THIRD)) {		//二级确认
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

				sSql += " (";
  				sSql += " IsSelected = '" + Var.CONST_YES + "'";
				sSql += " AND Processing = '" + Var.CONST_YES + "'";
				sSql += " AND Processed = '" + Var.CONST_YES + "'" ;
				sSql += " )";
			} else if(sd.getProcessed().equalsIgnoreCase(Var.CONST_CONFIRM_NOTYET)) {		//未确认
  				if(!bWhere) {
  					sSql += " WHERE ";
  					bWhere = true;
  				} else {
  					sSql += " AND ";
  				}

				sSql += " (";
  				sSql += " IsSelected = '" + Var.CONST_NO + "'";
				sSql += " AND Processing = '" + Var.CONST_NO + "'";
				sSql += " AND Processed = '" + Var.CONST_NO + "'" ;
				sSql += " )";
  			}

    		if(sd.getSortBy().length() > 0) {

  				sSql += " ORDER BY ";
  				sSql += sd.getSortBy() + " " + sd.getSortSeq();
  			}

  			//log("C_Order: SearchByCondition:\n" + sSql);
  			ps = con.prepareStatement(sSql);
  			ps.executeQuery();
  			rs = ps.getResultSet();
  			Vector v = new Vector();
  			C_OrderPK pk;
  			while (rs.next()) {
    			pk = new C_OrderPK(rs.getString(1));
    		    v.addElement(pk);
     		}

  			rs.close();
  			return v.elements();
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			throw new FinderException (sqe.getMessage());
		} finally {
  			try {
    			setModified(false);
    			if (rs != null) rs.close();
    			if (ps != null) ps.close();
    			if (con!= null) con.close();
  			} catch (Exception e) {
				e.printStackTrace();
  			}
		}
  	}

	private void refresh(C_OrderPK pk) throws FinderException, RemoteException {
		if (pk == null) {
			throw new FinderException ("primary key cannot be null");
		}

		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
	      	con = Env.getConnection();
			String sSql = "SELECT C_Order_ID, AD_ORG_ID, AD_Client_ID, IsActive, Created, ";
			sSql += " CreatedBy, Updated, UpdatedBy, Value, Processing, ";
			sSql += " Processed, SalesRep_ID, DateOrdered, DatePromised, C_BPartner_ID, ";
			sSql += " BillTo_ID, C_BPartner_Location_ID, C_BPartner_Contact_ID, C_Currency_ID, PaymentRule, ";
			sSql += " M_WareHouse_ID, M_PriceList_ID, Discount, IsApproved, IsDelivered, ";
			sSql += " DeliveryRule, DeliveryCost, IsSelected, IsPrinted, MailType, ";
			sSql += " MailSentTime ";

			sSql += " FROM C_Order ";
			sSql += " WHERE C_Order_ID = ? ";

			log("Refresh sSql (Select): " + sSql);
			ps  = con.prepareStatement(sSql);
			ps.setString(1, pk.getC_Order_ID());

	      	ps.executeQuery();

			rs = ps.getResultSet();

	      	if (rs.next()) {
				this.cd.setC_Order_ID(Env.fmtStr(rs.getString(1)));
				this.cd.setAD_ORG_ID(Env.fmtStr(rs.getString(2)));
				this.cd.setAD_Client_ID(Env.fmtStr(rs.getString(3)));
				this.cd.setIsActive(Env.fmtStr(rs.getString (4)));
				this.cd.setCreated(rs.getDate (5));

				this.cd.setCreatedBy(Env.fmtStr(rs.getString (6)));
				this.cd.setUpdated(rs.getDate(7));
				this.cd.setUpdatedBy(Env.fmtStr(rs.getString(8)));
				this.cd.setValue(Env.fmtStr(rs.getString(9)));
				this.cd.setProcessing(Env.fmtStr(rs.getString(10)));

				this.cd.setProcessed(Env.fmtStr(rs.getString(11)));
				this.cd.setSalesRep_ID(Env.fmtStr(rs.getString(12)));
				this.cd.setDateOrdered(rs.getDate(13));
				this.cd.setDatePromised(rs.getDate(14));
				this.cd.setC_BPartner_ID(Env.fmtStr(rs.getString(15)));

				this.cd.setBillTo_ID(Env.fmtStr(rs.getString(16)));
				this.cd.setC_BPartner_Location_ID(Env.fmtStr(rs.getString(17)));
				this.cd.setC_BPartner_Contact_ID(Env.fmtStr(rs.getString(18)));
				this.cd.setC_Currency_ID(Env.fmtStr(rs.getString(19)));
				this.cd.setPaymentRule(Env.fmtStr(rs.getString(20)));

				this.cd.setM_WareHouse_ID(Env.fmtStr(rs.getString(21)));
				this.cd.setM_PriceList_ID(Env.fmtStr(rs.getString(22)));
				this.cd.setDiscount(rs.getInt(23));
				this.cd.setIsApproved(Env.fmtStr(rs.getString(24)));
				this.cd.setIsDelivered(Env.fmtStr(rs.getString(25)));

				this.cd.setDeliveryRule(Env.fmtStr(rs.getString(26)));
				this.cd.setDeliveryCost(rs.getInt(27));
				this.cd.setIsSelected(Env.fmtStr(rs.getString(28)));
				this.cd.setIsPrinted(Env.fmtStr(rs.getString(29)));
				this.cd.setMailType(Env.fmtStr(rs.getString(30)));

				this.cd.setMailSentTime(rs.getDate(31));

				Vector v = new Vector();
				v = getOrderLine(this.cd.getC_Order_ID());
				this.cd.setOrderLine(v);

	      	} else {
				throw new FinderException ("Refresh: C_Order ("
	                                 + pk.getC_Order_ID() + " not found");
	       }
		} catch (FinderException fe) {
			fe.printStackTrace();
			throw fe;
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			throw new FinderException (sqe.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				setModified(false);
				if (rs != null) rs.close();
				if (ps != null) ps.close();
				if (con!= null) con.close();
		     } catch (Exception e) {
					e.printStackTrace();
					throw new FinderException (e.getMessage());
		     }
		}
	}

	public void ejbStore() throws RemoteException {
		Connection con = null;
		PreparedStatement ps = null;

		try {
			con = Env.getConnection();

			String sSql = "UPDATE C_Order ";
			sSql += " SET AD_ORG_ID = ? ";
			sSql += ", AD_Client_ID = ? ";
			sSql += ", IsActive = ? ";
			sSql += ", Updated = GetDate() ";
			sSql += ", UpdatedBy = ? ";

			sSql += ", Value = ? ";
			sSql += ", Processing = ? ";
			sSql += ", Processed = ? ";
			sSql += ", SalesRep_ID = ? ";
			sSql += ", DateOrdered = ? ";

			sSql += ", DatePromised = ? ";
			sSql += ", C_BPartner_ID = ? ";
			sSql += ", BillTo_ID = ? ";
			sSql += ", C_BPartner_Location_ID = ? ";
			sSql += ", C_BPartner_Contact_ID = ? ";

			sSql += ", C_Currency_ID = ? ";
			sSql += ", PaymentRule = ? ";
			sSql += ", M_WareHouse_ID = ? ";
			sSql += ", M_PriceList_ID = ? ";
			sSql += ", Discount = ? ";

			sSql += ", IsApproved = ? ";
			sSql += ", IsDelivered = ? ";
			sSql += ", DeliveryRule = ? ";
			sSql += ", DeliveryCost = ? ";
			sSql += ", IsSelected = ? ";

			sSql += " WHERE C_Order_ID = ? ";

			log("ejbSore SQL: \n" + sSql);
			ps = con.prepareStatement(sSql);

			ps.setString(1, this.cd.getAD_ORG_ID());
			ps.setString(2, this.cd.getAD_Client_ID());
			ps.setString(3, this.cd.getIsActive());
			ps.setString(4, this.cd.getUpdatedBy());

			ps.setString(5, this.cd.getValue());
			ps.setString(6, this.cd.getProcessing());
			ps.setString(7, this.cd.getProcessed());
			ps.setString(8, this.cd.getSalesRep_ID());
			ps.setDate(9, this.cd.getDateOrdered());

			ps.setDate(10, this.cd.getDatePromised());
			ps.setString(11, this.cd.getC_BPartner_ID());
			ps.setString(12, this.cd.getBillTo_ID());
			ps.setString(13, this.cd.getC_BPartner_Location_ID());
			ps.setString(14, this.cd.getC_BPartner_Contact_ID());

			ps.setString(15, this.cd.getC_Currency_ID());
			ps.setString(16, this.cd.getPaymentRule());
			ps.setString(17, this.cd.getM_WareHouse_ID());
			ps.setString(18, this.cd.getM_PriceList_ID());
			ps.setInt(19, this.cd.getDiscount());

			ps.setString(20, this.cd.getIsApproved());
			ps.setString(21, this.cd.getIsDelivered());

			ps.setString(22, this.cd.getDeliveryRule());
			ps.setInt(23, this.cd.getDeliveryCost());

			ps.setString(24, this.cd.getIsSelected());
			ps.setString(25, this.cd.getC_Order_ID());

			int i = ps.executeUpdate();
			if (i == 0) {
				log("ejbStore: Execute Update Error, No Row Updated!");
			}

			updateOrderLine(this.cd.getOrderLine(), this.cd.getC_Order_ID());

		} catch (SQLException sqe) {
			sqe.printStackTrace();
		} finally {
		  try {
				setModified(false);
				ps.close();
				con.close();
		  } catch (Exception e) {
		  		e.printStackTrace();
		  }
		}
	}

	public void ejbRemove() throws RemoveException, RemoteException {
		Connection con = null;
		PreparedStatement ps = null;

		try {
			con = Env.getConnection();
	      	C_OrderPK pk = (C_OrderPK) ctx.getPrimaryKey();

			String sSql = "DELETE FROM C_Order where C_Order_ID = ? ";
			log("Remove sSql: " + sSql);

			ps  = con.prepareStatement(sSql);
			ps.setString(1, pk.getC_Order_ID());

			int i = ps.executeUpdate();
	      	if (i == 0) {
				throw new RemoveException ("C_Order ("
	               + pk.getC_Order_ID() + " not found");
	      	}

	      	ps.close();
	      	sSql = "DELETE C_OrderLine WHERE C_Order_ID = ? ";
	      	ps = con.prepareStatement(sSql);
	      	ps.setString(1, pk.getC_Order_ID());

	      	i = ps.executeUpdate();
	      	if (i == 0) {
				log("DLETE No Row From C_OrderLine: " + pk.getC_Order_ID());
	      }
		} catch (RemoveException re) {
			re.printStackTrace();
			throw re;
		} catch (SQLException sqe) {
	      	sqe.printStackTrace();
			throw new RemoveException (sqe.getMessage());
		} finally {
			try {
	        	setModified(false);
	        	ps.close();
	        	con.close();
		    } catch (Exception e) {
	        	e.printStackTrace();
		    }
    	}
	}

	public C_OrderPK ejbCreate(C_OrderData a_cd) throws CreateException,
		RemoteException, LotusException {

		Connection con = null;
		PreparedStatement ps = null;;

		try {
			this.setInfo(a_cd);

			con = Env.getConnection();

			String tmpStr = "SELECT C_Order_ID FROM C_Order WHERE C_Order_ID = ? ";
			log("ejbCreate: Select SQL:" + tmpStr);
			log("ejbCreate: PK:" + this.cd.getC_Order_ID());

			ps = con.prepareStatement(tmpStr);
			ps.setString(1, this.cd.getC_Order_ID());
			ps.executeQuery();

  			ResultSet rs = ps.getResultSet();
  			if(rs.next()){
				throw new LotusException("A00001","E","C_Order_ID");
			}
			rs.close();
			ps.close();

			String sSql = "INSERT INTO C_Order (";
			sSql += " C_Order_ID, AD_ORG_ID, AD_Client_ID, IsActive, Created, ";
			sSql += " CreatedBy, Updated, UpdatedBy, Value, Processing, ";
			sSql += " Processed, SalesRep_ID, DateOrdered, DatePromised, C_BPartner_ID, ";
			sSql += " BillTo_ID, C_BPartner_Location_ID, C_BPartner_Contact_ID, C_Currency_ID, PaymentRule, ";
			sSql += " M_WareHouse_ID, M_PriceList_ID, Discount, ";

			//These fields below are not used.
			sSql += " IsSotrx, DocumentNo, Docstatus, Docaction, C_DocType_ID, ";
			sSql += " C_DocTypeTarget_ID, Description, IsApproved, IsCreditApproved, IsDelivered, ";
			sSql += " IsInvoiced, IsPrinted, IsTransferred, IsSelected, DatePrinted, ";
			sSql += " DateAcct, POReference, IsDiscountPrinted, C_PaymentTerm_ID, InVoiceRule, ";
			sSql += " DeliveryRule, FreightCostRule, FreightAmt, DeliveryViaRule, M_Shipper_ID, ";
			sSql += " C_Charge_ID, ChargeAmt, PriorityRule, TotalLines, GrandTotal, ";
			sSql += " IsTaxInCluded, C_Campaign_ID, C_Project_ID, C_Activity_ID, Posted, ";
			sSql += " C_Payment_ID, C_CashLine_ID, DeliveryCost ";

			sSql += " ) ";
			sSql += " VALUES (?, ?, ?, ?, GetDate(), ";
			sSql += " ?, GetDate(), ?, ?, ?, ";
			sSql += " ?, ?, ?, ?, ?, ";
			sSql += " ?, ?, ?, ?, ?, ";
			sSql += " ?, ?, ?, ";

			//Fields Below is not used.
			sSql += " 'N', ' ', ' ', ' ', ' ', ";
			//sSql += " ' ', ' ', 'N', 'N', 'N', ";
			sSql += " ' ', ' ', ?, 'N', ?, ";
			sSql += " 'N', 'N', 'N', ?, GetDate(), ";
			sSql += " GetDate(), ' ', 'N', ' ', ' ', ";
			//sSql += " ' ', ' ', 0, ' ', ' ', ";
			sSql += " ?, ' ', 0, ' ', ' ', ";
			sSql += " ' ', 0, ' ', 0, 0, ";
			sSql += " 'N', ' ', ' ', ' ', ' ', ";
			//sSql += " ' ', ' ' ";
			sSql += " ' ', ' ', ? ";
			sSql += " )";

  			ps = con.prepareStatement(sSql);

  			log("ejbCreate: Create SQL:" + sSql);
			ps.setString(1, a_cd.getC_Order_ID());
			ps.setString(2, a_cd.getAD_ORG_ID());
			ps.setString(3, a_cd.getAD_Client_ID());
			ps.setString(4, a_cd.getIsActive());

			ps.setString(5, a_cd.getCreatedBy() );
			ps.setString(6, a_cd.getUpdatedBy());
			ps.setString(7, a_cd.getValue());
			ps.setString(8, a_cd.getProcessing());

			ps.setString(9, a_cd.getProcessed());
			ps.setString(10, a_cd.getSalesRep_ID());
			ps.setDate(11, a_cd.getDateOrdered());
			ps.setDate(12, a_cd.getDatePromised());
			ps.setString(13, a_cd.getC_BPartner_ID());

			ps.setString(14, a_cd.getBillTo_ID());
			ps.setString(15, a_cd.getC_BPartner_Location_ID());
			ps.setString(16, a_cd.getC_BPartner_Contact_ID());
			ps.setString(17, a_cd.getC_Currency_ID());
			ps.setString(18, a_cd.getPaymentRule());

			ps.setString(19, a_cd.getM_WareHouse_ID());
			ps.setString(20, a_cd.getM_PriceList_ID());
			ps.setInt(21, a_cd.getDiscount());
			ps.setString(22, a_cd.getIsApproved());
			ps.setString(23, a_cd.getIsDelivered());

			ps.setString(24, a_cd.getIsSelected());
			ps.setString(25, a_cd.getDeliveryRule());
			ps.setInt(26, a_cd.getDeliveryCost());

	      	if (ps.executeUpdate() != 1) {
				throw new CreateException ("JDBC did not create any row");
	      	}

			Vector vNew = a_cd.getOrderLine();

	      	insertOrderLine(vNew);

		} catch (LotusException ne) {
			ne.printStackTrace();
			throw ne;
		} catch (CreateException ce) {
			ce.printStackTrace();
			throw ce;
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			throw new CreateException (sqe.getMessage());
		} finally {
	      	try {
				setModified(false);
				if (ps != null) ps.close();
				if (con!= null) con.close();
	      	} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			refresh(new C_OrderPK(a_cd.getC_Order_ID()));
		} catch (Exception e) {
			e.printStackTrace();
			throw new CreateException(e.getMessage());
		}

		C_OrderPK pk = new C_OrderPK(a_cd.getC_Order_ID());
	    return pk;

    }

	public void ejbPostCreate(C_OrderData a_cd) throws RemoteException {
	}

	private Vector getOrderLine(String sC_Order_ID) throws FinderException, RemoteException
  	{
		if (sC_Order_ID.trim().length() == 0) {
			throw new FinderException ("C_Order, getOrderLine: Parameters Can't be Empty: Para1 = " + sC_Order_ID);
		}

		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Vector v = new Vector();
		C_OrderLineData c_od = null;

		log("C_Order, getOrderLine Entry, sC_Order_ID = " + sC_Order_ID);
		try {
			con = Env.getConnection();
			String sSql = "SELECT ";
			sSql += " C_Order_ID, C_OrderLine_ID, AD_Client_ID, AD_ORG_ID, IsActive, ";
            sSql += " Created, CreatedBy, Updated, UpdatedBy, Description, ";
            sSql += " M_Product_ID, C_UOM_ID, QtyOrdered ";

			sSql += " FROM C_OrderLine ";

			sSql += " WHERE LTRIM(C_Order_ID) = ? ";

			sSql += " Order By C_Order_ID, C_OrderLine_ID";

			log("C_Order, getOrderLine sSql: \n" + sSql);
			ps  = con.prepareStatement(sSql);
			ps.setString(1, sC_Order_ID.trim());
	        ps.executeQuery();

			rs = ps.getResultSet();
			while(rs.next()) {
		    	c_od = new C_OrderLineData();

				c_od.setC_Order_ID(Env.fmtStr(rs.getString(1)));
				c_od.setC_OrderLine_ID(Env.fmtStr(rs.getString(2)));
				c_od.setAD_Client_ID(Env.fmtStr(rs.getString(3)));
				c_od.setAD_ORG_ID(Env.fmtStr(rs.getString(4)));
				c_od.setIsActive(Env.fmtStr(rs.getString(5)));

				c_od.setCreated(rs.getDate(6));
				c_od.setCreatedBy(Env.fmtStr(rs.getString(7)));
				c_od.setUpdated(rs.getDate(8));
				c_od.setUpdatedBy(Env.fmtStr(rs.getString(9)));
				c_od.setDescription(Env.fmtStr(rs.getString(10)));

				c_od.setM_Product_ID(Env.fmtStr(rs.getString(11)));
				c_od.setC_UOM_ID(Env.fmtStr(rs.getString(12)));
				c_od.setQtyOrdered(rs.getInt(13));

				v.addElement(c_od);
		    }

 		    return v;

		} catch (SQLException sqe) {
			sqe.printStackTrace();
		    throw new FinderException(sqe.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new FinderException(e.getMessage());
		} finally {
			try {
				if (rs != null) rs.close();
				if (ps != null) ps.close();
				if (con!= null) con.close();
		    } catch (Exception e) {
				e.printStackTrace();
				throw new FinderException (e.getMessage());
	      	}
		}
	}

	public void updateOrderLine(Vector v, String sC_Order_ID) throws SQLException {
		//Enumeration e = null;
		//C_OrderLineData c_od = null;
		Connection con = null;
		PreparedStatement ps = null;
		int i = 0;

		try {
			if(v.size() <= 0 || v == null) {
				throw new SQLException("You can't deleted all records, use 'delete' instead!");
			//} else if(v.size() > 0 && v != null) {
			} else {
				con = Env.getConnection();

				sC_Order_ID = sC_Order_ID.trim();

				//Firstly, Delete all the existing data of C_OrderLine;

				String sSql = "DELETE C_OrderLine WHERE C_Order_ID = ? ";
				ps = con.prepareStatement(sSql);
				ps.setString(1, sC_Order_ID);

				log("updateOrderLine: To del existing data of C_OrderLine!C_Order_ID=" + sC_Order_ID);

				i = ps.executeUpdate();
				if (i == 0) {
					log("DELED No Row From C_OrderLine: " + sC_Order_ID);
				}

				insertOrderLine(v);

				/*
				sSql = "UPDATE C_OrderLine ";
				sSql += " SET AD_Client_ID = ?, ";
				sSql += " AD_ORG_ID = ?, ";
				sSql += " IsActive = ?, ";
				sSql += " Updated = GetDate(), ";
				sSql += " UpdatedBy = ?, ";
				sSql += " Description = ?, ";
				sSql += " M_Product_ID = ?, ";
				sSql += " C_UOM_ID = ?, ";
				sSql += " QtyOrdered = ? ";

				sSql += " WHERE lTrim(C_Order_ID) = ? AND ";
				sSql += " lTrim(C_OrderLine_ID) = ? ";

				ps = con.prepareStatement(sSql);
				e = v.elements();

				while(e.hasMoreElements()) {
					c_od = (C_OrderLineData) e.nextElement();

					ps.setString(1, c_od.getAD_Client_ID());
					ps.setString(2, c_od.getAD_ORG_ID());
					ps.setString(3, c_od.getIsActive());
					ps.setString(4, c_od.getUpdatedBy());
					ps.setString(5, c_od.getDescription());
					ps.setString(6, c_od.getM_Product_ID());
					ps.setString(7, c_od.getC_UOM_ID());
					ps.setInt(8, c_od.getQtyOrdered());

					ps.setString(9, c_od.getC_Order_ID());
					ps.setString(10, c_od.getC_OrderLine_ID());

					i = ps.executeUpdate();

					if(i == 0) {
						log("updateOrderLine: No Row Updated: OrderID= " + c_od.getC_Order_ID() + ", OrderLineID= " + c_od.getC_OrderLine_ID());
						//throw new SQLException("No Row Updated!");
					}
				}
				*/
			}
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			throw sqe;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if(ps != null) ps.close();
				if(con != null) con.close();
			} catch (Exception ex2) {
				ex2.printStackTrace();
			}
		}
	}

	public void insertOrderLine(Vector v) throws SQLException {
		Enumeration e = null;
		C_OrderLineData c_od = null, od = null;
		Connection con = null;
		PreparedStatement ps = null;
		int i = 0;
		String sSql = "";

		log("insertOrderLine in C_Order Entry. Size = " + v.size());

		try {
			if(v.size() <= 0 || v == null) {
				throw new SQLException("You can't create an Order with no OrderLine!");
			//} else if(v.size() > 0 && v != null) {
			} else {
                con = Env.getConnection();
				e = v.elements();

				sSql = "INSERT INTO C_OrderLine (";

				sSql += " C_Order_ID, C_OrderLine_ID, AD_Client_ID, AD_ORG_ID, IsActive, ";
				sSql += " Created, CreatedBy, Updated, UpdatedBy, Description, ";
				sSql += " M_Product_ID, C_UOM_ID, QtyOrdered, ";

				//The fields below are not used.
				sSql += " Line, C_BPartner_ID, C_BPartner_Location_ID, DateOrdered, DatePromised, ";
				sSql += " DateDelivered, DateInvoiced, M_WareHouse_ID, DirectShip, QtyReserved, ";
				sSql += " QtyDelivered, QtyInvoiced, M_Shipper_ID, C_Currency_ID, PriceList, ";
				sSql += " PriceActual, PriceLimit, LineNetAmt, Discount, FreightAmt, ";
				sSql += " C_Charge_ID, ChargeAmt, C_Tax_ID, Lot, SerNo ";

				sSql += " ) ";

				sSql += " VALUES (?, ?, ?, ?, ?, ";
				sSql += " GetDate(), ?, GetDate(), ?, ?, ";
				sSql += " ?, ?, ?, ";

				//The Fields Below Are Not Used.
				sSql += " 0, ' ', ' ', GetDate(), GetDate(), ";
				sSql += " GetDate(), GetDate(), ' ', ' ', 0, ";
				sSql += " 0, 0, ' ', ' ', 0, ";
				sSql += " 0, 0, 0, 0, 0, ";
				sSql += " ' ', 0, ' ', ' ', ' ') ";

                log("insertOrderLine sSql: " + sSql);
				ps = con.prepareStatement(sSql);

				while(e.hasMoreElements()) {
					c_od = (C_OrderLineData) e.nextElement();

					ps.clearParameters();
					ps.setString(1, c_od.getC_Order_ID());
					ps.setString(2, c_od.getC_OrderLine_ID());
					ps.setString(3, c_od.getAD_Client_ID());
					ps.setString(4, c_od.getAD_ORG_ID());
					ps.setString(5, c_od.getIsActive());
					ps.setString(6, c_od.getCreatedBy());
					ps.setString(7, c_od.getUpdatedBy());
					ps.setString(8, c_od.getDescription());
					ps.setString(9, c_od.getM_Product_ID());
					ps.setString(10, c_od.getC_UOM_ID());
					ps.setInt(11, c_od.getQtyOrdered());

					i = ps.executeUpdate();

					if(i == 0) {
						log("insertOrderLine: No Row Inserted!");
						throw new SQLException("No Row Inserted: OrderID= " + c_od.getC_Order_ID() + ", OrderLineID= " + c_od.getC_OrderLine_ID());
					}

					//if(ps != null) ps.close();
				}
			}
		} catch (SQLException sqe) {
			log("insertOrderLine: SQLException: " + sqe.getMessage());
            throw sqe;
		} catch (Exception ex) {
			log("insertOrderLine: Exception: " + ex.getMessage());
			ex.printStackTrace();
		} finally {
			try {
				if(ps != null) ps.close();
				if(con != null) con.close();
			} catch (Exception ex2) {
				log(ex2.getMessage());
			}
		}
	}

	public void update(C_OrderData a_cd) throws RemoteException, LotusException {
		try {
			/*
			this.cd.setC_Order_ID(a_cd.getC_Order_ID());
			this.cd.setAD_ORG_ID(a_cd.getAD_ORG_ID());
			this.cd.setAD_Client_ID(a_cd.getAD_Client_ID());
			this.cd.setIsActive(a_cd.getIsActive());
			this.cd.setUpdatedBy(a_cd.getUpdatedBy());
			this.cd.setName(a_cd.getName());
			this.cd.setDescription(a_cd.getDescription());
			*/

			this.setInfo(a_cd);

			setModified(true);
		} catch (Exception e) {
			e.printStackTrace();
			throw new LotusException("A00001","E","C_Order_ID");
		}
	}

	public C_OrderData getInfo() throws RemoteException, LotusException {
		return this.cd;
	}

	private void setInfo(C_OrderData a_cd) throws RemoteException, LotusException {
		this.cd = a_cd;
	}

	private static void log(String s) {
		if(DEBUG) {
			System.out.println("" + s);
		}
	}
}