cancel
Showing results for 
Search instead for 
Did you mean: 

File upload webservice creates 2 versions, 1 void

jnova
Champ in-the-making
Champ in-the-making
Hi everyone Smiley Happy

I'm new in Alfresco, so I dunno if maybe I'm doing something wrong or whatsoever.

The thhing is: I created a Web Service to upload a file, making it immediately versionable. It uploads with no problems, but it creates two versions right away.
And when I check de version 1.0 of the file, it sends me an error message. The version 1.1, although, works just fine!
I don't know what's happening… If anyone could help me, please, I'd appreciate it very much Smiley Happy

Here's the code:


public String writeContentInSpace(String contentName, String spaceName, String mimeType, String encoding, byte[] content) throws AlfrescoAdapterException {

      try {
         // Start the session
         AuthenticationUtils.startSession(username, password);
      } catch (AuthenticationFault fault) {
         System.out.println("fallo startSession en writeContentInSpace");
         fault.printStackTrace();
         return null;
      }
      try {

         // Searching for the space UUID
         // Get a reference to the respository web service
         RepositoryServiceSoapBindingStub repositoryService;
         if (isLocalService)
            repositoryService = WebServiceFactory.getRepositoryService();
         else
            repositoryService = WebServiceFactory.getRepositoryService(serviceURL);

         // Get a reference to the space we have named
         Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
         Node[] nodes = null;
         Reference reference = null;
         Predicate predicate =null;
         try {
            reference = new Reference(storeRef, null, "/app:company_home/app:user_homes/cm:" + spaceName);
            predicate = new Predicate(new Reference[] { reference }, null, null);
            nodes = repositoryService.get(predicate);
         } catch (Exception ex) {
            System.out.println("fallo en repositoryService.get()");
            System.out.println("excepcion es = " + ex.getMessage());
            ex.printStackTrace();
            return null;
         }
         // Searching for the UUID out of the result
         String spaceUUID = "";
         if (nodes[0] != null) {
            NamedValue[] properties = nodes[0].getProperties();
            SpaceNode sn = new SpaceNode();
            for (int j = 0; j < properties.length; j++) {
               NamedValue nv = properties[j];
               sn.getNodeInformation().put(nv.getName(), nv.getValue());
            }
            spaceUUID = sn.getNodeUUID();
         } else
            throw new AlfrescoAdapterException(true, "No space '" + spaceName + "' found!", "No space '" + spaceName + "' found!", null);

         // Create a reference to the parent where we want to insert content
         Reference referenceWrite = new Reference(storeRef, spaceUUID, null);

         ParentReference parentReference = new ParentReference(referenceWrite.getStore(), referenceWrite.getUuid(), null, Constants.ASSOC_CONTAINS, Constants.ASSOC_CONTAINS);

         // Create content
         NamedValue[] newProperties = new NamedValue[] { Utils.createNamedValue(Constants.PROP_NAME, contentName)};
         CMLCreate create = new CMLCreate("1", parentReference, null, null, null, Constants.TYPE_CONTENT, newProperties);
         
         CMLAddAspect addAspect = new CMLAddAspect();
         addAspect.setAspect(Constants.ASPECT_VERSIONABLE);
         addAspect.setProperty(newProperties);
         addAspect.setWhere(new Predicate(new Reference[] { referenceWrite }, null, null));
         addAspect.setWhere_id("1");
         
         CML cml = new CML();
         cml.setCreate(new CMLCreate[] { create });
         cml.setAddAspect(new CMLAddAspect[]{addAspect});

         // update
         UpdateResult[] results;
         if (isLocalService)
            results = WebServiceFactory.getRepositoryService().update(cml);
         else
            results = WebServiceFactory.getRepositoryService(serviceURL).update(cml);

         // Set content
         ContentFormat format = new ContentFormat(mimeType.toString(), encoding);
         Content newContent;
         if (isLocalService)
            newContent = WebServiceFactory.getContentService().write(results[0].getDestination(), Constants.PROP_CONTENT, content, format);
         else
            newContent = WebServiceFactory.getContentService(serviceURL).write(results[0].getDestination(), Constants.PROP_CONTENT, content, format);

         return newContent.getNode().getUuid();
      } catch (Exception e) {
         System.out.println(e.getMessage());
         System.out.println(e.getClass().getName());
         e.printStackTrace();
         throw new AlfrescoAdapterException(true, "Exception occurred during \"writeContentInSpace\"", "Exception occurred during \"writeContentInSpace\"", e);
      } finally {
         // End the session
         AuthenticationUtils.endSession();
      }
   }

In advance, thanks!!!!  Smiley Tongue
2 REPLIES 2

jnova
Champ in-the-making
Champ in-the-making
I've just realized something:

It's in this section of code:


// update
         UpdateResult[] results;
         if (isLocalService)
            results = WebServiceFactory.getRepositoryService().update(cml);
         else
            results = WebServiceFactory.getRepositoryService(serviceURL).update(cml);

where it creates the version 1.0. But empty 😕

The question then is: How can I do it to, at the moment the web service writes the content, create the version 1.0, and not before??
Any suggestions??

jnova
Champ in-the-making
Champ in-the-making
Solved!!

All I did was to create an extra function to add the versionable aspect RIGHT AFTER the content was uploaded.


public void makeVersionableContent(String destinationNodeUuid)
         throws AlfrescoAdapterException {
      try {
         // Iniciar sesion
         AuthenticationUtils.startSession(username, password);

         // Obtener repositorio
         RepositoryServiceSoapBindingStub repositoryService;
         if (isLocalService)
            repositoryService = WebServiceFactory.getRepositoryService();
         else
            repositoryService = WebServiceFactory
                  .getRepositoryService(serviceURL);

         // Obtener referencia al nodo
         Store storeRef = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
         Reference referenceVersion = new Reference(storeRef,
               destinationNodeUuid, null);
         Predicate predicateVersion = new Predicate(
               new Reference[] { referenceVersion }, null, null);

         // Crear aspecto versionable
         CMLAddAspect addVersionableAspect = new CMLAddAspect();
         addVersionableAspect.setAspect(Constants.ASPECT_VERSIONABLE);
         addVersionableAspect.setWhere(predicateVersion);

         // Crear bloque CML
         CML cml = new CML();
         cml.setAddAspect(new CMLAddAspect[] { addVersionableAspect });

         // Actualizar repositorio
         UpdateResult[] results = repositoryService.update(cml);
      } catch (Exception e) {
         System.out.println(e.getMessage());
         System.out.println(e.getClass().getName());
         e.printStackTrace();
         throw new AlfrescoAdapterException(true,
               "Exception occurred during \"makeVersionableContent\"",
               "Exception occurred during \"makeVersionableContent\"", e);
      } finally {
         // Terminar sesion
         AuthenticationUtils.endSession();
      }
   }

And call this function just after the previous one (for creating content). Tah' daaaa!!!!  :lol:

Thanks anyway. I'll left this in case someone finds it useful.

Greetings.
Jose.