• 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.
 #13095  by Rudolf21
 
Hi again
Thanks for the Hint!

But with this class:
Code: Select all
import com.openkm.module.direct.*;
...
DirectDocumentModule.move(token, Document.getPath(), okmroot.getPath() + "/Manuals/");
...
I get the same error:
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.direct.DirectDocumentModule' : at Line: 28 : in file: inline evaluation of: ``import javax.jcr.*; import com.openkm.core.*; import com.openkm.bean.*; import c . . . '' : DirectDocumentModule .move ( token , Document .getPath ( ) , okmroot .getPath ( ) + "/Manuals/" )
I guess static is the problem. But even if I remove the " + "/Manuals/" " (the only static thing in the parameter), it still gives me the same error message...

I created a testskript just for this document move puzzle:
Code: Select all
import javax.jcr.*;
import com.openkm.core.*;
import com.openkm.bean.*;
import com.openkm.module.*;
import com.openkm.module.direct.*;
import com.openkm.api.*;

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);

DirectDocumentModule.move(token, "/okm:mail/Docs", okmroot.getPath() + "/Anleitungen/");
(Pdf Document is on "/okm:mail/Docs")
This give me exactly the same error...

I'm at the end... :roll:
Have you another Idea how I can get my script running correctly?

Greetings and nice weekend!
 #13101  by jllort
 
It's not a static method
Code: Select all
public void move(String token, String docPath, String dstPath) throws PathNotFoundException,
			ItemExistsException, AccessDeniedException, RepositoryException, DatabaseException,
			ExtensionException {
You'll need something like
Code: Select all
new DirectDocumentModule().move( token, docPath, dstPath)
 #14587  by Rudolf21
 
Hi!

I've finished the Beanshell script (some time ago) - but have one question left.

First, here is the script:
Code: Select all
import javax.jcr.*;
import java.lang.String;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import com.openkm.core.*;
import com.openkm.bean.*;
import com.openkm.module.*;
import com.openkm.module.direct.*;
import com.openkm.api.*;

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);

String DirectoryPath;
String DocumentName;
String DocumentDate;
Boolean DateOk;
DateFormat dateFormat;
Date date;
Boolean done = false;

for (NodeIterator it = okmmail.getNodes(); it.hasNext(); ) {
    Node userMailFolder = it.nextNode();
	
	if(userMailFolder.getPath().equals("/okm:mail/Docs") ) {
		
		//Check the mails in the doc Folder
		for (NodeIterator itut = userMailFolder.getNodes(); itut.hasNext(); ) {
		//aktuelles Mail aufbereiten
        Node mail = itut.nextNode();
		Mail actMail = OKMMail.getInstance().getProperties(null, mail.getPath());
		List attach = actMail.getAttachments();
		
		//get the subject from the mail
		String mailSubject = actMail.getSubject();

		//get the keyword from the subject
		String Keyword = mailSubject.replaceAll(" ", "");
		Keyword = Keyword.replaceAll("[0-9-,.]", ""); 
		Keyword = Keyword.toLowerCase();

		//Path to the right TaxDirectory and Document name
		if(Keyword.equals("pro")){
			DirectoryPath = "/Calculations/";
			DocumentName = "Prodega";
		}
		else if(Keyword.equals("oth")){
			DirectoryPath = "/Others/";
			DocumentName = "Prodega";
		}
		
		//check if date is typed in. Else date is today
		if(mailSubject.matches(".*[0-9-].*")){
			DateOk = true;	
			//create string just with the date
			DocumentDate = mailSubject.replaceAll(" ", "");
			DocumentDate = DocumentDate.replaceAll("[aA-zZ,+-]", "");
			DocumentName = DocumentName + " " + DocumentDate;
		}else{
			DateOk = false;
			//get ready for date today
			dateFormat = new SimpleDateFormat("yyyyMMdd");
			date = new Date();
		}	
			//Move the attachments to the right TaxDirectory and rename it
			for(Document:attach){
					new DirectDocumentModule().copy(token, Document.getPath(), okmroot.getPath() + DirectoryPath);
					if(DateOk == true){
						new DirectDocumentModule().rename(token, okmroot.getPath() + DirectoryPath + "Untitled .pdf", DocumentName);
						done = true;
					}else{
						new DirectDocumentModule().rename(token, okmroot.getPath() + DirectoryPath + "Untitled .pdf", DocumentName +" "+ dateFormat.format(date));
						done = true;
					}
				}
			//Delete mail, if attachment movement is successful.
			if(done == true){
				new DirectDocumentModule().delete(token, actMail.getPath());
				print("Verschieben nach " + DirectoryPath +" erfolgreich. Mail wurde gelöscht");
			}
			session.save();
		}		
	userMailFolder.save();
	}
}
How does it works?
Here is an example...

Let's say you have following environment:
Taxonomy:
- Manuals
- Calculations
- Others

Mail:
- Docs (Which checks the given mail-inbox every 5 minutes an import the mails).

Now, the script automatically checks the subject of the mails for keywords, move the attachments in the right taxonomy-folder and rename it (with date if necessary).

The Keywords are set in the script.
All you need to do is scan in the document with the right keyword. After, it will be imported to the Docs-mailfolder. Then, the script checks the subject with the keyword and move the attachment in the right folder incl. renaming the file.

Input - without date:
(Mail with .pdf-attachment)
<to>openkm@domain.ch</to>
<subject>pro</subject>

Result:
PDF Document in Calculations --> Prodega 20120319 (Date today).

Input - with date:
(Mail with .pdf-attachment)
<to>openkm@domain.ch</to>
<subject>pro 20120101</subject>

Result:
PDF Document in Calculations --> Prodega 20120101.

I hope my explanation is clearly. Else, feel free to ask :wink:

My problem is the cronjob.
When I start the script manually (with the execute-button), everything works fine.
But when the script starts from the cronjob, there is a error-message:
Sourced file: inline evaluation of: ``import javax.jcr.*; import java.lang.String; import java.text.DateFormat; import . . . '' : Typed variable declaration : target exception : at Line: 34 : in file: inline evaluation of: ``import javax.jcr.*; import java.lang.String; import java.text.DateFormat; import . . . '' : .getProperties ( null , mail .getPath ( ) ) Target exception: com.openkm.core.RepositoryException: CallbackHandler does not support: javax.security.auth.callback.NameCallback@1e311e31: CallbackHandler does not support: javax.security.auth.callback.NameCallback@1e311e31
Can anyone give me a hint how to handle this problem? Is there a problem with the security?
Thank for the support!

Kind regards!
 #14883  by pavila
 
This is because the null token passed as first argument. If the script is not executer by a authenticated user, you have a problem. In this case, is better to get the system user token and use it:
Code: Select all
String sysToken = JcrSessionManager.getInstance().getSystemToken()
 #15257  by Rudolf21
 
I already use this token:
Code: Select all
...
import com.openkm.api.*;

String token = JcrSessionManager.getInstance().getSystemToken();
Session session = JcrSessionManager.getInstance().get(token);
...
Is the line on the wrong place?

Greetings!
 #15279  by jllort
 
pavila tells to you if you are executing from administration, in this case really it is not executed as a logged user. Some tests should be done in a openkm folder node.
 #15293  by Rudolf21
 
When I try to set a script in the mail folder node (as a user with UserRole and AdminRole), the message "Sorry, only for admin user" appears.
With the okmAdmin user this works fine. So I guess the problem are the rights...
Is there a way so that the crontab automatically will be executed with AdminRole rights?
 #15318  by jllort
 
in which method appears this error, because this kind of message only should appearing if you are using administrators methods
 #15333  by Rudolf21
 
This error appears, when the cronjob starts automatically (via cronjob-expression).
OpenKM sends me a Email with the error message.
If I start the cronjob manually (via excecute-button), everything works fine...

Where exactly is the cronjob stored on the server?
In /etc/cron.d, daily, weekly etc., I saw nothing...
 #15344  by jllort
 
Crontab is an application task is not stored in server.

I continue thinking you are executing some method that is only available to okmAdmin and you should use other. When you execute manually crontab probably you are using okmAdmin user but when is executed in batch is using other system user and here I think could be the problem. Could you indicate which kind of method are you using to produce this error ?
 #15362  by Rudolf21
 
The error message tells me that the line:
Code: Select all
Mail actMail = OKMMail.getInstance().getProperties(null, mail.getPath());
makes trouble...

The "mail" variable is created a line above:
Code: Select all
Node mail = itut.nextNode();
The error message says:
Target exception: com.openkm.core.RepositoryException: CallbackHandler does not support: javax.security.auth.callback.NameCallback@68086808: CallbackHandler does not support: javax.security.auth.callback.NameCallback@68086808
Is there a special import needed?

Greetings!
 #15400  by jllort
 
I think you should use something like this
Code: Select all
DocumentModule.getInstace().move(XXXX
That has this implementation because internally can work with EJB or directly ...
 #15425  by Rudolf21
 
I've changed:
Code: Select all
new DirectDocumentModule().copy(XXXX
to
Code: Select all
DirectDocumentModule().getInstance().copy(XXXX
This still works with manual execution, but throws the same error as before when runs automatically.

The error-message said that line 36 is incorrect:
Code: Select all
Mail actMail = OKMMail.getInstance().getProperties(null, mail.getPath());
There, getInstance() is already implemented... :?

What is about that CallbackHandler, who is mentioned in the error?

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.