Adding a category to an asset in a custom policy
Hello
I need to create a custom policy that will add a category to an asset. I've been digging through the Java API and think I have a good approach, but it is not working. My code looks like this...
CategorySchema newCategorySchema = myAsset.getAvailableCategorySchema("Service Usage Indicator");Category newCategory = newCategorySchema.getCategory("Service Usage");SubCategory newSubCategory = newCategory.getSubCategory("First Use");SubCategory[] subCategories1 = {newSubCategory};newCategory.setSubCategories(subCategories1);Category[] categories1 = {newCategory};newCategorySchema.setCategories(categories1);CategorySchema[] schemas1 = {newCategorySchema};myAsset.setCategorySchemas(schemas1);
However, this gives me an exception...
exception java.lang.NullPointerException stack trace=[com.ibm.ram.client.RAMCategorySchema.setDirty(RAMCategorySchema.java:411), com.ibm.ram.client.RAMCategory.setDirty(RAMCategory.java:415), com.ibm.ram.client.RAMSubCategory.setDirty(RAMSubCategory.java:432), com.ibm.ram.client.RAMSubCategory.setSubCategories(RAMSubCategory.java:706), com.ibm.ram.client.RAMCategory.setSubCategories(RAMCategory.java:474), ......
What is the correct way to categorize an asset via a custom policy? I've tried many variations of the above, but keep hitting exceptions of one kind or another.
Thanks for any help!
Accepted answer
Well, I was making this harder than it needed to be. Plus i skipped over the part of the documentation that actually answered by question!
From the IBM documentation found here >
Extending Product Function > Extending Rational Asset Manager > Using Rational Asset Manager Java API > Work with assets > Create, modify, or delete assets > Categorize an asset
//Categorize using the category schema fetched from the sessionCategorySchema automobilesSchema =session.getCategorySchema("Automobiles");
Category priceCategory = automobilesSchema.getCategory("Price");newAsset.categorize(priceCategory, "25000");
//Categorize using a category fetched through the assetautomobilesSchema = newAsset.getAvailableCategorySchema("Automobiles");
Category colorCategory = automobilesSchema.getCategory("Color");newAsset.categorize(colorCategory, "Red");
//Categorize using sub category objectsCategory modelCategory = automobilesSchema.getCategory("Model");SubCategory domestic = modelCategory.getSubCategory("Domestic");SubCategory foreign = modelCategory.getSubCategory("Foreign");SubCategory honda = foreign.getSubCategory("Honda");SubCategory camry = foreign.getSubCategory("Toyota/Camry");newAsset.categorize(domestic);newAsset.categorize(honda);newAsset.categorize(camry);
session.put(newAsset, new NullProgressMonitor());
Comments
getSubCategory("SubCategory-Name"); this method does not work when the input argument "sub-category-name" has backslash(/).
E.g: getSubCategory("sub/category") --> returns null
Please let me know if there is any workaround for this. I tried to eescape it by placing \ before /, but did not work.