Example 23 : Advanced options search for producing delivery date matrices

Printer-friendly version

  One of the common questions from developers arose because they wanted to present the customer with the set of possible delivery dates, and then to allow them to choose.

It was further refined that the developer (or retailer) wanted to control what kinds of services could be chosen. The DM type names are far from friendly to anyone outside of the warehouse, so it made sense for the retailer to be able to control these names.

This example combines all these requirements, and produces a data set suitable for populating a 2D matrix with delivery days on one axis, and group names on the other. Notice how this corresponds to the filter group parameters.

A couple of things about this example require more explanation:

Firstly, as I've assumed that you will be presenting a single column for each delivery date, the time is actually unimportant. i.e. If two services can deliver on a single date, it doesn't matter that one delivers between 09:00 and 17:30, whereas the other guarantees it by 10 am. This will simply take the cheapest on each delivery date.

Secondly, the setExpandGroups flag has been set to true. This takes care of the situation where a particular service is present in more than one group.

package com.metapack.dm.test;
 
import com.metapack.deliverymanager.client.AllocationFilter;
import com.metapack.deliverymanager.client.Consignment;
import com.metapack.deliverymanager.client.DateRange;
import com.metapack.deliverymanager.client.DeliveryOption;
import com.metapack.deliverymanager.client.webservices.AllocationService;
 
public class Example23 extends ExampleBase {
 
	public void run() throws Exception {
		AllocationService service = createAllocationService();
 
		Consignment consignment = createDefaultConsignment("EXAMPLE-23", 1);
 
		DateRange acceptableCollectionSlot = new DateRange(createDate("2009-02-14T00:00:00"), null);
		DateRange acceptableDeliverySlot = new DateRange(createDate("2009-02-14T00:00:00"), createDate("2009-02-30T23:59:59"));
 
		AllocationFilter filter = new AllocationFilter();
		filter.setFirstCollectionOnly( false );
		filter.setAcceptableCollectionSlots(new DateRange[] { acceptableCollectionSlot });
		filter.setAcceptableDeliverySlots(new DateRange[] { acceptableDeliverySlot });
		filter.setPreFilterSortOrder1( ExampleBase.SORT_COST_CHEAPEST );
		filter.setPreFilterSortOrder2( ExampleBase.SORT_EARLIEST_COLLECTION_ASCENDING );
		filter.setFilterGroup1( ExampleBase.FILTER_GROUP_DELIVERY_DAY );
		filter.setFilterGroup2( ExampleBase.FILTER_GROUP_SERVICE_GROUP );
		filter.setSortOrder1( ExampleBase.SORT_SERVICE_GROUP );
		filter.setSortOrder2( ExampleBase.SORT_EARLIEST_DELIVERY_DAY_ASCENDING );
		filter.setExpandGroups( true );
 
		DeliveryOption[] options = service.findDeliveryOptions(consignment, filter);
 
		for (DeliveryOption option : options) {
			System.out.println("Service " + option.getCarrierServiceCode() 
					+ " can deliver it and it will cost " + option.getCost() 
					+ " collection between " + formatDateRangeNice(option.getCollectionWindow()) 
					+ " and delivery between " + formatDateRangeNice(option.getDeliveryWindow()));
		}
	}
}