Translate

Saturday, March 7, 2015

Example 13: Create, update and delete Asset (Asset API Write)

This section of blog lists how to create, update and delete assets (flex, basic and flex parents only) using Asset API. Most of examples are present in Developer's guide which can be accessed here -> http://docs.oracle.com/cd/E29542_01/doc.1111/e29634/asset_api_tutorial.htm#WBCSD2403

All examples for creating, updating and deleting assets are already present.
Although methods are defined in developer's guide, but some are missing, so I have listed those:
1. Updating an asset's attribute of type-asset

a.getAttributeData("<Attribute Name>").setDataAsList(Arrays.<AssetId>asList( new AssetIdImpl( "<ASSET TYPE>", <ASSET ID in LONG>)));

2. Get an asset's locale

data.getAttributeData("Dimension").getData());

3. Avoid getting parent's attribute value if child has no attribute value by setting immediateOnly

query.getProperties().setIsImmediateOnly(true);

4. While querying asset, you can set Site condition as shown:

query.getProperties().setSite(<SITE ID in Long>);

5. While querying asset, you can set locale condition only if you have copied its name to some other custom attribute via flex filter

6. Get parents or immediate parents

java.util.List<AssetId> parents = data.getImmediateParents();
java.util.List<AssetId> parents = data.getParents();

NOTE: Before using those methods mentioned in guide, please note that:
  1. Flex family or Basic asset family should already be created i.e. framework must be ready
  2. For flex -> attributes, flex parent definition, flex content definition and flex filters must be present too.
  3. Considering that you have FSIISite in your installation, examples provided in guide are valid
General steps to follow:
    1. Get a session
    2. Get a handle to AssetDataManager
    3. Use appropriate method to
      • CREATE - AssetDataManager.insert( List<AssetData> data )
      • UPDATE - AssetDataManager.update( List<AssetData> data )
      • DELETE - AssetDataManager.delete( List<AssetData> data )
    4. Use the above API with care and always handle all exceptions with logging

----------------------------------------------------
SUGGESTIONS/COMMENTS ARE INVITED
----------------------------------------------------





Example 12: Rendering Basic and Flex Asset (Asset API Read)

Asset API are nothing but FatWire / Oracle WebCenter Sites JAVA API which provides classes to perform CRUD operation on assets. This were created in order to use them in non-servlet context, such as standalone java programs. Thus, this API can be used regardless of servlet framework as stated in guide.

Following code snippet is taken from guide and I have updated it little bit for blog purpose only.

You can go through full Asset API chapter here -> http://docs.oracle.com/cd/E29542_01/doc.1111/e29634/asset_api_tutorial.htm#WBCSD2387

Reading Data provided Asset Type and Asset Id

// Following 2 lines are always required if you want to use Asset API
// Don't forget to include correct java classes
Session ses = SessionFactory.getSession();
AssetDataManager mgr =(AssetDataManager) ses.getManager( AssetDataManager.class.getName() );

// After getting the AssetDataManager object, we use it to read the asset data by using method - readAttributes(<AssetType:AssetId>,<List of attributes>)

// First set the id in AssetId object as shown
AssetId id = new AssetIdImpl( <AssetType as String>, <AssetId in Long> );

// Create list of attributes; even for single attribute have to generate list only
List attrNames = new ArrayList();
attrNames.add( "name" );
attrNames.add( "description" );

//Now read attributes using mgr.readAttributes method and print output
AssetData data = mgr.readAttributes( id, attrNames );
AttributeData attrDataName = data.getAttributeData( "name" );
AttributeData attrDataDescr = data.getAttributeData( "description" );
//Output
out.println( "name:" + attrDataName.getData() );
out.println( "<br/>" );
out.println( "description:" + attrDataDescr.getData() );
out.println( "<br/>" );

//Above example was for the case of loading a single asset id (<assettype>:<assetid in Long>) to readAttributes method
//Multiple AssetIds can be processed via the AssetManager.read(List<AssetId> ids) method as shown below (For single AssetId object, we have to pass it as list only) and get all attributes:
Iterable<AssetData> dataItr = mgr.read( Collections.singletonList( id ) );

for( AssetData data : dataItr )
{
  for(AttributeData atrData : data.getAttributeData() )
  {
    out.println( "<br/>" );
    out.println( "attribute name:" + atrData.getAttributeName() );
    out.println( "data: " + atrData.getData() );
  }
}

Reading data on basis of some Criteria - Using Query

//For example: If you want to search against particular attribute
//Note: This example is totally copied from the developer guide
//We use Condition class to create the criteria for search
//Query class to process this criteria and generate query
//Read the Query using AssetManager.read(Query query) method as shown
<%@ page import="com.fatwire.system.*"%>
<%@ page import="com.fatwire.assetapi.data.*"%>
<%@ page import="com.fatwire.assetapi.query.*"%>
<%@ page import="java.util.*"%>
<cs:ftcs>
<%
Session ses = SessionFactory.getSession();
AssetDataManager mgr = (AssetDataManager) ses.getManager( AssetDataManager.class.getName() );
Condition c = ConditionFactory.createCondition( "FSIISKU", OpTypeEnum.EQUALS, "iAC-008" );
Query query = new SimpleQuery( "Product_C", "FSII Product", c, Collections.singletonList( "name" ) );

for( AssetData data : mgr.read( query ) )
{
AttributeData attrData = data.getAttributeData( "name" );
out.println( "name:" + attrData.getData() );
out.println( "<br/>" );
out.println( "id:" + data.getAssetId() );
}
%>
</cs:ftcs>
// Read other topics from the guide: