Skip to content

Commit

Permalink
added e-mail support
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinoAvonEFSA committed Jan 25, 2018
1 parent de3f206 commit 2d990e1
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/app_config/AppPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class AppPaths {
public static final String TEMP_FOLDER = "temp" + System.getProperty("file.separator");
public static final String DB_FOLDER = "database" + System.getProperty("file.separator");
public static final String COMPAT_FOLDER = "compat" + System.getProperty("file.separator");
public static final String LOG_FOLDER = "logs" + System.getProperty("file.separator");

// config files
public static final String TABLES_SCHEMA_FILENAME = "tablesSchema";
Expand Down
97 changes: 97 additions & 0 deletions src/app_config/PropertiesReader.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package app_config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Optional;
import java.util.Properties;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import email.Email;
import report.Report;

/**
Expand All @@ -20,6 +28,8 @@ public class PropertiesReader {
private static final Logger LOGGER = LogManager.getLogger(PropertiesReader.class);

private static final String TECH_SUPPORT_EMAIL_PROPERTY = "TechnicalSupport.Email";
private static final String TECH_SUPPORT_EMAIL_SUBJECT = "TechnicalSupport.Email.Subject";
private static final String TECH_SUPPORT_EMAIL_BODY = "TechnicalSupport.Email.Body";
private static final String DB_REQUIRED_VERSION_PROPERTY = "Db.MinRequiredVersion";
private static final String APP_NAME_PROPERTY = "Application.Name";
private static final String APP_VERSION_PROPERTY = "Application.Version";
Expand Down Expand Up @@ -95,6 +105,93 @@ public static String getSupportEmail() {
return getValue(TECH_SUPPORT_EMAIL_PROPERTY);
}

private static String solveKeywords(String input) {

if (input == null)
return null;

String solved = input
.replace("%appVersion", getAppVersion())
.replace("%appName", getAppName());

if (solved.contains("%appLog")) {
try {
File log = getLastLog();

if (log == null)
return solved;

// add date
String logData = new String(Files.readAllBytes(Paths.get(log.getAbsolutePath())));

solved = solved.replace("%appLog", logData);
} catch (IOException e) {
e.printStackTrace();
}
}

return solved;
}

public static boolean openMailPanel() {

String subj = getSupportEmailSubject();
String body = getSupportEmailBody();
String address = getSupportEmail();

if (subj == null || body == null || address == null) {
LOGGER.error("Cannot create e-mail without subject or body or e-mail address. Check configuration file");
return false;
}

Email mail = new Email(subj, body, ";", address);

if (!mail.isSupported())
return false;

try {
mail.openEmailClient();
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
LOGGER.error("Cannot open e-mail client", e);
return false;
}

return true;
}

/**
* Get the latest log of the application
* @return
* @throws IOException
*/
private static File getLastLog() throws IOException {

Path dir = Paths.get(AppPaths.LOG_FOLDER); // specify your directory

// get the last file comparing lastModified field
Optional<Path> lastFilePath = Files.list(dir)
.filter(f -> !Files.isDirectory(f)) // exclude sub-directories
.max(Comparator.comparingLong(f -> f.toFile().lastModified()));

File latestLog = null;
if (lastFilePath.isPresent()) {
latestLog = lastFilePath.get().toFile();
}

return latestLog;
}

public static String getSupportEmailSubject() {
String subject = getValue(TECH_SUPPORT_EMAIL_SUBJECT);
return solveKeywords(subject);
}

public static String getSupportEmailBody() {
String body = getValue(TECH_SUPPORT_EMAIL_BODY);
return solveKeywords(body);
}

/**
* Check if the current data collection is the test one
* @return
Expand Down
14 changes: 14 additions & 0 deletions src/global_utils/Message.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
package global_utils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import app_config.PropertiesReader;

public class Message {

private String title;
private String message;
private int style;
private boolean fatal;

public Message(String title, String message, int style) {
this(title, message, style, false);
}

public Message(String title, String message, int style, boolean fatal) {
this.title = title;
this.message = message;
this.style = style;
this.fatal = fatal;
}

public int open(Shell shell) {
MessageBox mb = new MessageBox(shell, style);
mb.setText(title);
mb.setMessage(message);

if (fatal)
PropertiesReader.openMailPanel();

return mb.open();
}
}
30 changes: 24 additions & 6 deletions src/global_utils/Warnings.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,28 @@

public class Warnings {

public static Message create(String message) {
return create(Messages.get("error.title"), message, SWT.ICON_ERROR, false);
}

public static Message create(String title, String message) {
return create(title, message, SWT.ICON_ERROR, false);
}

public static Message create(String title, String message, int style) {
return new Message(title, message, style);
return create(title, message, style, false);
}

public static Message createFatal(String message) {
return create(Messages.get("error.title"), message, SWT.ICON_ERROR, true);
}

public static Message createFatal(String message, int style) {
return create(Messages.get("error.title"), message, style | SWT.ICON_ERROR, true);
}

public static Message create(String title, String message, int style, boolean fatal) {
return new Message(title, message, style, fatal);
}

/**
Expand All @@ -41,11 +61,9 @@ public static String getStackTrace(Exception e) {
return trace;
}

public static String[] getAckOperationWarning(DcfAckLog log) {
public static Message getAckOperationWarning(DcfAckLog log) {

OpResError error = log.getOpResError();

String title = Messages.get("error.title");
String message = null;

switch(error) {
Expand All @@ -65,8 +83,8 @@ public static String[] getAckOperationWarning(DcfAckLog log) {
PropertiesReader.getSupportEmail());
break;
}
return new String[] {title, message};

return createFatal(message);
}

public static String[] getSOAPWarning(MySOAPException e) {
Expand Down
2 changes: 0 additions & 2 deletions src/i18n_messages/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class Messages {

private static RCLBundle bundle = new RCLBundle(RESOURCE_BUNDLE);

private Messages() {}

public static String get(String key, String... values) {
return bundle.get(key, values);
}
Expand Down
7 changes: 3 additions & 4 deletions src/report/DownloadReportDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,9 @@ public DatasetList getSelectedDatasetVersions() {

LOGGER.warn("Duplicated sender dataset id in DCF for senderId=" + senderId);

int val = Warnings.warnUser(getParent(), Messages.get("error.title"),
Messages.get("download.duplicate.sender.id",
PropertiesReader.getSupportEmail()),
SWT.YES | SWT.NO | SWT.ICON_ERROR);
int val = Warnings.createFatal(Messages.get("download.duplicate.sender.id",
PropertiesReader.getSupportEmail()), SWT.YES | SWT.NO | SWT.ICON_ERROR)
.open(getParent());

if (val == SWT.NO)
return null;
Expand Down
12 changes: 5 additions & 7 deletions src/report/RefreshStatusThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ public Message refreshStatus() {
LOGGER.warn("Error found in ack=" + log.getOpResError());
LOGGER.warn("Error description found in ack=" + log.getOpResLog());

String[] warning = Warnings.getAckOperationWarning(log);
return Warnings.create(warning[0], warning[1], SWT.ICON_ERROR);
return Warnings.getAckOperationWarning(log);
}
else {
// not reachable
Expand Down Expand Up @@ -182,8 +181,8 @@ else if (oldStatus == RCLDatasetStatus.SUBMITTED &&
// otherwise inconsistent status
default:

mb = Warnings.create(Messages.get("error.title"), Messages.get("refresh.error", newStatus.getLabel(),
PropertiesReader.getSupportEmail()), SWT.ICON_ERROR);
mb = Warnings.createFatal(Messages.get("refresh.error", newStatus.getLabel(),
PropertiesReader.getSupportEmail()));

break;
}
Expand All @@ -199,9 +198,8 @@ else if (oldStatus == RCLDatasetStatus.SUBMITTED &&

LOGGER.error("Cannot refresh status", e);

Warnings.create(Messages.get("error.title"),
Messages.get("refresh.failed.no.senderId", PropertiesReader.getSupportEmail(), e.getMessage()),
SWT.ICON_ERROR);
Warnings.createFatal(Messages.get("refresh.failed.no.senderId",
PropertiesReader.getSupportEmail()));
}

// if we have an error show it and stop the process
Expand Down
5 changes: 2 additions & 3 deletions src/report/ReportDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ public void download() throws MySOAPException {
// if no data collection was retrieved
if (list.isEmpty()) {

Warnings.warnUser(shell, Messages.get("warning.title"),
Messages.get("dc.no.element.found", PropertiesReader.getSupportEmail()),
SWT.ICON_WARNING);
Warnings.createFatal(Messages.get("dc.no.element.found",
PropertiesReader.getSupportEmail())).open(shell);

return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/table_skeleton/TableRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public TableCell getTableColumnValue(String code, String picklistKey) {

if (sel == null) {

if (code != null) {
if (code != null && !code.trim().isEmpty()) {
LOGGER.warn("Cannot pick the value " + code + " from list " + picklistKey
+ ". Either the list or the element do not exist. Empty element returned instead.");
}
Expand Down

0 comments on commit 2d990e1

Please sign in to comment.