Blame | 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.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;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 AFLTrackingService {private static Logger log = Logger.getLogger(Class.class);private static String AFL_URL;static{try {AFL_URL = ConfigClient.getClient().get("afl_update_url");} catch (ConfigException e) {log.error("Unable to get the tracking url from the config service", e);}}public List<ShipmentUpdate> getUpdates(String awbNumber){//String uri = "http://trackntrace.aflwiz.com/aflwiztrack?shpntnum=" + awbNumber;String uri = AFL_URL + awbNumber;log.info("AFL Update URL: " + uri);InputSource inputSource = new InputSource(uri);XPath xpath = XPathFactory.newInstance().newXPath();String expression = "/SHIPMENTTRACK/SHIPMENTREPORT/CHECKPOINTDETAILS/CHECKPOINTS";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=0; i<nodes.getLength(); i++) {Element hawbUpdate = (Element) nodes.item(i);ShipmentUpdate update = new ShipmentUpdate();update.date = getElementTextContent(hawbUpdate, "CHECKDATE");SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd"),sdfDestination = new SimpleDateFormat("dd-MMM-yyyy");Date date = null;try {date = sdfSource.parse(update.date);update.date = sdfDestination.format(date);} catch (ParseException e) {log.error("Unable to parse the tracking date", e);}update.time = getElementTextContent(hawbUpdate, "CHECKTIME");update.city = getElementTextContent(hawbUpdate, "LOCATIONNAME");update.description = getElementTextContent(hawbUpdate, "CHECKPOINTDESCRIPTION");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("AFL number is " + "4252621801242006");AFLTrackingService service = new AFLTrackingService();List<ShipmentUpdate> updates = service.getUpdates("4252621801242006");for(ShipmentUpdate update: updates){System.out.println(update.toString());}}}