use of org.alfresco.web.config.ActionsConfigElement.ActionDefinition 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.config.ActionsConfigElement.ActionDefinition in project acs-community-packaging by Alfresco.
the class ActionsElementReader method parse.
/**
* @see org.springframework.extensions.config.xml.elementreader.ConfigElementReader#parse(org.dom4j.Element)
*/
@SuppressWarnings("unchecked")
public ConfigElement parse(Element element) {
ActionsConfigElement configElement = new ActionsConfigElement();
if (element != null) {
if (ActionsConfigElement.CONFIG_ELEMENT_ID.equals(element.getName()) == false) {
throw new ConfigException("ActionsElementReader can only parse config elements of type 'Actions'");
}
Iterator<Element> actionItr = element.elementIterator(ELEMENT_ACTION);
while (actionItr.hasNext()) {
// work on each 'action' element in turn
Element actionElement = actionItr.next();
// parse the action definition for the element
ActionDefinition actionDef = parseActionDefinition(actionElement);
// add our finished action def to the map of all actions
configElement.addActionDefinition(actionDef);
}
Iterator<Element> actionGroupItr = element.elementIterator(ELEMENT_ACTIONGROUP);
while (actionGroupItr.hasNext()) {
// work on each 'action-group' element in turn
Element groupElement = actionGroupItr.next();
String groupId = groupElement.attributeValue(ATTRIBUTE_ID);
if (groupId == null || groupId.length() == 0) {
throw new ConfigException("'action-group' config element specified without mandatory 'id' attribute.");
}
// build a structure to represent the action group
ActionGroup actionGroup = new ActionGroup(groupId);
// loop round each action ref and add them to the list for this action group
Iterator<Element> actionRefItr = groupElement.elementIterator(ELEMENT_ACTION);
while (actionRefItr.hasNext()) {
Element actionRefElement = actionRefItr.next();
// look for an action referred to be Id - this is the common use-case
String idRef = actionRefElement.attributeValue(ATTRIBUTE_IDREF);
if (idRef == null || idRef.length() == 0) {
// look for an action defined directly rather than referenced by Id
String id = actionRefElement.attributeValue(ATTRIBUTE_ID);
if (id != null && id.length() != 0) {
ActionDefinition def = parseActionDefinition(actionRefElement);
// override action definition ID based on the group name to avoid conflicts
def.id = actionGroup.getId() + '_' + def.getId();
configElement.addActionDefinition(def);
actionGroup.addAction(def.getId());
}
} else {
// look for the hide attribute
String hide = actionRefElement.attributeValue(ATTRIBUTE_HIDE);
if (hide != null && Boolean.parseBoolean(hide)) {
actionGroup.hideAction(idRef);
} else {
// add the action definition ID to the group
actionGroup.addAction(idRef);
}
}
}
// get simple string properties for the action group
actionGroup.Style = groupElement.elementTextTrim(ELEMENT_STYLE);
actionGroup.StyleClass = groupElement.elementTextTrim(ELEMENT_STYLECLASS);
if (groupElement.element(ELEMENT_SHOWLINK) != null) {
actionGroup.ShowLink = Boolean.parseBoolean(groupElement.element(ELEMENT_SHOWLINK).getTextTrim());
}
// add the action group to the map of all action groups
configElement.addActionGroup(actionGroup);
}
}
return configElement;
}
use of org.alfresco.web.config.ActionsConfigElement.ActionDefinition in project acs-community-packaging by Alfresco.
the class UIActions method buildActionGroup.
/**
* Build an action group as reusable UIActionLink components.
*
* @param context FacesContext
* @param config ActionsConfigElement
* @param actionGroup ActionGroup
* @param contextId String
* @throws IOException
*/
@SuppressWarnings("unchecked")
private void buildActionGroup(FacesContext context, ActionsConfigElement config, ActionGroup actionGroup, String contextId) throws IOException {
javax.faces.application.Application facesApp = context.getApplication();
ResourceBundle messages = Application.getBundle(context);
// get overriding display attributes
String style = (String) getAttributes().get(ATTR_STYLE);
String styleClass = (String) getAttributes().get(ATTR_STYLECLASS);
Boolean showLink = null;
if (getAttributes().get(ATTR_SHOWLINK) != null) {
showLink = (Boolean) getAttributes().get(ATTR_SHOWLINK);
}
// build parent wrapper component
HtmlPanelGroup wrapper = (HtmlPanelGroup) facesApp.createComponent(ComponentConstants.JAVAX_FACES_PANELGROUP);
wrapper.setId(createUniqueId());
wrapper.getAttributes().put("contextId", contextId);
this.getChildren().add(wrapper);
if (logger.isDebugEnabled())
logger.debug("UIActions id: " + this.getId() + " Children() structure size: " + this.getChildren().size());
this.groups.add(contextId);
// process each ActionDefinition in the order they were defined
for (String actionId : actionGroup) {
if (logger.isDebugEnabled())
logger.debug("---processing ActionDefinition: " + actionId);
ActionDefinition actionDef = config.getActionDefinition(actionId);
if (actionDef == null) {
throw new AlfrescoRuntimeException("Unable to find configured ActionDefinition Id: " + actionId);
}
UIComponent currentParent = wrapper;
// build a permissions evaluator component to wrap the actionlink
PermissionEvaluator permEval = null;
List<String> allow = actionDef.getAllowPermissions();
if (allow != null && allow.size() != 0) {
// found some permissions to test
permEval = (PermissionEvaluator) facesApp.createComponent(COMPONENT_PERMISSIONEVAL);
String condition = allow.get(0);
if (allow.size() != 1) {
for (int i = 1; i < allow.size(); i++) {
condition += "," + allow.get(i);
}
}
permEval.setAllow(condition);
}
List<String> deny = actionDef.getDenyPermissions();
if (deny != null && deny.size() != 0) {
if (permEval == null) {
permEval = (PermissionEvaluator) facesApp.createComponent(COMPONENT_PERMISSIONEVAL);
}
String condition = deny.get(0);
if (deny.size() != 1) {
for (int i = 1; i < deny.size(); i++) {
condition += "," + deny.get(i);
}
}
permEval.setDeny(condition);
}
if (permEval != null) {
// add the permission evaluator component and walk down the hierarchy
permEval.setId(createUniqueId());
permEval.setValueBinding(ATTR_VALUE, facesApp.createValueBinding("#{" + ACTION_CONTEXT + "}"));
if (logger.isDebugEnabled())
logger.debug("-----adding PermissionEvaluator to action");
currentParent.getChildren().add(permEval);
currentParent = permEval;
}
// now prepare any code based evaluators that may be present
if (actionDef.Evaluator != null) {
ActionInstanceEvaluator evaluator = (ActionInstanceEvaluator) facesApp.createComponent(COMPONENT_ACTIONEVAL);
evaluator.setId(createUniqueId());
evaluator.setEvaluator(actionDef.Evaluator);
evaluator.setValueBinding(ATTR_VALUE, facesApp.createValueBinding("#{" + ACTION_CONTEXT + "}"));
// add the action evaluator component and walk down the hiearchy
if (logger.isDebugEnabled())
logger.debug("-----adding ActionEvaluator to action");
currentParent.getChildren().add(evaluator);
currentParent = evaluator;
}
// now build the UIActionLink component for this action
UIActionLink control = (UIActionLink) facesApp.createComponent(COMPONENT_ACTIONLINK);
control.setRendererType(RENDERER_ACTIONLINK);
control.setId(actionDef.getId() + createUniqueId());
if (actionDef.Action != null) {
if (UIComponentTagUtils.isValueReference(actionDef.Action)) {
control.setAction(facesApp.createMethodBinding(actionDef.Action, null));
} else {
control.setAction(new ConstantMethodBinding(actionDef.Action));
}
}
if (actionDef.ActionListener != null) {
control.setActionListener(facesApp.createMethodBinding(actionDef.ActionListener, ACTION_CLASS_ARGS));
}
if (style != null) {
control.getAttributes().put(ATTR_STYLE, style);
} else if (actionDef.Style != null) {
control.getAttributes().put(ATTR_STYLE, actionDef.Style);
}
if (styleClass != null) {
control.getAttributes().put(ATTR_STYLECLASS, styleClass);
} else if (actionDef.StyleClass != null) {
control.getAttributes().put(ATTR_STYLECLASS, actionDef.StyleClass);
}
if (showLink != null) {
control.setShowLink(showLink.booleanValue());
} else {
control.setShowLink(actionDef.ShowLink);
}
if (actionDef.Onclick != null) {
if (UIComponentTagUtils.isValueReference(actionDef.Onclick)) {
control.setValueBinding("onclick", facesApp.createValueBinding(actionDef.Onclick));
} else {
control.setOnclick(actionDef.Onclick);
}
}
if (actionDef.Href != null) {
if (UIComponentTagUtils.isValueReference(actionDef.Href)) {
control.setValueBinding("href", facesApp.createValueBinding(actionDef.Href));
} else {
control.setHref(actionDef.Href);
}
} else if (actionDef.Script != null && actionDef.Script.length() != 0) {
// found a script reference - may be a Path or a NodeRef
StringBuilder scriptHref = new StringBuilder(100);
scriptHref.append("/command/script/execute");
if (actionDef.Script.charAt(0) == '/') {
// found a Path - encode it as a URL argument
scriptHref.append("?scriptPath=");
scriptHref.append(URLEncoder.encode(actionDef.Script));
} else {
// found a NodeRef string, encode as URL elements
NodeRef ref = new NodeRef(actionDef.Script);
scriptHref.append('/').append(ref.getStoreRef().getProtocol()).append('/').append(ref.getStoreRef().getIdentifier()).append('/').append(ref.getId());
}
// set the full script execution URL as the href for the control
control.setHref(scriptHref.toString());
}
control.setTarget(actionDef.Target);
control.setImage(actionDef.Image);
if (actionDef.TooltipMsg != null) {
control.setTooltip(messages.getString(actionDef.TooltipMsg));
} else if (actionDef.Tooltip != null) {
if (UIComponentTagUtils.isValueReference(actionDef.Tooltip)) {
control.setValueBinding("tooltip", facesApp.createValueBinding(actionDef.Tooltip));
} else {
control.setValue(actionDef.Tooltip);
}
}
if (actionDef.LabelMsg != null) {
control.setValue(messages.getString(actionDef.LabelMsg));
} else if (actionDef.Label != null) {
if (UIComponentTagUtils.isValueReference(actionDef.Label)) {
control.setValueBinding("value", facesApp.createValueBinding(actionDef.Label));
} else {
control.setValue(actionDef.Label);
}
}
// build any child params <f:param> components that are needed.
Map<String, String> params = actionDef.getParams();
if (params != null) {
for (String name : params.keySet()) {
UIParameter param = (UIParameter) facesApp.createComponent(ComponentConstants.JAVAX_FACES_PARAMETER);
param.setId(createUniqueId());
param.setName(name);
String value = params.get(name);
if (UIComponentTagUtils.isValueReference(value)) {
param.setValueBinding(ATTR_VALUE, facesApp.createValueBinding(value));
} else {
param.setValue(value);
}
control.getChildren().add(param);
}
}
if (logger.isDebugEnabled())
logger.debug("-----adding UIActionLink component for: " + actionId);
currentParent.getChildren().add(control);
}
}
Aggregations