AnsweredAssumed Answered

creatng parent->child folders via API

Question asked by arusellcampion on Oct 21, 2010
Latest reply on Oct 22, 2010 by arusellcampion
Dear all,
I'd really appreciate any clues as to how I create a parent/child set of nodes (folders) using the Alfresci API. Specifically I want to create a parent (student) folder for example, "ArmitageShanks123456" followed by a parent folder of "EmailCorrespondence". There will be thousands of these parent/child pairs to create; Indeed there may be a requirement to create up to 12 sub folders for each student folder but for now I'd be pleased to be able to create  a parent/child pair of nodes.

I am able to create a parent folder through explorer then create a sub-folder for it via the class below but I can't create a parent folder programatically then create a sub-folder for it - again programatically. Here's my class:


package uk.ac.sheffield.cics.extensions.addContent;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.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;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import uk.ac.sheffield.cics.extensions.utils.Attributes;

@SuppressWarnings("serial")
public class CreateContentServlet extends HttpServlet{
   private String folder = "";   
   private static Log log = LogFactory.getLog(CreateContentServlet.class);
   
   @Override
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      log.info("doGet called");
      folder = request.getParameter("folder");   
      createContent(request, response, Attributes.PARENT_FOLDER, folder);            
   }
   

   @Override
   protected void service(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
      log.info("service called");
      super.service(req, resp);
   }
   
   private void createContent(HttpServletRequest request, HttpServletResponse response, String parent, String folder) throws IOException
   {
      
      // Start the session
      AuthenticationUtils.startSession(Attributes.USERNAME, Attributes.PASSWORD);         
      try {
         // Make your web service call in here
         createFolder(parent, folder, request, response);
      }
      catch (Throwable e) {
         log.error("createContent erorr: " + e.toString());
         e.printStackTrace();
      }
      finally {
         // End the session
         AuthenticationUtils.endSession();
      }    
    }
   
   protected static void createFolder(String parent, String folder, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
      final Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
      // The default workspace store is SpacesStore
       //Store storeReference = new Store(Constants.WORKSPACE_STORE, "SpacesStore");

       // We want to create a new folder or workspace called child in the parent folder under company home
       // Create parent reference to the parent space in company home
       //ParentReference parentReference = new ParentReference(STORE, null, "/app:company_home/cm:parent",
      ParentReference parentReference = new ParentReference(
                    STORE,
                    null,
                    "/app:company_home/cm:" + parent,
                    Constants.ASSOC_CONTAINS,
                    Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, "sample_folder"));
                      
       // Create the child folder
       NamedValue[] properties = new NamedValue[] { Utils.createNamedValue(Constants.PROP_NAME, folder) };
       CMLCreate create = new CMLCreate("1", parentReference, null, null, null, Constants.TYPE_FOLDER, properties);      
       CML cml = new CML();
       cml.setCreate(new CMLCreate[] { create });
       UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);

       // From the result of the web service call we want to retrieve the node reference for the child folder so we can access it later
       String childReference = results[0].getDestination().getUuid();
       log.info("Created a new workspace, node is: " + childReference);
       OutputStream out = response.getOutputStream();
        response.setContentType("text/html");
        out.write(childReference.getBytes());
    }
}


What I'd envisaged (naively) was that I could make two calls to my createContent method:
First to create the student folder:
URL:

http://localhost:8080/alfresco/app/addContent?folder=ArmitageShanks123456
'>
http://localhost:8080/alfresco/app/addContent?folder=ArmitageShanks123456

Method call:

createContent(request, response,"TopLevel", "ArmitageShanks123456")
Secondly to create a sub folder for say, email correspondence:
URL:

http://localhost:8080/alfresco/app/addContent?folder=EmailCorrespondence
'>
http://localhost:8080/alfresco/app/addContent?folder=EmailCorrespondence

Method call:

createContent(request, response, "ArmitageShanks123456", Attributes.EMAIL_FOLDER)

The error thrown when I attempt to create the sub-folder using the latter example is below:


09:22:45,312  ERROR [extensions.addContent.CreateContentServlet] createContent erorr:
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.generalException
faultSubcode:
faultString:
faultActor:
faultNode:
faultDetail:
   {http://www.alfresco.org/ws/service/repository/1.0}RepositoryFault:<ns1:errorCode>0</ns1:errorCode><ns1:message>java.lang.IllegalStateException: Failed to resolve to a single NodeRef with parameters (store=workspace:SpacesStore uuid=null path=/app:company_home/cm:ArmitageShanks123456), found 0 nodes.</ns1:message>
   {http://xml.apache.org/axis/}exceptionName:org.alfresco.repo.webservice.repository.RepositoryFault
   {http://xml.apache.org/axis/}stackTrace:
   at org.alfresco.repo.webservice.repository.RepositoryWebService.update(RepositoryWebService.java:335)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397)
   at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186)
   at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
   at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
   at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
   at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
   at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
   at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
   at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
   at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
   at java.lang.Thread.run(Thread.java:619)

   {http://xml.apache.org/axis/}hostname:AndrewRussell


   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
   at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
   at java.lang.Class.newInstance0(Class.java:355)
   at java.lang.Class.newInstance(Class.java:308)
   at org.apache.axis.encoding.ser.BeanDeserializer.<init>(BeanDeserializer.java:104)
   at org.apache.axis.encoding.ser.BeanDeserializer.<init>(BeanDeserializer.java:90)
   at org.alfresco.webservice.repository.RepositoryFault.getDeserializer(RepositoryFault.java:146)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.apache.axis.encoding.ser.BaseDeserializerFactory.getSpecialized(BaseDeserializerFactory.java:154)
   at org.apache.axis.encoding.ser.BaseDeserializerFactory.getDeserializerAs(BaseDeserializerFactory.java:84)
   at org.apache.axis.encoding.DeserializationContext.getDeserializer(DeserializationContext.java:464)
   at org.apache.axis.encoding.DeserializationContext.getDeserializerForType(DeserializationContext.java:547)
   at org.apache.axis.message.SOAPFaultDetailsBuilder.onStartChild(SOAPFaultDetailsBuilder.java:157)
   at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
   at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
   at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
   at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
   at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
   at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
   at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
   at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
   at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
   at org.apache.xerces.jaxp.SAXParserImpl.parse(Unknown Source)
   at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
   at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
   at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
   at org.apache.axis.handlers.soap.MustUnderstandChecker.invoke(MustUnderstandChecker.java:62)
   at org.apache.axis.client.AxisClient.invoke(AxisClient.java:206)
   at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
   at org.apache.axis.client.Call.invoke(Call.java:2767)
   at org.apache.axis.client.Call.invoke(Call.java:2443)
   at org.apache.axis.client.Call.invoke(Call.java:2366)
   at org.apache.axis.client.Call.invoke(Call.java:1812)
   at org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub.update(RepositoryServiceSoapBindingStub.java:986)
   at uk.ac.sheffield.cics.extensions.addContent.CreateContentServlet.createFolder(CreateContentServlet.java:87)
   at uk.ac.sheffield.cics.extensions.addContent.CreateContentServlet.createContent(CreateContentServlet.java:54)
   at uk.ac.sheffield.cics.extensions.addContent.CreateContentServlet.doGet(CreateContentServlet.java:36)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
   at uk.ac.sheffield.cics.extensions.addContent.CreateContentServlet.service(CreateContentServlet.java:44)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
   at java.lang.Thread.run(Thread.java:619)




Thanks for looking,

AndyR

Outcomes