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";
}
}