create folder with document.properties and push files into this folder

cancel
Showing results for 
Search instead for 
Did you mean: 
castelsarrasin
Active Member II

create folder with document.properties and push files into this folder

Jump to solution

 Hi !

I have Alfresco community 5.2f

 

How can i create folder containing the name of some properties and push the files who get the same properties value in this new folder ?

Exemple :

File 1 have properties-test= XXX

File 2 have properties-test= XXX

then create folder "XXX" 

then push those files in "XXX" folder

 

Tell me what you need to know to help me, i'll answer.

2 Solutions

Accepted Solutions
roberto_gamiz
Established Member II

Re: create folder with document.properties and push files into this folder

Jump to solution

Hello,

You are receiving that null pointer error because you are passing a null value to the function childByNamePath.

I think this happens beacause the rule is executed for the subfolders that are created in the process, i'm not sure that the condition with null in the rule definition work properly in this case.

Try to add a condition in the rule definition for only execute the rule for documents and add the null comparation in the script code. 

Regards

View solution in original post

castelsarrasin
Active Member II

Re: create folder with document.properties and push files into this folder

Jump to solution

It work !!!

Thank you !!!!

I just add in the rule the condition as you expected :

If in the content "BDC-SEDIT-propriete" have "XXX" (XXX = number of the metadata)

The good java command for people who needed the same as me :

var Descr = document.properties["alah:BDC-SEDIT-propriete"];
var parent = document.parent;
var myfolder = parent.childByNamePath(Descr);
if (myfolder === null) {
    myfolder = parent.createFolder(Descr);
}
document.move(myfolder);

 

To finally add some more features ine the folder's name with more metadata it look like this 

var Descr = document.properties["test:BDC-SEDIT-propriete"];
var libelle =  document.properties["test:BDC-SEDIT-ObjetCommande"];
var tiers =  document.properties["test:BDC-SEDIT-LibelleTiers"];
var parent = document.parent;
var myfolder = parent.childByNamePath(Descr + "_" + tiers + "_" + libelle);
if (myfolder === null) {
    myfolder = parent.createFolder(Descr + "_" + tiers + "_" + libelle);
}
document.move(myfolder);

With this, the folder name look like this : "BDC-SEDIT-propriete_BDC-SEDIT-ObjetCommande_BDC-SEDIT-LibelleTiers"

 

Roberto you ownz ! if you send me private message with your postal adress i'll send you french wine ! Promise !

View solution in original post

11 Replies
Moderator
Moderator

Re: create folder with document.properties and push files into this folder

Jump to solution

Implement a folder rule or Implement a behavior which can listen to onCreateNode event and moves the files by creating the folder based on available metadata. With bevahior implementation, you get more control over the operation.

When the file will be uploaded, this behavior will trigger as soon as onCreateNode event is issues and does the required job. Your logic will go into onCreateNode method which you would implement for NodeServicePolicies.OnCreateNodePolicy

/* (non-Javadoc)
	 * @see org.alfresco.repo.node.NodeServicePolicies.OnCreateNodePolicy#onCreateNode(org.alfresco.service.cmr.repository.ChildAssociationRef)
	 */
	@Override
	public void onCreateNode(final ChildAssociationRef childAssocNode) {
		//Your logic goes here.
			}
		}
	}

 

Refer this documentation on creating the behavior: 

https://ecmarchitect.com/alfresco-developer-series-tutorials/behaviors/tutorial/tutorial.html 

https://docs.alfresco.com/5.2/references/dev-extension-points-behaviors.html

 

castelsarrasin
Active Member II

Re: create folder with document.properties and push files into this folder

Jump to solution

Thank you abhinavmishra14 

var Descr = document.properties["test:BDC-SEDIT-propriete"];
var myfolder = userhome.createFolder(Descr);

Caused by: org.alfresco.service.cmr.model.FileExistsException: 10194109 Le fichier ou dossier 19005114 existe déjà

castelsarrasin
Active Member II

Re: create folder with document.properties and push files into this folder

Jump to solution

The same folder, not the same file sorry

roberto_gamiz
Established Member II

Re: create folder with document.properties and push files into this folder

Jump to solution

Hello,

It seems that you are looking for something like that:

var Descr = document.properties["test:BDC-SEDIT-propriete"];
var myfolder = userhome.childByNamePath(Descr);
if (myfolder === null) {
    myfolder = userhome.createFolder(Descr);
}
document.move(myfolder);

Regards,

 

castelsarrasin
Active Member II

Re: create folder with document.properties and push files into this folder

Jump to solution

Thank you very much !

It nearly work as i want to.

 

Except that it move the folder in my share/user folder.

This script is set for a Site.

var Descr = document.properties["test:BDC-SEDIT-propriete"];
var myfolder = userhome.childByNamePath(Descr);
if (myfolder === null) {
    myfolder = userhome.createFolder(Descr);
}
document.move(myfolder);

I just have to change the red command to set it on the same folder.

Explaination :

I have the files with that metadata on share site folder alfresco\share\page\Sites\BDC\BC_INFO\

I need that the script create the folder in this node :

alfresco\share\page\Sites\BDC\BC_INFO\"Descr"

 

Again, thank you very much for you help roberto_gamiz 

 

roberto_gamiz
Established Member II

Re: create folder with document.properties and push files into this folder

Jump to solution

Hello,

To execute the process in the same folder just change the line

var myfolder = userhome.childByNamePath(Descr);

for

var parent = document.parent;
var myfolder = parent .childByNamePath(Descr);

Regards

castelsarrasin
Active Member II

Re: create folder with document.properties and push files into this folder

Jump to solution

Thank you again to take care about me !

 

Here is the code for now :

var Descr = document.properties["test:BDC-SEDIT-propriete"];
var parent = document.parent;
var myfolder = parent.childByNamePath(Descr);
if (myfolder === null) {
    myfolder = parent.createFolder(Descr);
}
document.move(myfolder);

But script failed 

 

Caused by: java.lang.NullPointerException: Illegal null path

I didn't paste all the javalog to not spam, but i can if you need all logs

roberto_gamiz
Established Member II

Re: create folder with document.properties and push files into this folder

Jump to solution

I think you need to add some integrity checks to your code.

For example before start the process you need to ensure that the property "test:BDC-SEDIT-propriete" have a value not equals to null and that the code is not executed for a content of folder type.

Regards.

 

castelsarrasin
Active Member II

Re: create folder with document.properties and push files into this folder

Jump to solution

Hi,

 

Thanks for you reply.

I put only files who contain "test:BDC-SEDIT-propriete" value.

And i still have the same error.

I add in the alfresco's rules to not execute if the "test:BDC-SEDIT-propriete" = null

I still have the same error.

When i use the script with this code value

var Descr = document.properties["test:BDC-SEDIT-propriete"];
var parent = document.parent;
var myfolder = parent.childByNamePath(Descr);
if (myfolder === null) {
myfolder = userhome.createFolder(Descr);
}
document.move(myfolder);

It work and move folders/files correctly on my user's folder.

We are so close to adjust it, do you think that "document.parent" is not the good command ?

var parent = document.parent;
var myfolder = parent.childByNamePath(Descr);

I just suppose because i'm not expert at all.

 

By advance thank you again !