(Code samples for this document can be downloaded here.)
You are working with generated code stubs in the SOAP API or unified schema XML API and you want to reuse objects between calls to reduce development time.
The solution is to define calls and objects separately. Depending on your application architecture, each call (for example, AddItem and RelistItem) should be defined in a separate class (or source file). Objects can be defined in source files or by data retrieved from a data store, but should be defined separately from calls.
This solution uses a CallManager class to set up communication between your application and the eBay SDK for Java. CallManager obtains credentials, sets the server URL, sets up logging, and returns an ApiCall object to the class that makes the call.
The basic techniques are described in Authenticate to the SOAP API from Java. What's different about the CallManager class shown in the CallsAndTypes sample package is that it has a method named setUpCall that creates and returns an ApiCall object to another class. This way, the communication code is isolated in one class and reused by all other calls.
The item used in this sample, a music CD, is defined in a separate Java class, ItemHandelCd, that simply sets predefined values. Your application will likely read data from a database or other type of data store to create the item.
package com.ebay.migration;
import com.ebay.soap.eBLBaseComponents.*;
public class ItemHandelCd {
// this method creates the item definition for a Handel CD
public ItemType buildItem() {
ItemType myItem = new ItemType();
// define some needed values
CategoryType cat = new CategoryType();
cat.setCategoryID("307");
ShippingDetailsType shipDetails = new ShippingDetailsType();
shipDetails.setChangePaymentInstructions(new Boolean(true));
BuyerPaymentMethodCodeType[] payments = new BuyerPaymentMethodCodeType[2];
payments[0] = BuyerPaymentMethodCodeType.PaymentSeeDescription;
payments[1] = BuyerPaymentMethodCodeType.VisaMC;
// set these and other values in the item
myItem.setSite(SiteCodeType.US);
myItem.setPrimaryCategory( cat );
myItem.setShippingDetails(shipDetails);
myItem.setCountry(CountryCodeType.US);
myItem.setCurrency(CurrencyCodeType.USD);
myItem.setDescription("Perfect for easy listening");
myItem.setListingDuration(ListingDurationCodeType.Days_1);
myItem.setLocation("San Jose, CA");
myItem.setLotSize( new Integer(6));
myItem.setStartPrice(new AmountType(new Double("1").doubleValue()));
myItem.setPaymentMethods(payments);
myItem.setQuantity( new Integer(1));
myItem.setRegionID("60");
myItem.setShippingTerms(ShippingTermsCodeType.SellerPays);
myItem.setTitle("Handel Water Music CD");
return myItem;
}
}
Then, the AddItem, EndItem, and RelistItem classes define SOAP API requests for three calls. Each request class calls setUpCall in CallManager to create an ApiCall object. A new ApiCall object is created each time a call is made.
AddItem then creates a new item object from ItemHandelCd, passes an AddItemRequest object through ApiCall, and receives and handles the response. The response contains the ItemID, which your application would likely store (in this sample, it is written to a properties file), so that EndItem, RelistItem, and other calls can use it.
public class AddItem {
public static void main(String[] args) throws MalformedURIException, RemoteException {
// get an ApiCall object - we'll use it to make the call
CallManager manager = new CallManager();
ApiCall call = manager.setUpCall();
// get the item from the class that defines it
ItemType item = new ItemType();
item = (new ItemHandelCd()).buildItem();
// make the call and handle the response
try {
// create soap api request and response objects
AddItemRequestType request = new AddItemRequestType();
// set the item in the request
request.setItem(item);
// be sure to cast the response type
AddItemResponseType response = (AddItemResponseType)
call.executeByApiName("AddItem", request);
ItemIDType itemID = response.getItemID();
// write the item ID to a properties file
// your application will probably store to a database
Properties idProperties = new Properties();
idProperties.setProperty("id", itemID.toString() );
idProperties.store(new FileOutputStream("id.properties"), null);
} catch (ApiException ae) {
System.out.println(ae);
} catch (SdkSoapException sse) {
System.out.println(sse);
} catch (SdkException se) {
System.out.println(se);
} catch (IOException e) {
System.out.println(e);
}
}
}
The next call in the series, EndItem, retrieves the ItemID from the properties file, creates an EndItemRequest, and sets the ItemId and ending reason in the request before passing it to the SOAP API through the ApiCall object as usual.
RelistItem also builds the item from ItemHandelCd, then retrieves the ItemID from the properties file, sets the ItemID in the item object, and relists it. After relisting, RelistItem stores the new ItemID to the properties file.
Because the object is created separately from the calls, in ItemHandelCD rather than in AddItem or RelistItem, you can easily reuse it, with the addition of the ItemID needed for RelistItem and EndItem.