Subversion Repositories SmartDukaan

Rev

Rev 4710 | Details | Compare with Previous | 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 {
20722 kshitij.so 21
    //public static String ARAMAX_URL = "https://www.aramex.com/track_xml.asp?ShipmentNumber=";
4710 anupam.sin 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
            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 = ARAMEX_URL + awbNumber;
36
        log.info("Aramex Update URL: " + uri);
37
        //String uri = ARAMEX_URL;
38
        InputSource inputSource = new InputSource(uri);
39
        XPath xpath = XPathFactory.newInstance().newXPath();
40
 
41
        String expression = "/HAWBs/HAWBDetails/HAWBHistory/HAWBUpdate";
42
        NodeList nodes = null;
43
        try {
44
            nodes = (NodeList) xpath.evaluate(expression, inputSource,  XPathConstants.NODESET);
45
        } catch(XPathExpressionException xpee) {
46
            return null;
47
        }
48
 
49
        List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
50
 
51
        System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );
52
        for(int i=nodes.getLength()-1; i>=0; i--) {
53
            Element hawbUpdate = (Element) nodes.item(i);
54
            ShipmentUpdate update = new ShipmentUpdate();
55
 
56
            String dateString = getElementTextContent(hawbUpdate, "ActionDate");
57
            String[] tokens = dateString.split("\\s");
58
            update.date = tokens[0];
59
            update.time = tokens[1] + " " + tokens[2];
60
            update.city = getElementTextContent(hawbUpdate, "UpdateLocationFormatted");
61
            update.description = getElementTextContent(hawbUpdate, "CustomerDescription") + " - " + getElementTextContent(hawbUpdate, "Comments1");         
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){
20722 kshitij.so 72
        System.out.println("Aramax number is " + "42227972964");
4710 anupam.sin 73
        AramexTrackingService service = new AramexTrackingService();
20722 kshitij.so 74
        List<ShipmentUpdate> updates = service.getUpdates("42227972964");
4710 anupam.sin 75
        for(ShipmentUpdate update: updates){
76
            System.out.println(update.toString());
77
        }
78
    }
79
}