cancel
Showing results for 
Search instead for 
Did you mean: 

assign a category via WebService

danisowa
Champ in-the-making
Champ in-the-making
Hell,
i just solved my problem of uploading a file.
but now i want to assign one or more categories to this file……
how to manage this….. i have really no idea and searched now for more than 4 ours in the web.
Please help me.

Thx a lot
public class AddIssue
{
   
   private final static Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
   
   private String username = "admin";
   private String password = "admin";
   private static final String ASSOC_CONTAINS = "{http://www.alfresco.org/model/content/1.0}contains";

   public static void main(String[] args) {
      new AddIssue();
   }

   public AddIssue() {
     //filename for the document
      String filename = "issue1.jpg";
     
      // path to the file that should be uploaded
      String file = "org/alfresco/webservice/test/resources/test.jpg";
     
      // where to put the file
      String location = "/app:company_home/cm:Dokumente/cm:Daniel";
     
      // what kind of file is it?
      String filetype = "image/jpeg";
           
        try {
           // Authentication
           AuthenticationUtils.startSession(username, password);
         
           // get classification Service
           ClassificationServiceSoapBindingStub classificationService = WebServiceFactory.getClassificationService();
          
       
          
           // Upload
          
           // Upload binary content into the repository
           //Reference reference = Query1.executeSearch();
          
           ParentReference parentReference = new ParentReference(STORE, null, location ,ASSOC_CONTAINS, ASSOC_CONTAINS);
          
         
           // Create the content
           NamedValue[] properties = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, filename)};
         
           CMLCreate create = new CMLCreate("1", parentReference, null, null, null, Constants.TYPE_CONTENT, properties);
                      CML cml = new CML();
           cml.setCreate(new CMLCreate[]{create});
           UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml);
         
          
           // Get the created node and create the format
           Reference newContentNode = result[0].getDestination();             
           ContentFormat format = new ContentFormat(filetype, "UTF-8"); 
          
           // Open the file and convert to byte array
           InputStream viewStream = newContentNode.getClass().getClassLoader().getResourceAsStream(file);
           byte[] bytes = ContentUtils.convertToByteArray(viewStream);
          
          
           // Write the content
           WebServiceFactory.getContentService().write(newContentNode, Constants.PROP_CONTENT, bytes, format);
          
          
          
          
          
             } catch (Exception e) {
         e.printStackTrace();
      } finally {
         AuthenticationUtils.endSession();
         System.out.println("Everything seems to be good");
      }
   }
}
10 REPLIES 10

dongq
Champ in-the-making
Champ in-the-making
This is my code to apply a category to a node. itemId is the uuid of the node and categoryId is the uuid of the category.

Reference reference = new Reference(storeRef, itemId, null);
Predicate predicate = new Predicate(new Reference[]{reference}, null, null);
ArrayList<Reference> categoryList = new ArrayList<Reference>();
AppliedCategory appliedCategory = new AppliedCategory();
Reference catRef = new Reference(storeRef, categoryId, null);
categoryList.add(catRef);
appliedCategory.setCategories(categoryList.toArray(new Reference[0]));
appliedCategory.setClassification(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, "generalclassifiable"));
AppliedCategory[] appliedCategories = new AppliedCategory[]{appliedCategory};
// Now classify the node
ClassificationServiceSoapBindingStub classificationService = WebServiceFactory.getClassificationService();
CategoriesResult[] result2 = classificationService.setCategories(predicate, appliedCategories);

danisowa
Champ in-the-making
Champ in-the-making
Hi dongq

i would try your solution tomorrow….. i'll give a feedback if it would work… thanks for so long

just a little question at the moment.. i've not programmed a lot with alfresco webservices… how do i get the uuids of the node and the category?

thanks a lot

regards

danisowa

dongq
Champ in-the-making
Champ in-the-making
If you don't know the uuid, you can use the path.
public Reference(
           org.alfresco.webservice.types.Store store,
           java.lang.String uuid,
           java.lang.String path)
The constructor for Reference will take either the uuid or the path.

danisowa
Champ in-the-making
Champ in-the-making
Hi,
i've tried your code snipped right now.
it seems to be good but i got an error assigning the category….
it says could not resolve to a single node….
what is the path to the category?
i've got a category "Test" with a subcategory "Test2"

whats wrong with the following code!?

private final static Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
Reference catRef = new Reference(STORE, null, "cm:generalclassifiable/cm:Test/cm:Test2");

Thanks a lot

regards

danisowa

dongq
Champ in-the-making
Champ in-the-making
I think the path is probably /cm:categoryRoot/cm:generalclassifiable/cm:Test/cm:Test2 .
You can use the classficationservice's getClassification and getChildCategories to iterate through the category hierarchy.

danisowa
Champ in-the-making
Champ in-the-making
Hi dongq…. thanks for all your answers.. now everything works fine….

is it possible to run this code on a Application Server?!
i would crate a html form where i can choose title and the category and something else.
and on submittion of this form my code should be executed….. is this possible?

Thanks a lot.

danisowa
Champ in-the-making
Champ in-the-making
ok,
i think i should run my code as servlet.

but how can i manage to use for example org.alfresco.webservice.types.Store

my servlet reports an error….

please help me

regards

danisowa

robertoroberto
Champ in-the-making
Champ in-the-making
…And…How Can I assign 2 or more categories to the Content?

hitesh
Champ in-the-making
Champ in-the-making
Just add one more Reference for your category , and add that reference to categoryList mentined in the above code…. that's it.
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.