Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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