Commit 9c696fbc authored by vbhosale's avatar vbhosale

Added new ejb to view sales trends dashboard (dwh_sales_sum)

git-svn-id: http://15.206.35.175/svn/proteus/business-java/trunk@193097 ce508802-f39f-4f6c-b175-0d175dae99d5
parent 6b07698e
package ibase.dashboard.scm.ejb;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONArray;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import ibase.system.config.ConnDriver;
import ibase.utility.E12GenericUtility;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.ITMException;
public class SalesTrends extends ValidatorEJB implements SalesTrendsLocal, SalesTrendsRemote
{
/* @Override
public JSONArray getSalesSummary(String dataSourceName) throws RemoteException, ITMException {
JSONArray salesSummaryArr = new JSONArray();
return null;
}
*/
@SuppressWarnings({ "null", "unchecked" })
@Override
public String getSalesSummary( String dataSourceName ) throws Exception
{
System.out.println("INSIDE SalesTrends ::::::::::::::: getSalesSummary ["+dataSourceName+"]");
JSONObject dashboardConfig = (JSONObject) xmlToJson(dataSourceName);
System.out.println("Inside get sales Summary "+dashboardConfig);
JSONObject group0=dashboardConfig.optJSONObject("group0");
JSONObject Header0=group0.optJSONObject("Header0");
JSONObject Detail1=Header0.optJSONObject("Detail1");
String itemser_fm="";
itemser_fm=Detail1.optString("ITEM_SER__FR");
String itemser_to="";
itemser_to=Detail1.optString("ITEM_SER__TO");
String period="";
period=Detail1.optString("PERIOD");
String period1=getPeriod(period);
Double grossValue=0.0;
System.out.println("period >>>>"+period);
if(period !="" && !period.equalsIgnoreCase("{}")) {
System.out.println("period >>>>Inside");
String year =period.substring(0, 4);
String month =period.substring(4,6 );
JSONObject netSummary=getNetAvg(year, month);
System.out.println("net netSummary "+netSummary);
Integer year1=Integer.parseInt(year)-1;
year=year1.toString();
JSONObject netSummaryPrv=getNetAvg(year, month);
System.out.println("net netSummaryPrv "+netSummaryPrv);
grossValue=calculateGross(netSummary, netSummaryPrv);
}
JSONArray salesSummaryArr = null;
String salesTrendsData = "";
String sqlCurrYear = "" , sqlPrevYear = "";
PreparedStatement pStmt = null;
ResultSet rs = null;
Connection conn = null;
try
{
salesSummaryArr = new JSONArray();
System.out.println("BEFORE CONN ESTBD");
ConnDriver connDriver = new ConnDriver();
conn = connDriver.getConnectDB( getUserInfo().getTransDB() );
System.out.println("AFTER CONN ESTBD");
JSONObject rowData = null;
sqlCurrYear = "select DSS.DOC_DATE, " +
" DSS.CUST_CODE, " +
" (SELECT CUST_NAME from CUSTOMER where CUST_CODE = DSS.CUST_CODE) as CUST_NAME, " +
" DSS.SITE_CODE, " +
" (SELECT DESCR from SITE where SITE_CODE = DSS.SITE_CODE) as SITE_DESCR, " +
" DSS.ITEM_CODE, I.DESCR, " +
" I.ITEM_SER, " +
" ISER.DESCR AS ISER_DESCR, " +
" ISER.PRODUCT_LINE, " +
" (SELECT descr from GENCODES where RPAD(fld_value, 5, '' ) = ISER.PRODUCT_LINE) as PRODUCT_LINE_DESCR, " +
" DSS.NET_SALES_VALUE, " +
" DSS.SALES_QTY, " +
" DSS.FREE_SALES_VALUE, " +
" DSS.FREE_QTY, " +
" DSS.GROSS_SALES_VALUE " +
" from DWH_SALES_SUM DSS " +
" LEFT OUTER JOIN ITEM I ON DSS.ITEM_CODE = I.ITEM_CODE " +
" LEFT OUTER JOIN ITEMSER ISER ON I.ITEM_SER = ISER.ITEM_SER " +
" WHERE " +
" ISER.item_ser >= '"+itemser_fm+"' " +
" AND ISER.item_ser <= '"+itemser_to+"' "
+ period1;
System.out.println("SQL QUERY EXECUTED ::::::::::::::::: " + sqlCurrYear );
pStmt = conn.prepareStatement( sqlCurrYear );
rs = pStmt.executeQuery();
E12GenericUtility genericUtility = new E12GenericUtility();
String applDate=genericUtility.getApplDateFormat();
String dbDate=genericUtility.getDBDateFormat();
while( rs.next() )
{
String docDate=rs.getString("DOC_DATE");
docDate=genericUtility.getValidDateString(docDate, dbDate, applDate);
System.out.println("doc date "+docDate);
rowData = new JSONObject();
rowData.put("DOC_DATE", docDate);
rowData.put("ITEM_CODE", rs.getString("ITEM_CODE"));
rowData.put("SALES_QTY", rs.getInt("SALES_QTY"));
rowData.put("CUST_CODE", rs.getString("CUST_CODE"));
rowData.put("CUST_NAME", rs.getString("CUST_NAME"));
rowData.put("SITE_CODE", rs.getString("SITE_CODE"));
rowData.put("SITE_DESCR", rs.getString("SITE_DESCR"));
rowData.put("ITEM_CODE", rs.getString("ITEM_CODE"));
rowData.put("ITEM_SER", rs.getString("ITEM_SER"));
rowData.put("DESCR", rs.getString("DESCR"));
rowData.put("FREE_SALES_VALUE", rs.getInt("FREE_SALES_VALUE"));
rowData.put("FREE_QTY", rs.getInt("FREE_QTY"));
rowData.put("GROSS_SALES_VALUE", rs.getInt("GROSS_SALES_VALUE"));
rowData.put("PRODUCT_LINE", rs.getString("PRODUCT_LINE"));
rowData.put("PRODUCT_LINE_DESCR", rs.getString("PRODUCT_LINE_DESCR"));
rowData.put("NET_SALES_VALUE", rs.getInt("NET_SALES_VALUE"));
rowData.put("GROSS_VALUE",grossValue);
salesSummaryArr.add(rowData);
}
salesTrendsData = salesSummaryArr.toString();
System.out.println("getSalesSummary DATA :::::::::::::: " + salesTrendsData);
if( pStmt != null )
{
pStmt.close();
pStmt = null;
}
if ( rs != null )
{
rs.close();
rs = null;
}
}
catch(Exception e )
{
System.err.println( "Exception :: SalesTrends : getSalesSummary : ["+ e.getMessage() +"]" );
throw new ITMException(e);
}
finally
{
try
{
if( pStmt != null )
{
pStmt.close();
pStmt = null;
}
if ( rs != null )
{
rs.close();
rs = null;
}
if ( conn != null )
{
conn.close();
conn = null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
return salesSummaryArr.toString();
}
private String getPeriod(String period) throws Exception {
System.out.println("Get Period "+period);
String period1="";
PreparedStatement pStmt = null;
ResultSet rs = null;
Connection conn = null;
E12GenericUtility genericUtility = new E12GenericUtility();
String applDate=genericUtility.getApplDateFormat();
String dbDate=genericUtility.getDBDateFormat();
try
{
ConnDriver connDriver = new ConnDriver();
conn = connDriver.getConnectDB( getUserInfo().getTransDB() );
JSONObject rowData = null;
String sqlCurrYear = "select fr_date,to_date from period where code='"+period+"'";
System.out.println("SQL QUERY getPeriod ::::::::::::::::: " + sqlCurrYear );
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dbDate);
pStmt = conn.prepareStatement( sqlCurrYear );
rs = pStmt.executeQuery();
while( rs.next() )
{
System.out.println("date >>>>>>>>>>>>>["+rs.getDate("fr_date")+"] ["+rs.getDate("to_date")+"]");
Date frmDate=rs.getDate("fr_date");
Date toDate=rs.getDate("to_date");
String frmDate1=simpleDateFormat.format(frmDate);
String toDate1=simpleDateFormat.format(toDate);
System.out.println("date ::::::::::::::["+frmDate1+"] ["+toDate1+"]");
period1=" and DSS.DOC_DATE between TO_DATE('"+ frmDate1+"','"+dbDate+"') AND TO_DATE('"+toDate1+"','"+dbDate+"')";
System.out.println(">>>>>>>>>.. "+period1);
}
System.out.println("period :::::"+period1);
if( pStmt != null )
{
pStmt.close();
pStmt = null;
}
if ( rs != null )
{
rs.close();
rs = null;
}
}
catch(Exception e )
{
System.err.println( "Exception :: SalesTrends : getSalesSummary : ["+ e.getMessage() +"]" );
throw new ITMException(e);
}
finally
{
try
{
if( pStmt != null )
{
pStmt.close();
pStmt = null;
}
if ( rs != null )
{
rs.close();
rs = null;
}
if ( conn != null )
{
conn.close();
conn = null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
return period1;
}
private JSONObject getNetAvg(String year,String month) throws ITMException {
System.out.println("get net avg ["+year+"] month ["+month+"]");
PreparedStatement pStmt = null;
ResultSet rs = null;
Connection conn = null;
JSONObject rowData = new JSONObject();
try
{
ConnDriver connDriver = new ConnDriver();
conn = connDriver.getConnectDB( getUserInfo().getTransDB() );
String sqlCurrYear = "select DSS_YEAR, DSS_MONTH, SUM(DSS_NSV) as NET_AVG " +
" from( " +
" select to_char(doc_date,'YYYY') DSS_YEAR, to_char(doc_date,'MM') DSS_MONTH, NET_SALES_VALUE DSS_NSV " +
" from DWH_SALES_SUM " +
" ) " +
" where DSS_YEAR = '"+year+"' " +
" AND DSS_MONTH = '"+month+"' " +
" group by DSS_YEAR, DSS_MONTH " +
" order by DSS_YEAR, DSS_MONTH ";
System.out.println("SQL QUERY EXECUTED ::::::::::::::::: " + sqlCurrYear );
pStmt = conn.prepareStatement( sqlCurrYear );
rs = pStmt.executeQuery();
while( rs.next() )
{
rowData = new JSONObject();
rowData.put("DSS_YEAR", rs.getString("DSS_YEAR"));
rowData.put("DSS_MONTH", rs.getString("DSS_MONTH"));
rowData.put("NET_AVG", rs.getInt("NET_AVG"));
}
if( pStmt != null )
{
pStmt.close();
pStmt = null;
}
if ( rs != null )
{
rs.close();
rs = null;
}
}
catch(Exception e )
{
System.err.println( "Exception :: SalesTrends : getSalesSummary : ["+ e.getMessage() +"]" );
throw new ITMException(e);
}
finally
{
try
{
if( pStmt != null )
{
pStmt.close();
pStmt = null;
}
if ( rs != null )
{
rs.close();
rs = null;
}
if ( conn != null )
{
conn.close();
conn = null;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
System.out.println("row data "+rowData);
return rowData;
}
public JSONObject xmlToJson(String transData) throws JSONException
{
try
{
E12GenericUtility utility = new E12GenericUtility();
Document dashboardDom = utility.parseString(transData);
NodeList rootNodeList = dashboardDom.getElementsByTagName("DocumentRoot");
Node rootNode = null;
if( rootNodeList != null && rootNodeList.getLength() > 0 ){
rootNode = rootNodeList.item(0);
return (JSONObject) xmlToJson(rootNode);
}
}
catch (ITMException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public Object xmlToJson(Node xmlObj) throws JSONException
{
// Create the return object
JSONObject jsonObj = new JSONObject();
// console.log('xmlObj.nodeType---', xmlObj.nodeType, xmlObj);
if (xmlObj.getNodeType() == Node.ELEMENT_NODE) { // element
// do attributes
if (xmlObj.hasAttributes()) {
NamedNodeMap attributes = xmlObj.getAttributes();
int attribLen = attributes.getLength();
for (int j = 0; j < attribLen; j++) {
Node attribute = attributes.item(j);
String name = attribute.getNodeName();
String value = attribute.getNodeValue();
jsonObj.put(name, value);
}
}
}
//System.out.println("jsonObj>>" + jsonObj);
// do children
if (xmlObj.hasChildNodes()) {
NodeList childNodes = xmlObj.getChildNodes();
int childNodeLen = childNodes.getLength();
for (int i = 0; i < childNodeLen; i++)
{
Node childNode = childNodes.item(i);
String childNodeName = childNode.getNodeName();
String childNodeValue = childNode.getNodeValue();
if("#cdata-section".equalsIgnoreCase(childNodeName) )
{
if (xmlObj.hasAttributes())
{
jsonObj.put("value", childNodeValue);
return jsonObj;
}
else
{
return childNodeValue;
}
}
else if( !"#text".equalsIgnoreCase(childNodeName) )
{
System.out.println(childNodeName + "<<childNodeValue>>" + childNodeValue);
if( jsonObj.optJSONObject(childNodeName) != null )
{
JSONObject old = jsonObj.optJSONObject(childNodeName);
jsonObj.put(childNodeName, new JSONArray());
jsonObj.optJSONArray(childNodeName).put(old);
jsonObj.optJSONArray(childNodeName).put(xmlToJson(childNode));
}
else if( jsonObj.optJSONArray(childNodeName) != null )
{
jsonObj.optJSONArray(childNodeName).put(xmlToJson(childNode));
}
else
{
jsonObj.put(childNodeName, xmlToJson(childNode));
}
}
}
}
//System.out.println(xmlObj.getNodeName() + ">>childNodeName>" + jsonObj);
return jsonObj;
}
private Double calculateGross(JSONObject netSummary,JSONObject netSummaryPrv) throws JSONException{
System.out.println("netSummary "+netSummary+" netSummaryPrv "+netSummaryPrv);
Double grossValue=0.0;
Double netSumm=netSummary.optDouble("NET_AVG");
Double netSummPv=netSummaryPrv.optDouble("NET_AVG");
System.out.println("net netSummPv "+(netSumm-netSummPv));
Double minusNet=netSumm-netSummPv;
grossValue=(minusNet/netSumm)*100;
System.out.println("grossValue netSummPv "+grossValue);
return grossValue;
}
}
package ibase.dashboard.scm.ejb;
import java.rmi.RemoteException;
import org.json.JSONException;
import org.json.simple.JSONArray;
import ibase.webitm.utility.ITMException;
public interface SalesTrendsLocal
{
public String getSalesSummary(String dataSourceName) throws RemoteException, ITMException, JSONException, Exception;
}
package ibase.dashboard.scm.ejb;
import java.rmi.RemoteException;
import org.json.JSONException;
import org.json.simple.JSONArray;
import ibase.webitm.utility.ITMException;
public interface SalesTrendsRemote {
public String getSalesSummary(String dataSourceName) throws RemoteException, ITMException, JSONException, Exception;
}
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