question

Lee Tickett avatar image
Lee Tickett asked Lee Tickett commented

Can't Create/Update Item with Category using Inventory Connector

Our code looks like;

Category newCategory = new Category();
newCategory.setName("Test Category");
newCategory.setItems(new ArrayList<Reference>());
mCategory = Collections.singletonList(mInventoryConnector.createCategory(newCategory));

Item newItem = new Item();
newItem.setName("Test Item");
newItem.setCategories(mCategory);
newItem.setPrice(100L);
mInventoryConnector.createItem(newItem);

The category is created.
The item is created but is not tagged with the category. Is there a bug in the createItem() method or am I doing something wrong?

As a workaround we are now creating our items first then creating the category and passing in the item list;
Item newItem = new Item();
newItem.setName("Test Item");
newItem.setPrice(100L);
newItem = mInventoryConnector.createItem(newItem);

Category newCategory = new Category();
newCategory.setName("Test Category");
List<Reference> items = new ArrayList<>();
items.add(new Reference(newItem.getJSONObject()));
newCategory.setItems(items);
mInventoryConnector.createCategory(newCategory);

I was curious why the .setItems() method takes a list of Reference rather than a list of Item? Is my technique of converting the Item to a Reference OK/the best?

Thanks
Inventory
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Raymond Lee avatar image
Raymond Lee Deactivated answered Lee Tickett commented
For adding an item to an existing category, there is an addItemToCategory() function in the InventoryConnector that takes in an item id and the category id. You can get the category id from the Category object returned from the createCategory() function, and the item id from the Item object returned from the createItem() function.

Here is an example:
Category newCategory = new Category();
newCategory.setName("New Category");
newCategory.setItems(new ArrayList<Reference>());
newCategory = mInventoryConnector.createCategory(newCategory);

Item newItem = new Item();
newItem.setName("New Item");
newItem.setPrice(100L);
newItem = mInventoryConnector.createItem(newItem);

mInventoryConnector.addItemToCategory(newItem.getId(), newCategory.getId());

As for why setItems() takes a list of References, it is because it is intended to be used to set the Category's item field, which is a list of References (A Reference object is a wrapper around an ID, so in this case, it is a list of Item IDs). That is also why calling Category.getItems() will return you a list of Reference objects, each representing an item ID of an existing Inventory Item.

The way you converted the Item to a Reference works, another way is you can create an new Reference object, and just call Reference.setId() and pass in the Item ID. But either way works.
3 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Lee Tickett avatar image Lee Tickett commented ·

Some really helpful stuff in there, i'll give some of that a try- thanks.

The one point you didn't address was "Is there a bug in the createItem() method or am I doing something wrong?" (i.e. why when I try and create an item and tell it which categories the item should be tagged with, it doesn't work).

0 Likes 0 ·
Raymond Lee avatar image Raymond Lee Lee Tickett commented ·

Ah, sorry for missing that point:

It is not necessarily a bug, nor that you were doing it wrong, but it is because Category-Items is a many-to-many type of association, so we require a special function to link the two. See here for details.

The API is similar in that when you create an Item, you could specify a category/categories during the creation, but it will be ignored like what happened in your case.

0 Likes 0 ·
Lee Tickett avatar image Lee Tickett Raymond Lee commented ·

It makes a little sense but; a) the statement about many-to-many relationships isn't true as I am able to supply a list of items when I create a category and that works? b) why provide the methods if they don't work?

I think I have enough options now, but still consider it a bug.

Thanks

1 Like 1 ·

Welcome to the
Clover Developer Community