...
 
Commits (3)
......@@ -26,4 +26,5 @@ Import-Package: ch.elexis.core.documents,
org.apache.commons.io;version="2.5.0",
org.apache.commons.lang;version="2.6.0",
org.eclipse.core.runtime;version="3.5.0",
org.eclipse.osgi.util;version="1.1.0",
org.json
......@@ -8,7 +8,10 @@ import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
/**
* Class representing a Message that can be sent using a {@link MailAccount} and a
......@@ -26,6 +29,18 @@ public class MailMessage implements Serializable {
*/
private static final long serialVersionUID = 5874662524515670629L;
private static Gson gson = new Gson();
public static MailMessage fromJson(Serializable serializable){
try {
String valueToString = JSONObject.valueToString(serializable);
return gson.fromJson(valueToString, MailMessage.class);
} catch (JSONException e) {
// do nothing
}
return null;
}
private String to;
private String cc;
private String subject;
......
package ch.elexis.core.mail.internal;
import org.eclipse.osgi.util.NLS;
public class Messages extends NLS {
private static final String BUNDLE_NAME = "ch.elexis.core.mail.internal.messages"; //$NON-NLS-1$
public static String SendMailRunnable_1;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
}
private Messages(){}
}
......@@ -6,12 +6,8 @@ import java.util.Map;
import java.util.Optional;
import org.eclipse.core.runtime.IProgressMonitor;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import com.google.gson.Gson;
import ch.elexis.core.mail.IMailClient;
import ch.elexis.core.mail.MailAccount;
import ch.elexis.core.mail.MailMessage;
......@@ -20,15 +16,12 @@ import ch.elexis.core.model.tasks.TaskException;
public class SendMailRunnable implements IIdentifiedRunnable {
public static final String RUNNABLE_ID = "sendMailFromContext";
private Gson gson = new Gson();
public static final String RUNNABLE_ID = "sendMailFromContext"; //$NON-NLS-1$
private IMailClient mailClient;
public SendMailRunnable(IMailClient mailClient){
this.mailClient = mailClient;
this.gson = new Gson();
}
@Override
......@@ -38,14 +31,14 @@ public class SendMailRunnable implements IIdentifiedRunnable {
@Override
public String getLocalizedDescription(){
return "Send an email using the information from the task context";
return Messages.SendMailRunnable_1;
}
@Override
public Map<String, Serializable> getDefaultRunContext(){
Map<String, Serializable> defaultRunContext = new HashMap<>();
defaultRunContext.put("accountId", RunContextParameter.VALUE_MISSING_REQUIRED);
defaultRunContext.put("message",
defaultRunContext.put("accountId", RunContextParameter.VALUE_MISSING_REQUIRED); //$NON-NLS-1$
defaultRunContext.put("message", //$NON-NLS-1$
RunContextParameter.VALUE_MISSING_REQUIRED);
return defaultRunContext;
}
......@@ -53,24 +46,12 @@ public class SendMailRunnable implements IIdentifiedRunnable {
@Override
public Map<String, Serializable> run(Map<String, Serializable> runContext,
IProgressMonitor progressMonitor, Logger logger) throws TaskException{
String accountId = (String) runContext.get("accountId");
MailMessage message =
getRunContextEntryTyped(runContext, "message", MailMessage.class);
String accountId = (String) runContext.get("accountId"); //$NON-NLS-1$
MailMessage message = MailMessage.fromJson(runContext.get("message"));
Optional<MailAccount> account = mailClient.getAccount(accountId);
if (message != null && account.isPresent()) {
mailClient.sendMail(account.get(), message);
}
return null;
}
private <T> T getRunContextEntryTyped(Map<String, Serializable> map, String key,
Class<T> clazz){
try {
String valueToString = JSONObject.valueToString(map.get(key));
return gson.fromJson(valueToString, clazz);
} catch (JSONException e) {
// do nothing
}
return null;
}
}
......@@ -16,6 +16,7 @@ Require-Bundle: org.eclipse.core.runtime,
org.apache.commons.io,
com.eclipsesource.jaxrs.consumer;bundle-version="5.3.1"
Import-Package: bsh,
ch.elexis.core.tasks.model,
com.google.common.cache;version="15.0.0",
com.google.gson;version="2.8.5",
javax.persistence,
......
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="ch.elexis.core.services.holder.TaskServiceHolder">
<reference bind="setTaskService" interface="ch.elexis.core.tasks.model.ITaskService" name="TaskService"/>
<implementation class="ch.elexis.core.services.holder.TaskServiceHolder"/>
</scr:component>
\ No newline at end of file
package ch.elexis.core.services.holder;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import ch.elexis.core.tasks.model.ITaskService;
@Component
public class TaskServiceHolder {
private static ITaskService taskService;
@Reference
public void setTaskService(ITaskService taskService){
TaskServiceHolder.taskService = taskService;
}
public static ITaskService get() {
if (taskService == null) {
throw new IllegalStateException("No ITaskService available");
}
return taskService;
}
}
\ No newline at end of file