/
Java SDK Sample: Change Item Attribute Name
Java SDK Sample: Change Item Attribute Name
This tutorial will load all the items from the account, look for ones with a particular attribute name, change it, and then perform batch updates of those items.
Change Item Attribute Sample Program
package com.ultracart.javasdksamples; import com.ultracart.admin.v2.ItemApi; import com.ultracart.admin.v2.models.Item; import com.ultracart.admin.v2.models.ItemContentAttribute; import com.ultracart.admin.v2.models.ItemsRequest; import com.ultracart.admin.v2.models.ItemsResponse; import com.ultracart.admin.v2.swagger.ApiClient; import com.ultracart.admin.v2.swagger.ApiException; import com.ultracart.admin.v2.swagger.Configuration; import com.ultracart.admin.v2.swagger.auth.ApiKeyAuth; import java.util.ArrayList; import java.util.List; /* TODO: Add the dependency for the SDK JAR to your program Maven users Add this dependency to your project's POM: <dependency> <groupId>com.ultracart</groupId> <artifactId>rest-sdk</artifactId> <version>1.0.4</version> <scope>compile</scope> </dependency> Gradle users Add this dependency to your project's build file: compile "com.ultracart:rest-sdk:1.0.4" */ public class ChangeItemAttribute { // TODO: Replace this with your API Simple Key private static final String API_KEY = "YOUR API KEY HERE"; private static void initClient() { ApiClient defaultClient = Configuration.getDefaultApiClient(); ApiKeyAuth ultraCartSimpleApiKey = (ApiKeyAuth) defaultClient.getAuthentication("ultraCartSimpleApiKey"); ultraCartSimpleApiKey.setApiKey(API_KEY); defaultClient.addDefaultHeader("X-UltraCart-Api-Version", "2017-03-01"); defaultClient.setVerifyingSsl(true); } public static void main(String... args) { initClient(); ItemApi api = new ItemApi(); try { int limit = 200; // int | The maximum number of records to return on this one API call. Maximum is 200 String sort = "merchant_item_id"; // string | The sort order of the orders. See Sorting documentation for examples of using multiple values and sorting by ascending and descending. String expand = "content.attributes"; // TODO: If you modify this program to update something other than attributes, make sure you adjust your expansion string. See https://www.ultracart.com/api/#resource_item.html -> Expansion for details ArrayList<Item> itemsToUpdate = new ArrayList<Item>(); // Collect the items to update here int itemsQueried = 0; for (int offset = 0; ; offset += limit) { ItemsResponse itemsResponse = api.getItems(null, null, limit, offset, null, sort, expand, false); if (itemsResponse.isSuccess()) { System.out.println("Retrieved " + itemsResponse.getItems().size() + " items."); itemsQueried += itemsResponse.getItems().size(); for (Item item : itemsResponse.getItems()) { System.out.println("Item: " + item.getMerchantItemId()); if (item.getContent() != null && item.getContent().getAttributes() != null) { for (ItemContentAttribute attribute : item.getContent().getAttributes()) { // Look for the attribute name that is old if (attribute.getName().equalsIgnoreCase("offer")) { // Change the attribute name attribute.setName("attOffer"); // Add the item to a list of items we want to update at the end. itemsToUpdate.add(item); System.out.println(" new attribute " + attribute.getName() + " => " + attribute.getValue()); break; } } } } // Are we done paginating through the list of items? if (itemsResponse.getItems().size() < limit) break; } else { System.out.println("Error retrieving items"); break; } } // Output some statistics System.out.println("Queried [" + itemsQueried + "] items."); System.out.println("Update [" + itemsToUpdate.size() + "] items."); // TODO: If you have a large amount of updates you can have them run async by setting this to true // TODO: UltraCart will receive the request, buffer it and then process the updates in the backward. // TODO: Async is not recommended for small numbers of updates. boolean asyncUpdate = false; // Change this to true if you want async updates to take place int batchSize = asyncUpdate ? 100 : 20; List<Item> batch = new ArrayList<Item>(); for (Item item : itemsToUpdate) { batch.add(item); // Send an async batch every 20 or 100 items depending upon async if (batch.size() == batchSize) { ItemsRequest itemsRequest = new ItemsRequest(); itemsRequest.setItems(batch); api.updateItems(itemsRequest, expand, false, asyncUpdate); batch.clear(); // TODO: Remove this break statement after you have verified your updates are working correctly before you // TODO: run this program against your entire item database if (true) break; } } // Send a final batch if (batch.size() > 0) { ItemsRequest itemsRequest = new ItemsRequest(); itemsRequest.setItems(batch); api.updateItems(itemsRequest, expand, false, asyncUpdate); batch.clear(); } } catch (ApiException e) { System.err.println("Exception when calling Api#getItems or Api#updateItems"); e.printStackTrace(); } } }
, multiple selections available,
Related content
API Simple Key
API Simple Key
Read with this
Batch Item Import
Batch Item Import
More like this
API
Read with this
Activation Code API
Activation Code API
More like this
Channel Partner API - SOAP Guide (Deprecated)
Channel Partner API - SOAP Guide (Deprecated)
More like this
Tools
More like this