Subversion Repositories SmartDukaan

Rev

Rev 5264 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5264 Rev 18989
Line 2... Line 2...
2
 
2
 
3
import in.shop2020.config.ConfigException;
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.serving.model.ShipmentUpdate;
4
import in.shop2020.serving.model.ShipmentUpdate;
5
import in.shop2020.thrift.clients.config.ConfigClient;
5
import in.shop2020.thrift.clients.config.ConfigClient;
6
 
6
 
-
 
7
import java.io.BufferedReader;
-
 
8
import java.io.InputStreamReader;
-
 
9
import java.io.StringReader;
7
import java.util.ArrayList;
10
import java.util.ArrayList;
8
import java.util.List;
11
import java.util.List;
9
 
12
 
10
import javax.xml.xpath.XPath;
13
import javax.xml.xpath.XPath;
11
import javax.xml.xpath.XPathConstants;
14
import javax.xml.xpath.XPathConstants;
12
import javax.xml.xpath.XPathExpressionException;
15
import javax.xml.xpath.XPathExpressionException;
13
import javax.xml.xpath.XPathFactory;
16
import javax.xml.xpath.XPathFactory;
14
 
17
 
-
 
18
import org.apache.http.HttpResponse;
-
 
19
import org.apache.http.client.HttpClient;
-
 
20
import org.apache.http.client.methods.HttpGet;
-
 
21
import org.apache.http.impl.client.DefaultHttpClient;
15
import org.apache.log4j.Logger;
22
import org.apache.log4j.Logger;
16
import org.w3c.dom.Element;
23
import org.w3c.dom.Element;
17
import org.w3c.dom.NodeList;
24
import org.w3c.dom.NodeList;
18
import org.xml.sax.InputSource;
25
import org.xml.sax.InputSource;
19
 
26
 
Line 30... Line 37...
30
            log.error("Unable to get the tracking url from the config service", e);
37
            log.error("Unable to get the tracking url from the config service", e);
31
        }
38
        }
32
    }
39
    }
33
    
40
    
34
    public List<ShipmentUpdate> getUpdates(String awbNumber){
41
    public List<ShipmentUpdate> getUpdates(String awbNumber){
35
        String uri = Delhivery_URL + "waybill=" + awbNumber;
42
    	String uri = Delhivery_URL + "waybill=" + awbNumber;
36
        log.info("Delhivery Update URL: " + uri);
43
    	log.info("Delhivery Update URL: " + uri);
37
        InputSource inputSource = new InputSource(uri);
44
    	try {
38
        XPath xpath = XPathFactory.newInstance().newXPath();
45
    		HttpClient client = new DefaultHttpClient();
39
 
46
 
-
 
47
    		HttpGet get_new = new HttpGet(uri);
40
        String expression = "/ShipmentData/Shipment/Scans/ScanDetail";
48
    		HttpResponse response = client.execute(get_new);
-
 
49
    		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
41
        NodeList nodes = null;
50
    		String line = "";
42
        try {
51
    		int i=1;
43
            nodes = (NodeList) xpath.evaluate(expression, inputSource,  XPathConstants.NODESET);
52
    		List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
44
        } catch(XPathExpressionException xpee) {
53
    		while ((line = rd.readLine()) != null) {
45
            return null;
54
    			System.out.println(line);
-
 
55
    			if(i==1){
46
        }
56
    				i++;
-
 
57
    				continue;
47
        
58
    			}
48
        List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
59
    			InputSource inputSource = new InputSource(new StringReader(line));
-
 
60
    			XPath xpath = XPathFactory.newInstance().newXPath();
-
 
61
 
-
 
62
    			String expression = "/ShipmentData/Shipment/Scans/ScanDetail";
49
        
63
    			NodeList nodes = null;
-
 
64
 
-
 
65
    			nodes = (NodeList) xpath.evaluate(expression, inputSource,  XPathConstants.NODESET);
-
 
66
 
-
 
67
 
50
        System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );
68
    			System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );
51
        for(int i=0; i<=nodes.getLength()-1; i++) {
69
    			for(int j=0; j<=nodes.getLength()-1; j++) {
52
            Element hawbUpdate = (Element) nodes.item(i);
70
    				Element hawbUpdate = (Element) nodes.item(j);
53
            ShipmentUpdate update = new ShipmentUpdate();
71
    				ShipmentUpdate update = new ShipmentUpdate();
54
 
72
 
55
            String dateString = getElementTextContent(hawbUpdate, "ScanDateTime");
73
    				String dateString = getElementTextContent(hawbUpdate, "ScanDateTime");
56
            String[] tokens = dateString.split("T");
74
    				String[] tokens = dateString.split("T");
57
            update.date = tokens[0];
75
    				update.date = tokens[0];
58
            String[] timetokens = tokens[1].split("\\.");
76
    				String[] timetokens = tokens[1].split("\\.");
59
            update.time = timetokens[0];
77
    				update.time = timetokens[0];
60
            update.city = getElementTextContent(hawbUpdate, "ScannedLocation");
78
    				update.city = getElementTextContent(hawbUpdate, "ScannedLocation");
61
            update.description = getElementTextContent(hawbUpdate, "Scan") + " - " + getElementTextContent(hawbUpdate, "Instructions");         
79
    				update.description = getElementTextContent(hawbUpdate, "Scan") + " - " + getElementTextContent(hawbUpdate, "Instructions");         
62
            updates.add(update);
80
    				updates.add(update);
63
        }   
81
    			}
-
 
82
 
-
 
83
    		}
64
        return updates;
84
    		return updates;
-
 
85
    	} catch(Exception xpee) {
-
 
86
    		xpee.printStackTrace();
-
 
87
    		return null;
-
 
88
    	}
65
    }
89
    }
66
    
90
    
67
    private String getElementTextContent(Element element, String tagName){
91
    private String getElementTextContent(Element element, String tagName){
68
        return element.getElementsByTagName(tagName).item(0).getTextContent();
92
        return element.getElementsByTagName(tagName).item(0).getTextContent();
69
    }
93
    }
70
    
94
    
71
    public static void main(String[] args){
95
    public static void main(String[] args){
72
        System.out.println("Delhivery number is " + "13410100004");
96
        System.out.println("Delhivery number is " + "13410934216");
73
        DelhiveryTrackingService service = new DelhiveryTrackingService();
97
        DelhiveryTrackingService service = new DelhiveryTrackingService();
-
 
98
        List<ShipmentUpdate> updates;
-
 
99
		try {
74
        List<ShipmentUpdate> updates = service.getUpdates("13410100004");
100
			updates = service.getUpdates("13410934216");
75
        for(ShipmentUpdate update: updates){
101
			for(ShipmentUpdate update: updates){
76
            System.out.println(update.toString());
102
	            System.out.println(update.toString());
77
        }
103
	        }
-
 
104
		} catch (Exception e) {
-
 
105
			e.printStackTrace();
-
 
106
		}
78
    }
107
    }
79
}
108
}