Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4710 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.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 AramexTrackingService {
21
    //public static String ARAMAX_URL = "http://www.aramex.com/track_xml.asp?ShipmentNumber=";
22
    private static Logger log = Logger.getLogger(Class.class);
23
 
24
    private static String ARAMEX_URL;
25
 
26
    static{
27
        try {
28
            ARAMEX_URL = ConfigClient.getClient().get("aramex_update_url");
29
        } catch (ConfigException e) {
30
            //ARAMEX_URL = "file:///home/ashish/Downloads/AWB_Track.xml";
31
            log.error("Unable to get the tracking url from the config service", e);
32
        }
33
    }
34
 
35
    public List<ShipmentUpdate> getUpdates(String awbNumber){
36
        String uri = ARAMEX_URL + awbNumber;
37
        log.info("Aramex Update URL: " + uri);
38
        //String uri = ARAMEX_URL;
39
        InputSource inputSource = new InputSource(uri);
40
        XPath xpath = XPathFactory.newInstance().newXPath();
41
 
42
        String expression = "/HAWBs/HAWBDetails/HAWBHistory/HAWBUpdate";
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
 
57
            String dateString = getElementTextContent(hawbUpdate, "ActionDate");
58
            String[] tokens = dateString.split("\\s");
59
            update.date = tokens[0];
60
            update.time = tokens[1] + " " + tokens[2];
61
            update.city = getElementTextContent(hawbUpdate, "UpdateLocationFormatted");
62
            update.description = getElementTextContent(hawbUpdate, "CustomerDescription") + " - " + getElementTextContent(hawbUpdate, "Comments1");         
63
            updates.add(update);
64
        }   
65
        return updates;
66
    }
67
 
68
    private String getElementTextContent(Element element, String tagName){
69
        return element.getElementsByTagName(tagName).item(0).getTextContent();
70
    }
71
 
72
    public static void main(String[] args){
73
        System.out.println("Aramax number is " + "6199186044");
74
        AramexTrackingService service = new AramexTrackingService();
75
        List<ShipmentUpdate> updates = service.getUpdates("6199186044");
76
        for(ShipmentUpdate update: updates){
77
            System.out.println(update.toString());
78
        }
79
    }
80
}