What is Flex Filter?
Flex filter is a post processor for an asset. The filter is invoked whenever the asset is saved/edited.
Some common usages of flex filter are
- Image Processing: For instance, you may have a image of a particular resolution. You can write a flex filter, which creates images of different resolutions
- Document: For instance, you may have an article asset, and a flex filter for it, which creates corresponding txt files or PDF files.
You can get more details on the flex filter Webcenter Sites 11g Developer guide. The idea of this writeup is to help you guys to to implement a very simple flex filter.
For the purpose of this, we are assuming that
- 3 fields(firstname, lastname, username) are already created for your site
- We have a sample asset definition with 2 fields (firstName, lastName), We will be writing a simple flex filter to have 3 field named username. The filter is append "_Username" to the value of firstName field. For instance if firstname is "John", then when you save the asset, the filter will add a value of "John_Username" to username attribute.
Implementing a flex filter 4 step process
- Create custom flex filter class
- Register flex filter
- Create flex filter asset
- Assign the flex filter asset to any other asset
Create Customer Flex Filter
- Create a custom class that implements AbstractFlexFilter
}
b. Implement the below 2 mandatory methods
public void filterAsset(IFilterEnvironment env, String identifier, FTValList fList, IFilterableAssetInstance instance) throws AssetException;
This is the main method which will have your logic. For our example, this method, will get the value of the inputfieldname(firstname in our case), and set the username field with the updated value.
public FTValList getLegalArguments(IFilterEnvironment arg0, String arg1) throws AssetException;
This method is invoked to populate the combobox after selecting the filter or clicking getArguments
package com.demo;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import COM.FutureTense.Interfaces.FTValList;
import com.openmarket.basic.interfaces.AssetException;
import com.openmarket.gator.flexfilters.AbstractFlexFilter;
import com.openmarket.gator.interfaces.IFilterEnvironment;
import com.openmarket.gator.interfaces.IFilterableAssetInstance;
import com.openmarket.basic.interfaces.IListBasic;
public class DemoFilter extends AbstractFlexFilter {
private String Filter_Attributes[] = {
"Input Attribute"
, "Output Attribute"
};
private Map<String, String> filterParameters = null;
public DemoFilter(FTValList ftvallist) {
super(ftvallist);
filterParameters = new HashMap<String, String>();
System.out.println("DemoFilter Constructor: " + ftvallist);
//If any parameter is required to be passed during filter registration, we can fetch the parameters from filter table
if ( ftvallist != null) {
System.out.println(ftvallist);
Set<String> entries = ftvallist.keySet();
for ( String key : entries ) {
filterParameters.put(key, ftvallist.getValString(key));
}
}
}
@Override
public void filterAsset(IFilterEnvironment env, String identifier,
FTValList fList, IFilterableAssetInstance instance)
throws AssetException {
String value = null;
System.out.println(instance.getAssetID() + ":" + instance.getAssetID());
// Put some interesting information in the log.
System.out.println(identifier+" SampleFlexFilter filterAsset :"
+" FilterType="+env.getFilterType()
+", AttributeType="+env.getAttributeType()
+", AssetTypeName="+instance.getAssetTypeName()
+", GroupTypeName="+instance.getGroupTypeName()
);
//Get the attribute details... When filter asset is created, we specify the name of the inputfield & output field... In this case inputfield is set as "firstname" and output field is set as "username"
String inputattr = getAttrID(env, fList, Filter_Attributes[0]);
System.out.println(4 + ":" + inputattr + " : " + Filter_Attributes[0]);
IListBasic ilistbasic = instance.getAttribute(inputattr);
if ( inputattr != null && ilistbasic != null && ilistbasic.hasData() ) {
try {
value = ilistbasic.getValue("value");
}
catch ( NoSuchFieldException e) {
System.out.println("SampleFlexFilter : NoSuchFieldException");
}
}
if (value == null) {
throw new AssetException("Value cannot be blank");
}
// Create the derived attribute using the attribute name and the string values
// that were initialized above.
System.out.println("filterParameters: " + this.filterParameters);
instance.addDerivedDataValue(identifier
, env.getAttributeIdentifier(fList.getValString(Filter_Attributes[1]))
, value + "_user");
}
@Override
public FTValList getLegalArguments(IFilterEnvironment arg0, String arg1)
throws AssetException {
FTValList ftvallist = new FTValList();
ftvallist.setValString(Filter_Attributes[0], Filter_Attributes[0]);
ftvallist.setValString(Filter_Attributes[1], Filter_Attributes[1]);
return ftvallist;
}
}
package com.demo;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import COM.FutureTense.Interfaces.FTValList;
import com.openmarket.basic.interfaces.AssetException;
import com.openmarket.gator.flexfilters.AbstractFlexFilter;
import com.openmarket.gator.interfaces.IFilterEnvironment;
import com.openmarket.gator.interfaces.IFilterableAssetInstance;
import com.openmarket.basic.interfaces.IListBasic;
public class DemoFilter extends AbstractFlexFilter {
private String Filter_Attributes[] = {
"Input Attribute"
, "Output Attribute"
};
private Map<String, String> filterParameters = null;
public DemoFilter(FTValList ftvallist) {
super(ftvallist);
filterParameters = new HashMap<String, String>();
System.out.println("DemoFilter Constructor: " + ftvallist);
//If any parameter is required to be passed during filter registration, we can fetch the parameters from filter table
if ( ftvallist != null) {
System.out.println(ftvallist);
Set<String> entries = ftvallist.keySet();
for ( String key : entries ) {
filterParameters.put(key, ftvallist.getValString(key));
}
}
}
@Override
public void filterAsset(IFilterEnvironment env, String identifier,
FTValList fList, IFilterableAssetInstance instance)
throws AssetException {
String value = null;
System.out.println(instance.getAssetID() + ":" + instance.getAssetID());
// Put some interesting information in the log.
System.out.println(identifier+" SampleFlexFilter filterAsset :"
+" FilterType="+env.getFilterType()
+", AttributeType="+env.getAttributeType()
+", AssetTypeName="+instance.getAssetTypeName()
+", GroupTypeName="+instance.getGroupTypeName()
);
//Get the attribute details... When filter asset is created, we specify the name of the inputfield & output field... In this case inputfield is set as "firstname" and output field is set as "username"
String inputattr = getAttrID(env, fList, Filter_Attributes[0]);
System.out.println(4 + ":" + inputattr + " : " + Filter_Attributes[0]);
IListBasic ilistbasic = instance.getAttribute(inputattr);
if ( inputattr != null && ilistbasic != null && ilistbasic.hasData() ) {
try {
value = ilistbasic.getValue("value");
}
catch ( NoSuchFieldException e) {
System.out.println("SampleFlexFilter : NoSuchFieldException");
}
}
if (value == null) {
throw new AssetException("Value cannot be blank");
}
// Create the derived attribute using the attribute name and the string values
// that were initialized above.
System.out.println("filterParameters: " + this.filterParameters);
instance.addDerivedDataValue(identifier
, env.getAttributeIdentifier(fList.getValString(Filter_Attributes[1]))
, value + "_user");
}
@Override
public FTValList getLegalArguments(IFilterEnvironment arg0, String arg1)
throws AssetException {
FTValList ftvallist = new FTValList();
ftvallist.setValString(Filter_Attributes[0], Filter_Attributes[0]);
ftvallist.setValString(Filter_Attributes[1], Filter_Attributes[1]);
return ftvallist;
}
}
Register flex filter
Once you have your flex filter implementation is in place, the next step is to create the jar and put it in /cs/web-inf/lib folder and then register the filter in your sites. To register the filter, you may use sites explorer and add a new row to the filter table as shown below.
Once you complete this step, make sure to restart your wc sites.
Create flex filter asset
On successful registration of the flex filter asset, you create a flex filter asset using WC Sites admin console for your site. For this create a new filter, and chose the filter you created from the dropdown and click on getArguments.
As you can see, this retrieves the arguments that is required for this filter. As you may have noted that "getArguments" invokes getLegalArguments method from the flex filter class that you created.
There are 2 parameters which are required for this filter. Input Attribute (which is firstname) and Output attribute(which is username). Both firstname & lastname are attributes that should have been created in your site.
Assign the flex filter asset to any other asset
Assign the filter to a asset definition. In this example, I have created a asset defnation named "Sample Asset". This is 2 attributes (Firstname & lastname), apart from this I have assigned the filter that I have created.
Test your filter
Create a new sample asset using contributor interface. Give all the required values
Name: Demo Content
firstName: John
LastName: Corner
Save the attribute.
Open the asset in read only mode, You should be able to see username field with value "John_Username".
The code for this example is also being attached with this tutorial. Please feel free to add your comments for any questions.
The jar that may be required with this project would be
- gator.jar
- cs.jar
- xcelarate.jar
- basic.jar
- framework.jar
- cs-cache-1.2.2.jar
3 comments:
Hi Shashank,
I followed all the below steps:
1. Creating custom flex filter class
2. Registering flex filter
3. When I am Creating flex filter asset I am getting the following error. Can you please help me why I'm getting this error. Please find the following error:
Arguments:Could not get arguments from filter, -100, Error in FLEXFILTER.GETLEGALARGS endTag() in tag com.openmarket.gator.seed.FlexFilterInstance in page 'OpenMarket/Xcelerate/Actions/NewContentFront' in element 'OpenMarket/Gator/FlexibleAssets/Filters/ContentForm' errno: 0 errdetail: 0 java.lang.UnsupportedClassVersionError: com/demo/DemoFilter : Unsupported major.minor version 52.0 (unable to load class com.demo.DemoFilter)
Thank you in advance!!
Hi Shashank,
1. Created custom flex filter class
2. Registered flex filter
3. When I am creating flex filter asset, I am getting following error. Can you please help me why I am getting this error.
Arguments: Could not get arguments from filter, -100, Error in FLEXFILTER.GETLEGALARGS endTag() in tag com.openmarket.gator.seed.FlexFilterInstance in page 'OpenMarket/Xcelerate/Actions/NewContentFront' in element 'OpenMarket/Gator/FlexibleAssets/Filters/ContentForm' errno: 0 errdetail: 0 java.lang.UnsupportedClassVersionError: com/demo/DemoFilter : Unsupported major.minor version 52.0 (unable to load class com.demo.DemoFilter)
It seems that you compiled the code with one version of Java and tried to run the compiled code with a lower version
Post a Comment