Commit a4788231 authored by kpandey's avatar kpandey

Added EDetailing dao for EDetailing Analysis

git-svn-id: http://15.206.35.175/svn/proteus/business-java/trunk@191722 ce508802-f39f-4f6c-b175-0d175dae99d5
parent 443f6917
package ibase.dashboard.common.hibernate.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.json.JSONException;
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import ibase.system.config.ConnDriver;
import ibase.utility.E12GenericUtility;
import ibase.utility.UserInfoBean;
public class EDetailingDao {
private UserInfoBean userInfo = null;
public UserInfoBean getUserInfo()
{
return this.userInfo;
}
public void setUserInfo(UserInfoBean userInfo)
{
System.out.println("getUserInfo"+userInfo);
this.userInfo = userInfo;
System.out.println("getUserInfo"+userInfo);
}
public JSONArray getDetailingData( String loginCode, String fromDate, String toDate)
{
System.out.println("loginCode["+loginCode+"]fromDate["+fromDate+"] toDate :["+toDate+"]");
JSONArray jsnArray = new JSONArray();
PreparedStatement pstmt = null;
ResultSet rs = null;
Connection conn =null;
boolean isLocalConn =false;
String sql="";
ConnDriver connDriver = new ConnDriver();
try
{
if(conn == null)
{
conn = getConnection();
isLocalConn =true;
}
sql = " SELECT SM.SALES_PERS AS SP_CODE," +
" EMPLOYEE.EMP_FNAME ||' '||EMPLOYEE.EMP_LNAME AS SP_NAME," +
" EMPLOYEE.HOBBY1 AS TERRITORY," +
" EMP.STAN_CODE__HQ," +
" (SELECT DESCR FROM STATION WHERE STAN_CODE = EMP.STAN_CODE__HQ) AS ESI_HQ," +
" ABM.EMP_CODE AS REPORT_TO," +
" (SELECT EMP_FNAME ||' ' ||EMP_LNAME FROM EMPLOYEE WHERE EMP_CODE = ABM.EMP_CODE ) AS REPORT_TO_NAME," +
" SP.ITEM_SER," +
" (SELECT DESCR FROM ITEMSER WHERE ITEM_SER = SP.ITEM_SER ) AS ITEM_SER_DESCRIPTION," +
" SM.EVENT_DATE AS EVENT_DATE," +
" SC.SC_CODE," +
" SC.FIRST_NAME ||' '|| SC.MIDDLE_NAME ||' '|| SC.LAST_NAME AS CUSTOMER," +
" SCC.CLASS_CODE_DESCR," +
" SCS.SUBCLASS_DESCR," +
" SMB.BRAND_DESCR AS BRAND_DESCR," +
" SMB.BRAND_CODE AS BRAND_CODE," +
" SMB.USAGE_DETAIL_DATA AS DURATION_DATA," +
" SMB.PITCH_SEQUENCE," +
" SM.EVENT_TIME_START AS REPORTED_TIME," +
" SM.CHG_DATE AS CHG_DATE" +
" FROM EMPLOYEE EMPLOYEE" +
" LEFT OUTER JOIN ORG_STRUCTURE EMP" +
" ON EMPLOYEE.EMP_CODE = EMP.EMP_CODE " +
" LEFT OUTER JOIN SALES_PERS SP ON SP.SALES_PERS=EMP.EMP_CODE" +
" LEFT OUTER JOIN ORG_STRUCTURE ABM" +
" ON EMP.POS_CODE__REPTO = ABM.POS_CODE" +
" LEFT OUTER JOIN ORG_STRUCTURE SUPERVISOR" +
" ON ABM.POS_CODE__REPTO = SUPERVISOR.POS_CODE ," +
" STRG_MEET SM left outer join FIELDACTIVITY F on SM.EVENT_TYPE=F.ACTIVITY_CODE LEFT OUTER JOIN STRG_CUSTOMER SC" +
" ON SC.SC_CODE = SM.STRG_CODE LEFT OUTER JOIN STRG_SERIES SS ON SC.SC_CODE=SS.SC_CODE" +
" LEFT OUTER JOIN STRG_CUST_CLASS SCC ON SS.CLASS_CODE=SCC.CLASS_CODE AND SS.CUST_TYPE=SCC.CUST_TYPE" +
" LEFT OUTER JOIN STRG_CUST_SUBCLASS SCS ON SCC.CLASS_CODE=SCS.CLASS_CODE AND SCC.CUST_TYPE=SCS.CUST_TYPE," +
" STRG_BRAND_PITCH SMB" +
" WHERE SMB.TRAN_ID = SM.TRAN_ID" +
" AND SMB.SALES_PERS = SM.SALES_PERS" +
" AND SMB.WORK_DATE = SM.EVENT_DATE" +
" AND EMP.EMP_CODE IN ('"+loginCode+"')" +
" AND SM.SALES_PERS = EMP.EMP_CODE" +
" AND SM.EVENT_DATE >= '"+fromDate+"'" +
" AND SM.EVENT_DATE <='"+toDate+"'" +
" AND F.ACTIVITY_TYPE = 'FW'" +
" AND EMPLOYEE.EMP_CODE = SM.SALES_PERS" +
" ORDER BY EVENT_DATE";
System.out.println("sql["+sql+"]");
pstmt = conn.prepareStatement( sql );
System.out.println("pstmt["+pstmt+"]");
E12GenericUtility genericUtility = new E12GenericUtility();
SimpleDateFormat applFormat = new SimpleDateFormat( genericUtility.getApplDateFormat() );
rs = pstmt.executeQuery();
System.out.println("rs querry execute ["+rs+"]");
while( rs.next() )
{
JSONObject jsonObject = new JSONObject();
String event_date = checkNull(rs.getString( "EVENT_DATE" ));
System.out.println("even date for from and to date before"+event_date);
SimpleDateFormat dbFormat = new SimpleDateFormat( genericUtility.getDBDateFormat() );
Date eventDate = dbFormat.parse( event_date );
event_date = applFormat.format( eventDate );
System.out.println("even date for from and to date after"+event_date);
jsonObject.put( "EVENT_DATE", event_date );
jsonObject.put("Sales Person", checkNull(rs.getString( "SP_CODE" )));
jsonObject.put("Sales Person Name", checkNull(rs.getString( "SP_NAME" )));
jsonObject.put("Territory", checkNull(rs.getString( "TERRITORY" )));//
jsonObject.put("ESI Headquarter", checkNull(rs.getString( "ESI_HQ" )));
jsonObject.put("Manager Name", checkNull(rs.getString( "REPORT_TO_NAME" )));
jsonObject.put("Item Series", checkNull(rs.getString( "ITEM_SER" )));
jsonObject.put("Item Series Description", checkNull(rs.getString( "ITEM_SER_DESCRIPTION" )));
jsonObject.put("Customer", checkNull(rs.getString( "SC_CODE" )));
jsonObject.put("Customer Name", checkNull(rs.getString( "CUSTOMER" )));
jsonObject.put("Class", checkNull(rs.getString( "CLASS_CODE_DESCR" )));
jsonObject.put("Subclass", checkNull(rs.getString( "SUBCLASS_DESCR" )));
jsonObject.put("Brand Description", checkNull(rs.getString( "BRAND_DESCR" )));
jsonObject.put("Brand", checkNull(rs.getString( "BRAND_CODE" )));
jsonObject.put("Priority", checkNull(rs.getString( "PITCH_SEQUENCE" )));
jsonObject.put("Visit Time", checkNull(rs.getString( "REPORTED_TIME" )));
jsonObject.put("CHG_DATE", checkNull(rs.getString( "CHG_DATE" )));
jsonObject.put("Duration", 0 );
String duration_data = checkNull(rs.getString( "DURATION_DATA" ));
jsonObject.put("DURATION_DATA", duration_data);
System.out.println("DURATION_DATA String :: ["+duration_data+"]");
System.out.println("JSONObject data for edetailing["+jsonObject+"]");
long totalMili=0;
//String docName="";
boolean flag = true;
if( duration_data.length() > 0 )
{
System.out.println("DURATION_DATA String if part :: ["+duration_data+"]");
String totalTime ="";
duration_data = duration_data.trim();
System.out.println("DURATION_DATA trim part :: ["+duration_data+"]");
try
{
if( duration_data.startsWith("[") )
{
System.out.println("DURATION_DATA startsWith [ :: ["+duration_data+"]");
JSONArray jsonArray= null;
jsonArray = (JSONArray)new org.json.simple.parser.JSONParser().parse(duration_data);
System.out.println("DURATION_DATA Arraysize:: ["+jsonArray.size()+"] DURATION_DATA Array:: ["+jsonArray+"]");
int jsnArrSize =jsonArray.size();
for( int i=0; i< jsnArrSize;i++ )
{
JSONObject jsnObject = (JSONObject)jsonArray.get(i);
totalTime = String.valueOf(jsnObject.get("TOTAL_TIME"));
String docId = (String) jsnObject.get("DOC_ID");
String docName = (String) jsnObject.get("DOC_NAME");
JSONObject docInfoJson = getDocObj(docId);
String docTypeAttch = (String) docInfoJson.get("DOC_TYPE_ATTACH");
String fileTypeAttch = (String) docInfoJson.get("FILE_TYPE_ATTACH");
System.out.println("docTypeAttch :["+docTypeAttch+"] fileTypeAttch :["+fileTypeAttch+"]");
jsonObject.put("Document Type", docTypeAttch);
jsonObject.put("Content Type", fileTypeAttch);
jsonObject.put("Content Name", docName);
System.out.println("totalTime["+totalTime+"]");
if( i>0 && flag )
{
JSONObject jsnObj = jsonObject;
jsnArray.add( jsnObj );
}
System.out.println("totalTime["+totalTime+"]");
if( totalTime.length()>0 )
{
totalMili =Integer.parseInt( totalTime );
System.out.println("totalMili["+totalMili+"]");
putTotalTimeInJsn( totalMili, jsonObject );
String time = String.valueOf( jsonObject.get("Duration") );
System.out.println("time is 1111["+time+"]");
if( time.equalsIgnoreCase("0") )
{
flag = false;
continue;
}
else
{
flag = true;
}
}
}
if( !flag )
{
continue;
}
}
if( duration_data.startsWith("{") )
{
System.out.println("DURATION_DATA startsWith { :: ["+duration_data+"]");
JSONObject json= null;
json = (JSONObject)new org.json.simple.parser.JSONParser().parse(duration_data);
totalTime = String.valueOf(json.get("TOTAL_TIME"));
System.out.println( "totalTime["+totalTime+"]" );
String docId = (String) json.get("DOC_ID");
String docName = (String) json.get("DOC_NAME");
JSONObject docInfoJson = getDocObj(docId);
String docTypeAttch = (String) docInfoJson.get("DOC_TYPE_ATTACH");
String fileTypeAttch = (String) docInfoJson.get("FILE_TYPE_ATTACH");
System.out.println("docTypeAttch :["+docTypeAttch+"] fileTypeAttch :["+fileTypeAttch+"]");
jsonObject.put("Document Type", docTypeAttch);
jsonObject.put("Content Type", fileTypeAttch);
jsonObject.put("Content Name", docName);
if( totalTime.length()>0 )
{
totalMili =Integer.parseInt( totalTime );
System.out.println("totalMili["+totalMili+"]");
putTotalTimeInJsn( totalMili, jsonObject );
String time = String.valueOf( jsonObject.get("Duration") );
System.out.println("time is 1212["+time+"]");
if( time.equalsIgnoreCase("0") )
{
flag = false;
continue;
}
else
{
flag = true;
}
}
}
System.out.println("totalMili["+totalMili+"]");
}
catch (Exception e)
{
System.out.println("Exception in json ["+e.getMessage()+"]");
}
}
jsnArray.add(jsonObject );
}
System.out.println("jsnArray for edetailing["+jsnArray+"]");
if (rs!= null)
{
rs.close();
rs = null;
}
if(pstmt!=null)
{
pstmt.close();
pstmt = null;
}
if (conn != null)
{
conn.close();
conn = null;
}
}
catch (Exception e)
{
System.out.println("Exception in EDetailingDao["+e.getMessage()+"]");
}
finally
{
try
{
if(rs != null)
{
rs.close();
rs = null;
}
if(pstmt != null)
{
pstmt.close();
pstmt = null;
}
if(isLocalConn && conn != null && ! conn.isClosed() )
{
conn.close();
conn = null;
}
}
catch(Exception e)
{
}
}
return jsnArray;
}
public JSONObject getDocObj(String docId) {
System.out.println("Inside getDocObj:: docId["+docId+"]");
PreparedStatement pstmt = null;
ResultSet rs = null;
Connection conn =null;
boolean isLocalConn =false;
JSONObject docInfoJson = new JSONObject();
try
{
if(conn == null)
{
conn = getConnection();
isLocalConn =true;
}
String sql = "select DOC_TYPE_ATTACH,FILE_TYPE_ATTACH from doc_transaction_link where doc_id = '"+docId+"'";
System.out.println("sql+++"+sql);
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
if(rs.next())
{
docInfoJson.put("DOC_TYPE_ATTACH", checkNull(rs.getString( "DOC_TYPE_ATTACH" )));
docInfoJson.put("FILE_TYPE_ATTACH", checkNull(rs.getString( "FILE_TYPE_ATTACH" )));
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs != null)
{
rs.close();
rs = null;
}
if(pstmt != null)
{
pstmt.close();
pstmt = null;
}
if(isLocalConn && conn != null && ! conn.isClosed() )
{
conn.close();
conn = null;
}
}
catch(Exception e)
{
}
}
return docInfoJson;
}
public Connection getConnection()
{
Connection conn = null;
ConnDriver connDriver = new ConnDriver();
try
{
if(this.userInfo != null)
{
String transDB = this.userInfo.getTransDB();
System.out.println("transDB :: ["+transDB+"]");
if( transDB != null && transDB.trim().length() > 0 && !"null".equalsIgnoreCase(transDB))
{
conn = connDriver.getConnectDB(transDB);
connDriver = null;
}
else
{
conn = connDriver.getConnectDB("DriverITM");
connDriver = null;
}
}
else
{
conn = connDriver.getConnectDB("DriverITM");
connDriver = null;
}
}
catch(Exception e)
{
System.out.println("Exception :[getConnection] :==>\n"+e.getMessage());
e.printStackTrace();
}
return conn;
}
private String checkNull(String str)
{
if( str == null || str.equalsIgnoreCase("ull") )
{
return "";
}
else
{
return str.trim() ;
}
}
private void putTotalTimeInJsn(long totalMili, JSONObject jsonObject ) throws JSONException
{
double totalMinute = 0;
long minutes = (totalMili / 1000) / 60;
long seconds = (totalMili / 1000) % 60;
System.out.println("minutes["+minutes+"]seconds["+seconds+"]");
if( seconds != 0 )
{
String strTime = minutes+"."+seconds;
totalMinute = Double.parseDouble( strTime );
jsonObject.put( "Duration", totalMinute );
}
else
{
jsonObject.put( "Duration", minutes );
}
if( totalMili< 60000 )
{
minutes = (totalMili / 1000);
String strTime = "0"+"."+minutes;
totalMinute = Double.parseDouble( strTime );
jsonObject.put( "Duration", totalMinute );
}
System.out.println("jsonObject in putTotalTimeInJsn"+jsonObject);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment