Example 60 : Allocating a consignment to the first available option

Printer-friendly version

This example shows how to allocate a consignment to a delivery option.

package com.metapack.dm.test;
 
import com.metapack.deliverymanager.client.AllocationFilter;
import com.metapack.deliverymanager.client.Consignment;
import com.metapack.deliverymanager.client.DeliveryOption;
import com.metapack.deliverymanager.client.webservices.AllocationService;
 
/**
 * The 2.x version was demonstrated in Example 55.
 * 
 * @author apowney
 *
 */
public class Example60 extends ExampleBase {
 
	public void run() throws Exception {
		AllocationService service = createAllocationService();
		Consignment consignment = createDefaultConsignment( "EXAMPLE-60" , 1 );
		consignment.setRecipientName( "Mr MetaPack" );
		consignment.setRecipientEmail( "noone@metapack.com" );
		Consignment[] createdConsignments = service.createAndAllocateConsignmentsWithBookingCode( 
				new Consignment[] { consignment } , "@LOCKERS" );
		Consignment createdConsignment = createdConsignments[ 0 ];
 
		// Report if allocation was successful
 
		if( createdConsignment.getCarrierConsignmentCode() == null ) {
			System.out.println( "The consignment could not be allocated" );
		} else {
			System.out.println( "The consignment was allocated and a given tracking code of " 
					+ createdConsignment.getCarrierConsignmentCode() );
		}
 
		// Now deallocate it
 
		service.deallocate( new String[] { createdConsignment.getConsignmentCode() } );
 
		// Find the options for lockers (effectively a list of addresses)
 
		AllocationFilter lockersOnlyFilter = new AllocationFilter();
		lockersOnlyFilter.setFirstCollectionOnly( true );
		lockersOnlyFilter.setAcceptableCarrierServiceGroupCodes( new String[] { "LOCKERS" } );
		lockersOnlyFilter.setPreFilterSortOrder1( ExampleBase.SORT_EARLIEST_COLLECTION_ASCENDING );
		lockersOnlyFilter.setFilterGroup1( ExampleBase.FILTER_GROUP_SERVICE );
		lockersOnlyFilter.setSortOrder1( ExampleBase.SORT_COST_CHEAPEST );
 
		DeliveryOption[] options = service.findDeliveryOptionsForConsignment( 
				createdConsignment.getConsignmentCode() , lockersOnlyFilter );
 
		if( options.length > 0 ) {
			DeliveryOption option = options[ options.length / 2 ];
 
			System.out.println( "Attempting to allocate " + createdConsignment.getConsignmentCode() 
					+ " to " + option.getName() + " (" + option.getBookingCode() + ")" );
 
			Consignment[] testConsignments = service.allocateConsignmentsWithBookingCode( 
					new String[] { createdConsignment.getConsignmentCode() } , 
					option.getBookingCode() );
 
			if( "Allocated".equals( testConsignments[0].getStatus() ) ) {
				System.out.println( "Successfully allocated" );
			} else {
				System.out.println( "Failed to allocate" );
			}
		}
	}
}