Page 1 of 3

Using Beanshell to move Mails

PostPosted:Fri Sep 02, 2011 12:22 pm
by Rudolf21
Hi everyone!

I want to write a Beanshell script that moves incoming Mails (importet from Outlook) into a Taxonomy-folder.
I've searched through several Topics, tried many things out, but nothing works :?

Webservices, SOAP, Beanshell - all this things are new for me. Wikipedia and co. helped me a lot, but I would be very thankful if someone can give me a hint, how i can get this skript running.
Code: Select all
import javax.jcr.*;
import com.openkm.core.*;
import com.openkm.bean.*;
import com.openkm.module.base.*;

String token = JcrSessionManager.getInstance().getSystemToken();
Session session = JcrSessionManager.getInstance().get(token);
Node mail = session.getRootNode().getNode(Repository.MAIL);

eventNode.move(token, mail.getPath(), "okm:root");
The script is set in the okm:mail Folder in the Repository...

Thanks for the support!

Kind regards
Fabian

Re: Using Beanshell to move Mails

PostPosted:Mon Sep 05, 2011 8:49 am
by pavila
According to this script, you are moving the "okm:mail" folder to "okm:root" which is a very bad idea because will make your OpenKM installation unusable. You need to move the email in concrete, not the whole folder. For that, take a look at http://wiki.openkm.com/index.php/Scripting, in concrete "Variable substitution" section.

Re: Using Beanshell to move Mails

PostPosted:Tue Sep 06, 2011 1:04 pm
by Rudolf21
Thanks for the Warning, I didn't notice that :/
Now my OpenKM still works and the script is already finished:
Code: Select all
import javax.jcr.*;
import com.openkm.core.*;
import com.openkm.bean.*;
import com.openkm.module.*;

String token = JcrSessionManager.getInstance().getSystemToken();
Session session = JcrSessionManager.getInstance().get(token);

Node okmmail = session.getRootNode().getNode(Repository.MAIL);

for (NodeIterator it = okmmail.getNodes(); it.hasNext(); ) {
    Node userMailFolder = it.nextNode();
	
      if(userMailFolder.getPath().equals("/okm:mail/Docs") ) {
		//Check the mails in the "Docs" Folder
		   for (NodeIterator itut = userMailFolder.getNodes(); itut.hasNext(); ) {
           Node mail = itut.nextNode();
	           mail.move(token, mail.getPath(), "/okm:root/Mails");
		}	
	}	
	userMailFolder.save();
	}
This runs as a Cronjob...but there is a problem:
The Script should check a specific Mail folder ("Docs") and move all the mails in another folder ("okm:root/Mails") in the Taxonomy.
But the method move doesn't work. The error said that this method is not available for this class. I've searched in the documentation for another matching method, but found nothing. Is there a way to convert the node ("mail") object into a Mail-object? Then i could use this methods:
http://doxygen.openkm.com/5.1.x/d9/d79/ ... b6a4c14ec5

I've learning and learning... :D
Thanks for your help!

Greetz

Re: Using Beanshell to move Mails

PostPosted:Wed Sep 07, 2011 5:07 pm
by pavila
No that mail is an instance of javax.jcr.Node. You need to check Jackrabbit API to see the methods. For now, you can use the session.move() method.

Re: Using Beanshell to move Mails

PostPosted:Mon Sep 12, 2011 11:19 am
by Rudolf21
:Edit

Script is working now :)
It looks for some keywords in the subject of the mails and put they in different folders in the taxonomy.
I can post the code if someone wish that...

Re: Using Beanshell to move Mails

PostPosted:Wed Sep 14, 2011 2:39 pm
by pavila
Yes, please. Post it and will be included in the documentation wiki for future references.

Re: Using Beanshell to move Mails

PostPosted:Fri Sep 16, 2011 12:10 pm
by Rudolf21
Here we go:
(I changed the variable a bit, so it's more general...)
Code: Select all
import javax.jcr.*;
import com.openkm.core.*;
import com.openkm.bean.*;
import com.openkm.module.*;

String token = JcrSessionManager.getInstance().getSystemToken();
Session session = JcrSessionManager.getInstance().get(token);

Node okmmail = session.getRootNode().getNode(Repository.MAIL);
Node okmroot = session.getRootNode().getNode(Repository.ROOT);

for (NodeIterator it = okmmail.getNodes(); it.hasNext(); ) {
    Node userMailFolder = it.nextNode();
	
	if(userMailFolder.getPath().equals("/okm:mail/Docs") ) {
		
		//Check the mail in the "Docs" folder
		for (NodeIterator itut = userMailFolder.getNodes(); itut.hasNext(); ) {
        Node mail = itut.nextNode();
		String name = mail.getName();
		//Check the keyword
		int available = name.indexOf("al");
			if(available != -1){
				session.move(mail.getPath(), okmroot.getPath() + "/alFolder/" + mail.getName());
				session.save();
			}
		available = name.indexOf("ka");
			if(available != -1){
				session.move(mail.getPath(), okmroot.getPath() + "/kaFolder/" + mail.getName());
				session.save();
			}
		}
	userMailFolder.save();
	}
}
Again, thanks for the support, you helped me a lot!
Have a nice day!

Re: Using Beanshell to move Mails

PostPosted:Mon Sep 19, 2011 4:23 pm
by pavila

Re: Using Beanshell to move Mails

PostPosted:Mon Nov 07, 2011 12:51 pm
by Rudolf21
Hi!

The Script works well, but now i want to add an additional feature:
After the Mail is moved, the attachment should automatically be detached (in the folder where the mail is).
Is there any possibilty to do that? I haven't found any suitable method yet...

Thx & Greetz

Re: Using Beanshell to move Mails

PostPosted:Wed Nov 09, 2011 9:35 am
by jllort
Yes, simply navigate across repository ( administration ) and take a look at mail structure ( some mail node with attachment ). You simply must copy or move document node under mail node structure to your folder path. There're a method on mail that gives you the attachments list.

Re: Using Beanshell to move Mails

PostPosted:Mon Nov 14, 2011 10:31 am
by Rudolf21
I've found an attachment method:
List<Document> com.openkm.bean.Mail.getAttachments();

But when I try to implement it in my Code:
Code: Select all
Node mail = itut.nextNode();
		List <Document> attach = mail.getAttachments();
		for(Document : attach){
		print(Document);
		} 
It wont work.
The Error Mail from the cronjob doesn't help me in this case. Its just :
Parse error at line 21, column 40. Encountered: =
Where is my error in reasoning?

Re: Using Beanshell to move Mails

PostPosted:Tue Nov 15, 2011 12:26 pm
by jllort
Beanshell don't likes generic:

Try with:
Code: Select all
List attach = mail.getAttachments();

Re: Using Beanshell to move Mails

PostPosted:Fri Nov 18, 2011 1:10 pm
by Rudolf21
I've tried this one too, also some things with BaseMailModule.copy...but nothings seems to work :(
The Error is now:
Code: Select all
Sourced file: inline evaluation of: ``import javax.jcr.*; import com.openkm.core.*; import com.openkm.bean.*; import c . . . '' : Typed variable declaration : Error in method invocation: No args method getAttachments() not found in class'org.apache.jackrabbit.core.NodeImpl' : at Line: 22 : in file: inline evaluation of: ``import javax.jcr.*; import com.openkm.core.*; import com.openkm.bean.*; import c . . . '' : mail .getAttachments ( ) 
When I test my node like:
Code: Select all
if (mail.isNodeType(Mail.TYPE)) {
print("success");
}
It works...so the Node "mail" should be the right type...
Why then is mail.getAttachment() not found?

Re: Using Beanshell to move Mails

PostPosted:Sun Nov 20, 2011 9:13 pm
by jllort
The problem is that you're working with Node and you might working with Mail objects you should use something like this:
Code: Select all
OKMMail.getInstance().getProperties(null, mailPath)
or
Code: Select all
BaseMailModule.getProperties(session, mailNode)
To get a return Mail object

Re: Using Beanshell to move Mails

PostPosted:Mon Nov 21, 2011 9:46 am
by Rudolf21
Thanks a lot!
I've importet openkm.api.* and added the OKMMail.getInstance...Method and now it works! :D

But there is one last question:
I want to move the mail attachment in a taxonomy folder:
Code: Select all
		List attach = actMail.getAttachments();
		for(Document:attach){
		DocumentModule.move(token, Document.getPath(), okmroot.getPath() + "/Anleitungen/");}
In the bean.Document Class there isn't a move method (http://doxygen.openkm.com/5.1.x/de/d62/ ... ument.html). So i guess "DocumentModule" is a good solution...

But when the cronjob starts the script, an error message is given:
Code: Select all
Sourced file: inline evaluation of: ``import javax.jcr.*; import com.openkm.core.*; import com.openkm.bean.*; import c . . . '' : Error in method invocation: Static method move( java.lang.String, java.lang.String, java.lang.String ) not found in class'com.openkm.module.DocumentModule' : at Line: 27 : in file: inline evaluation of: ``import javax.jcr.*; import com.openkm.core.*; import com.openkm.bean.*; import c . . . '' : DocumentModule .move ( token , Document .getPath ( ) , okmroot .getPath ( ) + "/DemoFolder/" ) 
Why is the move method in my code static? :?

Thanks again for the support!