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