• Using Beanshell to move Mails

  • Do you want to create a native client or integrate with third party applications: webservices are the solution.
Do you want to create a native client or integrate with third party applications: webservices are the solution.
Forum rules: Please, before asking something see the documentation wiki or use the search feature of the forum. And remember we don't have a crystal ball or mental readers, so if you post about an issue tell us which OpenKM are you using and also the browser and operating system version. For more info read How to Report Bugs Effectively.
 #12037  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
 #12051  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.
 #12069  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
 #12084  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.
 #12121  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...
 #12145  by pavila
 
Yes, please. Post it and will be included in the documentation wiki for future references.
 #12162  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!
 #12839  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
 #12843  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.
 #12887  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?
 #12968  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?
 #12981  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
 #12989  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!

About Us

OpenKM is part of the management software. A management software is a program that facilitates the accomplishment of administrative tasks. OpenKM is a document management system that allows you to manage business content and workflow in a more efficient way. Document managers guarantee data protection by establishing information security for business content.