Subversion Repositories SmartDukaan

Rev

Rev 5262 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5262 phani.kuma 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
 
15
import org.apache.log4j.Logger;
16
import org.w3c.dom.Element;
17
import org.w3c.dom.NodeList;
18
import org.xml.sax.InputSource;
19
 
20
public class DelhiveryTrackingService {
21
    //public static String Delhivery_URL = "http://track.delhivery.com/api/packages/xml/?token=bda066415b6b56698559e80b1a8345980882c90f&";
22
    private static Logger log = Logger.getLogger(Class.class);
23
 
24
    private static String Delhivery_URL;
25
 
26
    static{
27
        try {
28
        	Delhivery_URL = ConfigClient.getClient().get("Delhivery_update_url");
29
        } catch (ConfigException e) {
30
            log.error("Unable to get the tracking url from the config service", e);
31
        }
32
    }
33
 
34
    public List<ShipmentUpdate> getUpdates(String awbNumber){
35
        String uri = Delhivery_URL + "waybill=" + awbNumber;
36
        log.info("Delhivery Update URL: " + uri);
37
        InputSource inputSource = new InputSource(uri);
38
        XPath xpath = XPathFactory.newInstance().newXPath();
39
 
40
        String expression = "/ShipmentData/Shipment/Scans/ScanDetail";
41
        NodeList nodes = null;
42
        try {
43
            nodes = (NodeList) xpath.evaluate(expression, inputSource,  XPathConstants.NODESET);
44
        } catch(XPathExpressionException xpee) {
45
            return null;
46
        }
47
 
48
        List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
49
 
50
        System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );
51
        for(int i=0; i<=nodes.getLength()-1; i++) {
52
            Element hawbUpdate = (Element) nodes.item(i);
53
            ShipmentUpdate update = new ShipmentUpdate();
54
 
5264 phani.kuma 55
            String dateString = getElementTextContent(hawbUpdate, "ScanDateTime");
5262 phani.kuma 56
            String[] tokens = dateString.split("T");
57
            update.date = tokens[0];
5264 phani.kuma 58
            String[] timetokens = tokens[1].split("\\.");
5262 phani.kuma 59
            update.time = timetokens[0];
60
            update.city = getElementTextContent(hawbUpdate, "ScannedLocation");
61
            update.description = getElementTextContent(hawbUpdate, "Scan") + " - " + getElementTextContent(hawbUpdate, "Instructions");         
62
            updates.add(update);
63
        }   
64
        return updates;
65
    }
66
 
67
    private String getElementTextContent(Element element, String tagName){
68
        return element.getElementsByTagName(tagName).item(0).getTextContent();
69
    }
70
 
71
    public static void main(String[] args){
72
        System.out.println("Delhivery number is " + "13410100004");
73
        DelhiveryTrackingService service = new DelhiveryTrackingService();
74
        List<ShipmentUpdate> updates = service.getUpdates("13410100004");
75
        for(ShipmentUpdate update: updates){
76
            System.out.println(update.toString());
77
        }
78
    }
79
}