Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8842 anupam.sin 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.text.ParseException;
8
import java.text.SimpleDateFormat;
9
import java.util.ArrayList;
10
import java.util.Date;
11
import java.util.List;
12
 
13
import javax.xml.xpath.XPath;
14
import javax.xml.xpath.XPathConstants;
15
import javax.xml.xpath.XPathExpressionException;
16
import javax.xml.xpath.XPathFactory;
17
 
18
import org.apache.log4j.Logger;
19
import org.w3c.dom.Element;
20
import org.w3c.dom.NodeList;
21
import org.xml.sax.InputSource;
22
 
23
public class AFLTrackingService {
24
	private static Logger log = Logger.getLogger(Class.class);
25
 
26
	private static String AFL_URL;
27
 
28
	static{
29
		try {
30
			AFL_URL = ConfigClient.getClient().get("afl_update_url");
31
		} catch (ConfigException e) {
32
		    log.error("Unable to get the tracking url from the config service", e);
33
		}
34
	}
35
 
36
	public List<ShipmentUpdate> getUpdates(String awbNumber){
37
		//String uri = "http://trackntrace.aflwiz.com/aflwiztrack?shpntnum=" + awbNumber;
38
		String uri = AFL_URL + awbNumber;
39
		log.info("AFL Update URL: " + uri);
40
		InputSource inputSource = new InputSource(uri);
41
		XPath xpath = XPathFactory.newInstance().newXPath();
42
 
43
		String expression = "/SHIPMENTTRACK/SHIPMENTREPORT/CHECKPOINTDETAILS/CHECKPOINTS";
44
		NodeList nodes = null;
45
		try {
46
			nodes = (NodeList) xpath.evaluate(expression, inputSource,	XPathConstants.NODESET);
47
		} catch(XPathExpressionException xpee) {
48
			return null;
49
		}
50
 
51
		List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
52
 
53
		System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );
54
		for(int i=0; i<nodes.getLength(); i++) {
55
			Element hawbUpdate = (Element) nodes.item(i);
56
			ShipmentUpdate update = new ShipmentUpdate();
57
			update.date = getElementTextContent(hawbUpdate, "CHECKDATE");
58
			SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd"),
59
				sdfDestination = new SimpleDateFormat("dd-MMM-yyyy");
60
		    Date date = null;
61
			try {
62
				date = sdfSource.parse(update.date);
63
				update.date = sdfDestination.format(date);
64
			} catch (ParseException e) {
65
			    log.error("Unable to parse the tracking date", e);
66
			}
67
			update.time = getElementTextContent(hawbUpdate, "CHECKTIME");
68
			update.city = getElementTextContent(hawbUpdate, "LOCATIONNAME");
69
			update.description = getElementTextContent(hawbUpdate, "CHECKPOINTDESCRIPTION");			
70
			updates.add(update);
71
		}	
72
		return updates;
73
	}
74
 
75
	private String getElementTextContent(Element element, String tagName){
76
		return element.getElementsByTagName(tagName).item(0).getTextContent();
77
	}
78
 
79
	public static void main(String[] args){
80
		System.out.println("AFL number is " + "4252621801242006");
81
		AFLTrackingService service = new AFLTrackingService();
82
		List<ShipmentUpdate> updates = service.getUpdates("4252621801242006");
83
		for(ShipmentUpdate update: updates){
84
			System.out.println(update.toString());
85
		}
86
	}
87
}