AnsweredAssumed Answered

Call a WebScript through WebService as action

Question asked by luca on Feb 2, 2009
Hi all,
I wanted to create Groups using webService.

I found some post that suggested to use Web Script to create them using AuthorityService and call it in your Web Service.
I done it using a Java Action instead of WebScript.

How can I change it if I want to use a WebScrip?

Here is the code, maybe someone can found these examples helpful.

1- WebService:
public class GroupUtil {

   protected static final Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
   
   public static final String PARAM_GROUP_NAME = "group-name";
    public static final String PARAM_GROUP_PARENT_NAME = "group-parent-name";
   
   public static void main(String[] args) {
      try {
         // Start the session
         AuthenticationUtils.startSession("admin", "admin");
      
         listActions();
         
         createGroup("Gruppo di test 2", "Gruppo di test");
         
         AuthenticationUtils.endSession();
         
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   
   public static void createGroup(String groupName, String parentGroupName) {
      try {
         
         // Reference
         Store spacesStore = new Store(Constants.WORKSPACE_STORE,"SpacesStore");
         Reference reference = new Reference(spacesStore, null,"/app:company_home");
         Predicate predicate = new Predicate(new Reference[] { reference },null, null);      
          
         // Parameters
         NamedValue groupNameParam = new NamedValue(PARAM_GROUP_NAME, false, groupName, null);
         NamedValue groupParentNameParam = new NamedValue(PARAM_GROUP_PARENT_NAME, false, parentGroupName, null);
         NamedValue[] params = new NamedValue[]{groupNameParam, groupParentNameParam};
         
         // Action
         Action action = new Action();
         action.setActionName("test-luca");
         action.setParameters(params);
         
         // Execute Action
         ActionExecutionResult[] actionResults = WebServiceFactory.getActionService().executeActions(predicate, new Action[]{action});

         // Print results
         Action actionResult = actionResults[0].getActions(0);
         for (NamedValue nv : actionResult.getParameters()) {
            System.out.println(nv.getName() + ": " + nv.getValue());
         }
         
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   
   public static void listActions() {
      try {
         System.out.println("—- Lista actions disponibili —–");
         
         ActionItemDefinition[] results = WebServiceFactory.getActionService().getActionDefinitions();
         
         for (int i = 0; i<results.length; i++) {
            ActionItemDefinition definition = results[i];
            System.out.println(definition.getName() + " - " + definition.getName());
         }
         System.out.println("————————————\n");
         
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

}

2- ActionExecuter:
public class TestLucaActionExecuter extends ActionExecuterAbstractBase {

   private static Log logger = LogFactory.getLog(TestLucaActionExecuter.class);
   
    public static final String NAME = "test-luca";
   
    public static final String PARAM_GROUP_NAME = "group-name";
    public static final String PARAM_GROUP_PARENT_NAME = "group-parent-name";
   
    private AuthorityService authorityService;
   
    public void setAuthorityService(AuthorityService authorityService)
    {
        this.authorityService = authorityService;
    }

   @Override
   protected void addParameterDefinitions(List<ParameterDefinition> paramList)
    {
        // Specify the parameters
        paramList.add(new ParameterDefinitionImpl(PARAM_GROUP_NAME, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_GROUP_NAME)));
        paramList.add(new ParameterDefinitionImpl(PARAM_GROUP_PARENT_NAME, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_GROUP_PARENT_NAME)));
    }

    public void executeImpl(Action action, NodeRef actionedUponNodeRef)
    {
       
       String groupName = (String)action.getParameterValue(PARAM_GROUP_NAME);
       String groupParentName = (String) action.getParameterValue(PARAM_GROUP_PARENT_NAME);
       
       
       createGroup(groupName, groupParentName);
    }
   
    private void createGroup(String shortName, String parentShortName)
    {
        // create new Group using authority Service
        String groupName = this.authorityService.getName(AuthorityType.GROUP, shortName);
        if (this.authorityService.authorityExists(groupName) == false)
        {
           String parentGroupName = null;
           if (parentShortName != null)
           {
               parentGroupName = this.authorityService.getName(AuthorityType.GROUP, parentShortName);
               if (this.authorityService.authorityExists(parentGroupName) == false)
               {
                   logger.warn("Parent group does not exist: " + parentShortName);
                   return;
               }
           }          
          
           this.authorityService.createAuthority(AuthorityType.GROUP, parentGroupName, shortName);

        }
        else
        {
            logger.warn("Group already exists: " + shortName);
        }
    }

}

3- Bean action-context:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>
     
    <!– Action Bean –>
    <bean id="test-luca" class="org.alfresco.repo.action.executer.TestLucaActionExecuter" parent="action-executer">
        <property name="authorityService">
            <ref bean="AuthorityService" />
        </property>
    </bean>
     
</beans>

Outcomes