• Automation

  • OpenKM has many interesting features, but requires some configuration process to show its full potential.
OpenKM has many interesting features, but requires some configuration process to show its full potential.
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.
 #28925  by wilsoneric
 
I am new to OpenKM but i have followed the instructions on how to enable automation, and all works except one action which is send mail. When i put the code into sql under the sql tab, it comes back with error cannot find object AMDTYPE002. When i remove this file from code and resubmit, i get error column mismatch. Any help would be appreciated. We are trying to setup email notifications when people create new documents. Different emails for each user creating files is what we would like to accomplish.
 #28936  by jllort
 
Please do a screenshot because I want to be sure we're seeing the same. I think could be a bug on it.
 #31270  by c01dik
 
I have the same problem. Is there a solution?
screenshot
screenshot
ScreenShot.png (58.29 KiB) Viewed 3994 times
Using openkm Version: 6.3.0 (build: 8156)
 #31296  by jllort
 
Hi
Seems the sendMail class is not compatible with community version, sorry for the mistake in wiki ( seems is only compatible with professional 6.2 ).

As you can see, the sendMail class really was not included in source code https://sourceforge.net/p/openkm/code/H ... on/action/

I can send you the sendMail, but you will need to modify it to get it running in community, and some field should be hard coded, because this class need two fields and community only works with one.
 #31471  by jllort
 
Is not so easy, actual automation table in version 6.2 only has two fields and for sending mail are needed tree. It's not only uploading a simple class, also must change table structure and change other classes. I put here the class, but you should decide what section want to hardcode ( I can not upload as is to sourceforge ).
Code: Select all
/**
 * OpenKM, Open Document Management System (http://www.openkm.com)
 * Copyright (c) 2006-2015 Paco Avila & Josep Llort
 * 
 * No bytes were intentionally harmed during the development of this application.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package com.openkm.automation.action;

import java.io.StringReader;
import java.io.StringWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.xeoh.plugins.base.annotations.PluginImplementation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;

import com.openkm.api.OKMAuth;
import com.openkm.automation.Action;
import com.openkm.automation.AutomationUtils;
import com.openkm.core.Config;
import com.openkm.dao.bean.Automation;
import com.openkm.dao.bean.NodeBase;
import com.openkm.module.db.DbAuthModule;
import com.openkm.spring.PrincipalUtils;
import com.openkm.util.MailUtils;
import com.openkm.util.PathUtils;
import com.openkm.util.TemplateUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * SendMail
 * 
 * 
 */
public class SendMail implements Action {
	private static Logger log = LoggerFactory.getLogger(SendMail.class);
	
	@Override
	public void executePre(Map<String, Object> env, Object... params) {
	}
	
	@Override
	public void executePost(Map<String, Object> env, Object... params) {
		String message = AutomationUtils.getString(0, params);
		List<String> users = AutomationUtils.getList(1, params);
		List<String> roles = AutomationUtils.getList(2, params);
		
		try {
			NodeBase node = AutomationUtils.getNode(env);
			
			// Complete userlist with roles
			for (String role : roles) {
				List<String> usersInRole;
				usersInRole = OKMAuth.getInstance().getUsersByRole(null, role);
				for (String user : usersInRole) {
					if (!users.contains(user)) {
						users.add(user);
					}
				}
			}
			
			Authentication auth = PrincipalUtils.getAuthentication();
			StringWriter swSubject = new StringWriter();
			StringWriter swBody = new StringWriter();
			Configuration cfg = TemplateUtils.getConfig();
			
			Map<String, String> model = new HashMap<String, String>();
			model.put("documentUrl", Config.APPLICATION_URL + "?uuid=" + URLEncoder.encode(node.getUuid(), "UTF-8"));
			model.put("documentPath", node.getPath()); // some problem here on move event
			model.put("documentName", PathUtils.getName(node.getPath()));
			model.put("userId", "system");
			model.put("notificationMessage", message);
			
			if (TemplateUtils.templateExists(Config.NOTIFICATION_MESSAGE_SUBJECT)) {
				Template tpl = cfg.getTemplate(Config.NOTIFICATION_MESSAGE_SUBJECT);
				tpl.process(model, swSubject);
			} else {
				StringReader sr = new StringReader(Config.NOTIFICATION_MESSAGE_SUBJECT);
				Template tpl = new Template("NotificationMessageSubject", sr, cfg);
				tpl.process(model, swSubject);
				sr.close();
			}
			
			if (TemplateUtils.templateExists(message)) {
				Template tpl = cfg.getTemplate(message);
				tpl.process(model, swBody);
			} else {
				StringReader sr = new StringReader(message);
				Template tpl = new Template("AutomationSendMessageBody", sr, cfg);
				tpl.process(model, swBody);
				sr.close();
			}
			
			List<String> to = new ArrayList<String>();
			for (String usr : users) {
				String mail = new DbAuthModule().getMail(null, usr);
				if (mail != null) {
					to.add(mail);
				}
			}

			// Get session user email address && send notification
			String from = new DbAuthModule().getMail(null, auth.getName());
			MailUtils.sendMessage(from, to, swSubject.toString(), swBody.toString());
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
	
	@Override
	public boolean hasPost() {
		return true;
	}
	
	@Override
	public boolean hasPre() {
		return false;
	}
	
	@Override
	public String getName() {
		return "Send Mail";
	}
	
	@Override
	public String getParamType00() {
		return Automation.PARAM_TYPE_TEXTAREA;
	}
	
	@Override
	public String getParamSrc00() {
		return Automation.PARAM_SOURCE_EMPTY;
	}
	
	@Override
	public String getParamDesc00() {
		return "Message";
	}
	
	@Override
	public String getParamType01() {
		return Automation.PARAM_TYPE_USER;
	}
	
	@Override
	public String getParamSrc01() {
		return Automation.PARAM_SOURCE_EMPTY;
	}
	
	@Override
	public String getParamDesc01() {
		return "Users";
	}
	
	@Override
	public String getParamType02() {
		return Automation.PARAM_TYPE_ROLE;
	}
	
	@Override
	public String getParamSrc02() {
		return Automation.PARAM_SOURCE_EMPTY;
	}
	
	@Override
	public String getParamDesc02() {
		return "Roles";
	}
}

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.