Subversion Repositories SmartDukaan

Rev

Rev 3093 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
745 chandransh 1
package in.shop2020.serving.services;
2
 
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.serving.model.ShipmentUpdate;
5
import in.shop2020.thrift.clients.config.ConfigClient;
6
 
7
import java.util.ArrayList;
8
import java.util.List;
9
 
10
import javax.xml.xpath.XPath;
11
import javax.xml.xpath.XPathConstants;
12
import javax.xml.xpath.XPathExpressionException;
13
import javax.xml.xpath.XPathFactory;
14
 
832 rajveer 15
 
16
import org.apache.log4j.Logger;
745 chandransh 17
import org.w3c.dom.Element;
18
import org.w3c.dom.NodeList;
19
import org.xml.sax.InputSource;
20
 
21
public class BlueDartTrackingService {
832 rajveer 22
	private static Logger log = Logger.getLogger(Class.class);
745 chandransh 23
 
24
	private static String BLUEDART_URL;
25
 
26
	static{
27
		try {
28
			BLUEDART_URL = ConfigClient.getClient().get("bluedart_update_url");
29
		} catch (ConfigException e) {
2942 chandransh 30
		    log.error("Unable to get the tracking url from the config service", e);
6217 amar.kumar 31
		    BLUEDART_URL = "http://www.bluedart.com/servlet/RoutingServlet?handler=tnt&action=custawbquery&loginid=DEL24119&awb=awb&format=XML&lickey=6265d61bafa6292c5ddfdb1ee335ca80&verno=1.3&scan=1&numbers=";
745 chandransh 32
		}
33
	}
34
 
35
	public List<ShipmentUpdate> getUpdates(String awbNumber){
903 chandransh 36
		String uri = BLUEDART_URL + awbNumber + ",";
37
		//String uri = BLUEDART_URL;
745 chandransh 38
		log.info("Blue Dart Update URL: " + uri);
39
		InputSource inputSource = new InputSource(uri);
40
		XPath xpath = XPathFactory.newInstance().newXPath();
41
 
42
		String expression = "/ShipmentData/Shipment/Scans/ScanDetail";
43
		NodeList nodes = null;
44
		try {
45
			nodes = (NodeList) xpath.evaluate(expression, inputSource,	XPathConstants.NODESET);
46
		} catch(XPathExpressionException xpee) {
47
			return null;
48
		}
49
 
50
		List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
51
 
52
		System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );
53
		for(int i=nodes.getLength()-1; i>=0; i--) {
54
			Element hawbUpdate = (Element) nodes.item(i);
55
			ShipmentUpdate update = new ShipmentUpdate();
56
			update.date = getElementTextContent(hawbUpdate, "ScanDate");
57
			update.time = getElementTextContent(hawbUpdate, "ScanTime");
58
			update.city = getElementTextContent(hawbUpdate, "ScannedLocation");
59
			update.description = getElementTextContent(hawbUpdate, "Scan");			
60
			updates.add(update);
61
		}	
62
		return updates;
63
	}
64
 
65
	private String getElementTextContent(Element element, String tagName){
66
		return element.getElementsByTagName(tagName).item(0).getTextContent();
67
	}
68
 
69
	public static void main(String[] args){
70
		System.out.println("Aramax number is " + "6199186044");
71
		BlueDartTrackingService service = new BlueDartTrackingService();
3093 mandeep.dh 72
		List<ShipmentUpdate> updates = service.getUpdates("43726936680");
745 chandransh 73
		for(ShipmentUpdate update: updates){
74
			System.out.println(update.toString());
75
		}
76
	}
77
}