Subversion Repositories SmartDukaan

Rev

Rev 536 | Blame | Last modification | View Log | RSS feed

package in.shop2020.serving.services;


import in.shop2020.config.ConfigException;
import in.shop2020.logistics.ShipmentUpdate;
import in.shop2020.thrift.clients.LogisticsServiceClient;
import in.shop2020.thrift.clients.config.ConfigClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.apache.thrift.TException;
import org.w3c.dom.Node;
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 String ARAMEX_URL;

        static{
                try {
                        ARAMEX_URL = ConfigClient.getClient().get("aramex_update_url");
                        
                } catch (ConfigException e) {
                        ARAMEX_URL = "file:///root/docs/logistics/AWB_Track.xml";
                        e.printStackTrace();
                }
                
        }
        /*
        private static AramaxTrackingService trackingService;

        static{
                synchronized(AramaxTrackingService.class){
                        trackingService = new AramaxTrackingService();
                }
        }
         */
        
        /*
        public String getAwbNumber() {
                return awbNumber;
        }

        public void setAwbNumber(String awbNumber) {
                this.awbNumber = awbNumber;
        }

        public InputSource getInputSource() {
                return inputSource;
        }

        public void setInputSource(InputSource inputSource) {
                this.inputSource = inputSource;
        }
        */

        
        public static void getShipmentUpdates(String awbNumber){
                        String expression = "/HAWBs/HAWBDetails/HAWBHistory/HAWBUpdate";
                        //String uri = ARAMAX_URL + awbNumber; 
                        String uri = ARAMEX_URL;
                        InputSource inputSource = new InputSource(uri);
                        XPath xpath= XPathFactory.newInstance().newXPath();

                        NodeList nodes = null;
                        try {
                                nodes = (NodeList) xpath.evaluate(expression, inputSource,      XPathConstants.NODESET);
                        } 
                        catch(XPathExpressionException xpee) {
                                return ;
                        }
                        

                        for(int i=nodes.getLength()-1; i>=0; i--) {
                                Node node = nodes.item(i);
                                NodeList childNodes = node.getChildNodes();
                                ShipmentUpdate update = new ShipmentUpdate();
                                update.setDescription(childNodes.item(14).getTextContent());
                                update.setPin(childNodes.item(1).getTextContent());
                                update.setTimestamp(10);
                

                        try {
                                LogisticsServiceClient lsc = new LogisticsServiceClient();
                                lsc.getClient().updateShipmentStatus(awbNumber, update);
                                
                        } catch (TException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        }       
                
        }

        
        public static List<Map<String, String>> getUpdates(String awbNumber){
                //String uri = ARAMAX_URL + awbNumber;
                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<Map<String, String>> updates = new ArrayList<Map<String,String>>();
                
                System.out.println("Action date         "+  "Update City        " + "Tracki Code Desc" );
                for(int i=nodes.getLength()-1; i>=0; i--) {
                        Node node = nodes.item(i);
                        NodeList childNodes = node.getChildNodes();
                        /*
                        System.out.println("Action date is :            " + childNodes.item(0).getTextContent());
                        System.out.println("PIN number  is :            " + childNodes.item(1).getTextContent());
                        System.out.println("Entry Date  is :            " + childNodes.item(3).getTextContent());
                        System.out.println("Update City is :            " + childNodes.item(6).getTextContent());
                        System.out.println("Tracking Code Desc is  :" + childNodes.item(7).getTextContent());
                        System.out.println("UpdateLocation              is :" + childNodes.item(8).getTextContent());
                        System.out.println("UpdateCountry               is :" + childNodes.item(11).getTextContent());
                        System.out.println("Tracking Condition  is :" + childNodes.item(12).getTextContent());
                        System.out.println("Customer Desc               is :" + childNodes.item(14).getTextContent());
                        System.out.println("\n\n\n\n ");
                        */
                        
                        System.out.println(childNodes.item(0).getTextContent() +"\t\t"+childNodes.item(6).getTextContent()+"\t\t"
                                        +childNodes.item(7).getTextContent());
                        
                        Map<String,String> update = new HashMap<String,String>();
                        
                        String dateString =  childNodes.item(0).getTextContent();
                        String[] tokens = dateString.split("\\s");
                        String date = tokens[0];
                        String time = tokens[1] + " " + tokens[2];
                        String city = childNodes.item(6).getTextContent();
                        String status = childNodes.item(7).getTextContent();
                        
                        System.out.println("date:"+date+":time:" + time +":city:"+city+":status:"+status);
                        
                        update.put("date", date);
                        update.put("time", time);
                        update.put("city", city);
                        update.put("status", status);
                        
                        updates.add(update);
                        
                }
                
                
                System.out.println("nodes length " +  nodes.getLength() );
                
                return updates;
                //return nodes.toString(); 
                
        }

        /*
        public String getOriginCity(){
                
                String expression = "/HAWBs/HAWBDetails/HAWBOriginEntity";
                
                NodeList nodes = null;
                try {
                        nodes = (NodeList) xpath.evaluate(expression, this.inputSource, XPathConstants.NODESET);
                } 
                catch(XPathExpressionException xpee) {
                        return null;
                }
                
                System.out.println("nodes length " +  nodes.getLength() + nodes.item(0).getTextContent());
                
                
                return nodes.toString(); 
        }
        */
        
        
        public static void main(String[] args){
                System.out.println("Aramax number is " + "6149183666");
                
        }
}