Products API Examples - Java

Overview

This document reflects over the aforementioned use cases of the Product API. All examples defined within demonstrate calls to the SOAP interface and are provided as is.

Requirements

The following examples demonstrate calls to the Products API in Java. Java examples demonstrated here assume the use of a generated XML web service client. To generate such a client, a tool such as wsdl2java can be used. A detailed description on this tool can be found on the Apache Web Services website, but all you will need to do is point it at your site's WSDL description.

Retrieve a single Products' details.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.authenticate(username, password);

String style = "LU0456-DarkGreen";
// Retrieve the product
ProductApiResponse retrieveResponse = 
	service.retrieve(authenticationResponse.getMessage(), style);

// Examine the ProductApiResponse for our product.
if (retrieveResponse.getData()[0] == null) {
	System.out.printf("Failed to retrieve style: %s", style);
} else {
	// Extract the product from the response.
	Product product = retrieveResponse.getData()[0];
}
			

List style codes for Products by term, catalogue and all (unfiltered).

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.authenticate(username, password);

// A simple search of all the product Styles for the provided search term.
StringArrayApiResponse searchResponse = 
	service.search(authenticationResponse.getMessage(), "dress");

for(String style : searchResponse.getData()) {
	System.out.println(style);
}
// List all style codes under the provided catalogue
StringArrayApiResponse listing = 
	service.listByCatalogue(authenticationResponse.getMessage(), "Women");

for(String style : listing.getData()) {
	System.out.println(style);
}
// Lists all product style codes
StringArrayApiResponse productListing = 
	service.list(authenticationResponse.getMessage());

for(String style: productListing.getData()) {
	System.out.println(style);
}
			

Add a new Product.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.authenticate(username, password);

Product product = new Product();
product.setAvailableFrom(Calendar.getInstance());
product.setCatalogues(new String[] { "Womens"});
product.setContentLocked(false);
product.setCost(new BigDecimal(56.95));
product.setForeignIdentity(UUID.randomUUID().toString());

ProductItem item = new ProductItem();
item.setAvailableFrom(Calendar.getInstance());
item.setBarcode("9300000034267");
item.setColour("Black");
item.setColourCode("BLK");
item.setCost(new BigDecimal(56.95));
item.setForeignIdentity(UUID.randomUUID().toString());
item.setInStock(true);
item.setPrice(new BigDecimal(149.00));
item.setQuantity(100);
item.setRetail(new BigDecimal(45.3));
item.setSize("Extra Large");
item.setSizeCode("XL");
item.setWeight(44d);

product.setItems(new ProductItem[] { item});
product.setShortDescription("100% Soft Merino Wool");
product.setStatus(0);
product.setStyle("LU0456-DarkGreen");
product.setSupplier("Default Supplier");
product.setTitle("Snap Dome Placket Swing Top");
product.setWeight(44d);

// Invoke the updateProduct web method.
service.updateProduct(authenticationResponse.getMessage(), product);
			

Update an existing Product.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.authenticate(username, password);

// Style code (SKU) to search the Products Store for.
String style = "LU0456-DarkGreen";

ProductApiResponse retrieveResponse = 
	service.retrieve(authenticationResponse.getMessage(), style);

if (retrieveResponse.getData()[0] == null) {
	System.out.printf("Failed to retrieve style:", style);
} else {
	Product product = retrieveResponse.getData()[0];
	product.setShortDescription("Updated Short Description.");
	// Invoke the updateProduct web method.
	service.updateProduct(authenticationResponse.getMessage(), product);
		
	}
}
			

Delete a Product from the database.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.authenticate(username, password);

// Product to delete from the system.
String style = "BC-M005";

// Delete the product from the system.
service.delete(authenticationResponse.getMessage(), style);
			

Add Product Items.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse ar = service.authenticate(username, password);

ProductItem item = new ProductItem(); 
item.setAvailableFrom(Calendar.getInstance());
item.setBarcode("9300000034268");
item.setColour("Orange");
item.setColourCode("ORA");
item.setCost(new BigDecimal(98.67));
item.setForeignIdentity(UUID.randomUUID().toString());
item.setInStock(true);
item.setPrice(new BigDecimal(124.00));
item.setQuantity(100);
item.setRetail(new BigDecimal(99.99));
item.setSize("Extra Small");
item.setSizeCode("XS");
item.setWeight(43d);

ProductApiResponse productResponse = 
	service.retrieve(ar.getMessage(), "LU0456-DarkGreen");

Product product = productResponse.getData()[0];

List<ProductItem> items = 
	new ArrayList<ProductItem>(Arrays.asList(product.getItems()));

items.add(item);

product.setItems(items.toArray(new ProductItem[items.size()]));

// Invoke the updateProduct web method.
service.updateProduct(ar.getMessage(), product);
			

Update Product Item details.

	// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse ar = service.authenticate(username, password);

// Extract the product from the response.
Product product = 
	service.retrieve(ar.getMessage(), "LU0456-DarkGreen").getData()[0];
List<ProductItem> items = Arrays.asList(product.getItems());

if (!items.isEmpty()) {
	
	ProductItem item = items.get(0);
	
	item.setPrice(new BigDecimal(87.98));
	
	product.setItems(items.toArray(new ProductItem[items.size()]));
}

// Invoke the updateProduct web method.
service.updateProduct(authenticationResponse.getMessage(), product);
			

Update Site availability for Product Items.

// Instantiate the web service proxy.
ProductsAPI service = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse authenticationResponse = service.authenticate(username, password);

// Send through an availability entry for a given barcode
AvailabilityEntry availability = new AvailabilityEntry();
availability.setBarcode("9300000034268");
availability.setCost(new BigDecimal(87.2));
availability.setPrice(new BigDecimal(87.1));
availability.setQuantity(100);
availability.setSalePrice(new BigDecimal(76.2));

// Invoke the updateAvailability web method. 
	AvailabilityEntry[] array = {availability};
	service.updateAvailability(authenticationResponse.getMessage(), array);
			

Update Store availability for Product Items.

// Instantiate the web service proxy.
ProductsAPI svc = new ProductsAPI();

// User to authenticate with.
String username = "Testing";
// Corresponding password to authenticate with.
String password = "Sup94zex";

// Authenticate
ApiResponse ar = svc.authenticate(username, password);

// Send through an availability entry for a given barcode. 
// This will update the availability for the given barcode, store and website.
AvailabilityEntry ave = new AvailabilityEntry(); 
ave.setBarcode("9300000034268");
ave.setBranch("454");
ave.setQuantity(900);
ave.setWebsite(1);

// Invoke the updateAvailability web method. 
svc.updateAvailability(ar.getMessage(), new AvailabilityEntry[] { ave } );