use of org.alfresco.web.action.ActionEvaluator in project acs-community-packaging by Alfresco.
the class ActionsElementReader method parseActionDefinition.
/**
* Parse an ActionDefinition from the specific config element.
*
* @param actionElement The config element containing the action def
*
* @return The populated ActionDefinition
*/
public ActionDefinition parseActionDefinition(Element actionElement) {
String actionId = actionElement.attributeValue(ATTRIBUTE_ID);
if (actionId == null || actionId.length() == 0) {
throw new ConfigException("'action' config element specified without mandatory 'id' attribute.");
}
// build a structure to represent the action definition
ActionDefinition actionDef = new ActionDefinition(actionId);
// look for the permissions element - it can contain many permission
Element permissionsElement = actionElement.element(ELEMENT_PERMISSIONS);
if (permissionsElement != null) {
// read and process each permission element
Iterator<Element> permissionItr = permissionsElement.elementIterator(ELEMENT_PERMISSION);
while (permissionItr.hasNext()) {
Element permissionElement = permissionItr.next();
boolean allow = true;
if (permissionElement.attributeValue(ATTRIBUTE_ALLOW) != null) {
allow = Boolean.parseBoolean(permissionElement.attributeValue(ATTRIBUTE_ALLOW));
}
String permissionValue = permissionElement.getTextTrim();
if (allow) {
actionDef.addAllowPermission(permissionValue);
} else {
actionDef.addDenyPermission(permissionValue);
}
}
}
// find and construct the specified evaluator class
Element evaluatorElement = actionElement.element(ELEMENT_EVALUATOR);
if (evaluatorElement != null) {
Object evaluator;
String className = evaluatorElement.getTextTrim();
try {
Class clazz = Class.forName(className);
evaluator = clazz.newInstance();
} catch (Throwable err) {
throw new ConfigException("Unable to construct action '" + actionId + "' evaluator classname: " + className);
}
if (evaluator instanceof ActionEvaluator == false) {
throw new ConfigException("Action '" + actionId + "' evaluator class '" + className + "' does not implement ActionEvaluator interface.");
}
actionDef.Evaluator = (ActionEvaluator) evaluator;
}
// find any parameter values that the action requires
Element paramsElement = actionElement.element(ELEMENT_PARAMS);
if (paramsElement != null) {
Iterator<Element> paramsItr = paramsElement.elementIterator(ELEMENT_PARAM);
while (paramsItr.hasNext()) {
Element paramElement = paramsItr.next();
String name = paramElement.attributeValue(ATTRIBUTE_NAME);
if (name == null || name.length() == 0) {
throw new ConfigException("Action '" + actionId + "' param does not have mandatory 'name' attribute.");
}
String value = paramElement.getTextTrim();
if (value == null || value.length() == 0) {
throw new ConfigException("Action '" + actionId + "' param '" + name + "'" + "' does not have a value.");
}
actionDef.addParam(name, value);
}
}
// get simple string properties for the action
actionDef.Label = actionElement.elementTextTrim(ELEMENT_LABEL);
actionDef.LabelMsg = actionElement.elementTextTrim(ELEMENT_LABELMSG);
actionDef.Tooltip = actionElement.elementTextTrim(ELEMENT_TOOLTIP);
actionDef.TooltipMsg = actionElement.elementTextTrim(ELEMENT_TOOLTIPMSG);
actionDef.Href = actionElement.elementTextTrim(ELEMENT_HREF);
actionDef.Target = actionElement.elementTextTrim(ELEMENT_TARGET);
actionDef.Script = actionElement.elementTextTrim(ELEMENT_SCRIPT);
actionDef.Action = actionElement.elementTextTrim(ELEMENT_ACTION);
actionDef.ActionListener = actionElement.elementTextTrim(ELEMENT_ACTIONLISTENER);
actionDef.Onclick = actionElement.elementTextTrim(ELEMENT_ONCLICK);
actionDef.Image = actionElement.elementTextTrim(ELEMENT_IMAGE);
actionDef.Style = actionElement.elementTextTrim(ELEMENT_STYLE);
actionDef.StyleClass = actionElement.elementTextTrim(ELEMENT_STYLECLASS);
if (actionElement.element(ELEMENT_SHOWLINK) != null) {
actionDef.ShowLink = Boolean.parseBoolean(actionElement.element(ELEMENT_SHOWLINK).getTextTrim());
}
return actionDef;
}
use of org.alfresco.web.action.ActionEvaluator in project acs-community-packaging by Alfresco.
the class ActionInstanceEvaluator method getEvaluator.
/**
* @return the ActionEvaluator to execute
*/
public ActionEvaluator getEvaluator() {
if (this.evaluator == null) {
Object objEvaluator;
try {
Class clazz = Class.forName(getEvaluatorClassName());
objEvaluator = clazz.newInstance();
} catch (Throwable err) {
throw new AlfrescoRuntimeException("Unable to construct action evaluator: " + getEvaluatorClassName());
}
if (objEvaluator instanceof ActionEvaluator == false) {
throw new AlfrescoRuntimeException("Must implement ActionEvaluator interface: " + getEvaluatorClassName());
}
this.evaluator = (ActionEvaluator) objEvaluator;
}
return this.evaluator;
}
use of org.alfresco.web.action.ActionEvaluator in project acs-community-packaging by Alfresco.
the class ActionInstanceEvaluator method evaluateCachedResult.
/**
* To reduce invocations of a particular evaluator for a particular node
* save a cache of evaluator result for a node against the current request.
* Since the same evaluator may get reused several times for multiple actions, but
* in effect execute against the same node instance, this can significantly reduce
* the number of invocations required for a particular evaluator.
*
* @param node Node to evaluate against
*
* @return evaluator result
*/
private boolean evaluateCachedResult(Node node) {
Boolean result;
ActionEvaluator evaluator = getEvaluator();
String cacheKey = node.getNodeRef().toString() + '_' + evaluator.getClass().getName();
Map<String, Boolean> cache = getEvaluatorResultCache();
result = cache.get(cacheKey);
if (result == null) {
result = evaluator.evaluate(node);
cache.put(cacheKey, result);
}
return result;
}
Aggregations