Commit 717c44e3 authored by msingh's avatar msingh

Added method in CommonWmsUtil.java


git-svn-id: http://15.206.35.175/svn/proteus/business-java/trunk@98940 ce508802-f39f-4f6c-b175-0d175dae99d5
parent 248495ec
package ibase.webitm.utility.wms;
import ibase.system.config.ConnDriver;
import ibase.utility.CommonConstants;
import ibase.webitm.bean.wms.Product;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.TransIDGenerator;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
......@@ -13,6 +27,11 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.w3c.dom.Document;
public class CommonWmsUtil {
public static int ALL_PTCN_EX_COUNT;
......@@ -764,4 +783,290 @@ public class CommonWmsUtil {
return dataVolumeMap;
}
// Changed by Sneha on 23-10-2015, for generation of transaction id [end]
// Changed by Manish on 28-10-2015 [start]
public String getDDSalesConfig(String tagName)
{
CommonConstants objCommon =new CommonConstants();
GenericUtility genericUtility=GenericUtility.getInstance();
String fileName = null,xmlString = "",xmlFileName = "";
Document dom=null;
String sCurrentLine = "";
String xmlFile="DDSalesConfig";
String apiUrl = "";
System.out.println(" in setDDSalesConfig -------------------------");
System.out.println("CommonConstants.APPLICATION_CONTEXT-->>["+CommonConstants.APPLICATION_CONTEXT+"]");
System.out.println("CommonConstants.JBOSSHOME-->>["+CommonConstants.JBOSSHOME+"]");
if(CommonConstants.APPLICATION_CONTEXT != null)
{
xmlFileName = CommonConstants.APPLICATION_CONTEXT + "setting" + File.separator + "DDSalesConfig.xml";
}
else
{
xmlFileName = CommonConstants.JBOSSHOME + File.separator + "server" + File.separator + "default" + File.separator + "deploy" + File.separator + "ibase.ear" + File.separator + "ibase.war" + File.separator +"setting" + File.separator +"DDSalesConfig.xml";
}
//fileName = ".." + File.separator + "setting" + File.separator + xmlFile + ".xml";
StringBuilder sb = new StringBuilder();
System.out.println("xmlFileName-->>["+xmlFileName+"]");
try
{
BufferedReader br = new BufferedReader(new FileReader(xmlFileName));
while (( sCurrentLine = br.readLine()) != null)
{
sb.append(sCurrentLine);
}
xmlString =sb.toString();
System.out.println("DDSalesConfig xmlString---->>["+xmlString+"]");
dom = genericUtility.parseString(xmlString);
if(dom != null)
{
apiUrl = genericUtility.getColumnValue(tagName,dom);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return apiUrl;
}
public int Call(Product product,String url, List<Integer> simpleProductIds) throws Exception
{
try
{
System.out.println("url111--->>["+url+"] ["+product.getSku()+"]");
StringBuilder urlParameters = new StringBuilder();
urlParameters.append(GetUrlParameters(Product.class, product));
/* String url = Config.apiUrl;
//Concat simple product ids in url parameter
*/
if(simpleProductIds != null && !simpleProductIds.isEmpty())
{
urlParameters.append("simples=").append(AppendWithDelimeter(simpleProductIds,","));
// url = Config.apiConfigurableUrl; // if parameter available then call configurable api url
}
System.out.println("urlParameters----->>["+urlParameters.toString()+"]");
//url = Config.apiUrl; //API url call, defined in config file
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST"); //Always do the POST method
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setDoOutput(true);
try
{
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters.toString());
wr.flush();
}
catch(Exception e)
{
//Changed by samadhan D15AKAT003 on 18/05/2015 for throw exception. Start
System.out.println("Exception in CommonWMS Util ::" + e.getMessage() + ":");
e.printStackTrace();
throw new Exception(e);
//Changed by samadhan D15AKAT003 on 18/05/2015 for throw exception. End
}
int responseCode = con.getResponseCode();
System.out.println("responseCode-------->>["+responseCode+"]");
switch (responseCode) { //Capture the response code from server
case 500:
System.out.println("Unexpected server error.");
break;
case 404:
System.out.println("Target not found. Either files is missing or moved.");
break;
case 200: // if call successful, it may return 0 in case of product exist
StringBuilder response;
try
{
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
Map map = splitQuery(response.toString());
System.out.println("response---->>: "+response.toString());
System.out.println("Status: "+(String) map.get("status"));
System.out.println("Message: "+(String) map.get("message"));
String id = map.get("id").toString();
if(id != null)
{
return Integer.valueOf((String) map.get("id"));
}
else
{
return 0;
}
}
catch(Exception e)
{
//Changed by samadhan D15AKAT003 on 18/05/2015 for throw exception. Start
System.out.println("Exception in CommonWMS Util ::" + e.getMessage() + ":");
e.printStackTrace();
throw new Exception(e);
//Changed by samadhan D15AKAT003 on 18/05/2015 for throw exception. End
}
}
} catch (IOException e)
{
//Changed by samadhan D15AKAT003 on 18/05/2015 for throw exception. Start
System.out.println("Exception in CommonWMS Util ::" + e.getMessage() + ":");
e.printStackTrace();
throw new Exception(e);
//Changed by samadhan D15AKAT003 on 18/05/2015 for throw exception. End
}
return 0;
}
static Map<String, String> splitQuery(String queryParam) throws UnsupportedEncodingException {
Map<String, String> query_pairs = new LinkedHashMap<String, String>();
String[] pairs = queryParam.split("&");
for (String pair : pairs) {
System.out.println("Current Pair["+pair+"]");
if(pair.indexOf("=") != -1)
{
int idx = pair.indexOf("=");
if(pair != null)
{
if(pair.indexOf("%")!= -1)
{
System.out.println("Before pair--------->>["+pair+"]");
pair = URLEncoder.encode(pair.substring(0, idx), "UTF-8");
System.out.println("After pair--------->>["+pair+"]");
}
}
query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
}
}
return query_pairs;
}
static <T> String GetUrlParameters(Class<T> oObject, Product insObject) {
try {
System.out.println("GetUrlParameters@----->>"+insObject.getLongDescription());
Field[] fields = oObject.getDeclaredFields();
Product objProductInst = insObject;
StringBuilder parameters = new StringBuilder();
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(objProductInst);
parameters.append(String.format("%s=%s&",
//Modifier.toString(field.getModifiers()),
//field.getType().getSimpleName(),
field.getName(), value
));
}
return parameters.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//Changed by samadhan D15AKAT003 on 25/05/2015 for delete product. Start
public int CallDelete(String sku,String url) throws Exception
{
String urlParameters = "";
String responseString = "";
System.out.println("Sku Params["+sku+"]");
urlParameters="skus="+sku+"";
System.out.println("url["+url+"]");
System.out.println("urlParameters: "+urlParameters);
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST"); //Always do the POST method
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setDoOutput(true);
try
{
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
}
catch(Exception e)
{
System.out.println("Exception in CommonWMS Util :: callDelete ::" + e.getMessage() + ":");
e.printStackTrace();
throw new Exception(e);
}
int responseCode = con.getResponseCode();
switch (responseCode)
{ //Capture the response code from server
case 500:
System.out.println("Unexpected server error.");
break;
case 404:
System.out.println("Target not found. Either files is missing or moved.");
break;
case 200: // if call successful, it may return 0 in case of product exist
StringBuilder response;
try
{
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
response = new StringBuilder();
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
System.out.println("Response From magento Server ["+response.toString()+"]");
}
catch(Exception e)
{
System.out.println("Exception in CommonWMS Util :: callDelete ::" + e.getMessage() + ":");
e.printStackTrace();
throw new Exception(e);
}
}
return 0;
}
//Changed by samadhan D15AKAT003 on 25/05/2015 for delete product. End
static String AppendWithDelimeter(List<Integer> intList, String delimeter) {
//Iterator<Integer> i = intList.iterator();
StringBuilder commaAppender = new StringBuilder();
//Looping through the list
for ( int i = 0; i< intList.size(); i++){
//append the value into the builder
commaAppender.append(intList.get(i));
//if the value is not the last element of the list
//then append the comma(,) as well
if ( i != intList.size()-1){
commaAppender.append(delimeter);
}
}
//return intList.stream().map(number -> String.valueOf(number)).collect(toStringJoiner(delimeter));
return commaAppender.toString();
}
// Changed by Manish on 28-10-2015 [end]
}
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