Subversion Repositories SmartDukaan

Rev

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

Rev 681 Rev 745
Line 1... Line 1...
1
package in.shop2020.serving.services;
1
package in.shop2020.serving.services;
2
 
2
 
3
 
3
 
4
import in.shop2020.config.ConfigException;
4
import in.shop2020.config.ConfigException;
5
import in.shop2020.thrift.clients.LogisticsServiceClient;
5
import in.shop2020.serving.model.ShipmentUpdate;
6
import in.shop2020.thrift.clients.config.ConfigClient;
6
import in.shop2020.thrift.clients.config.ConfigClient;
7
 
7
 
8
import java.util.ArrayList;
8
import java.util.ArrayList;
9
import java.util.HashMap;
-
 
10
import java.util.List;
9
import java.util.List;
11
import java.util.Map;
-
 
12
 
10
 
13
import javax.xml.xpath.XPath;
11
import javax.xml.xpath.XPath;
14
import javax.xml.xpath.XPathConstants;
12
import javax.xml.xpath.XPathConstants;
15
import javax.xml.xpath.XPathExpressionException;
13
import javax.xml.xpath.XPathExpressionException;
16
import javax.xml.xpath.XPathFactory;
14
import javax.xml.xpath.XPathFactory;
17
 
15
 
18
import org.apache.thrift.TException;
16
import org.apache.juli.logging.Log;
-
 
17
import org.apache.juli.logging.LogFactory;
19
import org.w3c.dom.Node;
18
import org.w3c.dom.Element;
20
import org.w3c.dom.NodeList;
19
import org.w3c.dom.NodeList;
21
import org.xml.sax.InputSource;
20
import org.xml.sax.InputSource;
22
 
21
 
23
public class AramexTrackingService {
22
public class AramexTrackingService {
24
	//public static String ARAMAX_URL = "http://www.aramex.com/track_xml.asp?ShipmentNumber=";
23
	//public static String ARAMAX_URL = "http://www.aramex.com/track_xml.asp?ShipmentNumber=";
-
 
24
	private static Log log = LogFactory.getLog(AramexTrackingService.class);
25
	
25
	
26
	private static String ARAMEX_URL;
26
	private static String ARAMEX_URL;
27
 
27
 
28
	static{
28
	static{
29
		try {
29
		try {
30
			ARAMEX_URL = ConfigClient.getClient().get("aramex_update_url");
30
			ARAMEX_URL = ConfigClient.getClient().get("aramex_update_url");
31
			
-
 
32
		} catch (ConfigException e) {
31
		} catch (ConfigException e) {
33
			ARAMEX_URL = "file:///root/docs/logistics/AWB_Track.xml";
32
			//ARAMEX_URL = "file:///home/ashish/Downloads/AWB_Track.xml";
34
			e.printStackTrace();
33
			e.printStackTrace();
35
		}
34
		}
36
		
-
 
37
	}
-
 
38
	/*
-
 
39
	private static AramaxTrackingService trackingService;
-
 
40
 
-
 
41
	static{
-
 
42
		synchronized(AramaxTrackingService.class){
-
 
43
			trackingService = new AramaxTrackingService();
-
 
44
		}
-
 
45
	}
35
	}
46
	 */
-
 
47
	
36
	
48
	/*
-
 
49
	public String getAwbNumber() {
-
 
50
		return awbNumber;
-
 
51
	}
-
 
52
 
-
 
53
	public void setAwbNumber(String awbNumber) {
-
 
54
		this.awbNumber = awbNumber;
-
 
55
	}
-
 
56
 
-
 
57
	public InputSource getInputSource() {
-
 
58
		return inputSource;
-
 
59
	}
-
 
60
 
-
 
61
	public void setInputSource(InputSource inputSource) {
-
 
62
		this.inputSource = inputSource;
-
 
63
	}
-
 
64
	*/
-
 
65
 
-
 
66
	/*
-
 
67
	public static void getShipmentUpdates(String awbNumber){
37
	public List<ShipmentUpdate> getUpdates(String awbNumber){
68
			String expression = "/HAWBs/HAWBDetails/HAWBHistory/HAWBUpdate";
-
 
69
			//String uri = ARAMAX_URL + awbNumber; 
38
		String uri = ARAMEX_URL + awbNumber;
70
			String uri = ARAMEX_URL;
-
 
71
			InputSource inputSource = new InputSource(uri);
-
 
72
			XPath xpath= XPathFactory.newInstance().newXPath();
-
 
73
 
-
 
74
			NodeList nodes = null;
-
 
75
			try {
-
 
76
				nodes = (NodeList) xpath.evaluate(expression, inputSource,	XPathConstants.NODESET);
-
 
77
			} 
-
 
78
			catch(XPathExpressionException xpee) {
-
 
79
				return ;
-
 
80
			}
-
 
81
			
-
 
82
 
-
 
83
			for(int i=nodes.getLength()-1; i>=0; i--) {
-
 
84
				Node node = nodes.item(i);
-
 
85
				NodeList childNodes = node.getChildNodes();
-
 
86
				ShipmentUpdate update = new ShipmentUpdate();
-
 
87
				update.setDescription(childNodes.item(14).getTextContent());
-
 
88
				update.setPin(childNodes.item(1).getTextContent());
-
 
89
				update.setTimestamp(10);
-
 
90
		
-
 
91
 
-
 
92
			try {
-
 
93
				LogisticsServiceClient lsc = new LogisticsServiceClient();
-
 
94
				lsc.getClient().updateShipmentStatus(awbNumber, update);
-
 
95
				
-
 
96
			} catch (TException e) {
-
 
97
				// TODO Auto-generated catch block
-
 
98
				e.printStackTrace();
-
 
99
			}catch (Exception e) {
-
 
100
				// TODO Auto-generated catch block
-
 
101
				e.printStackTrace();
-
 
102
			}
-
 
103
			}	
-
 
104
		
-
 
105
	}
-
 
106
*/
-
 
107
	
-
 
108
	public static List<Map<String, String>> getUpdates(String awbNumber){
-
 
109
		//String uri = ARAMAX_URL + awbNumber;
39
		log.info("Aramex Update URL: " + uri);
110
		String uri = ARAMEX_URL;
40
		//String uri = ARAMEX_URL;
111
		InputSource inputSource = new InputSource(uri);
41
		InputSource inputSource = new InputSource(uri);
112
		XPath xpath = XPathFactory.newInstance().newXPath();
42
		XPath xpath = XPathFactory.newInstance().newXPath();
113
 
43
 
114
		String expression = "/HAWBs/HAWBDetails/HAWBHistory/HAWBUpdate";
44
		String expression = "/HAWBs/HAWBDetails/HAWBHistory/HAWBUpdate";
115
		NodeList nodes = null;
45
		NodeList nodes = null;
116
		try {
46
		try {
117
			nodes = (NodeList) xpath.evaluate(expression, inputSource,	XPathConstants.NODESET);
47
			nodes = (NodeList) xpath.evaluate(expression, inputSource,	XPathConstants.NODESET);
118
		} 
-
 
119
		catch(XPathExpressionException xpee) {
48
		} catch(XPathExpressionException xpee) {
120
			return null;
49
			return null;
121
		}
50
		}
122
		
51
		
123
		List<Map<String, String>> updates = new ArrayList<Map<String,String>>();
52
		List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
124
		
53
		
125
		System.out.println("Action date		"+  "Update City 	" + "Tracki Code Desc" );
54
		System.out.println("Action date\t\t\tUpdate City\t\t\tTracki Code Desc" );
126
		for(int i=nodes.getLength()-1; i>=0; i--) {
55
		for(int i=nodes.getLength()-1; i>=0; i--) {
127
			Node node = nodes.item(i);
56
			Element hawbUpdate = (Element) nodes.item(i);
128
			NodeList childNodes = node.getChildNodes();
-
 
129
			/*
-
 
130
			System.out.println("Action date is :		" + childNodes.item(0).getTextContent());
-
 
131
			System.out.println("PIN number  is :		" + childNodes.item(1).getTextContent());
-
 
132
			System.out.println("Entry Date  is :		" + childNodes.item(3).getTextContent());
-
 
133
			System.out.println("Update City is :		" + childNodes.item(6).getTextContent());
-
 
134
			System.out.println("Tracking Code Desc is  :" + childNodes.item(7).getTextContent());
-
 
135
			System.out.println("UpdateLocation		is :" + childNodes.item(8).getTextContent());
-
 
136
			System.out.println("UpdateCountry		is :" + childNodes.item(11).getTextContent());
-
 
137
			System.out.println("Tracking Condition	is :" + childNodes.item(12).getTextContent());
-
 
138
			System.out.println("Customer Desc		is :" + childNodes.item(14).getTextContent());
-
 
139
			System.out.println("\n\n\n\n ");
-
 
140
			*/
-
 
141
			
-
 
142
			System.out.println(childNodes.item(0).getTextContent() +"\t\t"+childNodes.item(6).getTextContent()+"\t\t"
-
 
143
					+childNodes.item(7).getTextContent());
-
 
144
			
-
 
145
			Map<String,String> update = new HashMap<String,String>();
57
			ShipmentUpdate update = new ShipmentUpdate();
146
			
58
 
147
			String dateString =  childNodes.item(0).getTextContent();
59
			String dateString = getElementTextContent(hawbUpdate, "ActionDate");
148
			String[] tokens = dateString.split("\\s");
60
			String[] tokens = dateString.split("\\s");
149
			String date = tokens[0];
61
			update.date = tokens[0];
150
			String time = tokens[1] + " " + tokens[2];
62
			update.time = tokens[1] + " " + tokens[2];
151
			String city = childNodes.item(6).getTextContent();
63
			update.city = getElementTextContent(hawbUpdate, "UpdateLocationFormatted");
152
			String status = childNodes.item(7).getTextContent();
-
 
153
			
-
 
154
			System.out.println("date:"+date+":time:" + time +":city:"+city+":status:"+status);
64
			update.description = getElementTextContent(hawbUpdate, "CustomerDescription") + " - " + getElementTextContent(hawbUpdate, "Comments1");			
155
			
-
 
156
			update.put("date", date);
-
 
157
			update.put("time", time);
-
 
158
			update.put("city", city);
-
 
159
			update.put("status", status);
-
 
160
			
-
 
161
			updates.add(update);
65
			updates.add(update);
162
			
-
 
163
		}
66
		}	
164
		
-
 
165
		
-
 
166
		System.out.println("nodes length " +  nodes.getLength() );
-
 
167
		
-
 
168
		return updates;
67
		return updates;
169
		//return nodes.toString(); 
-
 
170
		
-
 
171
	}
-
 
172
 
-
 
173
	/*
-
 
174
	public String getOriginCity(){
-
 
175
		
-
 
176
		String expression = "/HAWBs/HAWBDetails/HAWBOriginEntity";
-
 
177
		
-
 
178
		NodeList nodes = null;
-
 
179
		try {
-
 
180
			nodes = (NodeList) xpath.evaluate(expression, this.inputSource,	XPathConstants.NODESET);
-
 
181
		} 
-
 
182
		catch(XPathExpressionException xpee) {
-
 
183
			return null;
-
 
184
		}
-
 
185
		
-
 
186
		System.out.println("nodes length " +  nodes.getLength() + nodes.item(0).getTextContent());
-
 
187
		
-
 
188
		
-
 
189
		return nodes.toString(); 
-
 
190
	}
68
	}
191
	*/
-
 
192
	
69
	
-
 
70
	private String getElementTextContent(Element element, String tagName){
-
 
71
		return element.getElementsByTagName(tagName).item(0).getTextContent();
-
 
72
	}
193
	
73
	
194
	public static void main(String[] args){
74
	public static void main(String[] args){
195
		System.out.println("Aramax number is " + "6149183666");
75
		System.out.println("Aramax number is " + "6199186044");
196
		AramexTrackingService.getUpdates(ARAMEX_URL);
76
		AramexTrackingService service = new AramexTrackingService();
-
 
77
		List<ShipmentUpdate> updates = service.getUpdates("6199186044");
-
 
78
		for(ShipmentUpdate update: updates){
-
 
79
			System.out.println(update.toString());
-
 
80
		}
197
	}
81
	}
198
}
82
}