Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.services;

import in.shop2020.config.ConfigException;
import in.shop2020.serving.model.ShipmentUpdate;
import in.shop2020.thrift.clients.config.ConfigClient;

import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class AramexTrackingService {
    //public static String ARAMAX_URL = "http://www.aramex.com/track_xml.asp?ShipmentNumber=";
    private static Logger log = Logger.getLogger(Class.class);
    
    private static String ARAMEX_URL;

    static{
        try {
            ARAMEX_URL = ConfigClient.getClient().get("aramex_update_url");
        } catch (ConfigException e) {
            //ARAMEX_URL = "file:///home/ashish/Downloads/AWB_Track.xml";
            log.error("Unable to get the tracking url from the config service", e);
        }
    }
    
    public List<ShipmentUpdate> getUpdates(String awbNumber){
        String uri = ARAMEX_URL + awbNumber;
        log.info("Aramex Update URL: " + uri);
        //String uri = ARAMEX_URL;
        InputSource inputSource = new InputSource(uri);
        XPath xpath = XPathFactory.newInstance().newXPath();

        String expression = "/HAWBs/HAWBDetails/HAWBHistory/HAWBUpdate";
        NodeList nodes = null;
        try {
            nodes = (NodeList) xpath.evaluate(expression, inputSource,  XPathConstants.NODESET);
        } catch(XPathExpressionException xpee) {
            return null;
        }
        
        List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
        
        System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );
        for(int i=nodes.getLength()-1; i>=0; i--) {
            Element hawbUpdate = (Element) nodes.item(i);
            ShipmentUpdate update = new ShipmentUpdate();

            String dateString = getElementTextContent(hawbUpdate, "ActionDate");
            String[] tokens = dateString.split("\\s");
            update.date = tokens[0];
            update.time = tokens[1] + " " + tokens[2];
            update.city = getElementTextContent(hawbUpdate, "UpdateLocationFormatted");
            update.description = getElementTextContent(hawbUpdate, "CustomerDescription") + " - " + getElementTextContent(hawbUpdate, "Comments1");         
            updates.add(update);
        }   
        return updates;
    }
    
    private String getElementTextContent(Element element, String tagName){
        return element.getElementsByTagName(tagName).item(0).getTextContent();
    }
    
    public static void main(String[] args){
        System.out.println("Aramax number is " + "6199186044");
        AramexTrackingService service = new AramexTrackingService();
        List<ShipmentUpdate> updates = service.getUpdates("6199186044");
        for(ShipmentUpdate update: updates){
            System.out.println(update.toString());
        }
    }
}