cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a rule

rulo
Champ in-the-making
Champ in-the-making
Hi ….i woud like to know if exists samples for creating a rule for a giving space..
Thanks in advance!!!
4 REPLIES 4

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

I don't believe there is an example of this user case at the moment.

Perhaps the best place to look for now is the web service client unit tests.

Cheers,
Roy

kumbach
Champ in-the-making
Champ in-the-making
Hi ….i woud like to know if exists samples for creating a rule for a giving space..
Thanks in advance!!!

Here is some code I wrote to add a rule to a space. As Roy suggested, the unit tests may be a source of more code you can use. Documentation is almost non-existent for this area of the web service, I had to actually go into the core Alfresco code to figure out some of the methods and parameters. Good luck!



   private void addRootRules() throws RemoteException
   {
      String rootSpaceName = "Test Space";

      Reference rootReference = new Reference(AppConstants.SPACES_STORE, null, "/app:company_home/cm:" + ISO9075.encode(rootSpaceName));

      NamedValue[] parameters = new NamedValue[]{new NamedValue("aspect-name", false, "SR Aspect", null)};
      Action newAction = new Action();
      newAction.setActionName("add-features");
      newAction.setParameters(parameters);

      Condition condition = new Condition();
      condition.setId("1");
      condition.setConditionName("no-condition");
      condition.setInvertCondition(false);
      condition.setParameters(new NamedValue[]{new NamedValue("type", false, Constants.TYPE_CONTENT, null)});

      Rule newRule = new Rule();
      newRule.setRuleTypes(new String[]{"Inbound"});
      newRule.setTitle("This rule adds the "SR Aspect" aspect");
      newRule.setDescription("Add "SR Aspect"");
      newRule.setAction(newAction);

      Rule[] savedRule = WebServiceFactory.getActionService().saveRules(rootReference, new Rule[]{newRule});

        Reference reference = new Reference(AppConstants.SPACES_STORE, savedRule[0].getRuleReference().getUuid(), null);
        Predicate predicate = new Predicate(new Reference[]{reference}, AppConstants.SPACES_STORE, null);

        Node[] ruleReferenceWithProperties = WebServiceFactory.getRepositoryService().get(predicate);

//      NamedValue[] properties = new NamedValue[] { Utils.createNamedValue(Constants.PROP_NAME, "applyToChildren") };
      //NamedValue[] properties = new NamedValue[] { Utils.createNamedValue("applyToChildren", "True") };

      NamedValue properties = Utils.createNamedValue(Constants.createQNameString("http://www.alfresco.org/model/rule/1.0", "applyToChildren"), "true");

      CMLUpdate update = new CMLUpdate(new NamedValue[] {properties}, predicate, null);
      CML cml = new CML();
      cml.setUpdate(new CMLUpdate[] { update });
      UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);


   }

rulo
Champ in-the-making
Champ in-the-making
Thanks kumbach…

andresaimar
Champ in-the-making
Champ in-the-making
I am currently trying to create a rule but I always got a NULL EXCEPTION when trying to save the rule.

Did anyone knows something about it?

By the way, I am doing it by using web services.