Making A Transaction Filter
To make a transaction filter, create a Java class that extends the
abstract class
neo.xredsys.api.services.TransactionFilterService
.
This is a convenience class containing empty "do nothing"
implementations of the methods defined in the
neo.xredsys.api.TransactionFilter
interface:
doCreate(object)
-
which is executed immediately before a new object is saved.
doUpdate(object)
-
which is executed immediately before changes to an existing object are saved.
doStagedUpdate(object)
-
which is executed immediately before changes to an existing staged object are saved. For an explanation of staging, see Content Item Staging.
Note that you cannot change the state of a staged object to
published
in a transaction filter. Any attempt to do so will result in an exception. doDelete(object)
-
which is executed immediately before an existing object is deleted.
isEnabled()
-
which is called by the Content Engine to determine whether or not the filter is currently enabled.
All you need to do in your class is re-implement the "do"
method(s) that you are interested in. The object
parameter passed in to these methods is a
neo.xredsys.api.IOTransaction
object and represents
the object being created, updated or deleted. You can both query this
object and modify it. In the case of create and update transactions, any
changes you make to it will be reflected in the saved object.
Here is an example transaction filter called
com.mycompany.transactionFilters.WordCount
that:
-
Counts the words in the body of a newly-created or updated content item
-
Writes the word count to one of the content item's fields (called
wordcount
- the example assumes that the content item has a field with this name)
package com.mycompany.transactionFilters; import java.util.*; import neo.xredsys.api.*; import neo.xredsys.api.services.*; public classWordCount
extendsTransactionFilterService
{ publicWordCount
() { } public void doCreate(IOTransaction pObject) throws FilterException { if (pObject instanceof ArticleTransaction) { countWords((ArticleTransaction)pObject); } } public void doUpdate(IOTransaction pObject) throws FilterException { if (pObject instanceof ArticleTransaction) { countWords((ArticleTransaction)pObject); } } public void doStagedUpdate(IOTransaction pObject) throws FilterException { if (pObject instanceof ArticleTransaction) { countWords((ArticleTransaction)pObject); } } public void countWords(ArticleTransaction pArticle) { String original = pArticle.getElementText("body"); int count = new StringTokenizer(original).countTokens(); pArticle.setElementText("wordcount", Integer.toString(count)); } }
Only update and create operations are of interest here, so the
class does not contain an implementation of
doDelete()
. The doCreate()
and
doUpdate()
methods are identical: they simply check
if the transaction object is a content item and if it is, call
countWords()
. countWords()
counts
the words in the content item's body
field and writes
the result to the wordcount
field.
This version of the filter cannot easily be re-used because the
field names body
and wordcount
are
hard-coded. It is better to create properties for these field
names:
private String mFieldToCount = "body"; private String mFieldToUpdate = "wordcount"; public void setFieldToCount(String pFieldToCount) { mFieldToCount = pFieldToCount; } public String getFieldToCount() { return mFieldToCount; } public void setFieldToUpdate(String pFieldToUpdate) { mFieldToUpdate = pFieldToUpdate; } public String getFieldToUpdate() { return mFieldToUpdate; }
and modify the countWords()
method
accordingly:
public void countWords(ArticleTransaction pArticle) { String original = pArticle.getElementText(getFieldToCount()); int count = new StringTokenizer(original).countTokens(); pArticle.setElementText(getFieldToUpdate(), Integer.toString(count)); }
If you do this, then the field names can be configured externally by setting parameters - see Using A Transaction Filter for further information.
The countWords()
method is a very rudimentary
word counter and not recommended for real use.
Before a transaction filter can be used it must be:
-
Compiled
-
Added to the Content Engine's classpath
To compile a transaction filter you need the Escenic JAR file
engine-core-5.6.13.183224.jar
and the corresponding current
version of
common-nursery-
version-number.jar
in your classpath.