Example 30 : Base class for example code (ExampleBase)
This is the code for the base client object used in our examples. It provides the most common functionality for creating service connections (stubs). By factoring it out this way, we hope that it is easier for you to distinguish the code supporting the Web Services technology, and the business logic.
We have assumed that you have generated code from the WSDLs and placed it in a package called com.metapack.deliverymanager.client. It is, of course, entirely up to you what you call it.
package com.metapack.dm.test; import java.rmi.RemoteException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import javax.xml.rpc.Stub; import com.metapack.deliverymanager.client.Address; import com.metapack.deliverymanager.client.Consignment; import com.metapack.deliverymanager.client.Parcel; import com.metapack.deliverymanager.client.webservices.AllocationService; import com.metapack.deliverymanager.client.webservices.AllocationServiceServiceLocator; import com.metapack.deliverymanager.client.webservices.ConsignmentSearchService; import com.metapack.deliverymanager.client.webservices.ConsignmentSearchServiceServiceLocator; import com.metapack.deliverymanager.client.webservices.ConsignmentService; import com.metapack.deliverymanager.client.webservices.ConsignmentServiceServiceLocator; import com.metapack.deliverymanager.client.webservices.ConsignmentTrackingService; import com.metapack.deliverymanager.client.webservices.ConsignmentTrackingServiceServiceLocator; import com.metapack.deliverymanager.client.webservices.DeliveryOptionsService; import com.metapack.deliverymanager.client.webservices.DeliveryOptionsServiceServiceLocator; import com.metapack.deliverymanager.client.webservices.InformationService; import com.metapack.deliverymanager.client.webservices.InformationServiceServiceLocator; import com.metapack.deliverymanager.client.webservices.ManifestService; import com.metapack.deliverymanager.client.webservices.ManifestServiceServiceLocator; public abstract class ExampleBase { public static final int SORT_NONE = 0; public static final int SORT_CARRIER_SERVICE_TYPE = 1; public static final int SORT_EARLIEST_COLLECTION_ASCENDING = 2; public static final int SORT_LATEST_COLLECTION_DESCENDING = 3; public static final int SORT_EARLIEST_DELIVERY_ASCENDING = 4; public static final int SORT_LATEST_DELIVERY_DESCENDING = 5; public static final int SORT_COST_CHEAPEST = 6; public static final int SORT_CARRIER = 7; public static final int SORT_EARLIEST_COLLECTION_DAY_ASCENDING = 8; public static final int SORT_EARLIEST_COLLECTION_DAY_DESCENDING = 9; public static final int SORT_EARLIEST_DELIVERY_DAY_ASCENDING = 10; public static final int SORT_EARLIEST_DELIVERY_DAY_DESCENDING = 11; public static final int SORT_SERVICE = 12; public static final int SORT_LOWEST_SCORE = 13; public static final int SORT_HIGHEST_SCORE = 14; public static final int SORT_EARLIEST_CUT_OFF = 15; public static final int SORT_LATEST_CUT_OFF = 16; public static final int SORT_SERVICE_GROUP = 17; public static final int SORT_EARLIEST_COLLECTION_DESCENDING = 18; public static final int SORT_LATEST_COLLECTION_ASCENDING = 19; public static final int SORT_EARLIEST_DELIVERY_DESCENDING = 20; public static final int SORT_LATEST_DELIVERY_ASCENDING = 21; public static final int SORT_LATEST_COLLECTION_DAY_ASCENDING = 22; public static final int SORT_LATEST_COLLECTION_DAY_DESCENDING = 23; public static final int SORT_LATEST_DELIVERY_DAY_ASCENDING = 24; public static final int SORT_LATEST_DELIVERY_DAY_DESCENDING = 25; public static final int SORT_EARLIEST_COLLECTION_TIME_ASCENDING = 26; public static final int SORT_EARLIEST_COLLECTION_TIME_DESCENDING = 27; public static final int SORT_EARLIEST_DELIVERY_TIME_ASCENDING = 28; public static final int SORT_EARLIEST_DELIVERY_TIME_DESCENDING = 29; public static final int SORT_LATEST_COLLECTION_TIME_ASCENDING = 30; public static final int SORT_LATEST_COLLECTION_TIME_DESCENDING = 31; public static final int SORT_LATEST_DELIVERY_TIME_ASCENDING = 32; public static final int SORT_LATEST_DELIVERY_TIME_DESCENDING = 33; public static final int SORT_TRANSIT_TIME_ASCENDING = 34; public static final int SORT_TRANSIT_TIME_DESCENDING = 35; public static final int FILTER_GROUP_NONE = 0; public static final int FILTER_GROUP_CARRIER = 1; public static final int FILTER_GROUP_SERVICE = 2; public static final int FILTER_GROUP_TYPE = 3; public static final int FILTER_GROUP_COLLECTION_DAY = 4; public static final int FILTER_GROUP_DELIVERY_DAY = 5; public static final int FILTER_GROUP_CUT_OFF_DAY = 6; public static final int FILTER_GROUP_CUT_OFF_HOUR = 7; public static final int FILTER_GROUP_SERVICE_GROUP = 8; public static final int FILTER_GROUP_SERVICE_WINDOW = 9; public static final int FILTER_GROUP_COLLECTION_SLOT = 10; public static final int FILTER_GROUP_DELIVERY_SLOT = 11; public static final String SOAP_URL = "http://test2.metapack.com/dm/services"; public static final String SOAP_USER = "MyCaseSensitiveUserName"; public static final String SOAP_PASSWORD = "MyCaseSensitivePassword"; public ExampleBase() { } protected static String getErrorCode(RemoteException e) { if (e.getMessage() == null || e.getMessage().length() < 6) { return ""; } else { return e.getMessage().substring(0, 6); } } protected static Consignment createDefaultConsignment(String orderNumber, int numParcels) { Consignment consignment = new Consignment(); consignment.setOrderNumber(orderNumber); consignment.setSenderCode("DEFAULT"); consignment.setSenderName("Automatic test warehouse name"); consignment.setSenderAddress(createDefaultAddress()); consignment.setRecipientName("Automatic test " + orderNumber); consignment.setRecipientAddress(createDefaultAddress()); consignment.setParcelCount(numParcels); consignment.setParcels(new Parcel[numParcels]); consignment.setConsignmentWeight(0.1); for (int i = 0; i < numParcels; i++) consignment.getParcels()[i] = new Parcel(); return consignment; } protected static Address createDefaultAddress() { Address address = new Address(); address.setLine1("12-16 Laystall Street"); address.setLine2("London"); address.setPostCode("EC1R 4PF"); address.setCountryCode("GBR"); return address; } protected static DeliveryOptionsService createDeliveryOptionsService() throws Exception { DeliveryOptionsServiceServiceLocator locator = new DeliveryOptionsServiceServiceLocator(); locator.setDeliveryOptionsServiceEndpointAddress(SOAP_URL + "/DeliveryOptionsService?wsdl"); DeliveryOptionsService service = locator.getDeliveryOptionsService(); Stub stub = (Stub) service; stub._setProperty(Stub.USERNAME_PROPERTY, SOAP_USER); stub._setProperty(Stub.PASSWORD_PROPERTY, SOAP_PASSWORD); return service; } protected static InformationService createInformationService() throws Exception { InformationServiceServiceLocator locator = new InformationServiceServiceLocator(); locator.setInformationServiceEndpointAddress(SOAP_URL + "/InformationService?wsdl"); InformationService service = locator.getInformationService(); Stub stub = (Stub) service; stub._setProperty(Stub.USERNAME_PROPERTY, SOAP_USER); stub._setProperty(Stub.PASSWORD_PROPERTY, SOAP_PASSWORD); return service; } protected static ConsignmentService createConsignmentService() throws Exception { ConsignmentServiceServiceLocator locator = new ConsignmentServiceServiceLocator(); locator.setConsignmentServiceEndpointAddress(SOAP_URL + "/ConsignmentService?wsdl"); ConsignmentService service = locator.getConsignmentService(); Stub stub = (Stub) service; stub._setProperty(Stub.USERNAME_PROPERTY, SOAP_USER); stub._setProperty(Stub.PASSWORD_PROPERTY, SOAP_PASSWORD); return service; } protected static AllocationService createAllocationService() throws Exception { AllocationServiceServiceLocator locator = new AllocationServiceServiceLocator(); locator.setAllocationServiceEndpointAddress(SOAP_URL + "/AllocationService?wsdl"); AllocationService service = locator.getAllocationService(); Stub stub = (Stub) service; stub._setProperty(Stub.USERNAME_PROPERTY, SOAP_USER); stub._setProperty(Stub.PASSWORD_PROPERTY, SOAP_PASSWORD); return service; } protected static ConsignmentService createConsignmentService(String userId, String password) throws Exception { ConsignmentServiceServiceLocator locator = new ConsignmentServiceServiceLocator(); locator.setConsignmentServiceEndpointAddress(SOAP_URL + "/ConsignmentService?wsdl"); ConsignmentService service = locator.getConsignmentService(); Stub stub = (Stub) service; stub._setProperty(Stub.USERNAME_PROPERTY, userId); stub._setProperty(Stub.PASSWORD_PROPERTY, password); return service; } protected static ConsignmentSearchService createConsignmentSearchService() throws Exception { ConsignmentSearchServiceServiceLocator locator = new ConsignmentSearchServiceServiceLocator(); locator.setConsignmentSearchServiceEndpointAddress(SOAP_URL + "/ConsignmentSearchService?wsdl"); ConsignmentSearchService service = locator.getConsignmentSearchService(); Stub stub = (Stub) service; stub._setProperty(Stub.USERNAME_PROPERTY, SOAP_USER); stub._setProperty(Stub.PASSWORD_PROPERTY, SOAP_PASSWORD); return service; } protected static ManifestService createManifestService() throws Exception { ManifestServiceServiceLocator locator = new ManifestServiceServiceLocator(); locator.setManifestServiceEndpointAddress(SOAP_URL + "/ManifestService?wsdl"); ManifestService service = locator.getManifestService(); Stub stub = (Stub) service; stub._setProperty(Stub.USERNAME_PROPERTY, SOAP_USER); stub._setProperty(Stub.PASSWORD_PROPERTY, SOAP_PASSWORD); return service; } protected static ConsignmentTrackingService createConsignmentTrackingService() throws Exception { ConsignmentTrackingServiceServiceLocator locator = new ConsignmentTrackingServiceServiceLocator(); locator.setConsignmentTrackingServiceEndpointAddress(SOAP_URL + "/ConsignmentTrackingService?wsdl"); ConsignmentTrackingService service = locator.getConsignmentTrackingService(); Stub stub = (Stub) service; stub._setProperty(Stub.USERNAME_PROPERTY, SOAP_USER); stub._setProperty(Stub.PASSWORD_PROPERTY, SOAP_PASSWORD); return service; } public static Calendar createDate(String date) { Calendar calendar = Calendar.getInstance(); try { Date theDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse( date ); calendar.setTime( theDate ); } catch (ParseException e) { e.printStackTrace(); } return calendar; } private static boolean isSameDay(Calendar c1,Calendar c2) { return c1.get( Calendar.DAY_OF_YEAR ) == c2.get( Calendar.DAY_OF_YEAR ); } public static String formatDateTimeNice(Calendar time) { if( time == null ) return ""; SimpleDateFormat formatter = new SimpleDateFormat("HH:mm EEE dd MMM yyyy"); return formatter.format( time.getTime() ); } public static String formatDateRangeNice(com.metapack.deliverymanager.client.DateRange range) { if( range == null ) return ""; if( range.getFrom() == null && range.getTo() == null ) return ""; if( range.getFrom() == null ) { return "Before " + formatDateTimeNice( range.getTo() ); } else if( range.getTo() == null ) { return "After " + formatDateTimeNice( range.getFrom() ); } else { if( isSameDay( range.getFrom() , range.getTo() ) ) { SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm"); return timeFormatter.format( range.getFrom().getTime() ) + " - " + formatDateTimeNice( range.getTo() ); } else { return formatDateTimeNice( range.getFrom() ) + " - " + formatDateTimeNice( range.getTo() ); } } } }

