All Downloads are FREE. Search and download functionalities are using the official Maven repository.

in.succinct.plugins.ecommerce.integration.fedex.TrackWebServiceClient Maven / Gradle / Ivy

The newest version!
package in.succinct.plugins.ecommerce.integration.fedex;

import com.fedex.track.stub.Address;
import com.fedex.track.stub.CarrierCodeType;
import com.fedex.track.stub.ClientDetail;
import com.fedex.track.stub.CompletedTrackDetail;
import com.fedex.track.stub.CustomerExceptionRequestDetail;
import com.fedex.track.stub.DeliveryOptionEligibilityDetail;
import com.fedex.track.stub.Money;
import com.fedex.track.stub.Notification;
import com.fedex.track.stub.NotificationSeverityType;
import com.fedex.track.stub.TrackChargeDetail;
import com.fedex.track.stub.TrackDetail;
import com.fedex.track.stub.TrackEvent;
import com.fedex.track.stub.TrackIdentifierType;
import com.fedex.track.stub.TrackOtherIdentifierDetail;
import com.fedex.track.stub.TrackPackageIdentifier;
import com.fedex.track.stub.TrackPortType;
import com.fedex.track.stub.TrackReply;
import com.fedex.track.stub.TrackRequest;
import com.fedex.track.stub.TrackRequestProcessingOptionType;
import com.fedex.track.stub.TrackSelectionDetail;
import com.fedex.track.stub.TrackServiceLocator;
import com.fedex.track.stub.TrackStatusAncillaryDetail;
import com.fedex.track.stub.TrackStatusDetail;
import com.fedex.track.stub.TrackingDateOrTimestamp;
import com.fedex.track.stub.TrackingDateOrTimestampType;
import com.fedex.track.stub.TransactionDetail;
import com.fedex.track.stub.VersionId;
import com.fedex.track.stub.WebAuthenticationCredential;
import com.fedex.track.stub.WebAuthenticationDetail;
import com.fedex.track.stub.Weight;
import com.venky.core.collections.SequenceSet;
import com.venky.core.date.DateUtils;
import com.venky.core.log.SWFLogger;
import com.venky.core.util.Bucket;
import com.venky.core.util.ObjectUtil;
import com.venky.swf.db.Database;
import com.venky.swf.routing.Config;
import com.venky.swf.sql.Conjunction;
import com.venky.swf.sql.Expression;
import com.venky.swf.sql.Operator;
import com.venky.swf.sql.Select;
import in.succinct.plugins.ecommerce.db.model.order.Manifest;
import in.succinct.plugins.ecommerce.db.model.order.Order;
import in.succinct.plugins.ecommerce.db.model.order.OrderAttribute;
import in.succinct.plugins.ecommerce.db.model.order.OrderIntransitEvent;
import in.succinct.plugins.ecommerce.db.model.participation.PreferredCarrier;

import javax.xml.crypto.Data;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.logging.Level;

/** 
 * Demo of using the Track service with Axis 
 * to track a shipment.
 * 

* com.fedex.track.stub is generated via WSDL2Java, like this:
*

 * java org.apache.axis.wsdl.WSDL2Java -w -p com.fedex.track.stub http://www.fedex.com/...../TrackService?wsdl
 * 
* * This sample code has been tested with JDK 7 and Apache Axis 1.4 */ public class TrackWebServiceClient { //\ Manifest manifest = null; PreferredCarrier carrier = null; public TrackWebServiceClient(Manifest manifest) { this.manifest = manifest; this.carrier = manifest.getPreferredCarrier(); } public void track(){ Set orderIds = manifest.getOrderIdsPendingDelivery(); Map trackingNumbers = new HashMap<>(); { Select s = new Select().from(OrderAttribute.class); Expression where = new Expression(s.getPool(), Conjunction.AND); where.add(new Expression(s.getPool(), "NAME", Operator.EQ, "tracking_number")); where.add(new Expression(s.getPool(), "ORDER_ID", Operator.IN, orderIds.toArray())); List orderAttributes = new Select().from(OrderAttribute.class). where(where).execute(); orderAttributes.forEach(oa->{ trackingNumbers.put(oa.getValue(),oa.getOrderId()); }); } Stack> batches = new Stack<>(); int batchSize = 30; for (String trackingNumber: trackingNumbers.keySet()){ if (batches.isEmpty() || batches.peek().size() >= batchSize){ batches.push(new HashSet<>()); } batches.peek().add(trackingNumber); } for (Set batch : batches){ TrackRequest request = new TrackRequest(); request.setClientDetail(createClientDetail()); request.setWebAuthenticationDetail(createWebAuthenticationDetail()); // TransactionDetail transactionDetail = new TransactionDetail(); transactionDetail.setCustomerTransactionId(manifest.getManifestNumber()); //This is a reference field for the customer. Any value can be used and will be provided in the response. request.setTransactionDetail(transactionDetail); // VersionId versionId = new VersionId("trck", 16, 0, 0); request.setVersion(versionId); // List list = new ArrayList<>(); for (String trackingNumber :batch){ TrackSelectionDetail selectionDetail=new TrackSelectionDetail(); selectionDetail.setCarrierCode(CarrierCodeType.FDXE); TrackPackageIdentifier packageIdentifier=new TrackPackageIdentifier(); packageIdentifier.setType(TrackIdentifierType.TRACKING_NUMBER_OR_DOORTAG); packageIdentifier.setValue(trackingNumber); // tracking number selectionDetail.setPackageIdentifier(packageIdentifier); list.add(selectionDetail); } request.setSelectionDetails(list.toArray(new TrackSelectionDetail[]{})); TrackRequestProcessingOptionType processingOption=TrackRequestProcessingOptionType.INCLUDE_DETAILED_SCANS; request.setProcessingOptions(new TrackRequestProcessingOptionType[]{processingOption}); // try { // Initializing the service TrackServiceLocator service; TrackPortType port; // service = new TrackServiceLocator(); updateEndPoint(service); port = service.getTrackServicePort(); // TrackReply reply = port.track(request); // This is the call to the web service passing in a request object and returning a reply object // if(printNotifications(reply.getNotifications())){ printCompletedTrackDetail(reply.getCompletedTrackDetails(),trackingNumbers); } if (isResponseOk(reply.getHighestSeverity())) // check if the call was successful { cat.info("--Track Reply--"); } } catch (Exception e) { cat.log(Level.WARNING,"",e); } } } private void printCompletedTrackDetail(CompletedTrackDetail[] ctd,Map trackingNumberOrderMap) { for (int i=0; i< ctd.length; i++) { // package detail information boolean cont=true; cat.info("--Completed Tracking Detail--"); if(ctd[i].getNotifications()!=null){ cat.info(" Completed Track Detail Notifications--"); cont=printNotifications(ctd[i].getNotifications()); cat.info(" Completed Track Detail Notifications--"); } if(cont){ print("DuplicateWayBill", ctd[i].getDuplicateWaybill()); print("Track Details Count", ctd[i].getTrackDetailsCount()); if(ctd[i].getMoreData()){ cat.info(" Additional package data not yet retrieved"); if(ctd[i].getPagingToken()!=null){ print(" Paging Token", ctd[i].getPagingToken()); } } printTrackDetail(ctd[i].getTrackDetails(),trackingNumberOrderMap); } cat.info("--Completed Tracking Detail--"); } } private void printTrackDetail(TrackDetail[] td, Map trackingOrderIdMap){ for (int i=0; i< td.length; i++) { boolean cont=true; cat.info("--Track Details--"); Order order = null ; if(td[i].getNotification()!=null){ cat.info(" Track Detail Notification--"); cont=printNotifications(td[i].getNotification()); cat.info(" Track Detail Notification--"); } if(cont){ print("Tracking Number", td[i].getTrackingNumber()); Long orderId = trackingOrderIdMap.get(td[i].getTrackingNumber()); order = Database.getTable(Order.class).get(orderId); print("Carrier code", td[i].getCarrierCode()); if(td[i].getService()!=null){ if(td[i].getService().getType()!=null && td[i].getService().getDescription()!=null){ print("Service", td[i].getService().getType()); print("Description", td[i].getService().getDescription()); } } if(td[i].getOtherIdentifiers()!=null){ cat.info("--Track Package Identifer--"); printTrackOtherIdentifierDetail(td[i].getOtherIdentifiers()); cat.info("--Track Package Identifer--"); } if(td[i].getStatusDetail()!=null){ cat.info("--Status Details--"); printStatusDetail(td[i].getStatusDetail()); cat.info("--Status Details--"); } if(td[i].getOriginLocationAddress()!=null){ cat.info("--Origin Location--"); print(td[i].getOriginLocationAddress()); cat.info("--Origin Location--"); } if(td[i].getDestinationAddress()!=null){ cat.info("--Destination Location--"); printDestinationInformation(td[i]); cat.info("--Destination Location--"); } if(td[i].getActualDeliveryAddress()!=null){ cat.info("--Delivery Address--"); print(td[i].getActualDeliveryAddress()); cat.info("--Delivery Address--"); } Timestamp deliveryTimestamp = null; Timestamp pickUpTimestamp = null; DateFormat ISO_8601_24H_FULL_FORMAT = DateUtils.getFormat( "yyyy-MM-dd'T'HH:mm:ssXXX"); if(td[i].getDatesOrTimes()!=null){ TrackingDateOrTimestamp[] dates = td[i].getDatesOrTimes(); for(int j=0; j0){ cat.info("--Delivery Information--"); printDeliveryInformation(td[i]); cat.info("--Delivery Information--"); } if(td[i].getCustomerExceptionRequests()!=null){ cat.info("--Customer Exception Information--"); printCustomerExceptionRequests(td[i].getCustomerExceptionRequests()); cat.info("--Customer Exception Information--"); } if(td[i].getCharges()!=null){ cat.info("--Charges--"); printCharges(td[i].getCharges()); cat.info("--Charges--"); } if(td[i].getEvents()!=null){ cat.info("--Tracking Events--"); printTrackEvents(td[i].getEvents(),order,pickUpTimestamp,deliveryTimestamp); cat.info("--Tracking Events--"); } cat.info("--Track Details--"); } } } private void printCustomerExceptionRequests(CustomerExceptionRequestDetail[] exceptions){ if(exceptions!=null){ for(int i=0; i 0){ order.getIntransitUpdates().forEach(e->e.destroy()); } for(int i= events.length -1; i > 0 ; i--) { TrackEvent event = events[i]; OrderIntransitEvent oie = Database.getTable(OrderIntransitEvent.class).newRecord(); oie.setEventSeqNo(eventSeqNo); oie.setOrderId(order.getId()); if (ObjectUtil.equals(event.getEventType(), "AR") || ObjectUtil.equals(event.getEventType(), "DL")){ oie.setEventType(OrderIntransitEvent.EVENT_TYPE_ARRIVED); }else if (ObjectUtil.equals(event.getEventType(),"DP") || ObjectUtil.equals(event.getEventType(),"PU")) { oie.setEventType(OrderIntransitEvent.EVENT_TYPE_LEFT); }else { continue; } if (ObjectUtil.equals(event.getEventType(),"DL")){ oie.setEventTimestamp(deliveryTS); }else if (ObjectUtil.equals(event.getEventType(),"PU")){ oie.setEventTimestamp(pickTS); }else { oie.setEventTimestamp(new Timestamp(event.getTimestamp().getTimeInMillis())); } oie.setEventDescription(event.getEventDescription()); print("Event no. ", i); print(event.getTimestamp()); if(event.getEventType()!=null){ print("Type", event.getEventType()); } print("Station Id", event.getStationId()); print("Exception Code", event.getStatusExceptionCode()); print("", event.getStatusExceptionDescription()); print("Description", event.getEventDescription()); if(event.getAddress()!=null){ cat.info(" Event Address--"); printAddress(oie,event.getAddress()); cat.info(" Event Address--"); } oie.save(); eventSeqNo ++; } } } private void printStatusDetail(TrackStatusDetail tsd){ if(tsd!=null){ print(tsd.getCreationTime()); print("Code", tsd.getCode()); if(tsd.getLocation()!=null){ cat.info("--Location Address Detail--"); print(tsd.getLocation()); cat.info("--Location Address Detail--"); } if(tsd.getAncillaryDetails()!=null){ cat.info("--Ancillary Details--"); printAncillaryDetails(tsd.getAncillaryDetails()); cat.info("--Ancillary Details--"); } } } private void printAncillaryDetails(TrackStatusAncillaryDetail[] details){ if(details!=null){ for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy