cancel
Showing results for 
Search instead for 
Did you mean: 

Add ascpect dependent on subfolder level

d_franz
Champ on-the-rise
Champ on-the-rise
Hello,

I want to add a script to the root folder of a Site in Alfresco. This script should add a specific aspect to every new folder, dependent on its subfolder level.
This Aspects I want to use later to identify the subfolder level out of a other scripts when a file is added.
In the end I will show a Indicator if a specific file/aspect on a subfolder is missing.


My first try was the following code. But it doesn’t shows an effect.

if (document.isContainer)
{
   var level = document.partens.length;
   if(level == 0)
   {
      document.addAspect("marker:folderLevel_0");   
   }
   if(level == 1)
   {
      document.addAspect("marker:folderLevel_0");
   }
   if(level == 2)
   {
      document.addAspect("marker:folderLevel_0");
   }
}


Can anybody help me with a better code or even with a better strategy?

Thanks in advance
11 REPLIES 11

mitpatoliya
Star Collaborator
Star Collaborator
you need to refer to space.parent rather then document.parent
One more point this script should be invoked whenever new sub space is created
Also this aspects should be added via your custom content model.
And rather then as aspect you can use custom property.
when you invoke script on space creation
AND BEWARE
   for a script executing from a rule, the space object will be the space that the rule resides in, this may not be the space you are expecting if the rule is inherited. 

I can not get this working.
My last try was as follows:

if (document.isContainer)
{
   if(space.parent.parent.parent != null)
   {
      document.addAspect("marker:folderLevel_0");
   }
   if(space.parent.parent != null)
   {
      document.addAspect("marker:folderLevel_1");
   }
   if(space.parent != null)
   {
      document.addAspect("marker:folderLevel_2");
   }   
}

But for this every folder gets all aspects.

Using “space.parent.lenght” I allways get 1 and checking for “space.parent == null” never hits.

Is there a special command to check for an existing folder?

zladuric
Champ on-the-rise
Champ on-the-rise
document.parent or space.parent actually refers to a node, so you can't count the length.

Perhaps try something like:

function getDepth(node, level) {
  if (node == companyhome) return level;
  return getDepth(node.parent, level +1);
}

Then you can use something like this for your documents, for file upload scripts, movement operations and stuff like that.

document.addAspect('marker:folderLevel_' + getDepth(document, 0));


You could also make the (node == companyhome) parametrized, so you could search depth relative to some custom folder.
Ie add a third param to declaration:
function getDepth(node, level, rootNode)
, then use this as a comparison:
if (node == rootNode)…
then also you can call the script to find the depth relative to, in example, document library of a site, like this:
document.addAspect('marker:folderLevel_' + getDepth(document, 0, site.getContainer('documentLibrary')));



This approach (aspects) is not looking very safe, my intuition tells me it's really error prone. What is your goal? Why do you need those markers? Maybe there is a better way for it?

d_franz
Champ on-the-rise
Champ on-the-rise
Thank you for your fast reply!

I tried the script below. It is similar to your proposal. The script always runs until the limiting counter. It looks like I can not detect the root folder with "node.parent != space". I also tried "companyhome" and "userhome", but it does not work.

Why I need this markers.
For specific subfolder levels a minimum number of files have to use a specific type of aspect. If no file in such a folder has this aspect an indicator should appear on all higher folders. 


if (document.isContainer)
{   
   var i = 0;
   var fdepth = 0;
   var totalDepth = 0;
   totalDepth = lookForParent(document, i, fdepth);
   //name folder with max depth
   document.name = totalDepth;
}

function lookForParent(node, cnt, depth)
{
   //max depth for recursion
   if(cnt == 5)
   {
      return depth;
   }
   
   if(node.parent != userhome)
   {
      cnt++;
      depth++;
      node.addAspect("marker:folderLevel_1");
      return lookForParent(node.parent, cnt, depth);
   }   
   return depth;
}


Many thanks in advance!

d_franz
Champ on-the-rise
Champ on-the-rise
Stepping trough the parent folder now is no problem.

But I cannot detect the root folder of a site or the repository folder.

In the moment I have to stop the script with a counter. Is there a command to compare the current node with the root folder of a site?

Thanks!

zladuric
Champ on-the-rise
Champ on-the-rise
I believe that a simple = should do.

So I don't know what could be wrong, maybe try comparing nodeRefs instead?

 if (node.properties.nodeRef == site.getNode().properties.nodeRef) {
      // we've hit the site node
      // or userhome.properties.nodeRef, or companyhome.properties.nodeRef
}





zladuric
Champ on-the-rise
Champ on-the-rise
Btw, what do you call your function with?

d_franz
Champ on-the-rise
Champ on-the-rise
I tried to compare nodeRef but it is not working. I assume that’s because I am using it out of a recursive function. Now I'm searching for the name. Not the best solution, but it works.

My script are looking as follows:

if (document.isContainer)
{   
   var cnt = 0;
   if(checkSubFolderLevel(document, cnt) >= 3)
   {
      cnt = 0;
      addAspToParentFolder(document, cnt);
   }
}
else
{
   document.addAspect("ccm:marker_Aspect");
}


Recursice function “checkSubFolderLevel”:

function checkSubFolderLevel(node, cnt)
{
   //max depth for recursion
   if(cnt == 100)
   {
      return cnt;
   }
   
   //look for site folder
   if(node.name == "documentLibrary")
   {
      return cnt;
   }
   cnt++;
   return checkSubFolderLevel(node.parent, cnt);
}


Recusive function “addAspToParentFolder”:
<code>
function addAspToParentFolder(node, cnt)
{
   //max depth for recursion
   if(cnt == 100)
   {
      return cnt;
   }
      
   //look for site folder
   if(node.name == "documentLibrary")
   {
      return cnt;
   }
   
   cnt++;   pNode.addAspect("ccm:marker_aspect_I");
   pNode.addAspect("ccm:marker_aspect_II ");
   pNode.addAspect("ccm:marker_aspect_III ") ;
   
   return addAspToParentFolder(node.parent, cnt);   
}code>

d_franz
Champ on-the-rise
Champ on-the-rise
I am using this to show up indictors for a folder after creation. So I know that I have to fill in some specific files. Another script removes these aspects from the folders and files if it hast got the right content type.

[img]http://s7.postimg.org/swl68fddz/Test_Folder.jpg[/img]
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.