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:

Saturday, March 8, 2014

Example 6: Creating URLs for Hyperlinks


Creating URLs for hyperlinks:
Following instructions are for generating URLs for any asset type within FatWire/Oracle WebCenter Sites 11g.

GENERAL STEPS TO FOLLOW:
  • Add render taglib to your JSP.
  • Generally, you need to know few parameters like asset type(c), asset id(cid), parent id(p), packedargs(packedargs), arguments to pass (render:argument)
  • Use render:getpageurl, render:getbloburl, render:gettemplateurl and render:gettemplateurlparameters according to your requirement and generate the output variable.
  • Use string:stream to render the output variable wherever required. (Don't forget to add String taglib to your JSP)
CASE 1: render:getpageurl : This tag creates URLs for assets that are not blobs. NOTE: if you are creating links to assets that are being rendered through templates, you should use the render:gettemplateurl tag instead. This tag creates a URL for an asset, processing the arguments passed to it from the calling element into a URL-encoded string and returning it in a variable.

<render:getpageurl pagename="SiteCatalogPageEntry"
       cid='<%=ics.GetVar("cid")%>'
       c="AssetType"
       wrapperpage="WrapperPage"
       outstr="theURL"/>

CASE 2: render:bloburl : Creates a BlobServer URL without embedding it in an HTML tag and thus, provides an output variable which can be used inside img tag or other.

<render:getbloburl
       blobtable="ImageFile"
       blobcol="urlpicture"
       blobheader='<%=ics.GetVar("asset:mimetype")%>'
       blobkey="id"
       blobwhere='<%=ics.GetVar("asset:id")%>'
       outstr="theURL"/>
<img src='<%=ics.GetVar("theURL")'/>

CASE 3: render:gettemplateurl : Generates a valid URL to an asset, rendered through a template, with optional wrapper page support. User-defined arguments will be packed if wrapperpage is specified. This tag is the preferred method for generating a URL to an asset (provided that the asset is being rendered through a Template, which is the recommendation). It effectively replaces render:getpageurl in most (but certainly not all) cases. Instances of this tag must contain information about who the caller is in order to operate correctly. The caller must be either another Template or a CSElement. This tag will not operate correctly without valid information about the caller i.e. need to provide tid and ttype parameters.

<render:gettemplateurl
       outstr="aUrl"
       c='AssetType'
       cid='AssetId'
       tname='TemplateName (mostly it is layout)'
       wrapperpage='Wrapper' >
</render:gettemplateurl>

CASE 4: render:gettemplateurlparameters : This tag does not create any url but looks up all of the URL parameters that would have been set into a URL and returns them in the form of a List so that can be passed as used as required. Basically used in Satellite:form as below.

<%-- Look up the parameters --%>
<render:gettemplateurlparameters
       outlist="args"
       args="c,cid"
       tname='<%=ics.GetVar("LayoutVar")%>'
       wrapperpage='<%=ics.GetVar("WrapperVar")%>'>
   <render:argument name="p" value='<%=ics.GetVar("p")%>'/>
</render:gettemplateurlparameters>
<satellite:form method="post" id="AddToCartForm">
   <%-- Loop through all of the url parameters and set them
        into the form as hidden fields so the data is sent
        back to Sites as needed.  These variables will include
        pagename, wrapperpage, c, cid, p, possibly rendermode
        and possibly others. --%>
   <ics:listloop listname="args">
       <input type="hidden"
              name="<string:stream list="args" column="name"/>"
              value="<string:stream list="args" column="value"/>" />
   </ics:listloop>
</satellite:form>

CASE 4: satellite:link : This tag generates URL to a page (pagename in SiteCatalog entry) which is present in the OracleWCS/FatWire.

<satellite:link pagename="PAGENAME" outstr="theURL">
 <satellite:parameter name="param_name" value="param_value"/>
</satellite:link>

INFO:
  • Most of time, render:gettemplateurl is used to generate urls for assets
  • While creating URLs in Template or CSElement, developer should keep in mind to include all the parameters by passing them via arguments (render:arguments) so that no errors are found from assembler when it tries to create vanity urls.
----------------------------------------------------
SUGGESTIONS/COMMENTS ARE INVITED
----------------------------------------------------


Sunday, December 8, 2013

Example 5: Searching Assets

Searching assets is very basic functionality required in various online sites whether be a content search, tagged-content search or even media asset search. Content (Assets) can be any assets like product assets, article assets, etc.

Following instructions are for searching FLEX assets using searchstate tags.

GENERAL STEPS TO FOLLOW:
  1. For using searchstate tag, add the searchstate taglib at starting of your JSP/XML. 
  2. Create an empty searchstate object (using searchstate:create tag), which can contain 2 operations: "and" & "or".
  3. Add constraint to the searchstate i.e. adding a condition for restricting search based on single attribute or another searchstate (also called as nested constraint). For adding constraint, use anyone of the following tag as required: searchstate:addlikeconstraint, searchstate:addnestedconstraint, searchstate:addrangeconstraint, searchstate:addsimpleconstraint, searchstate:addsimplestandardconstraint, searchstate:addstandardconstraint, etc. according to you requirement. 
  4. Set the searchstate (Using assetset:setsearchedassets tag)
  5. Get the required attribute values (Using assetset:getmultiplevalues or assetset:getattributevalues)
  6. Then list through the result (Using ics:listloop and ics:listget tag)
CASE 1: A simple case: Finding attributes of a flex asset to search against value of one attribute

<searchstate:create name="ss" />
<searchstate:addsimplelikeconstraint name="ss" attribute="cat2" value="Watt%"/>
<assetset:setsearchedassets name="as" constraint="ss" assettypes="Products"/>
<assetset:getattributevalues name="as" attribute="productdesc" listvarname="resultlist"/>
<ics:listloop listname="resultlist">
<ics:listget listname="resultlist" fieldname="value"/><br/>
</ics:listloop>


Apart from using searchstate tags, there are other tags which can be used for searching assets (flex or basic or table data) which are described briefly below:
  1. ics:sql - It executes inline SQL statement. You can write a SQL query and search against tablename which should be registered SYTEMINFO table.
  2. ics:callsql - It retrieves and executes a SQL query stored in the SYSTEMSQL table
  3. ics:sqlexp - This tag is used only to build where clause based on given set of parameters. It does not execute any statement. It passes a column name and an expression that contains the column name to be used as the left side in the where clause.
  4. ics:selectto - It executes a simple query from a table, which is equivalent to run a simple SQL query like select * from tablename
  5. asset:search - It locates a list of asset primary table rows based on the asset type and a set of search criteria. Criteria can be defined which can narrow your search. Not recommended for searching FLEX assets.
  6. asset:list - This tag queries the database and retrieve a list of assets that meets the specified criteria. It creates a list of assets of one type. Specific criteria could be against field/value of certain tablename, passing argument in field name/value pairs, etc. to restrict the list.
Detailed explanation is provided in Tag Reference/Developer guide.

INFO:
  • The above case is very simple case provided by tag reference itself.
  • Check the examples and details of every searchstate tag in TagReference and Developer Guide
  • Following is the list which shows which searchstate tags to use for what case:
    • searchstate:addsimplestandardconstraint - Adds an attribute name/single value constraint to an existing searchstate object.
    • searchstate:addlikeconstraint - Adds a list of attribute like name/value constraints into a new or existing searchstate object.
    • searchstate:addnestedconstraint - Nests a searchstate as a constraint within another searchstate.
    • searchstate:addrangeconstraint - Adds a range constraint to a searchstate on a specific attribute.
    • searchstate:addsimplelikeconstraint - Adds an attribute like name/value constraint into a new or existing searchstate object.
    • searchstate:addstandardconstraint - Adds a list of attribute name/value constraints into a new or existing searchstate object.

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