cancel
Showing results for 
Search instead for 
Did you mean: 

Dashes in space names

grantk
Champ in-the-making
Champ in-the-making
Hello,

I am fairly new with Alfresco so forgive me if I do not explain everything properly. I am just using a test installation to see if Alfresco will be what is right for what I need. I am running Alfresco 3.4B on glassfish 3.0 . I am just trying to create spaces and documents right now. I am running into issues with dashes in space names though.

For example:

I create a space named "Testing Space"
Inside "Testing Space" I create "inside-test"

I was looking at the examples located here:
http://wiki.alfresco.com/wiki/IngresTutorial_Alfresco_Web_Service_API_for_Java

So my understanding goes if I am creating a space with the "createSpace" methods provided it will try to get a reference to that space, if that fails it will then assume the space has not been created and create that space.

All works well the first time around, I can create the spaces and the program exits fine. The second time the program is run it tries to create the "inside-test" space again, although you can browse the spaces on the web interface and see that they are there.  Is there a different method available for handling dashes in space names? I am getting a duplicate child name not allowed error

Here is the code I am using.

Actual space creation:

Reference baseFolder = createSpace("Testing Space");
Reference insideFolder = createSpace(baseFolder, "inside-test");


   protected static Reference createSpace(String spacename) throws Exception
{
      Reference space = null;

      // Create the space if it is not already existent
      try {

         //Therefore a reference to the maybe not existent space is required

         System.out.println("Entering space " + spacename);

         space = new Reference(STORE, null, getCompanyHome().getPath() + "/cm:" + normilizeNodeName(spacename));
         getRepositoryService().get(new Predicate(new Reference[]{space}, STORE, null));
      }
      catch (Exception e1)
      {
         System.out.println("The space named " + spacename + " does not exist. Creating it.");

         ParentReference companyHome = getCompanyHome();

         // Set Company Home as the parent space
         companyHome.setChildName(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, normilizeNodeName(spacename)));

         //Set the space's property name
         NamedValue[] properties = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME,spacename)};

         // Create the space using CML (Content Manipulation Language)
         CMLCreate create = new CMLCreate("1", companyHome, null, null, null, Constants.TYPE_FOLDER, properties);
                       CML cml = new CML();
                       cml.setCreate(new CMLCreate[]{create});

                    //Execute the CML create statement
                     try {
            getRepositoryService().update(cml);
         } catch (Exception e2) {

            System.err.println("Can not create the space.");
            throw e2;
         }

      }


   return space;
}

    protected static Reference createSpace(Reference parentref, String spacename) throws Exception
{

      Reference space = null;
      ParentReference parent = ReferenceToParent(parentref);



      try {

         System.out.println("Entering space " + spacename);
         space = new Reference(STORE, null, parent.getPath() + "/cm:" + normilizeNodeName(spacename));
         WebServiceFactory.getRepositoryService().get(new Predicate(new Reference[]{space}, STORE, null));
                        System.out.println("What the hell");
      }
      catch (Exception e1)
      {
         System.out.println("The space named " + spacename + " does not exist. Creating it.");

         parent.setChildName(Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, normilizeNodeName(spacename)));

         //Set the space's property name
         NamedValue[] properties = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, spacename)};


         // Create the space using CML (Content Manipulation Language)
         CMLCreate create = new CMLCreate("1", parent, null, null, null, Constants.TYPE_FOLDER, properties);
                        CML cml = new CML();
                        cml.setCreate(new CMLCreate[]{create});

                       //Execute the CML create statement
                       try {
            getRepositoryService().update(cml);
         } catch (Exception e2) {

            System.err.println("Can not create the space.");
            throw e2;
         }


      }


   return space;


}

    protected static ParentReference ReferenceToParent(Reference spaceref)
{
      ParentReference parent = new ParentReference();

      parent.setStore(STORE);
      parent.setPath(spaceref.getPath());
      parent.setUuid(spaceref.getUuid());
      parent.setAssociationType(Constants.ASSOC_CONTAINS);

      return parent;
}

//Blanks are allowed in space names but not in paths. Because the path should depend on the name we need a version of the name which contains no blanks

protected static String normilizeNodeName(String name)
{
      return name.replace(" ","_");
}

protected static RepositoryServiceSoapBindingStub getRepositoryService()
{
      return WebServiceFactory.getRepositoryService();
}

protected static ContentServiceSoapBindingStub getContentService()
{
      return WebServiceFactory.getContentService();
}

protected static ParentReference getCompanyHome()
{
        ParentReference companyHomeParent = new ParentReference(STORE, null, "/app:company_home", Constants.ASSOC_CONTAINS, null);
        return companyHomeParent;
}
4 REPLIES 4

jcustovic
Champ in-the-making
Champ in-the-making
When you create space use normaln name like u would. Use ISO9075 when creating path reference.

String spaceName = "Testing Space";
Reference baseFolder = createSpace(spaceName);

Also when you check if space exists you are missing the following code (you have to set returned value as your space):
Node[] nodeArray = getRepositoryService().get(new Predicate(new Reference[] { space }, WORKSPACE_STORE, null));
space = nodeArray[0].getReference();

Same goes when the space was created:
UpdateResult[] results = getRepositoryService().update(cml);
space = results[0].getDestination();

EDIT: Only use ISO9075 when creating path to space
space = new Reference(STORE, null, getCompanyHome().getPath() + "/cm:" + ISO9075.encode(spacename));
When you create a space name do not use ISO9075!

grantk
Champ in-the-making
Champ in-the-making
jcustovic,

Thank you for the reply. I got yanked of the Alfresco work right after I asked that last question so sorry for the delayed response.  I have tried using the ISO9075.encode() method with success, but is there a way to avoid my space names being made non user friendly? I was hoping one of the benefits of Alfresco was that at some point I could send users to the web interfaces. But if they have to go to a space they know as Test Space, but it is actually listed as Test_x00200_Space that will not help. I saw there is a ISO9075.decode() but I am not really looking to have to convert each string passed to make it non readable, then convert it back to make it readable again.

Am I wrong here in thinking this is a problem? I am not sure it is for anyone else but every bit of work done here involves a dash in a string.

Thank you.

Grant

jcustovic
Champ in-the-making
Champ in-the-making
WRONG: When you are working with web service api you have to use encode() when you create space and decode() when you read space names thru your java code. When you encode and create space on alfresco using alfresco web api users (alfresco web ui) will normally see dashes, not the encoded part (thats just for XML transportation). ISO 9075 is just used for encoding XML element names.

EDIT: Only use ISO9075 when creating path to space
space = new Reference(STORE, null, getCompanyHome().getPath() + "/cm:" + ISO9075.encode(spacename));
When you create a space name do not use ISO9075!

grantk
Champ in-the-making
Champ in-the-making
jcustovic :

Thanks again, I guess I was encoding wrong, I will look at it again in the future, I only got two hours of Alfresco yesterday before I was directed elsewhere again Smiley Sad