Translate

Saturday, March 7, 2015

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:

No comments: