AnsweredAssumed Answered

File upload webservice creates 2 versions, 1 void

Question asked by jnova on Nov 26, 2008
Latest reply on Nov 26, 2008 by jnova
Hi everyone :)

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 :)

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!!!!  :P

Outcomes