Merge DEV/BELARUS/HEAD-2010_02_22 to HEAD

18814 : Implementation of sub-tasks SAIL-326, SAIL-327 and SAIL-329 is complete.



git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@18831 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Roy Wetherall
2010-02-25 03:58:32 +00:00
parent 66363a5254
commit fc172da640
27 changed files with 1050 additions and 745 deletions

View File

@@ -46,6 +46,7 @@ import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
@@ -69,19 +70,20 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/
public abstract class AbstractRuleWebScript extends DeclarativeWebScript
{
public static final SimpleDateFormat dateFormate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
protected NodeService nodeService;
protected RuleService ruleService;
protected DictionaryService dictionaryService;
protected ActionService actionService;
protected FileFolderService fileFolderService;
protected NamespaceService namespaceService;
private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
private static Map<String, QName> propertyTypes = null;
private static Map<String, Map<String, QName>> propertyTypes = null;
/**
* Sets the node service instance
*
@@ -91,7 +93,7 @@ public abstract class AbstractRuleWebScript extends DeclarativeWebScript
{
this.nodeService = nodeService;
}
/**
* Set rule service instance
*
@@ -101,7 +103,7 @@ public abstract class AbstractRuleWebScript extends DeclarativeWebScript
{
this.ruleService = ruleService;
}
/**
* Set dictionary service instance
*
@@ -111,7 +113,7 @@ public abstract class AbstractRuleWebScript extends DeclarativeWebScript
{
this.dictionaryService = dictionaryService;
}
/**
* Set action service instance
*
@@ -121,7 +123,17 @@ public abstract class AbstractRuleWebScript extends DeclarativeWebScript
{
this.actionService = actionService;
}
/**
* Set file folder service instance
*
* @param fileFolderService the fileFolderService to set
*/
public void setFileFolderService(FileFolderService fileFolderService)
{
this.fileFolderService = fileFolderService;
}
/**
* Set namespace service instance
*
@@ -131,7 +143,7 @@ public abstract class AbstractRuleWebScript extends DeclarativeWebScript
{
this.namespaceService = namespaceService;
}
/**
* Parses the request and providing it's valid returns the NodeRef.
*
@@ -147,138 +159,210 @@ public abstract class AbstractRuleWebScript extends DeclarativeWebScript
String storeType = templateVars.get("store_type");
String storeId = templateVars.get("store_id");
String nodeId = templateVars.get("id");
// create the NodeRef and ensure it is valid
StoreRef storeRef = new StoreRef(storeType, storeId);
NodeRef nodeRef = new NodeRef(storeRef, nodeId);
if (!this.nodeService.exists(nodeRef))
{
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find node: " +
nodeRef.toString());
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find node: " + nodeRef.toString());
}
return nodeRef;
}
protected QName getPropertyType(String propertyName)
protected QName getPropertyType(String name, boolean isAction, String propertyName)
{
QName result = null;
if (propertyTypes == null)
{
// no parameter types was cached
propertyTypes = new HashMap<String, QName>();
{
propertyTypes = new HashMap<String, Map<String, QName>>();
// get parameters for all action definitions
List<ActionDefinition> actionDefinitions = actionService.getActionDefinitions();
for (ActionDefinition actionDefinition : actionDefinitions)
{
List<ParameterDefinition> parameterDefinitions = actionDefinition.getParameterDefinitions();
for (ParameterDefinition parameterDefinition : parameterDefinitions)
{
try
{
// cache parameter
lock.writeLock().lock();
propertyTypes.put(parameterDefinition.getName(), parameterDefinition.getType());
}
finally
{
lock.writeLock().unlock();
}
}
}
// get parameters for all action condition definitions
List<ActionConditionDefinition> actionConditionDefinitions = actionService.getActionConditionDefinitions();
for (ActionConditionDefinition actionConditionDefinition : actionConditionDefinitions)
{
List<ParameterDefinition> parameterDefinitions = actionConditionDefinition.getParameterDefinitions();
List<ParameterDefinition> parameterDefinitions = actionDefinition.getParameterDefinitions();
Map<String, QName> parameters = new HashMap<String, QName>();
for (ParameterDefinition parameterDefinition : parameterDefinitions)
{
try
{
// cache parameter
lock.writeLock().lock();
propertyTypes.put(parameterDefinition.getName(), parameterDefinition.getType());
}
finally
{
lock.writeLock().unlock();
}
String parameterName = parameterDefinition.getName();
QName parameterType = parameterDefinition.getType();
parameters.put(parameterName, parameterType);
}
try
{
// cache parameter
lock.writeLock().lock();
propertyTypes.put(actionDefinition.getName(), parameters);
}
finally
{
lock.writeLock().unlock();
}
}
// get parameters for all action condition definitions
List<ActionConditionDefinition> actionConditionDefinitions = actionService.getActionConditionDefinitions();
for (ActionConditionDefinition actionConditionDefinition : actionConditionDefinitions)
{
List<ParameterDefinition> parameterDefinitions = actionConditionDefinition.getParameterDefinitions();
Map<String, QName> parameters = new HashMap<String, QName>();
for (ParameterDefinition parameterDefinition : parameterDefinitions)
{
String parameterName = parameterDefinition.getName();
QName parameterType = parameterDefinition.getType();
parameters.put(parameterName, parameterType);
}
try
{
lock.writeLock().lock();
propertyTypes.put(actionConditionDefinition.getName(), parameters);
}
finally
{
lock.writeLock().unlock();
}
}
}
QName result = null;
try
if (propertyTypes.containsKey(name))
{
// getting cached parameter type
lock.readLock().lock();
result = propertyTypes.get(propertyName);
Map<String, QName> parameters;
try
{
lock.readLock().lock();
parameters = propertyTypes.get(name);
}
finally
{
lock.readLock().unlock();
}
result = parameters.get(propertyName);
return result;
}
finally
else
{
lock.readLock().unlock();
if (isAction)
{
ActionDefinition actionDefinition = actionService.getActionDefinition(name);
List<ParameterDefinition> parameterDefinitions = actionDefinition.getParameterDefinitions();
Map<String, QName> parameters = new HashMap<String, QName>();
for (ParameterDefinition parameterDefinition : parameterDefinitions)
{
String parameterName = parameterDefinition.getName();
QName parameterType = parameterDefinition.getType();
parameters.put(parameterName, parameterType);
if (parameterName.equals(propertyName))
{
result = parameterType;
}
}
try
{
// cache parameter
lock.writeLock().lock();
propertyTypes.put(name, parameters);
}
finally
{
lock.writeLock().unlock();
}
}
else
{
ActionConditionDefinition actionConditionDefinition = actionService.getActionConditionDefinition(name);
List<ParameterDefinition> parameterDefinitions = actionConditionDefinition.getParameterDefinitions();
Map<String, QName> parameters = new HashMap<String, QName>();
for (ParameterDefinition parameterDefinition : parameterDefinitions)
{
String parameterName = parameterDefinition.getName();
QName parameterType = parameterDefinition.getType();
parameters.put(parameterName, parameterType);
if (parameterName.equals(propertyName))
{
result = parameterType;
}
}
try
{
lock.writeLock().lock();
propertyTypes.put(name, parameters);
}
finally
{
lock.writeLock().unlock();
}
}
}
return result;
}
protected Rule parseJsonRule(JSONObject jsonRule) throws JSONException
{
Rule result = new Rule();
if (jsonRule.has("title") == false || jsonRule.getString("title").length() == 0)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Title missing when creating rule");
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Title missing when creating rule");
}
result.setTitle(jsonRule.getString("title"));
result.setDescription(jsonRule.has("description") ? jsonRule.getString("description") : "");
if (jsonRule.has("ruleType") == false || jsonRule.getJSONArray("ruleType").length() == 0)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Rule type missing when creating rule");
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Rule type missing when creating rule");
}
JSONArray types = jsonRule.getJSONArray("ruleType");
List<String> ruleTypes = new ArrayList<String>();
for (int i = 0; i < types.length(); i++)
{
ruleTypes.add(types.getString(i));
ruleTypes.add(types.getString(i));
}
result.setRuleTypes(ruleTypes);
result.applyToChildren(jsonRule.has("applyToChildren") ? jsonRule.getBoolean("applyToChildren") : false);
result.setExecuteAsynchronously(jsonRule.has("executeAsynchronously") ? jsonRule.getBoolean("executeAsynchronously") : false);
result.setRuleDisabled(jsonRule.has("disabled") ? jsonRule.getBoolean("disabled") : false);
JSONObject jsonAction = jsonRule.getJSONObject("action");
// parse action object
Action ruleAction = parseJsonAction(jsonAction);
result.setAction(ruleAction);
return result;
return result;
}
protected ActionImpl parseJsonAction(JSONObject jsonAction) throws JSONException
protected ActionImpl parseJsonAction(JSONObject jsonAction) throws JSONException
{
ActionImpl result = null;
String actionId = jsonAction.has("id") ? jsonAction.getString("id") : GUID.generate();
if (jsonAction.getString("actionDefinitionName").equalsIgnoreCase("composite-action"))
{
result = new CompositeActionImpl(null, actionId);
@@ -287,160 +371,176 @@ public abstract class AbstractRuleWebScript extends DeclarativeWebScript
{
result = new ActionImpl(null, actionId, jsonAction.getString("actionDefinitionName"));
}
// Post Action Queue parameter
if (jsonAction.has("actionedUponNode"))
{
NodeRef actionedUponNode = new NodeRef(jsonAction.getString("actionedUponNode"));
result.setNodeRef(actionedUponNode);
}
if (jsonAction.has("description"))
{
result.setDescription(jsonAction.getString("description"));
}
if (jsonAction.has("title"))
{
result.setTitle(jsonAction.getString("title"));
}
if (jsonAction.has("parameterValues"))
{
JSONObject jsonParameterValues = jsonAction.getJSONObject("parameterValues");
result.setParameterValues(parseJsonParameterValues(jsonParameterValues));
JSONObject jsonParameterValues = jsonAction.getJSONObject("parameterValues");
result.setParameterValues(parseJsonParameterValues(jsonParameterValues, result.getActionDefinitionName(), true));
}
if (jsonAction.has("executeAsync"))
{
result.setExecuteAsynchronously(jsonAction.getBoolean("executeAsync"));
}
if (jsonAction.has("runAsUser"))
{
result.setRunAsUser(jsonAction.getString("runAsUser"));
}
if (jsonAction.has("actions"))
{
JSONArray jsonActions = jsonAction.getJSONArray("actions");
for (int i = 0; i < jsonActions.length(); i++)
{
JSONObject innerJsonAction = jsonActions.getJSONObject(i);
Action innerAction = parseJsonAction(innerJsonAction);
// we assume that only composite-action contains actions json array, so should be no cast exception
((CompositeActionImpl)result).addAction(innerAction);
((CompositeActionImpl) result).addAction(innerAction);
}
}
if (jsonAction.has("conditions"))
{
JSONArray jsonConditions = jsonAction.getJSONArray("conditions");
for (int i = 0; i < jsonConditions.length(); i++)
{
JSONObject jsonCondition = jsonConditions.getJSONObject(i);
JSONObject jsonCondition = jsonConditions.getJSONObject(i);
// parse action conditions
ActionCondition actionCondition = parseJsonActionCondition(jsonCondition);
result.getActionConditions().add(actionCondition);
}
}
}
if (jsonAction.has("compensatingAction"))
{
Action compensatingAction = parseJsonAction(jsonAction.getJSONObject("compensatingAction"));
result.setCompensatingAction(compensatingAction);
}
return result;
}
protected ActionConditionImpl parseJsonActionCondition(JSONObject jsonActionCondition) throws JSONException
{
String id = jsonActionCondition.has("id") ? jsonActionCondition.getString("id"): GUID.generate();
String id = jsonActionCondition.has("id") ? jsonActionCondition.getString("id") : GUID.generate();
ActionConditionImpl result = new ActionConditionImpl(id, jsonActionCondition.getString("conditionDefinitionName"));
if (jsonActionCondition.has("invertCondition"))
{
result.setInvertCondition(jsonActionCondition.getBoolean("invertCondition"));
}
if (jsonActionCondition.has("parameterValues"))
{
JSONObject jsonParameterValues = jsonActionCondition.getJSONObject("parameterValues");
result.setParameterValues(parseJsonParameterValues(jsonParameterValues));
JSONObject jsonParameterValues = jsonActionCondition.getJSONObject("parameterValues");
result.setParameterValues(parseJsonParameterValues(jsonParameterValues, result.getActionConditionDefinitionName(), false));
}
return result;
}
protected Map<String, Serializable> parseJsonParameterValues(JSONObject jsonParameterValues) throws JSONException
@SuppressWarnings("unchecked")
protected Map<String, Serializable> parseJsonParameterValues(JSONObject jsonParameterValues, String name, boolean isAction) throws JSONException
{
Map<String, Serializable> parameterValues = new HashMap<String, Serializable>();
// get parameters names
JSONArray names = jsonParameterValues.names();
if (names != null)
if (names == null)
{
for (int i = 0; i < names.length(); i++)
return null;
}
for (int i = 0; i < names.length(); i++)
{
String propertyName = names.getString(i);
Object propertyValue = jsonParameterValues.get(propertyName);
// get parameter repository type
QName typeQName = getPropertyType(name, isAction, propertyName);
if (typeQName == null)
{
String propertyName = names.getString(i);
Object propertyValue = jsonParameterValues.get(propertyName);
// get parameter repository type
QName typeQName = getPropertyType(propertyName);
if (typeQName == null)
if (propertyValue.toString().equals("true") || propertyValue.toString().equals("false"))
{
if (propertyValue.toString().equals("true") || propertyValue.toString().equals("false"))
{
typeQName = DataTypeDefinition.BOOLEAN;
}
else
{
typeQName = DataTypeDefinition.TEXT;
}
typeQName = DataTypeDefinition.BOOLEAN;
}
Serializable value = null;
if (typeQName.equals(DataTypeDefinition.ANY))
else
{
typeQName = DataTypeDefinition.TEXT;
}
}
Serializable value = null;
if (typeQName.equals(DataTypeDefinition.QNAME))
{
value = QName.createQName(propertyValue.toString(), namespaceService);
}
if (typeQName.equals(DataTypeDefinition.ANY))
{
try
{
value = dateFormate.parse(propertyValue.toString());
}
catch (ParseException e)
{
try
{
value = dateFormate.parse(propertyValue.toString());
value = Long.valueOf(propertyValue.toString());
}
catch (ParseException e)
catch (NumberFormatException e1)
{
try
if (propertyValue instanceof JSONArray)
{
value = Long.valueOf(propertyValue.toString());
}
catch (NumberFormatException e1)
{
// do nothing
value = new ArrayList<String>();
JSONArray array = (JSONArray) propertyValue;
for (int j = 0; j < array.length(); j++)
{
((List<String>) value).add(array.getString(j));
}
}
}
}
if (value == null)
{
// convert to correct repository type
value = (Serializable)DefaultTypeConverter.INSTANCE.convert(dictionaryService.getDataType(typeQName), propertyValue);
}
parameterValues.put(propertyName, value);
}
if (value == null)
{
// convert to correct repository type
value = (Serializable) DefaultTypeConverter.INSTANCE.convert(dictionaryService.getDataType(typeQName), propertyValue);
}
parameterValues.put(propertyName, value);
}
return parameterValues;
}
}

View File

@@ -43,17 +43,17 @@ public class ActionConditionDefinitionsGet extends AbstractRuleWebScript
{
@SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(ActionConditionDefinitionsGet.class);
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
Map<String, Object> model = new HashMap<String, Object>();
// get all action condition definitions
List<ActionConditionDefinition> actionconditiondefinitions = actionService.getActionConditionDefinitions();
model.put("actionconditiondefinitions", actionconditiondefinitions);
return model;
}
}
}

View File

@@ -46,27 +46,26 @@ public class ActionConstraintGet extends AbstractRuleWebScript
@SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(ActionConstraintGet.class);
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
Map<String, Object> model = new HashMap<String, Object>();
// get request parameters
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String name = templateVars.get("name");
// get specified parameter constraint
ParameterConstraint parameterConstraint = actionService.getParameterConstraint(name);
if (parameterConstraint == null)
{
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find parameter constraint with name: " +
name);
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find parameter constraint with name: " + name);
}
model.put("actionConstraint", parameterConstraint);
return model;
}
}

View File

@@ -42,31 +42,30 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/
public class ActionConstraintsGet extends AbstractRuleWebScript
{
@SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(ActionConstraintsGet.class);
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
Map<String, Object> model = new HashMap<String, Object>();
// get request parameters
String[] names = req.getParameterValues("name");
List<ParameterConstraint> parameterConstraints = null;
if (names != null && names.length > 0)
{
// filter is present in request
parameterConstraints = new ArrayList<ParameterConstraint>();
// find specified parameter constraints
for (String name : names)
{
ParameterConstraint parameterConstraint = actionService.getParameterConstraint(name);
if (parameterConstraint != null)
{
parameterConstraints.add(parameterConstraint);
@@ -78,9 +77,9 @@ public class ActionConstraintsGet extends AbstractRuleWebScript
// no filter was provided, return all parameter constraints
parameterConstraints = actionService.getParameterConstraints();
}
model.put("actionConstraints", parameterConstraints);
return model;
}
}

View File

@@ -48,12 +48,12 @@ public class ActionDefinitionsGet extends AbstractRuleWebScript
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
Map<String, Object> model = new HashMap<String, Object>();
// get all action definitions
List<ActionDefinition> actiondefinitions = actionService.getActionDefinitions();
model.put("actiondefinitions", actiondefinitions);
return model;
}
}

View File

@@ -48,36 +48,36 @@ public class ActionQueuePost extends AbstractRuleWebScript
{
@SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(ActionQueuePost.class);
public static final String STATUS = "actionExecStatus";
public static final String STATUS_SUCCESS = "success";
public static final String STATUS_FAIL = "fail";
public static final String STATUS_QUEUED = "queued";
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
Map<String, Object> model = new HashMap<String, Object>();
// get request parameters
boolean async = Boolean.parseBoolean(req.getParameter("async"));
ActionImpl action = null;
JSONObject json = null;
try
{
// read request json
json = new JSONObject(new JSONTokener(req.getContent().getContent()));
// parse request json
action = parseJsonAction(json);
NodeRef actionedUponNode = action.getNodeRef();
// clear nodeRef for action
action.setNodeRef(null);
json.remove("actionedUponNode");
if (async)
{
model.put(STATUS, STATUS_QUEUED);
@@ -86,31 +86,29 @@ public class ActionQueuePost extends AbstractRuleWebScript
{
model.put(STATUS, STATUS_SUCCESS);
}
try
{
actionService.executeAction(action, actionedUponNode, true, async);
}
catch(Throwable e)
catch (Throwable e)
{
model.put(STATUS, STATUS_FAIL);
model.put("exception", e);
}
model.put("actionedUponNode", actionedUponNode.toString());
model.put("action", json);
model.put("action", json);
}
catch (IOException iox)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Could not read content from req.", iox);
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", iox);
}
catch (JSONException je)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Could not parse JSON from req.", je);
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je);
}
return model;
}
}
}

View File

@@ -0,0 +1,87 @@
/*
* Copyright (C) 2005-2009 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.web.scripts.rule;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.repo.web.scripts.rule.ruleset.RuleRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.rule.Rule;
import org.alfresco.service.cmr.rule.RuleType;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* @author unknown
*
*/
public class InheritedRulesGet extends AbstractRuleWebScript
{
@SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(InheritedRulesGet.class);
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
Map<String, Object> model = new HashMap<String, Object>();
// get request parameters
NodeRef nodeRef = parseRequestForNodeRef(req);
String ruleType = req.getParameter("ruleType");
RuleType type = ruleService.getRuleType(ruleType);
if (type == null)
{
ruleType = null;
}
// get all rules (including inherited) filtered by rule type
List<Rule> inheritedRules = ruleService.getRules(nodeRef, true, ruleType);
// get owned rules (excluding inherited) filtered by rule type
List<Rule> ownedRules = ruleService.getRules(nodeRef, false, ruleType);
// remove owned rules
inheritedRules.removeAll(ownedRules);
List<RuleRef> inheritedRuleRefs = new ArrayList<RuleRef>();
for (Rule rule : inheritedRules)
{
inheritedRuleRefs.add(new RuleRef(rule, fileFolderService.getFileInfo(ruleService.getOwningNodeRef(rule))));
}
model.put("inheritedRuleRefs", inheritedRuleRefs);
return model;
}
}

View File

@@ -47,23 +47,23 @@ public class RuleDelete extends AbstractRuleWebScript
{
@SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(RuleDelete.class);
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
Map<String, Object> model = new HashMap<String, Object>();
NodeRef nodeRef = parseRequestForNodeRef(req);
// get request parameters
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String ruleId = templateVars.get("rule_id");
Rule ruleToDelete = null;
// get all rules for given nodeRef
List<Rule> rules = ruleService.getRules(nodeRef);
List<Rule> rules = ruleService.getRules(nodeRef, false);
// filter by rule id
for (Rule rule : rules)
{
@@ -73,16 +73,15 @@ public class RuleDelete extends AbstractRuleWebScript
break;
}
}
if (ruleToDelete == null)
{
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find rule with id: " +
ruleId);
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find rule with id: " + ruleId);
}
// delete rule
ruleService.removeRule(nodeRef, ruleToDelete);
return model;
}
}

View File

@@ -30,6 +30,7 @@ import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.repo.web.scripts.rule.ruleset.RuleRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.rule.Rule;
import org.apache.commons.logging.Log;
@@ -47,23 +48,23 @@ public class RuleGet extends AbstractRuleWebScript
{
@SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(RuleGet.class);
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
Map<String, Object> model = new HashMap<String, Object>();
NodeRef nodeRef = parseRequestForNodeRef(req);
// get request parameters
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String ruleId = templateVars.get("rule_id");
Rule ruleToReturn = null;
// get all rules for given nodeRef
List<Rule> rules = ruleService.getRules(nodeRef);
// filter by rule id
for (Rule rule : rules)
{
@@ -73,18 +74,16 @@ public class RuleGet extends AbstractRuleWebScript
break;
}
}
if (ruleToReturn == null)
{
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find rule with id: " +
ruleId);
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find rule with id: " + ruleId);
}
model.put("rule", ruleToReturn);
model.put("storeType", nodeRef.getStoreRef().getProtocol());
model.put("storeId", nodeRef.getStoreRef().getIdentifier());
model.put("id", nodeRef.getId());
RuleRef ruleRefToReturn = new RuleRef(ruleToReturn, fileFolderService.getFileInfo(ruleService.getOwningNodeRef(ruleToReturn)));
model.put("ruleRef", ruleRefToReturn);
return model;
}
}

View File

@@ -28,6 +28,7 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.repo.web.scripts.rule.ruleset.RuleRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.rule.Rule;
import org.apache.commons.logging.Log;
@@ -45,48 +46,45 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*
*/
public class RulePost extends AbstractRuleWebScript
{
{
@SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(RulePost.class);
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
{
Map<String, Object> model = new HashMap<String, Object>();
// get request parameters
NodeRef nodeRef = parseRequestForNodeRef(req);
Rule rule = null;
JSONObject json = null;
try
{
// read request json
json = new JSONObject(new JSONTokener(req.getContent().getContent()));
// parse request json
rule = parseJsonRule(json);
// create rule
ruleService.saveRule(nodeRef, rule);
model.put("rule", rule);
model.put("storeType", nodeRef.getStoreRef().getProtocol());
model.put("storeId", nodeRef.getStoreRef().getIdentifier());
model.put("id", nodeRef.getId());
RuleRef ruleRef = new RuleRef(rule, fileFolderService.getFileInfo(ruleService.getOwningNodeRef(rule)));
model.put("ruleRef", ruleRef);
}
catch (IOException iox)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Could not read content from req.", iox);
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", iox);
}
catch (JSONException je)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Could not parse JSON from req.", je);
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je);
}
return model;
}
}

View File

@@ -35,6 +35,7 @@ import javax.servlet.http.HttpServletResponse;
import org.alfresco.repo.action.ActionConditionImpl;
import org.alfresco.repo.action.ActionImpl;
import org.alfresco.repo.action.CompositeActionImpl;
import org.alfresco.repo.web.scripts.rule.ruleset.RuleRef;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionCondition;
import org.alfresco.service.cmr.repository.NodeRef;
@@ -61,20 +62,20 @@ public class RulePut extends RulePost
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
{
Map<String, Object> model = new HashMap<String, Object>();
// get request parameters
NodeRef nodeRef = parseRequestForNodeRef(req);
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String ruleId = templateVars.get("rule_id");
Rule ruleToUpdate = null;
// get all rules for given nodeRef
List<Rule> rules = ruleService.getRules(nodeRef);
//filter by rule id
for (Rule rule : rules)
{
@@ -84,99 +85,95 @@ public class RulePut extends RulePost
break;
}
}
if (ruleToUpdate == null)
{
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find rule with id: " +
ruleId);
throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to find rule with id: " + ruleId);
}
JSONObject json = null;
try
{
// read request json
json = new JSONObject(new JSONTokener(req.getContent().getContent()));
// parse request json
updateRuleFromJSON(json, ruleToUpdate);
// save changes
ruleService.saveRule(nodeRef, ruleToUpdate);
model.put("rule", ruleToUpdate);
model.put("storeType", nodeRef.getStoreRef().getProtocol());
model.put("storeId", nodeRef.getStoreRef().getIdentifier());
model.put("id", nodeRef.getId());
RuleRef updatedRuleRef = new RuleRef(ruleToUpdate, fileFolderService.getFileInfo(ruleService.getOwningNodeRef(ruleToUpdate)));
model.put("ruleRef", updatedRuleRef);
}
catch (IOException iox)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Could not read content from req.", iox);
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not read content from req.", iox);
}
catch (JSONException je)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Could not parse JSON from req.", je);
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Could not parse JSON from req.", je);
}
return model;
}
protected void updateRuleFromJSON(JSONObject jsonRule, Rule ruleToUpdate) throws JSONException
{
if (jsonRule.has("title"))
{
ruleToUpdate.setTitle(jsonRule.getString("title"));
}
}
if (jsonRule.has("description"))
{
ruleToUpdate.setDescription(jsonRule.getString("description"));
}
if (jsonRule.has("ruleType"))
{
JSONArray jsonTypes = jsonRule.getJSONArray("ruleType");
List<String> types = new ArrayList<String>();
for (int i = 0; i < jsonTypes.length(); i++)
{
types.add(jsonTypes.getString(i));
types.add(jsonTypes.getString(i));
}
ruleToUpdate.setRuleTypes(types);
}
if (jsonRule.has("applyToChildren"))
{
ruleToUpdate.applyToChildren(jsonRule.getBoolean("applyToChildren"));
}
if (jsonRule.has("executeAsynchronously"))
{
ruleToUpdate.setExecuteAsynchronously(jsonRule.getBoolean("executeAsynchronously"));
}
if (jsonRule.has("disabled"))
{
ruleToUpdate.setRuleDisabled(jsonRule.getBoolean("disabled"));
}
if (jsonRule.has("action"))
{
JSONObject jsonAction = jsonRule.getJSONObject("action");
{
JSONObject jsonAction = jsonRule.getJSONObject("action");
// update rule action
Action action = updateActionFromJson(jsonAction, (ActionImpl)ruleToUpdate.getAction());
Action action = updateActionFromJson(jsonAction, (ActionImpl) ruleToUpdate.getAction());
ruleToUpdate.setAction(action);
}
}
protected Action updateActionFromJson(JSONObject jsonAction, ActionImpl actionToUpdate) throws JSONException
{
ActionImpl result = null;
if (jsonAction.has("id"))
{
// update existing action
@@ -186,82 +183,82 @@ public class RulePut extends RulePost
{
// create new object as id was not sent by client
result = parseJsonAction(jsonAction);
return result;
return result;
}
if (jsonAction.has("description"))
{
result.setDescription(jsonAction.getString("description"));
}
if (jsonAction.has("title"))
{
result.setTitle(jsonAction.getString("title"));
}
if (jsonAction.has("parameterValues"))
{
JSONObject jsonParameterValues = jsonAction.getJSONObject("parameterValues");
result.setParameterValues(parseJsonParameterValues(jsonParameterValues));
result.setParameterValues(parseJsonParameterValues(jsonParameterValues, result.getActionDefinitionName(), true));
}
if (jsonAction.has("executeAsync"))
{
result.setExecuteAsynchronously(jsonAction.getBoolean("executeAsync"));
}
if (jsonAction.has("runAsUser"))
{
result.setRunAsUser(jsonAction.getString("runAsUser"));
}
if (jsonAction.has("actions"))
{
JSONArray jsonActions = jsonAction.getJSONArray("actions");
if (jsonActions.length() == 0)
{
// empty array was sent -> clear list
((CompositeActionImpl)result).getActions().clear();
((CompositeActionImpl) result).getActions().clear();
}
else
{
List<Action> existingActions = ((CompositeActionImpl)result).getActions();
List<Action> newActions = new ArrayList<Action>();
List<Action> existingActions = ((CompositeActionImpl) result).getActions();
List<Action> newActions = new ArrayList<Action>();
for (int i = 0; i < jsonActions.length(); i++)
{
JSONObject innerJsonAction = jsonActions.getJSONObject(i);
if (innerJsonAction.has("id"))
{
// update existing object
String actionId = innerJsonAction.getString("id");
Action existingAction = getAction(existingActions, actionId);
existingActions.remove(existingAction);
Action updatedAction = updateActionFromJson(innerJsonAction, (ActionImpl)existingAction);
newActions.add(updatedAction);
Action updatedAction = updateActionFromJson(innerJsonAction, (ActionImpl) existingAction);
newActions.add(updatedAction);
}
else
{
//create new action as id was not sent
newActions.add(parseJsonAction(innerJsonAction));
}
newActions.add(parseJsonAction(innerJsonAction));
}
}
existingActions.clear();
for (Action action : newActions)
{
existingActions.add(action);
}
}
}
if (jsonAction.has("conditions"))
{
{
JSONArray jsonConditions = jsonAction.getJSONArray("conditions");
if (jsonConditions.length() == 0)
{
// empty array was sent -> clear list
@@ -271,20 +268,20 @@ public class RulePut extends RulePost
{
List<ActionCondition> existingConditions = result.getActionConditions();
List<ActionCondition> newConditions = new ArrayList<ActionCondition>();
for (int i = 0; i < jsonConditions.length(); i++)
{
JSONObject jsonCondition = jsonConditions.getJSONObject(i);
JSONObject jsonCondition = jsonConditions.getJSONObject(i);
if (jsonCondition.has("id"))
{
{
// update existing object
String conditionId = jsonCondition.getString("id");
ActionCondition existingCondition = getCondition(existingConditions, conditionId);
existingConditions.remove(existingCondition);
ActionCondition updatedActionCondition = updateActionConditionFromJson(jsonCondition, (ActionConditionImpl)existingCondition);
ActionCondition updatedActionCondition = updateActionConditionFromJson(jsonCondition, (ActionConditionImpl) existingCondition);
newConditions.add(updatedActionCondition);
}
else
@@ -292,31 +289,31 @@ public class RulePut extends RulePost
// create new object as id was not sent
newConditions.add(parseJsonActionCondition(jsonCondition));
}
}
}
existingConditions.clear();
for (ActionCondition condition : newConditions)
{
existingConditions.add(condition);
}
}
}
}
}
if (jsonAction.has("compensatingAction"))
{
JSONObject jsonCompensatingAction = jsonAction.getJSONObject("compensatingAction");
Action compensatingAction = updateActionFromJson(jsonCompensatingAction, (ActionImpl)actionToUpdate.getCompensatingAction());
Action compensatingAction = updateActionFromJson(jsonCompensatingAction, (ActionImpl) actionToUpdate.getCompensatingAction());
actionToUpdate.setCompensatingAction(compensatingAction);
}
return result;
}
protected ActionCondition updateActionConditionFromJson(JSONObject jsonCondition, ActionConditionImpl conditionToUpdate) throws JSONException
{
ActionConditionImpl result = null;
if (jsonCondition.has("id"))
{
// update exiting object
@@ -328,21 +325,21 @@ public class RulePut extends RulePost
result = parseJsonActionCondition(jsonCondition);
return result;
}
if (jsonCondition.has("invertCondition"))
{
result.setInvertCondition(jsonCondition.getBoolean("invertCondition"));
}
if (jsonCondition.has("parameterValues"))
{
JSONObject jsonParameterValues = jsonCondition.getJSONObject("parameterValues");
result.setParameterValues(parseJsonParameterValues(jsonParameterValues));
result.setParameterValues(parseJsonParameterValues(jsonParameterValues, result.getActionConditionDefinitionName(), false));
}
return result;
}
private Action getAction(List<Action> actions, String id)
{
Action result = null;
@@ -354,22 +351,22 @@ public class RulePut extends RulePost
break;
}
}
return result;
}
private ActionCondition getCondition(List<ActionCondition> conditions, String id)
{
ActionCondition result = null;
ActionCondition result = null;
for (ActionCondition сondition : conditions)
{
if (сondition.getId().equalsIgnoreCase(id))
{
result = сondition;
break;
}
}
}
return result;
return result;
}
}

View File

@@ -44,17 +44,17 @@ public class RuleTypesGet extends AbstractRuleWebScript
@SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(RuleTypesGet.class);
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
Map<String, Object> model = new HashMap<String, Object>();
// get all rule types
List<RuleType> ruletypes = ruleService.getRuleTypes();
model.put("ruletypes", ruletypes);
return model;
}
}

View File

@@ -24,10 +24,12 @@
*/
package org.alfresco.repo.web.scripts.rule;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.alfresco.repo.web.scripts.rule.ruleset.RuleRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.rule.Rule;
import org.alfresco.service.cmr.rule.RuleType;
@@ -45,31 +47,35 @@ public class RulesGet extends AbstractRuleWebScript
{
@SuppressWarnings("unused")
private static Log logger = LogFactory.getLog(RulesGet.class);
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
{
Map<String, Object> model = new HashMap<String, Object>();
// get request parameters
NodeRef nodeRef = parseRequestForNodeRef(req);
String ruleType = req.getParameter("ruleType");
RuleType type = ruleService.getRuleType(ruleType);
if (type == null)
{
ruleType = null;
}
// get all rules (including inherited) filtered by rule type
List<Rule> rules = ruleService.getRules(nodeRef, true, ruleType);
model.put("rules", rules);
model.put("storeType", nodeRef.getStoreRef().getProtocol());
model.put("storeId", nodeRef.getStoreRef().getIdentifier());
model.put("id", nodeRef.getId());
// get all rules (excluding inherited) filtered by rule type
List<Rule> rules = ruleService.getRules(nodeRef, false, ruleType);
List<RuleRef> ruleRefs = new ArrayList<RuleRef>();
for (Rule rule : rules)
{
ruleRefs.add(new RuleRef(rule, fileFolderService.getFileInfo(ruleService.getOwningNodeRef(rule))));
}
model.put("ruleRefs", ruleRefs);
return model;
}
}

View File

@@ -51,52 +51,55 @@ public class RulesetGet extends AbstractRuleWebScript
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
{
Map<String, Object> model = new HashMap<String, Object>();
// get request parameters
NodeRef nodeRef = parseRequestForNodeRef(req);
String ruleType = req.getParameter("ruleType");
RuleType type = ruleService.getRuleType(ruleType);
if (type == null)
{
ruleType = null;
}
RuleSet ruleset = new RuleSet();
// get all "owned" rules
List<Rule> ownedRules = ruleService.getRules(nodeRef, false, ruleType);
// get all rules (including inherited)
List<Rule> inheritedRules = ruleService.getRules(nodeRef, true, ruleType);
// remove "owned" rules
inheritedRules.removeAll(ownedRules);
List<RuleRef> rulesToSet = new ArrayList<RuleRef>();
for (Rule rule : ownedRules)
{
rulesToSet.add(new RuleRef(rule, ruleService.getOwningNodeRef(rule)));
}
rulesToSet.add(new RuleRef(rule, fileFolderService.getFileInfo(ruleService.getOwningNodeRef(rule))));
}
ruleset.setRules(rulesToSet);
List<RuleRef> inheritedRulesToSet = new ArrayList<RuleRef>();
for (Rule rule : inheritedRules)
{
inheritedRulesToSet.add(new RuleRef(rule, ruleService.getOwningNodeRef(rule)));
}
inheritedRulesToSet.add(new RuleRef(rule, fileFolderService.getFileInfo(ruleService.getOwningNodeRef(rule))));
}
ruleset.setInheritedRules(inheritedRulesToSet);
ruleset.setLinkedToRuleSet(ruleService.getLinkedToRuleNode(nodeRef));
ruleset.setLinkedFromRuleSets(ruleService.getLinkedFromRuleNodes(nodeRef));
ruleset.setRulesetNodeRef(nodeRef);
model.put("ruleset", ruleset);
return model;
}
}

View File

@@ -24,11 +24,9 @@
*/
package org.alfresco.repo.web.scripts.rule.ruleset;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.rule.Rule;
/**
* Rule object for REST API
*
@@ -40,17 +38,17 @@ public class RuleRef
/** Serial version UID */
private static final long serialVersionUID = -923276130307938661L;
private NodeRef owningNodeRef;
private FileInfo owningFileInfo;
private Rule rule;
public RuleRef(Rule rule, NodeRef owningNodeRef)
public RuleRef(Rule rule, FileInfo owningFileInfo)
{
this.rule = rule;
this.owningNodeRef = owningNodeRef;
this.owningFileInfo = owningFileInfo;
}
/**
* Set the rule
*
@@ -60,7 +58,7 @@ public class RuleRef
{
this.rule = rule;
}
/**
* Return the rule
*
@@ -70,24 +68,24 @@ public class RuleRef
{
return rule;
}
/**
* Set the owning node reference for rule
* Set the owning file info reference for rule
*
* @param owningNodeRef the owning node reference to set
* @param owningNodeRef the owning file info reference to set
*/
public void setOwningNodeRef(NodeRef owningNodeRef)
public void setOwningFileInfo(FileInfo owningFileInfo)
{
this.owningNodeRef = owningNodeRef;
this.owningFileInfo = owningFileInfo;
}
/**
* Returns the owning node reference for a rule.
* Returns the owning file info reference for a rule.
*
* @return the owning node reference
* @return the owning file info reference
*/
public NodeRef getOwningNodeRef()
public FileInfo getOwningFileInfo()
{
return owningNodeRef;
return owningFileInfo;
}
}

View File

@@ -35,19 +35,19 @@ import org.alfresco.service.cmr.repository.NodeRef;
*/
public class RuleSet implements Serializable
{
private static final long serialVersionUID = 6985140035928444095L;
private List<RuleRef> rules = null;
private List<RuleRef> inheritedRules = null;
private NodeRef rulesetNodeRef;
private NodeRef linkedToRuleSet;
private List<NodeRef> linkedFromRuleSets;
/**
* Set list of the rules "owned" by this rule set
*
@@ -57,7 +57,7 @@ public class RuleSet implements Serializable
{
this.rules = rules;
}
/**
* Get list of the rules "owned" by this rule set
*
@@ -67,7 +67,7 @@ public class RuleSet implements Serializable
{
return rules;
}
/**
* Set list of the rules inherited by this rule set from parent
*
@@ -77,7 +77,7 @@ public class RuleSet implements Serializable
{
this.inheritedRules = inheritedRules;
}
/**
* Get list of the rules inherited by this rule set from parent
*
@@ -87,7 +87,7 @@ public class RuleSet implements Serializable
{
return inheritedRules;
}
/**
* Set the nodeRef to which this ruleset belongs
*
@@ -97,7 +97,7 @@ public class RuleSet implements Serializable
{
this.rulesetNodeRef = rulesetNodeRef;
}
/**
* Get the nodeRef to which this ruleset belongs
*
@@ -107,7 +107,7 @@ public class RuleSet implements Serializable
{
return rulesetNodeRef;
}
/**
* Set the nodeRef to which this ruleset linked to
*
@@ -117,7 +117,7 @@ public class RuleSet implements Serializable
{
this.linkedToRuleSet = linkedToRuleSet;
}
/**
* Get the nodeRef to which this ruleset linked to
*
@@ -127,7 +127,7 @@ public class RuleSet implements Serializable
{
return linkedToRuleSet;
}
/**
* Set the list of nodeRefs that link to this ruleset
*
@@ -137,7 +137,7 @@ public class RuleSet implements Serializable
{
this.linkedFromRuleSets = linkedFromRuleSets;
}
/**
* Get the list of nodeRefs that link to this ruleset
*