package ibase.dashboard.sfa.ejb;

import ibase.system.config.ConnDriver;
import ibase.webitm.utility.ITMException;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.ejb.Stateless;

/**
 * Session Bean implementation class PlannedVSActualActionTypes
 */
@Stateless
public class PlannedVSActualActionTypes implements PlannedVSActualActionTypesRemote, PlannedVSActualActionTypesLocal {

    /**
     * Default constructor. 
     */
	private  String salesPerson=""; 
	
	
    public String getSalesPerson()
    {
    	return salesPerson;
    }
	public void setSalesPerson(String salesPerson)
    {
    	this.salesPerson = salesPerson;
    }
	public PlannedVSActualActionTypes() {
        
    }
	@Override
    public String getData() throws RemoteException, ITMException
    {
	    // TODO Auto-generated method stub
	    return null;
    }

	@Override
    public String getData(String loginCode) throws RemoteException, ITMException
    {
	    
		String sql = "";
		ResultSet rs = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ConnDriver connDriver = new ConnDriver();
		StringBuffer xmlData =null;

		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			     sql="select initcap(sales_pers.sp_name)as sp_name  from sales_pers where sales_pers = ?";
			    
			        pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, loginCode);
					rs = pstmt.executeQuery();
					if (rs.next())
					{
						
					this.setSalesPerson(rs.getString("sp_name"));
					}
			
			
				sql = "select INITCAP(trim(strg_customer.first_name)||''|| (CASE WHEN trim(strg_customer.middle_name) IS NULL THEN '' ELSE (' '||strg_customer.middle_name) END ) ||' ' ||trim(strg_customer.last_name) )AS DrName from sprs_plan, strg_customer where sprs_plan.plan_status = 'P' AND STRG_CUSTOMER.CUST_TYPE = 'D' AND  sprs_plan.strg_code = strg_customer.sc_code and sprs_plan.sprs_code__plan = ? and sprs_plan.plan_date = to_char(sysdate) minus select INITCAP(trim(strg_customer.first_name)||''|| (CASE WHEN trim(strg_customer.middle_name) IS NULL THEN '' ELSE (' '||strg_customer.middle_name) END ) ||' ' ||trim(strg_customer.last_name) )AS DrName from sprs_plan,strg_meet, strg_customer where sprs_plan.plan_status = 'P' AND STRG_CUSTOMER.CUST_TYPE = 'D' AND sprs_plan.plan_date = strg_meet.event_date AND sprs_plan.strg_code = strg_customer.sc_code AND SPRS_PLAN.STRG_CODE = STRG_MEET.STRG_CODE AND sprs_plan.sprs_code__plan = STRG_MEET.SALES_PERS and sprs_plan.sprs_code__plan = ? and sprs_plan.plan_date = to_char(sysdate) order by 1 ";
				
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, loginCode);
				pstmt.setString(2, loginCode);
				rs = pstmt.executeQuery();
				xmlData = new StringBuffer("<?xml version=\"1.0\"?><Root>");
				while (rs.next())
				{
					xmlData.append("<dr_not_visited>").append("<![CDATA[" +checkNull(rs.getString("DrName"))+ "]]>").append("</dr_not_visited>");
				}
				
				pstmt.close();
				pstmt = null;
				rs.close();
				rs = null;
				
				
				sql="select INITCAP(trim(strg_customer.first_name)||''|| (CASE WHEN trim(strg_customer.middle_name) IS NULL THEN '' ELSE (' '||strg_customer.middle_name) END ) ||' ' ||trim(strg_customer.last_name) )AS DrName from sprs_plan,strg_meet, strg_customer where sprs_plan.plan_status = 'P' AND STRG_CUSTOMER.CUST_TYPE = 'D' AND sprs_plan.plan_date = strg_meet.event_date AND sprs_plan.strg_code = strg_customer.sc_code AND SPRS_PLAN.STRG_CODE = STRG_MEET.STRG_CODE AND sprs_plan.sprs_code__plan = STRG_MEET.SALES_PERS and sprs_plan.sprs_code__plan = ? and sprs_plan.plan_date = to_char(sysdate) order by INITCAP(strg_customer.first_name||' '||strg_customer.middle_name||' '||strg_customer.last_name )"; 
			
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,loginCode);
				rs = pstmt.executeQuery();
				while (rs.next())
				{
					xmlData.append("<dr_visitd>").append("<![CDATA[" + checkNull(rs.getString("DrName"))+ "]]>").append("</dr_visitd>");
				}
				
				pstmt.close();
				pstmt = null;
				rs.close();
				rs = null;
				
				sql=" select INITCAP(trim(strg_customer.first_name)||''|| (CASE WHEN trim(strg_customer.middle_name) IS NULL THEN '' ELSE (' '||strg_customer.middle_name) END ) ||' ' ||trim(strg_customer.last_name) )AS DrName from strg_meet INNER JOIN strg_customer ON strg_customer.sc_code = STRG_MEET.STRG_CODE AND STRG_CUSTOMER.CUST_TYPE = 'D' and STRG_MEET.SALES_PERS = ? and STRG_MEET.EVENT_DATE =  to_char(sysdate) MINUS select INITCAP(trim(strg_customer.first_name)||''|| (CASE WHEN trim(strg_customer.middle_name) IS NULL THEN '' ELSE (' '||strg_customer.middle_name) END ) ||' ' ||trim(strg_customer.last_name) )AS DrName from sprs_plan, strg_customer where sprs_plan.plan_status = 'P' AND STRG_CUSTOMER.CUST_TYPE = 'D' AND  sprs_plan.strg_code = strg_customer.sc_code and sprs_plan.sprs_code__plan = ? and sprs_plan.plan_date = to_char(sysdate) ORDER BY 1"; 
			
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, loginCode);
				pstmt.setString(2, loginCode);
				rs = pstmt.executeQuery();
				while (rs.next())
				{
					xmlData.append("<un_dr_visitd>").append("<![CDATA[" + checkNull(rs.getString("DrName"))+ "]]>").append("</un_dr_visitd>");
				}
				pstmt.close();
				pstmt = null;
				rs.close();
				rs = null;
				xmlData.append("</Root>");
		} catch (Exception e)
		{
			e.printStackTrace();
			System.out.println("Exception in EJB :PlannedVSActualActionTypes:" + e.getMessage());
			throw new ITMException(e);
		} finally
		{
			try
			{
				if (conn != null)
				{
					if (rs != null)
						rs.close();
					rs = null;
					if (pstmt != null)
						pstmt.close();
					pstmt = null;
					conn.close();
					conn = null;
				}
				conn = null;
			} catch (Exception d)
			{
				d.printStackTrace();
				System.out.println("Exception in EJB :PlannedVSActualActionTypes:" + d.getMessage());
				throw new ITMException(d);
			}
		}
		return xmlData.toString();}
		
	private String checkNull(String input)
		{
			if (input == null)
			{
				input = "";
			} else
			{
				input = input.trim();
			}
			return input;
		}
    
}