script to seach in a specific space

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

script to seach in a specific space

I'm makin a script to get all the objects in a specific space (called "Aprobados") where the created or last modified date is today. And I also want to get all metadata from the searchrd objects.

Can anyone help me please?

Let me explain you the process I need to execute

1) Search for all the pdf documents of a given space called "Aprobados" which have been modified "today".
2) I need to create a txt file with all the metadata of all of those modified pdf documents stored in this space that matched this search criteria.
3) Place the txt file in a given space so another process can get it by FTP

This is the code I have written, however I am not very sure of the search funcion, as I need to search in the "Aprobados" space but also inside its child nodes recursively.

Could you please take a look to my code and let me know if you think it is ok?

I will really appreciate it.

Cheers!!


// We create the folder to place the txt file
var subir = space.childByNamePath("Subir Facturas");
var recoger = subir.childByNamePath("Recoger");


// If there is already a txt file we must delete it
for each (var doc in recoger.children) {
   if (doc.mimetype == "text/plain");
      doc.remove();
}


// Get the path to the parent space where to look for the files
var revision = space.childByNamePath("Revisar");
var aprobados = revision.childByNamePath("Aprobadas");
var today = new Date();
var ano = today.getFullYear();
var buscarAno = aprobados.childByNamePath(ano);


// Now we firstly create the txt file
//String fecha = today.getDate() + "-" + today.getMonth() +1 + "-" + today.getFullYear();
file = recoger.createFile("metadatos_facturas.txt");

// We run the search for all the pdf files that have been modified today in the
// space of the current year
var docs = search.xpathSearch([cm:modified]=today,buscarAno);

// For each doc found matching the search clause we must write their metadata in the
// txt file - one row with metadata per file found

for each (docs) {

   if (docs!=null && docs.mimetype == "application/pdf")
   {
      workingcopy=file.checkout();
      workingcopy.content = "|"+docs.name+"|"+docs.start_date+"|"+docs.Private_Id+"|=";
      file = workingcopy.checkin();
   }
}
3 Replies
Highlighted
Member II

Re: script to seach in a specific space

Hi cperez,

The best way to do this is to use lucene or xpath search, because it allows you to search recursively in the child nodes.

It should be something like this using lucene search:


  var docs = search.luceneSearch("PATH:'app:company_home/cm:Revisar/cm:Aprobados//.' AND  @cm\\:modified:[NOW TO MAX]");


Here you have more information about range queries:

http://wiki.alfresco.com/wiki/Search#Range_Queries
http://forums.alfresco.com/forum/developer-discussions/web-scripts/lucene-search-date-range-created-...
https://forums.alfresco.com/forum/developer-discussions/repository-services/attributes-lucene-date-r...
Highlighted
Member II

Re: script to seach in a specific space

Hi cybermakoki,

I got.
I changed the search and I use the luceneSearch like you said.
I use the dinamic path from a specific folder instead of a static path, the range date search to get all the documents today, and I also search only pdf documents.

This is the code that I use:


var sdate=today.getFullYear()+"\-"+(today.getMonth()+1)+"\-"+today.getDate() + "T00:00:00";
var tdate=today.getFullYear()+"\-"+(today.getMonth()+1)+"\-"+(today.getDate()+1) + "T00:00:00";
var spath="'+PATH:\""+aprobados.displayPath +"//*\" + ";
var scontent="@\\{http\\://www.alfresco.org/model/content/1.0\\}content.mimetype:application/pdf + ";
var qdate="@cm\\:modified:[" + sdate + " TO " + tdate +"]'";
var strq=spath+scontent+qdate;

// Do the search and save the vales
var nodes=search.luceneSearch(strq);



thank you so much.
Highlighted
Member II

Re: script to seach in a specific space

That's perfect!

I'm glad I helped you Smiley Happy