| 459 |
rajveer |
1 |
package in.shop2020.serving.services;
|
|
|
2 |
|
|
|
3 |
|
| 517 |
rajveer |
4 |
import in.shop2020.config.ConfigException;
|
| 745 |
chandransh |
5 |
import in.shop2020.serving.model.ShipmentUpdate;
|
| 517 |
rajveer |
6 |
import in.shop2020.thrift.clients.config.ConfigClient;
|
| 459 |
rajveer |
7 |
|
| 517 |
rajveer |
8 |
import java.util.ArrayList;
|
|
|
9 |
import java.util.List;
|
|
|
10 |
|
| 459 |
rajveer |
11 |
import javax.xml.xpath.XPath;
|
|
|
12 |
import javax.xml.xpath.XPathConstants;
|
|
|
13 |
import javax.xml.xpath.XPathExpressionException;
|
|
|
14 |
import javax.xml.xpath.XPathFactory;
|
|
|
15 |
|
| 832 |
rajveer |
16 |
import org.apache.log4j.Logger;
|
| 745 |
chandransh |
17 |
import org.w3c.dom.Element;
|
| 459 |
rajveer |
18 |
import org.w3c.dom.NodeList;
|
|
|
19 |
import org.xml.sax.InputSource;
|
|
|
20 |
|
| 545 |
rajveer |
21 |
public class AramexTrackingService {
|
| 517 |
rajveer |
22 |
//public static String ARAMAX_URL = "http://www.aramex.com/track_xml.asp?ShipmentNumber=";
|
| 832 |
rajveer |
23 |
private static Logger log = Logger.getLogger(Class.class);
|
| 517 |
rajveer |
24 |
|
| 545 |
rajveer |
25 |
private static String ARAMEX_URL;
|
| 459 |
rajveer |
26 |
|
| 517 |
rajveer |
27 |
static{
|
|
|
28 |
try {
|
| 545 |
rajveer |
29 |
ARAMEX_URL = ConfigClient.getClient().get("aramex_update_url");
|
| 517 |
rajveer |
30 |
} catch (ConfigException e) {
|
| 745 |
chandransh |
31 |
//ARAMEX_URL = "file:///home/ashish/Downloads/AWB_Track.xml";
|
| 517 |
rajveer |
32 |
e.printStackTrace();
|
|
|
33 |
}
|
| 459 |
rajveer |
34 |
}
|
|
|
35 |
|
| 745 |
chandransh |
36 |
public List<ShipmentUpdate> getUpdates(String awbNumber){
|
|
|
37 |
String uri = ARAMEX_URL + awbNumber;
|
|
|
38 |
log.info("Aramex Update URL: " + uri);
|
|
|
39 |
//String uri = ARAMEX_URL;
|
| 517 |
rajveer |
40 |
InputSource inputSource = new InputSource(uri);
|
|
|
41 |
XPath xpath = XPathFactory.newInstance().newXPath();
|
|
|
42 |
|
| 459 |
rajveer |
43 |
String expression = "/HAWBs/HAWBDetails/HAWBHistory/HAWBUpdate";
|
|
|
44 |
NodeList nodes = null;
|
|
|
45 |
try {
|
| 517 |
rajveer |
46 |
nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
|
| 745 |
chandransh |
47 |
} catch(XPathExpressionException xpee) {
|
| 459 |
rajveer |
48 |
return null;
|
|
|
49 |
}
|
|
|
50 |
|
| 745 |
chandransh |
51 |
List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
|
| 459 |
rajveer |
52 |
|
| 745 |
chandransh |
53 |
System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );
|
| 459 |
rajveer |
54 |
for(int i=nodes.getLength()-1; i>=0; i--) {
|
| 745 |
chandransh |
55 |
Element hawbUpdate = (Element) nodes.item(i);
|
|
|
56 |
ShipmentUpdate update = new ShipmentUpdate();
|
|
|
57 |
|
|
|
58 |
String dateString = getElementTextContent(hawbUpdate, "ActionDate");
|
| 517 |
rajveer |
59 |
String[] tokens = dateString.split("\\s");
|
| 745 |
chandransh |
60 |
update.date = tokens[0];
|
|
|
61 |
update.time = tokens[1] + " " + tokens[2];
|
|
|
62 |
update.city = getElementTextContent(hawbUpdate, "UpdateLocationFormatted");
|
|
|
63 |
update.description = getElementTextContent(hawbUpdate, "CustomerDescription") + " - " + getElementTextContent(hawbUpdate, "Comments1");
|
| 517 |
rajveer |
64 |
updates.add(update);
|
| 745 |
chandransh |
65 |
}
|
| 517 |
rajveer |
66 |
return updates;
|
| 459 |
rajveer |
67 |
}
|
| 745 |
chandransh |
68 |
|
|
|
69 |
private String getElementTextContent(Element element, String tagName){
|
|
|
70 |
return element.getElementsByTagName(tagName).item(0).getTextContent();
|
| 459 |
rajveer |
71 |
}
|
| 517 |
rajveer |
72 |
|
| 459 |
rajveer |
73 |
public static void main(String[] args){
|
| 745 |
chandransh |
74 |
System.out.println("Aramax number is " + "6199186044");
|
|
|
75 |
AramexTrackingService service = new AramexTrackingService();
|
|
|
76 |
List<ShipmentUpdate> updates = service.getUpdates("6199186044");
|
|
|
77 |
for(ShipmentUpdate update: updates){
|
|
|
78 |
System.out.println(update.toString());
|
|
|
79 |
}
|
| 459 |
rajveer |
80 |
}
|
|
|
81 |
}
|