cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to create categories using web services?

nuga
Champ in-the-making
Champ in-the-making
Is it possible to create categories using web services?..
if so how?
5 REPLIES 5

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

At the moment the creation of categories is not supported via the web service API.

As a work around a custom action could be developed to do the work you require and this could be executed from the web service API.

Cheers,
Roy

nuga
Champ in-the-making
Champ in-the-making
package com.nkics.alfresco.retrieval.test;

import java.rmi.RemoteException;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.alfresco.webservice.classification.ClassificationServiceSoapBindingStub;
import org.alfresco.webservice.content.ContentServiceSoapBindingStub;
import org.alfresco.webservice.repository.QueryResult;
import org.alfresco.webservice.repository.RepositoryFault;
import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
import org.alfresco.webservice.repository.UpdateResult;
import org.alfresco.webservice.types.CML;
import org.alfresco.webservice.types.CMLCreate;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.ParentReference;
import org.alfresco.webservice.types.Query;
import org.alfresco.webservice.types.Reference;
import org.alfresco.webservice.types.ResultSet;
import org.alfresco.webservice.types.ResultSetRow;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;



public class CategoriesTest extends TestCase {
   Store STORE = null;
   RepositoryServiceSoapBindingStub repository;
   ContentServiceSoapBindingStub content;
   ClassificationServiceSoapBindingStub classification;
   private static final String REF_TEXT1 = "/*[@cm:name=\"";

   private static final String REF_TEXT2 = "\"]";

   private static final String NODE_REF = "workspace://SpacesStore/";

   protected void setUp() throws Exception {
      super.setUp();
      
      AuthenticationUtils.startSession("admin", "admin");
      
      STORE = new Store(Constants.WORKSPACE_STORE,
      "SpacesStore");
      
      
      repository = WebServiceFactory
            .getRepositoryService();
      content = WebServiceFactory
      .getContentService();
      classification = WebServiceFactory
      .getClassificationService();
   

   }
   

   private Reference getRootReference()
   {
      Query query = new Query(Constants.QUERY_LANG_LUCENE, "PATH:\"/cm:generalclassifiable//cm:Regions\"");
      //Query query = new Query(Constants.QUERY_LANG_LUCENE, "PATH:\"/cm:generalclassifiable//cm:Eastern_x0020_Asia//member\"");
      
      try
      {
         QueryResult result= repository.query(STORE, query, true);
         ResultSet rs = result.getResultSet();
         ResultSetRow[] rows = rs.getRows();
         String uuid  = rows[0].getNode().getId();
         return new Reference(STORE, uuid, null);
      }
      catch (Exception e){
         e.printStackTrace();
         return null;
      }
   
      //String type = "{http://www.alfresco.org/model/content/1.0}category";
      
      
   }
   public void testCategoriesSearch() throws Exception
   {
      //Query query = new Query(Constants.QUERY_LANG_LUCENE, "PATH:\"/cm:generalclassifiable//cm:Regions\"");
      //Query query = new Query(Constants.QUERY_LANG_LUCENE, "PATH:\"/cm:generalclassifiable//cm:Eastern_x0020_Asia\"");
      Query query = new Query(Constants.QUERY_LANG_LUCENE, "PATH:\"/cm:generalclassifiable//cm:AK\"");
      QueryResult result = repository.query(STORE, query, true);
   
      ResultSet rs = result.getResultSet();
      ResultSetRow[] rows = rs.getRows();

   
      for (ResultSetRow row : rows) {
         System.out.println("UID: "+    row.getNode().getId());
         System.out.println("Type: "+    row.getNode().getType());
         NamedValue[] values = row.getColumns();
         System.out.println("Properties: ");
         for (NamedValue col : values) {
            System.out.println("\tName: " + col.getName());
            System.out.println("\tValue: " + col.getValue());
            
         }
      }
      
   
   }
private String parseParentPath(String path){
      
      if(path.lastIndexOf("/") > 0){
         return path.substring(1, path.lastIndexOf("/"));
      }else{
         return "";
      }
   }
   private String parseSpaceName(String path){
      if(path.lastIndexOf("/") > 0){
         return path.substring(path.lastIndexOf("/") + 1, path.length());
      }else{
         //Need to remove leading /
         return path.substring(1);
      }
   }

   private void findCategory(String category) throws Exception
   {
      Query query = new Query(Constants.QUERY_LANG_LUCENE, "PATH:\"/cm:generalclassifiable//cm:"+category+"\"");
      QueryResult result = repository.query(STORE, query, true);
      
      ResultSet rs = result.getResultSet();
      ResultSetRow[] rows = rs.getRows();

      System.out.println("Searching for: " + category );
      for (ResultSetRow row : rows) {
         System.out.println("UID: "+    row.getNode().getId());
         System.out.println("Type: "+    row.getNode().getType());
         NamedValue[] values = row.getColumns();
         System.out.println("Properties: ");
         for (NamedValue col : values) {
            System.out.println("\tName: " + col.getName());
            System.out.println("\tValue: " + col.getValue());
            
         }
      }   
   }
   public void atestCreateSpace(){
   
      Reference spaceRef = getRootReference();
      //String parentPath = alfrescoSpace(parseParentPath(path));
   //   String space = parseSpaceName(path);
      final String SUBCATEGORIES = "subcategories";
      final String CATEGORY = "category";
      String path =    "category6";
      //"/cm:categoryRoot//cm:generalclassifiable//cm:Regions"
      //either UUID or the strict path works"
      ParentReference parentRef = new ParentReference(STORE, spaceRef.getUuid(),null ,
            Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL,SUBCATEGORIES),
            Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, path));
      
      NamedValue[] properties = new NamedValue[]{
            Utils.createNamedValue(Constants.PROP_NAME, path)
            };
         
            
            
      CMLCreate create = new CMLCreate("1", parentRef, null, null, null, Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL,CATEGORY), properties);
      CML cml = new CML();
      cml.setCreate(new CMLCreate[]{create});
      UpdateResult[] results = null;
      try {
         results = WebServiceFactory.getRepositoryService().update(cml);
      
         findCategory(path);
         
      } catch (RepositoryFault e) {
         
         e.printStackTrace();
          Assert.assertTrue(false);
      } catch (RemoteException e) {
         
      }
      catch (Exception e) {
      
          e.printStackTrace();
          Assert.assertTrue(false);
   
      }   
   }
   /*
   public void testPropertiesSearch() throws Exception
   {
      //textQuery= "+PARENT:"workspace://SpacesStore/6948764b-a017-11db-b735-91446e6c9a04"+(TEXT:"+@fs\\Smiley Tongueroduct:"candy*"" OR @fs\\:description:"+@fs\\Smiley Tongueroduct:"candy*"")"


      //String qString = "@cm\\:name:\"hi.pdf\"";
      String qString = "@description\\:\"tutorial\"";
      //String qString = "@fs\\Smiley Tongueroduct:\"candy*\"";
   //   String qString = "PATH:\"/cm:*\"";
      Query query = new Query(Constants.QUERY_LANG_LUCENE,qString );
      QueryResult result = repository.query(STORE, query, true);
      
      ResultSet rs = result.getResultSet();
      ResultSetRow[] rows = rs.getRows();
      //if (rows==null)
      //   return;
      System.out.println("*****************************");
   
      for (ResultSetRow row : rows) {
         System.out.println("UID: "+    row.getNode().getId());
         System.out.println("Type: "+    row.getNode().getType());
         NamedValue[] values = row.getColumns();
         System.out.println("Properties: ");
         for (NamedValue col : values) {
            System.out.println("\tName: " + col.getName());
            System.out.println("\tValue: " + col.getValue());
            
         }
      }
      
      
      
   }*/
   protected void tearDown() throws Exception {
      super.tearDown();
   //   AuthenticationUtils.endSession();

   }
}

rwetherall
Confirmed Champ
Confirmed Champ
Are you seeing an issue with this unit test?

If so what seems to be the issue?

Cheers,
Roy

nuga
Champ in-the-making
Champ in-the-making
sorry i shoulda clarified

i'm able to create categories using this junit.

and i was posting the solution for others to see.

get rid of the "a" in front of atestcreateSpace" and that test will actually create a category.

testCreateSpace(){

rwetherall
Confirmed Champ
Confirmed Champ
Ok, thanks for that Smiley Happy

Cheers,
Roy