cancel
Showing results for 
Search instead for 
Did you mean: 

Inherit metadata from space

buurd
Champ in-the-making
Champ in-the-making
Hi!

i'm trying to write a script that will make sure that all documents in a space will be populated with some common metadata from the space. But I can't get the code working (se code below). Am I using the alfresco api in the wrong way??

Thanks in advance
Roland


/**
* CopySpaceMetadata
*
* This script makes sure that a content "inherits" is containing space metadata.
* It will not overwrite already existing metadata about the content
*/

// Make sure that the content have the aspects the space has
for(var i in space.aspects){
    if(!document.hasAspect(i)){
        document.addAspect(i);
    }
}

// Set the properties from the space to the content, unless they already are set
for(var i in space.properties){
    if(document.property[i] == null){
        document.property[i] = space.properties[i];
    }
}
5 REPLIES 5

sbuckle
Champ in-the-making
Champ in-the-making
You are probably missing this at the end:


document.save();

sbuckle
Champ in-the-making
Champ in-the-making
Then again maybe it's a bug: http://forums.alfresco.com/en/viewtopic.php?f=4&t=11847

Take a look in 'alfresco.log' to see if you get the same error message.

buurd
Champ in-the-making
Champ in-the-making
Thanks for your answers!
It seems like there was an error with the 2.9 release.

But the problem is still there in enterprise 2.2. 😞

This works perfectly

for(var i in space.aspects){
    logger.log("Found aspect " + i);
}

This tells me that the aspect is invalid.

for(var i in space.aspects){
    logger.log("Found aspect " + i);
    if(!document.hasAspect(i)){
        logger.log("Adding aspect: " + i + " to document");
        document.addAspect(i);
    }
}

I also tried with

for(var i in space.aspects){
    logger.log("Found aspect " + i);
    if(!document.hasAspect(i)){
        logger.log("Adding aspect: " + i + " to document");
        document.addAspect(space.aspects[i]);
    }
}

When looking at the printout non of the aspects I have added to the space are listed. To me the listing looks more like a list of method then a list of aspects.
    16:23:31,257 DEBUG [repo.jscript.ScriptLogger] Found aspect notifyAll
    16:23:31,257 DEBUG [repo.jscript.ScriptLogger] Found aspect removeAll
    16:23:31,257 DEBUG [repo.jscript.ScriptLogger] Found aspect containsAll
    16:23:31,257 DEBUG [repo.jscript.ScriptLogger] Found aspect contains
    16:23:31,257 DEBUG [repo.jscript.ScriptLogger] Found aspect empty
    16:23:31,257 DEBUG [repo.jscript.ScriptLogger] Found aspect equals
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect notify
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect class
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect isEmpty
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect add
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect size
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect iterator
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect clear
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect wait
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect toString
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect retainAll
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect hashCode
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect toArray
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect addAll
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect clone
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect getClass
    16:23:31,272 DEBUG [repo.jscript.ScriptLogger] Found aspect remove
A simular listing of space.aspects seems to display the mapping between the javascript and java code.

Do I really have to hardcode the name of the aspects and properties to be able to use the javascript engine to inherit properties from spaces to content?

Thanks in advance
Roland

sbuckle
Champ in-the-making
Champ in-the-making
It is printing out the list of methods because space.aspects is an Array and the iteration method that you chose iterates over the properties of an object; in addition, that construct that you used (for .. in) will also display the properties of the prototype object; if you want to loop through the properties that only belong to the object you are interested in use the hasOwnProperty() method:


for (prop in object)
{
   if (object.hasOwnProperty(prop) {
      // do something
   }
}
If you want to iterate over the values in an array, use the more traditional iteration construct:


for (var i=0; i < space.aspects.length; i++) {
   logger.log(space.aspects[i]);
}

buurd
Champ in-the-making
Champ in-the-making
Hi!

Thanks for you answer. I found the answer while you wrote your (with a little help from a friend) 🙂

As you pointed out I was wrong in the assumption of that space.aspects was the aspects and not a container holding them. The documentation seems so clear now that I finally found out what it was trying tell me  Smiley Surprisedops:

The "final" code looks like this:

/**
* CopySpaceMetadata
*
* This script makes sure that a content "inherits" is containing space metadata.
* It will not overwrite already existing metadata about the content
*/

// Make sure that the content have the aspects the space has
logger.log("Script CopySpaceMetadata started");
var myArr = space.aspects.toArray();

// Copy the aspects from the space to the content
for(i in myArr){
    logger.log("Found aspect " + myArr[i]);
    if(!document.hasAspect(myArr[i])){
        logger.log("Adding aspect: " + myArr[i] + " to document");
        document.addAspect(myArr[i]);
    }
}

// Set the properties from the space to the content, unless they already are set
for(var i in space.properties){
    logger.log("Found property: " + space.properties[i]);
    if(document.properties[i] == null){
        document.properties[i] = space.properties[i];
    }
}

Thanks for your help
Best regards
Roland