Subversion Repositories SmartDukaan

Rev

Rev 8368 | 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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import com.TrackWebServiceClient;
import com.fedex.track.stub.ClientDetail;
import com.fedex.track.stub.TrackDetail;
import com.fedex.track.stub.TrackEvent;
import com.fedex.track.stub.TrackReply;
import com.fedex.track.stub.WebAuthenticationCredential;
import com.fedex.track.stub.WebAuthenticationDetail;

public class FedExTrackingService {

        private TrackReply trackreply;
        /**
         * @param args
         */
        
        public List<ShipmentUpdate> getUpdates(String awbNumber){
                List<ShipmentUpdate> updates = new ArrayList<ShipmentUpdate>();
                 ClientDetail clientDetail = new ClientDetail();
                String accountNumber ="";
                String meterNumber ="";
                        try {
                                accountNumber = ConfigClient.getClient().get("fedex_account_number");
                                meterNumber = ConfigClient.getClient().get("fedex_meter_number");
                        } catch (ConfigException e) {
                                e.printStackTrace();
                        }                       
                clientDetail.setAccountNumber(accountNumber);
                clientDetail.setMeterNumber(meterNumber);
                WebAuthenticationCredential wac = new WebAuthenticationCredential();
                String key="";
                String password="";
                        try {
                                key = ConfigClient.getClient().get("fedex_authenication_key");
                                password = ConfigClient.getClient().get("fedex_authenication_password");
                        } catch (ConfigException e) {
                                e.printStackTrace();
                        }
                wac.setKey(key);
                wac.setPassword(password);
         String endPoint="";
                        try {
                                endPoint = ConfigClient.getClient().get("fedex_endpoint_address_track");
                        } catch (ConfigException e) {
                                e.printStackTrace();
                        }
                
                trackreply = TrackWebServiceClient.getTrackingUpdates(awbNumber, clientDetail, new WebAuthenticationDetail(wac), endPoint);
                if(trackreply!=null){
                        TrackDetail[] trackDetails=trackreply.getTrackDetails();
                        SimpleDateFormat dateformat = new SimpleDateFormat("dd-MMM-yyyy");
                        SimpleDateFormat timeformat = new SimpleDateFormat("HH:mm:ss");
                        for (int i=0; i< trackDetails.length; i++) {
                                TrackEvent trackEvents[]= trackDetails[i].getEvents();
                                for(TrackEvent te : trackEvents){
                                        ShipmentUpdate shipUpdate = new ShipmentUpdate();
                                        shipUpdate.city= te.getAddress().getCity();
                                        shipUpdate.date= dateformat.format(te.getTimestamp().getTime());
                                        shipUpdate.time= timeformat.format(te.getTimestamp().getTime());
                                        shipUpdate.description= te.getEventDescription();
                                        updates.add(shipUpdate);
                                }                       
                        }
                        return updates;
                }
                else{
                        return null;
                }
        }
        
        public static void main(String[] args) {
                FedExTrackingService trackClient = new FedExTrackingService();
                List<ShipmentUpdate> updates = trackClient.getUpdates("59104178545");
                for(ShipmentUpdate update: updates){
                        System.out.println(update.toString());
                }
        }

}