IDEMPIERE-3977 Allow context variables on subject and message when processing alerts (FHCA-967)

This commit is contained in:
Carlos Ruiz 2019-05-25 13:32:38 +02:00
parent f772f37e4d
commit 41f6590608
4 changed files with 56 additions and 14 deletions

View File

@ -0,0 +1,19 @@
SET SQLBLANKLINES ON
SET DEFINE OFF
-- IDEMPIERE-3977 Allow context variables on subject and message when processing alerts (FHCA-967)
-- May 24, 2019, 6:58:25 PM CEST
UPDATE AD_Column SET FieldLength=255,Updated=TO_DATE('2019-05-24 18:58:25','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Column_ID=9077
;
-- May 24, 2019, 6:58:27 PM CEST
-- ALTER TABLE AD_Alert MODIFY AlertSubject NVARCHAR2(255);
ALTER TABLE AD_Alert ADD Tmp_AlertSubject NVARCHAR2(255);
UPDATE AD_Alert SET Tmp_AlertSubject = AlertSubject;
ALTER TABLE AD_Alert MODIFY Tmp_AlertSubject NVARCHAR2(255) NOT NULL;
ALTER TABLE AD_Alert DROP COLUMN AlertSubject;
ALTER TABLE AD_Alert RENAME COLUMN Tmp_AlertSubject TO AlertSubject;
SELECT register_migration_script('201905241859_IDEMPIERE-3977.sql') FROM dual
;

View File

@ -0,0 +1,12 @@
-- IDEMPIERE-3977 Allow context variables on subject and message when processing alerts (FHCA-967)
-- May 24, 2019, 6:58:25 PM CEST
UPDATE AD_Column SET FieldLength=255,Updated=TO_TIMESTAMP('2019-05-24 18:58:25','YYYY-MM-DD HH24:MI:SS'),UpdatedBy=100 WHERE AD_Column_ID=9077
;
-- May 24, 2019, 6:58:27 PM CEST
INSERT INTO t_alter_column values('ad_alert','AlertSubject','VARCHAR(255)',null,null)
;
SELECT register_migration_script('201905241859_IDEMPIERE-3977.sql') FROM dual
;

View File

@ -1666,7 +1666,7 @@ public final class Env
String foreignTable = colToken.getReferenceTableName(); String foreignTable = colToken.getReferenceTableName();
if (v != null) { if (v != null) {
if (format != null && format.length() > 0) { if (format != null && format.length() > 0) {
if (v instanceof Integer && (Integer) v > 0 && !Util.isEmpty(foreignTable)) { if (v instanceof Integer && (Integer) v >= 0 && (!Util.isEmpty(foreignTable) || token.equalsIgnoreCase(po.get_TableName()+"_ID"))){
int tblIndex = format.indexOf("."); int tblIndex = format.indexOf(".");
String tableName = null; String tableName = null;
if (tblIndex > 0) if (tblIndex > 0)
@ -1674,7 +1674,7 @@ public final class Env
else else
tableName = foreignTable; tableName = foreignTable;
MTable table = MTable.get(ctx, tableName); MTable table = MTable.get(ctx, tableName);
if (table != null && tableName.equalsIgnoreCase(foreignTable)) { if (table != null && (tableName.equalsIgnoreCase(foreignTable) || tableName.equalsIgnoreCase(po.get_TableName()))) {
String columnName = tblIndex > 0 ? format.substring(tblIndex + 1) : format; String columnName = tblIndex > 0 ? format.substring(tblIndex + 1) : format;
MColumn column = table.getColumn(columnName); MColumn column = table.getColumn(columnName);
if (column != null) { if (column != null) {

View File

@ -36,6 +36,7 @@ import org.compiere.model.MAttachment;
import org.compiere.model.MClient; import org.compiere.model.MClient;
import org.compiere.model.MNote; import org.compiere.model.MNote;
import org.compiere.model.MSysConfig; import org.compiere.model.MSysConfig;
import org.compiere.model.MSystem;
import org.compiere.model.MUser; import org.compiere.model.MUser;
import org.compiere.util.CLogger; import org.compiere.util.CLogger;
import org.compiere.util.DB; import org.compiere.util.DB;
@ -132,8 +133,12 @@ public class AlertProcessor extends AdempiereServer
return false; return false;
if (log.isLoggable(Level.INFO)) log.info("" + alert); if (log.isLoggable(Level.INFO)) log.info("" + alert);
StringBuffer message = new StringBuffer(alert.getAlertMessage()) MSystem system = MSystem.get(Env.getCtx());
.append(Env.NL); MClient client = MClient.get(Env.getCtx());
// parse variables from Client, then from System
String alertMessage = Env.parseVariable(alert.getAlertMessage(), client, null, true);
alertMessage = Env.parseVariable(alertMessage, system, null, true);
StringBuffer message = new StringBuffer(alertMessage).append(Env.NL);
// //
boolean valid = true; boolean valid = true;
boolean processed = false; boolean processed = false;
@ -228,12 +233,15 @@ public class AlertProcessor extends AdempiereServer
// //
// Report footer - Date Generated // Report footer - Date Generated
DateFormat df = DisplayType.getDateFormat(DisplayType.DateTime, language); DateFormat df = DisplayType.getDateFormat(DisplayType.DateTime, language);
message.append("\n\n"); message.append(Env.NL).append(Env.NL);
message.append(Msg.translate(language, "Date")).append(" : ") message.append(Msg.translate(language, "Date")).append(" : ")
.append(df.format(new Timestamp(System.currentTimeMillis()))); .append(df.format(new Timestamp(System.currentTimeMillis())));
Collection<Integer> users = alert.getRecipientUsers(); Collection<Integer> users = alert.getRecipientUsers();
int countMail = notifyUsers(users, alert.getAlertSubject(), message.toString(), attachments); // parse variables from Client, then from System
String alertSubject = Env.parseVariable(alert.getAlertSubject(), client, null, true);
alertSubject = Env.parseVariable(alertSubject, system, null, true);
int countMail = notifyUsers(users, alertSubject, message.toString(), attachments);
// IDEMPIERE-2864 // IDEMPIERE-2864
for(File attachment : attachments) for(File attachment : attachments)
@ -260,7 +268,8 @@ public class AlertProcessor extends AdempiereServer
for (int user_id : users) { for (int user_id : users) {
MUser user = MUser.get(getCtx(), user_id); MUser user = MUser.get(getCtx(), user_id);
if (user.isNotificationEMail()) { if (user.isNotificationEMail()) {
if (m_client.sendEMailAttachments (user_id, subject, message, attachments)) String messageHTML = message.replaceAll(Env.NL, "<br>");
if (m_client.sendEMailAttachments (user_id, subject, messageHTML, attachments, true))
{ {
countMail++; countMail++;
} }
@ -276,15 +285,17 @@ public class AlertProcessor extends AdempiereServer
MNote note = new MNote(getCtx(), AD_Message_ID, user_id, trx.getTrxName()); MNote note = new MNote(getCtx(), AD_Message_ID, user_id, trx.getTrxName());
note.setClientOrg(m_model.getAD_Client_ID(), m_model.getAD_Org_ID()); note.setClientOrg(m_model.getAD_Client_ID(), m_model.getAD_Org_ID());
note.setTextMsg(message); note.setTextMsg(message);
note.setDescription(subject);
note.saveEx(); note.saveEx();
// Attachment if (attachments.size() > 0) {
MAttachment attachment = new MAttachment (getCtx(), MNote.Table_ID, note.getAD_Note_ID(), trx.getTrxName()); // Attachment
attachment.setClientOrg(m_model.getAD_Client_ID(), m_model.getAD_Org_ID()); MAttachment attachment = new MAttachment (getCtx(), MNote.Table_ID, note.getAD_Note_ID(), trx.getTrxName());
for (File f : attachments) { attachment.setClientOrg(m_model.getAD_Client_ID(), m_model.getAD_Org_ID());
attachment.addEntry(f); for (File f : attachments) {
attachment.addEntry(f);
}
attachment.saveEx();
} }
attachment.setTextMsg(message);
attachment.saveEx();
countMail++; countMail++;
trx.commit(); trx.commit();
} catch (Throwable e) { } catch (Throwable e) {