mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
ACS-3229: Rules v1 REST API - Get rule definition - mapping of actions (#1245)
ACS-3229: Rules v1 REST API - Get rule definition - adding mapping of "actions"
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Remote API
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2022 Alfresco Software Limited
|
||||
* %%
|
||||
* This file is part of the Alfresco software.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
package org.alfresco.rest.api.model.rules;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.alfresco.repo.action.ActionImpl;
|
||||
import org.alfresco.repo.action.CompositeActionImpl;
|
||||
import org.alfresco.repo.action.executer.SetPropertyValueActionExecuter;
|
||||
import org.alfresco.service.Experimental;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.util.GUID;
|
||||
|
||||
@Experimental
|
||||
public class Action
|
||||
{
|
||||
private String actionDefinitionId;
|
||||
private Map<String, Serializable> params;
|
||||
|
||||
/**
|
||||
* Converts service POJO action to REST model action.
|
||||
*
|
||||
* @param actionModel - {@link org.alfresco.service.cmr.action.Action} service POJO
|
||||
* @return {@link Action} REST model
|
||||
*/
|
||||
public static Action from(final org.alfresco.service.cmr.action.Action actionModel)
|
||||
{
|
||||
if (actionModel == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
final Action.Builder builder = builder().actionDefinitionId(actionModel.getActionDefinitionName());
|
||||
if (actionModel.getParameterValues() != null)
|
||||
{
|
||||
builder.params(new HashMap<>(actionModel.getParameterValues()));
|
||||
}
|
||||
|
||||
return builder.create();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the REST model object to the equivalent service POJO.
|
||||
*
|
||||
* @param nodeRef The node reference.
|
||||
* @return The action service POJO.
|
||||
*/
|
||||
public org.alfresco.service.cmr.action.Action toServiceModel(final NodeRef nodeRef)
|
||||
{
|
||||
return new ActionImpl(nodeRef, GUID.generate(), SetPropertyValueActionExecuter.NAME, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the REST model objects to composite action service POJO.
|
||||
*
|
||||
* @param actions List of actions.
|
||||
* @return The composite action service POJO.
|
||||
*/
|
||||
public static org.alfresco.service.cmr.action.Action toCompositeAction(final List<Action> actions) {
|
||||
if (actions == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
final org.alfresco.service.cmr.action.CompositeAction compositeAction = new CompositeActionImpl(null, GUID.generate());
|
||||
actions.forEach(action -> compositeAction.addAction(action.toServiceModel(null)));
|
||||
return compositeAction;
|
||||
}
|
||||
|
||||
public String getActionDefinitionId()
|
||||
{
|
||||
return actionDefinitionId;
|
||||
}
|
||||
|
||||
public void setActionDefinitionId(String actionDefinitionId)
|
||||
{
|
||||
this.actionDefinitionId = actionDefinitionId;
|
||||
}
|
||||
|
||||
public Map<String, Serializable> getParams()
|
||||
{
|
||||
return params;
|
||||
}
|
||||
|
||||
public void setParams(Map<String, Serializable> params)
|
||||
{
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Action{" + "actionDefinitionId='" + actionDefinitionId + '\'' + ", params=" + params + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
Action action = (Action) o;
|
||||
return Objects.equals(actionDefinitionId, action.actionDefinitionId) && Objects.equals(params, action.params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return Objects.hash(actionDefinitionId, params);
|
||||
}
|
||||
|
||||
public static Builder builder()
|
||||
{
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder
|
||||
{
|
||||
private String actionDefinitionId;
|
||||
private Map<String, Serializable> params;
|
||||
|
||||
public Builder actionDefinitionId(String actionDefinitionId)
|
||||
{
|
||||
this.actionDefinitionId = actionDefinitionId;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder params(Map<String, Serializable> params)
|
||||
{
|
||||
this.params = params;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Action create() {
|
||||
final Action action = new Action();
|
||||
action.setActionDefinitionId(actionDefinitionId);
|
||||
action.setParams(params);
|
||||
return action;
|
||||
}
|
||||
}
|
||||
}
|
@@ -26,22 +26,16 @@
|
||||
|
||||
package org.alfresco.rest.api.model.rules;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.alfresco.model.ContentModel;
|
||||
import org.alfresco.repo.action.ActionImpl;
|
||||
import org.alfresco.repo.action.executer.ScriptActionExecuter;
|
||||
import org.alfresco.repo.action.executer.SetPropertyValueActionExecuter;
|
||||
import org.alfresco.rest.api.Nodes;
|
||||
import org.alfresco.rest.framework.resource.UniqueId;
|
||||
import org.alfresco.service.Experimental;
|
||||
import org.alfresco.service.cmr.action.CompositeAction;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.util.GUID;
|
||||
|
||||
@Experimental
|
||||
public class Rule
|
||||
@@ -55,6 +49,7 @@ public class Rule
|
||||
private boolean shared;
|
||||
private String errorScript;
|
||||
private List<RuleTrigger> triggers;
|
||||
private List<Action> actions;
|
||||
|
||||
/**
|
||||
* Converts service POJO rule to REST model rule.
|
||||
@@ -84,9 +79,16 @@ public class Rule
|
||||
{
|
||||
builder.triggers(ruleModel.getRuleTypes().stream().map(RuleTrigger::of).collect(Collectors.toList()));
|
||||
}
|
||||
if (ruleModel.getAction() != null && ruleModel.getAction().getCompensatingAction() != null && ruleModel.getAction().getCompensatingAction().getParameterValue(ScriptActionExecuter.PARAM_SCRIPTREF) != null)
|
||||
if (ruleModel.getAction() != null)
|
||||
{
|
||||
builder.errorScript(ruleModel.getAction().getCompensatingAction().getParameterValue(ScriptActionExecuter.PARAM_SCRIPTREF).toString());
|
||||
if (ruleModel.getAction().getCompensatingAction() != null && ruleModel.getAction().getCompensatingAction().getParameterValue(ScriptActionExecuter.PARAM_SCRIPTREF) != null)
|
||||
{
|
||||
builder.errorScript(ruleModel.getAction().getCompensatingAction().getParameterValue(ScriptActionExecuter.PARAM_SCRIPTREF).toString());
|
||||
}
|
||||
if (ruleModel.getAction() instanceof CompositeAction && ((CompositeAction) ruleModel.getAction()).getActions() != null)
|
||||
{
|
||||
builder.actions(((CompositeAction) ruleModel.getAction()).getActions().stream().map(Action::from).collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
|
||||
return builder.create();
|
||||
@@ -100,20 +102,12 @@ public class Rule
|
||||
*/
|
||||
public org.alfresco.service.cmr.rule.Rule toServiceModel(Nodes nodes)
|
||||
{
|
||||
org.alfresco.service.cmr.rule.Rule ruleModel = new org.alfresco.service.cmr.rule.Rule();
|
||||
if (id != null)
|
||||
{
|
||||
NodeRef nodeRef = nodes.validateOrLookupNode(id, null);
|
||||
ruleModel.setNodeRef(nodeRef);
|
||||
}
|
||||
final org.alfresco.service.cmr.rule.Rule ruleModel = new org.alfresco.service.cmr.rule.Rule();
|
||||
final NodeRef nodeRef = (id != null) ? nodes.validateOrLookupNode(id, null) : null;
|
||||
ruleModel.setNodeRef(nodeRef);
|
||||
ruleModel.setTitle(name);
|
||||
|
||||
// TODO: Once we have actions working properly then this needs to be replaced.
|
||||
Map<String, Serializable> parameters = Map.of(
|
||||
SetPropertyValueActionExecuter.PARAM_PROPERTY, ContentModel.PROP_TITLE,
|
||||
SetPropertyValueActionExecuter.PARAM_VALUE, "UPDATED:" + GUID.generate());
|
||||
org.alfresco.service.cmr.action.Action action = new ActionImpl(null, GUID.generate(), SetPropertyValueActionExecuter.NAME, parameters);
|
||||
ruleModel.setAction(action);
|
||||
ruleModel.setAction(Action.toCompositeAction(actions));
|
||||
|
||||
return ruleModel;
|
||||
}
|
||||
@@ -209,16 +203,21 @@ public class Rule
|
||||
this.triggers = triggers;
|
||||
}
|
||||
|
||||
public List<Void> getActions()
|
||||
public List<Action> getActions()
|
||||
{
|
||||
return Collections.emptyList();
|
||||
return actions;
|
||||
}
|
||||
|
||||
public void setActions(List<Action> actions)
|
||||
{
|
||||
this.actions = actions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Rule{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", description='" + description + '\'' + ", enabled=" + enabled + ", cascade=" + cascade
|
||||
+ ", asynchronous=" + asynchronous + ", shared=" + shared + ", errorScript='" + errorScript + '\'' + ", triggers=" + triggers + '}';
|
||||
+ ", asynchronous=" + asynchronous + ", shared=" + shared + ", errorScript='" + errorScript + '\'' + ", triggers=" + triggers + ", actions=" + actions + '}';
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -230,13 +229,14 @@ public class Rule
|
||||
return false;
|
||||
Rule rule = (Rule) o;
|
||||
return enabled == rule.enabled && cascade == rule.cascade && asynchronous == rule.asynchronous && shared == rule.shared && Objects.equals(id, rule.id) && Objects.equals(
|
||||
name, rule.name) && Objects.equals(description, rule.description) && Objects.equals(errorScript, rule.errorScript) && Objects.equals(triggers, rule.triggers);
|
||||
name, rule.name) && Objects.equals(description, rule.description) && Objects.equals(errorScript, rule.errorScript) && Objects.equals(triggers, rule.triggers)
|
||||
&& Objects.equals(actions, rule.actions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return Objects.hash(id, name, description, enabled, cascade, asynchronous, shared, errorScript, triggers);
|
||||
return Objects.hash(id, name, description, enabled, cascade, asynchronous, shared, errorScript, triggers, actions);
|
||||
}
|
||||
|
||||
public static Builder builder()
|
||||
@@ -256,6 +256,7 @@ public class Rule
|
||||
private boolean shared;
|
||||
private String errorScript;
|
||||
private List<RuleTrigger> triggers;
|
||||
private List<Action> actions;
|
||||
|
||||
public Builder id(String id)
|
||||
{
|
||||
@@ -311,6 +312,12 @@ public class Rule
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder actions(List<Action> actions)
|
||||
{
|
||||
this.actions = actions;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Rule create()
|
||||
{
|
||||
Rule rule = new Rule();
|
||||
@@ -323,6 +330,7 @@ public class Rule
|
||||
rule.setShared(shared);
|
||||
rule.setErrorScript(errorScript);
|
||||
rule.setTriggers(triggers);
|
||||
rule.setActions(actions);
|
||||
return rule;
|
||||
}
|
||||
}
|
||||
|
@@ -27,6 +27,7 @@
|
||||
package org.alfresco.rest.api;
|
||||
|
||||
import org.alfresco.rest.api.impl.RulesImplTest;
|
||||
import org.alfresco.rest.api.model.rules.ActionTest;
|
||||
import org.alfresco.rest.api.model.rules.RuleTest;
|
||||
import org.alfresco.rest.api.nodes.NodeRulesRelationTest;
|
||||
import org.alfresco.service.Experimental;
|
||||
@@ -38,7 +39,8 @@ import org.junit.runners.Suite;
|
||||
@Suite.SuiteClasses({
|
||||
NodeRulesRelationTest.class,
|
||||
RulesImplTest.class,
|
||||
RuleTest.class
|
||||
RuleTest.class,
|
||||
ActionTest.class
|
||||
})
|
||||
public class RulesUnitTests
|
||||
{
|
||||
|
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* #%L
|
||||
* Alfresco Remote API
|
||||
* %%
|
||||
* Copyright (C) 2005 - 2022 Alfresco Software Limited
|
||||
* %%
|
||||
* This file is part of the Alfresco software.
|
||||
* If the software was purchased under a paid Alfresco license, the terms of
|
||||
* the paid license agreement will prevail. Otherwise, the software is
|
||||
* provided under the following open source license terms:
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco 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 Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
package org.alfresco.rest.api.model.rules;
|
||||
|
||||
import static org.alfresco.repo.action.executer.SetPropertyValueActionExecuter.PARAM_PROPERTY;
|
||||
import static org.alfresco.repo.action.executer.SetPropertyValueActionExecuter.PARAM_VALUE;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.alfresco.repo.action.ActionImpl;
|
||||
import org.alfresco.service.Experimental;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.junit.Test;
|
||||
|
||||
@Experimental
|
||||
public class ActionTest
|
||||
{
|
||||
|
||||
private static final String ACTION_DEFINITION_NAME = "actionDefName";
|
||||
private static final Map<String, Serializable> parameters = new HashMap<>();
|
||||
|
||||
static
|
||||
{
|
||||
parameters.put(PARAM_PROPERTY, "propertyName");
|
||||
parameters.put(PARAM_VALUE, "propertyValue");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrom()
|
||||
{
|
||||
final NodeRef nodeRef = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "ruleId");
|
||||
final org.alfresco.service.cmr.action.Action actionModel = new ActionImpl(nodeRef, "actionId", ACTION_DEFINITION_NAME, parameters);
|
||||
final Action expectedAction = Action.builder().actionDefinitionId(ACTION_DEFINITION_NAME).params(parameters).create();
|
||||
|
||||
final Action actualAction = Action.from(actionModel);
|
||||
|
||||
assertThat(actualAction).isNotNull().usingRecursiveComparison().isEqualTo(expectedAction);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromActionModelWithNullValues()
|
||||
{
|
||||
final org.alfresco.service.cmr.action.Action actionModel = new ActionImpl(null, null, null);
|
||||
final Action expectedAction = Action.builder().params(Collections.emptyMap()).create();
|
||||
|
||||
final Action actualAction = Action.from(actionModel);
|
||||
|
||||
assertThat(actualAction).isNotNull().usingRecursiveComparison().isEqualTo(expectedAction);
|
||||
}
|
||||
}
|
@@ -39,7 +39,6 @@ import org.alfresco.service.cmr.action.ActionCondition;
|
||||
import org.alfresco.service.cmr.repository.NodeRef;
|
||||
import org.alfresco.service.cmr.repository.StoreRef;
|
||||
import org.alfresco.service.cmr.rule.RuleType;
|
||||
import org.assertj.core.api.Condition;
|
||||
import org.junit.Test;
|
||||
|
||||
@Experimental
|
||||
@@ -50,7 +49,7 @@ public class RuleTest
|
||||
private static final String RULE_DESCRIPTION = "rule description";
|
||||
private static final boolean RULE_ENABLED = true;
|
||||
private static final boolean RULE_CASCADE = true;
|
||||
private static final boolean RULE_ASYNC = false;
|
||||
private static final boolean RULE_ASYNC = true;
|
||||
private static final boolean RULE_SHARED = true;
|
||||
private static final String ERROR_SCRIPT = "error-script-ref";
|
||||
|
||||
|
Reference in New Issue
Block a user