IDEMPIERE-4986 Extend events to prefill email dialog variables from report window (#908)

* IDEMPIERE-4986 Extend events to prefill email dialog variables from report window

* IDEMPIERE-4986 Extend events to prefill email dialog variables from report window / also in new Attachment button

* IDEMPIERE-4986 Extend events to prefill email dialog variables from report window / change approach to constructor

* IDEMPIERE-4992 email from archive dialog
This commit is contained in:
Carlos Ruiz 2021-10-08 10:28:45 +02:00 committed by GitHub
parent cf5c04427b
commit ca9aa33afb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 260 additions and 9 deletions

View File

@ -98,6 +98,9 @@ public interface IEventTopics {
public static final String REQUEST_SEND_EMAIL = "idempiere/requestSendEMail";
/** Called from dialog to send an email, to prefill dialog variables */
public static final String REPORT_SEND_EMAIL = "idempiere/reportSendEMail";
/** Called before starting a process, after prepared */
public static final String BEFORE_PROCESS = "idempiere/beforeProcess";
/** Called after a process finishes, before commit */

View File

@ -0,0 +1,99 @@
/***********************************************************************
* This file is part of iDempiere ERP Open Source *
* http://www.idempiere.org *
* *
* Copyright (C) Contributors *
* *
* 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. *
* *
* Contributors: *
* - Carlos Ruiz - globalqss - bxservice *
**********************************************************************/
/**
*
* @author Carlos Ruiz - globalqss - bxservice
*
*/
package org.adempiere.base.event;
import org.compiere.model.PrintInfo;
public class ReportSendEMailEventData {
public static final String CONTEXT_EMAIL_TO = "_EMAIL_TO_";
public static final String CONTEXT_EMAIL_USER_TO = "_EMAIL_USER_TO_";
public static final String CONTEXT_EMAIL_CC = "_EMAIL_CC_";
public static final String CONTEXT_EMAIL_USER_CC = "_EMAIL_USER_CC_";
public static final String CONTEXT_EMAIL_SUBJECT = "_EMAIL_SUBJECT_";
public static final String CONTEXT_EMAIL_MESSAGE = "_EMAIL_MESSAGE_";
private int m_windowNo;
private PrintInfo m_printInfo;
private String m_subject;
private int m_tableId;
private int m_recordId;
public ReportSendEMailEventData(int windowNo, int tableId, int recordId, PrintInfo printInfo, String subject) {
m_windowNo = windowNo;
m_tableId = tableId;
m_recordId = recordId;
m_printInfo = printInfo;
m_subject = subject;
}
public int getWindowNo() {
return m_windowNo;
}
public void setWindowNo(int windowNo) {
m_windowNo = windowNo;
}
public int getTableId() {
return m_tableId;
}
public void setTableId(int tableId) {
m_tableId = tableId;
}
public int getRecordId() {
return m_recordId;
}
public void setRecordId(int recordId) {
m_recordId = recordId;
}
public PrintInfo getFrom() {
return m_printInfo;
}
public void setFrom(PrintInfo printInfo) {
m_printInfo = printInfo;
}
public String getSubject() {
return m_subject;
}
public void setSubject(String subject) {
m_subject = subject;
}
}

View File

@ -21,14 +21,21 @@
package org.adempiere.webui.apps.form;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.sql.Timestamp;
import java.util.Date;
import java.util.logging.Level;
import javax.activation.FileDataSource;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.Callback;
import org.adempiere.webui.ClientInfo;
import org.adempiere.webui.LayoutUtils;
import org.adempiere.webui.apps.AEnv;
import org.adempiere.webui.component.Button;
import org.adempiere.webui.component.Checkbox;
import org.adempiere.webui.component.Column;
@ -58,11 +65,13 @@ import org.adempiere.webui.session.SessionManager;
import org.adempiere.webui.theme.ThemeManager;
import org.adempiere.webui.util.ZKUpdateUtil;
import org.adempiere.webui.window.FDialog;
import org.adempiere.webui.window.WEMailDialog;
import org.compiere.apps.form.Archive;
import org.compiere.model.MArchive;
import org.compiere.model.MLookup;
import org.compiere.model.MLookupFactory;
import org.compiere.model.MSysConfig;
import org.compiere.model.MUser;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;
@ -175,6 +184,7 @@ public class WArchiveViewer extends Archive implements IFormController, EventLis
private ConfirmPanel confirmPanel = new ConfirmPanel(true);
private Button updateArchive = new Button();
private Button deleteArchive = new Button();
private Button bEmail = new Button();
private Tabbox tabbox = new Tabbox();
private Tabs tabs = new Tabs();
@ -292,6 +302,13 @@ public class WArchiveViewer extends Archive implements IFormController, EventLis
bRefresh.setTooltiptext(Msg.getMsg(Env.getCtx(), "Refresh"));
bRefresh.addEventListener(Events.ON_CLICK, this);
if (ThemeManager.isUseFontIconForImage())
bEmail.setIconSclass("z-icon-SendMail");
else
bEmail.setImage(ThemeManager.getThemeResource("images/SendMail24.png"));
bEmail.setTooltiptext(Msg.getMsg(Env.getCtx(), "EMail"));
bEmail.addEventListener(Events.ON_CLICK, this);
if (ThemeManager.isUseFontIconForImage())
bBack.setIconSclass("z-icon-Previous");
else
@ -504,6 +521,7 @@ public class WArchiveViewer extends Archive implements IFormController, EventLis
Hbox hbox = new Hbox();
hbox.appendChild(deleteArchive);
hbox.appendChild(bRefresh);
hbox.appendChild(bEmail);
hbox.appendChild(updateArchive);
cell = new Cell();
cell.setColspan(3);
@ -611,6 +629,8 @@ public class WArchiveViewer extends Archive implements IFormController, EventLis
updateVDisplay(false);
else if (e.getTarget() == bNext)
updateVDisplay(true);
else if (e.getTarget() == bEmail)
sendMail();
else if (e.getTarget() == bRefresh)
iframe.invalidate();
else if (e.getTarget() instanceof Tab)
@ -666,6 +686,29 @@ public class WArchiveViewer extends Archive implements IFormController, EventLis
});
}
/**
* Send EMail with the current displayed file as attachment
*/
private void sendMail() {
MArchive ar = m_archives[m_index];
MUser from = MUser.get(Env.getCtx(), Env.getAD_User_ID(Env.getCtx()));
String fileName = System.getProperty("java.io.tmpdir") +
System.getProperty("file.separator") + ar.getName() + ".pdf";
File attachment = new File(fileName);
try {
Files.write(attachment.toPath(), ar.getBinaryData());
} catch (IOException e) {
throw new AdempiereException(e);
}
WEMailDialog dialog = new WEMailDialog (Msg.getMsg(Env.getCtx(), "SendMail"),
from, "", "", "", new FileDataSource(attachment),
m_WindowNo, m_AD_Table_ID, m_Record_ID, null);
AEnv.showWindow(dialog);
}
/**
* Update View Display
* @param next show next Archive

View File

@ -855,7 +855,9 @@ public class WAttachment extends Window implements EventListener<Event>
m_attachment.getEntryFile(index, attachment);
WEMailDialog dialog = new WEMailDialog (Msg.getMsg(Env.getCtx(), "SendMail"),
from, "", "", "", new FileDataSource(attachment));
from, "", "", "", new FileDataSource(attachment),
m_WindowNo, m_attachment.getAD_Table_ID(), m_attachment.getRecord_ID(), null);
AEnv.showWindow(dialog);
}
}

View File

@ -32,6 +32,9 @@ import java.util.regex.Pattern;
import javax.activation.DataSource;
import org.adempiere.base.event.EventManager;
import org.adempiere.base.event.IEventTopics;
import org.adempiere.base.event.ReportSendEMailEventData;
import org.adempiere.webui.AdempiereWebUI;
import org.adempiere.webui.ClientInfo;
import org.adempiere.webui.LayoutUtils;
@ -60,6 +63,7 @@ import org.compiere.model.MLookupFactory;
import org.compiere.model.MMailText;
import org.compiere.model.MUser;
import org.compiere.model.MUserMail;
import org.compiere.model.PrintInfo;
import org.compiere.util.ByteArrayDataSource;
import org.compiere.util.CLogger;
import org.compiere.util.DisplayType;
@ -101,7 +105,7 @@ public class WEMailDialog extends Window implements EventListener<Event>, ValueC
/**
*
*/
private static final long serialVersionUID = -3675260492572002393L;
private static final long serialVersionUID = 556391720307848225L;
/**
* EMail Dialog
@ -115,7 +119,27 @@ public class WEMailDialog extends Window implements EventListener<Event>, ValueC
public WEMailDialog (String title, MUser from, String to,
String subject, String message, DataSource attachment)
{
this(title, from, to, subject, message, attachment, -1, -1, -1, null);
} // EmailDialog
/**
* EMail Dialog
* @param title title
* @param from from
* @param to to
* @param subject subject
* @param message message
* @param attachment optional attachment
* @param m_WindowNo
* @param ad_Table_ID
* @param record_ID
* @param printInfo
*/
public WEMailDialog(String title, MUser from, String to, String subject, String message, DataSource attachment,
int m_WindowNo, int ad_Table_ID, int record_ID, PrintInfo printInfo) {
super();
this.m_AD_Table_ID = ad_Table_ID;
this.m_Record_ID = record_ID;
this.setTitle(title);
this.setSclass("popup-dialog email-dialog");
this.setClosable(true);
@ -139,8 +163,12 @@ public class WEMailDialog extends Window implements EventListener<Event>, ValueC
lang.put("language", Language.getLoginLanguage().getAD_Language());
fMessage.setConfig(lang);
commonInit(from, to, subject, message, attachment);
} // EmailDialog
commonInit(from, to, subject, message, attachment);
clearEMailContext(m_WindowNo);
sendEvent(m_WindowNo, m_AD_Table_ID, m_Record_ID, null, "");
setValuesFromContext(m_WindowNo);
}
/**
* Common Init
@ -192,6 +220,8 @@ public class WEMailDialog extends Window implements EventListener<Event>, ValueC
private String m_cc;
private String m_subject;
private String m_message;
private int m_Record_ID;
private int m_AD_Table_ID;
/** File to be optionally attached */
private DataSource m_attachment;
@ -871,6 +901,76 @@ public class WEMailDialog extends Window implements EventListener<Event>, ValueC
if (fUser != null)
fUser.getComponent().focus();
}
/**
* Set the user to editor and trigger the event change
* @param newUserTo
*/
public void setUserTo(int newUserTo) {
ValueChangeEvent vce = new ValueChangeEvent(fUser, fUser.getColumnName(), fUser.getValue(), newUserTo);
fUser.valueChange(vce);
}
/**
* Set the user Cc editor and trigger the event change
* @param newUserCc
*/
public void setUserCc(int newUserCc) {
ValueChangeEvent vce = new ValueChangeEvent(fCcUser, fCcUser.getColumnName(), fCc.getValue(), newUserCc);
fUser.valueChange(vce);
}
/**
* Clear the window context variables to prefill the dialog
* @param m_WindowNo
*/
private void clearEMailContext(int m_WindowNo) {
Env.setContext(Env.getCtx(), m_WindowNo, ReportSendEMailEventData.CONTEXT_EMAIL_TO, "");
Env.setContext(Env.getCtx(), m_WindowNo, ReportSendEMailEventData.CONTEXT_EMAIL_USER_TO, "");
Env.setContext(Env.getCtx(), m_WindowNo, ReportSendEMailEventData.CONTEXT_EMAIL_CC, "");
Env.setContext(Env.getCtx(), m_WindowNo, ReportSendEMailEventData.CONTEXT_EMAIL_USER_CC, "");
Env.setContext(Env.getCtx(), m_WindowNo, ReportSendEMailEventData.CONTEXT_EMAIL_SUBJECT, "");
Env.setContext(Env.getCtx(), m_WindowNo, ReportSendEMailEventData.CONTEXT_EMAIL_MESSAGE, "");
}
/**
* Send the event to listeners that prefill dialog variables
* @param windowNo
* @param tableId
* @param recordId
* @param printInfo
* @param subject
*/
private void sendEvent(int windowNo, int tableId, int recordId, PrintInfo printInfo, String subject) {
ReportSendEMailEventData eventData = new ReportSendEMailEventData(windowNo, tableId, recordId, printInfo,
subject);
org.osgi.service.event.Event event = EventManager.newEvent(IEventTopics.REPORT_SEND_EMAIL, eventData);
EventManager.getInstance().sendEvent(event);
}
/**
* Set the default dialog values from context
* @param windowNo
*/
private void setValuesFromContext(int windowNo) {
String newTo = Env.getContext(Env.getCtx(), windowNo, ReportSendEMailEventData.CONTEXT_EMAIL_TO);
if (!Util.isEmpty(newTo))
setTo(newTo);
int newUserTo = Env.getContextAsInt(Env.getCtx(), windowNo, ReportSendEMailEventData.CONTEXT_EMAIL_USER_TO);
if (newUserTo > 0)
setUserTo(newUserTo);
String newCc = Env.getContext(Env.getCtx(), windowNo, ReportSendEMailEventData.CONTEXT_EMAIL_CC);
if (!Util.isEmpty(newCc))
setCc(newCc);
int newUserCc = Env.getContextAsInt(Env.getCtx(), windowNo, ReportSendEMailEventData.CONTEXT_EMAIL_USER_CC);
if (newUserCc > 0)
setUserCc(newUserCc);
String newSubject = Env.getContext(Env.getCtx(), windowNo, ReportSendEMailEventData.CONTEXT_EMAIL_SUBJECT);
if (!Util.isEmpty(newSubject))
setSubject(newSubject);
String newMessage = Env.getContext(Env.getCtx(), windowNo, ReportSendEMailEventData.CONTEXT_EMAIL_MESSAGE);
if (!Util.isEmpty(newMessage))
setMessage(newMessage);
}
} // WEMailDialog

View File

@ -556,9 +556,10 @@ public class ZkJRViewer extends Window implements EventListener<Event>, ITabOnCl
String subject = m_title;
WEMailDialog dialog = new WEMailDialog (Msg.getMsg(Env.getCtx(), "SendMail"),
from, to, subject, "", new FileDataSource(attachment));
AEnv.showWindow(dialog);
from, to, subject, "", new FileDataSource(attachment),
m_WindowNo, m_printInfo.getAD_Table_ID(), m_printInfo.getRecord_ID(), m_printInfo);
AEnv.showWindow(dialog);
} // cmd_sendMail
public void onEvent(Event event) throws Exception {

View File

@ -1311,7 +1311,10 @@ public class ZkReportViewer extends Window implements EventListener<Event>, ITab
}
WEMailDialog dialog = new WEMailDialog (Msg.getMsg(Env.getCtx(), "SendMail"),
from, to, subject, message, new FileDataSource(attachment));
from, to, subject, message, new FileDataSource(attachment),
m_WindowNo, m_reportEngine.getPrintInfo().getAD_Table_ID(),
m_reportEngine.getPrintInfo().getRecord_ID(), m_reportEngine.getPrintInfo());
AEnv.showWindow(dialog);
} // cmd_sendMail