Example 04 : Creating a consignment with nominal data

Printer-friendly version

This example will create a single consignment in the DM system by using the smallest amount of data possible.

If the creation in DM fails for any reason (such as invalid postcodes), the service will respond with a fault code as an exception.

 

package com.metapack.dm.test;
 
import com.metapack.deliverymanager.client.Address;
import com.metapack.deliverymanager.client.Consignment;
import com.metapack.deliverymanager.client.Parcel;
import com.metapack.deliverymanager.client.webservices.ConsignmentService;
 
public class Example4 extends ExampleBase {
 
	public void run() throws Exception {
		ConsignmentService service = createConsignmentService();
 
		/*
		 * Create an empty consignment object, and specify the order number.
		 */
 
		Consignment consignment = new Consignment();
		consignment.setOrderNumber( "EXAMPLE-4" );
		consignment.setConsignmentLevelDetailsFlag( true );
 
		/*
		 * Specify the warehouse, and the source address. Normally, the address is the
		 * address of the warehouse, but not necessarily.  All these pieces of information are required.
		 */
 
		consignment.setSenderCode("DEFAULT");
		consignment.setSenderName("My default warehouse");
		Address senderAddress = new Address();
		senderAddress.setLine1( "12-14 Laystall Street" );
		senderAddress.setLine2( "London" );
		senderAddress.setCountryCode( "GBR" );
		senderAddress.setPostCode( "EC1R 4PF" );
		consignment.setSenderAddress( senderAddress );
 
		/*
		 * The destination. All these pieces of information are required.
		 */
 
		consignment.setRecipientName("The Queen");
		Address recipientAddress = new Address();
		recipientAddress.setLine1( "Buckingham Palace" );
		recipientAddress.setLine2( "London" );
		recipientAddress.setCountryCode( "GBR" );
		recipientAddress.setPostCode( "SW1A 1AA" );
		consignment.setRecipientAddress( recipientAddress );
 
		/*
		 * Now we specify how many parcels there are, and specify the consignment weight.
		 * Both of these are mandatory. NB. We have also passed in an array of four parcels.
		 * Even if you don't supply information at the parcel level, you need to do this.
		 */
 
		int parcelCount = 4;
		consignment.setParcelCount( parcelCount );
		Parcel[] parcels = new Parcel[parcelCount ];
		for( int i = 0 ; i < parcelCount ; i++ ) {
			parcels[ i ] = new Parcel();
		}
		consignment.setParcels( parcels );
		consignment.setConsignmentWeight( 1.56 );
 
		/*
		 * Create the consignments in DM
		 */
 
		Consignment[] createdConsignments = service.createConsignments( new Consignment[] { consignment } );
 
		/*
		 * Iterate through the responses to see what codes the consignments were given. In this example,
		 * there should be only one.
		 */
 
		for( int i = 0 ; i < createdConsignments.length ; i++ ) {
			System.out.println( "Consignment " 
					+ createdConsignments[i].getOrderNumber() 
					+ " was created with code " 
					+ createdConsignments[i].getConsignmentCode() );
		}
	}
 
}