...
 
......@@ -81,6 +81,16 @@
optional="true">
</commandParameter>
</command>
<command
defaultHandler="ch.elexis.core.mail.ui.handlers.SendMailTaskHandler"
id="ch.elexis.core.mail.ui.sendMailTask"
name="Open Send Mail Task">
<commandParameter
id="ch.elexis.core.mail.ui.sendMailTaskDescriptorId"
name="TaskDescriptorId"
optional="false">
</commandParameter>
</command>
</extension>
<extension
point="org.eclipse.core.expressions.propertyTesters">
......
......@@ -5,6 +5,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.jface.action.Action;
......@@ -65,6 +66,10 @@ public class SendMailDialog extends TitleAreaDialog {
private AttachmentsComposite attachments;
private Command createOutboxCommand;
private String accountId;
private String attachmentsString;
private String documentsString;
private boolean disableOutbox;
public SendMailDialog(Shell parentShell){
super(parentShell);
......@@ -98,6 +103,9 @@ public class SendMailDialog extends TitleAreaDialog {
accountsViewer.setInput(getSendMailAccounts());
accountsViewer.getControl()
.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
if (accountId != null) {
accountsViewer.setSelection(new StructuredSelection(accountId));
}
lbl = new Label(container, SWT.NONE);
lbl.setText("An");
......@@ -213,6 +221,8 @@ public class SendMailDialog extends TitleAreaDialog {
attachments = new AttachmentsComposite(container, SWT.NONE);
attachments.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
attachments.setAttachments(attachmentsString);
attachments.setDocuments(documentsString);
lbl = new Label(container, SWT.NONE);
lbl.setText("Text");
......@@ -220,18 +230,21 @@ public class SendMailDialog extends TitleAreaDialog {
GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
gd.heightHint = 128;
textText.setLayoutData(gd);
textText.setText(textString);
// set selected account for mandant
Mandant selectedMandant = ElexisEventDispatcher.getSelectedMandator();
if (selectedMandant != null) {
List<String> accounts = MailClientComponent.getMailClient().getAccounts();
for (String string : accounts) {
Optional<MailAccount> accountOptional =
MailClientComponent.getMailClient().getAccount(string);
if (accountOptional.isPresent()
&& accountOptional.get().isForMandant(selectedMandant.getId())) {
accountsViewer
.setSelection(new StructuredSelection(accountOptional.get().getId()));
if (accountId == null) {
// set selected account for mandant
Mandant selectedMandant = ElexisEventDispatcher.getSelectedMandator();
if (selectedMandant != null) {
List<String> accounts = MailClientComponent.getMailClient().getAccounts();
for (String string : accounts) {
Optional<MailAccount> accountOptional =
MailClientComponent.getMailClient().getAccount(string);
if (accountOptional.isPresent()
&& accountOptional.get().isForMandant(selectedMandant.getId())) {
accountsViewer.setSelection(
new StructuredSelection(accountOptional.get().getId()));
}
}
}
}
......@@ -297,7 +310,8 @@ public class SendMailDialog extends TitleAreaDialog {
protected void createButtonsForButtonBar(Composite parent){
Button outboxBtn = createButton(parent, -1, "in Oubox ablegen", false);
super.createButtonsForButtonBar(parent);
outboxBtn.setEnabled(createOutboxCommand != null && createOutboxCommand.isEnabled());
outboxBtn.setEnabled(
!disableOutbox && createOutboxCommand != null && createOutboxCommand.isEnabled());
outboxBtn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e){
......@@ -331,6 +345,8 @@ public class SendMailDialog extends TitleAreaDialog {
LoggerFactory.getLogger(getClass())
.warn("Create OutboxElement command not available");
}
// close dialog with cancel status, do not send mail
cancelPressed();
}
});
}
......@@ -372,6 +388,10 @@ public class SendMailDialog extends TitleAreaDialog {
return ccString;
}
public void setCc(String cc){
this.ccString = cc;
}
public String getSubject(){
return subjectString;
}
......@@ -380,6 +400,10 @@ public class SendMailDialog extends TitleAreaDialog {
return textString;
}
public void setAccountId(String accountId){
this.accountId = accountId;
}
public MailAccount getAccount(){
return account;
}
......@@ -391,4 +415,25 @@ public class SendMailDialog extends TitleAreaDialog {
public String getDocumentsString(){
return attachments.getDocuments();
}
public void setMailMessage(MailMessage message){
setTo(StringUtils.defaultString(message.getTo()));
setCc(StringUtils.defaultString(message.getCc()));
setSubject(StringUtils.defaultString(message.getSubject()));
setText(StringUtils.defaultString(message.getText()));
attachmentsString = message.getAttachmentsString();
documentsString = message.getDocumentsString();
}
public void disableOutbox(){
this.disableOutbox = true;
}
public void setDocumentsString(String documents){
this.documentsString = documents;
}
public void setAttachmentsString(String attachments){
this.attachmentsString = attachments;
}
}
package ch.elexis.core.mail.ui.handlers;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ui.handlers.HandlerUtil;
import ch.elexis.core.mail.MailMessage;
import ch.elexis.core.mail.TaskUtil;
import ch.elexis.core.mail.ui.client.MailClientComponent;
import ch.elexis.core.mail.ui.dialogs.SendMailDialog;
import ch.elexis.core.model.tasks.TaskException;
import ch.elexis.core.tasks.model.ITask;
import ch.elexis.core.tasks.model.ITaskDescriptor;
import ch.elexis.core.tasks.model.TaskState;
......@@ -38,19 +31,16 @@ import ch.elexis.core.tasks.model.TaskState;
*/
public class SendMailHandler extends AbstractHandler implements IHandler {
private ITask task;
@Override
public Object execute(ExecutionEvent event) throws ExecutionException{
SendMailDialog sendMailDialog = new SendMailDialog(HandlerUtil.getActiveShell(event));
sendMailDialog.create();
String attachments = event.getParameter("ch.elexis.core.mail.ui.sendMail.attachments");
if (attachments != null) {
sendMailDialog.setAttachments(attachments);
sendMailDialog.setAttachmentsString(attachments);
}
String documents = event.getParameter("ch.elexis.core.mail.ui.sendMail.documents");
if (documents != null) {
sendMailDialog.setDocuments(documents);
sendMailDialog.setDocumentsString(documents);
}
String to = event.getParameter("ch.elexis.core.mail.ui.sendMail.to");
if (to != null) {
......@@ -74,40 +64,8 @@ public class SendMailHandler extends AbstractHandler implements IHandler {
Optional<ITaskDescriptor> taskDescriptor =
TaskUtil.createSendMailTaskDescriptor(sendMailDialog.getAccount().getId(), message);
if (taskDescriptor.isPresent()) {
try {
new ProgressMonitorDialog(HandlerUtil.getActiveShell(event)).run(false, false,
new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException{
try {
monitor.beginTask("Send Mail ...", IProgressMonitor.UNKNOWN);
task = TaskUtil.executeTaskSync(taskDescriptor.get(), monitor);
monitor.done();
} catch (TaskException e) {
MessageDialog.openError(HandlerUtil.getActiveShell(event),
"Fehler", "Versenden konnte nicht gestartet werden.");
}
}
});
} catch (InvocationTargetException | InterruptedException e) {
MessageDialog.openError(HandlerUtil.getActiveShell(event), "Fehler",
"Versenden konnte nicht gestartet werden.");
}
if (!task.isSucceeded()) {
String errorMessage = MailClientComponent.getLastErrorMessage();
if (errorMessage.isEmpty()) {
MessageDialog.openError(HandlerUtil.getActiveShell(event), "Fehler",
"Versenden konnte nicht gestartet werden.");
} else {
MessageDialog.openError(HandlerUtil.getActiveShell(event), "Fehler",
errorMessage);
}
} else {
MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
"E-Mail versand", "E-Mail erfolgreich versendet.");
}
ITask task = new SendMailTaskWithProgress()
.execute(HandlerUtil.getActiveShell(event), taskDescriptor.get());
return task.getState() == TaskState.COMPLETED;
}
}
......
......@@ -8,11 +8,18 @@ import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.slf4j.LoggerFactory;
import ch.elexis.core.mail.MailAccount;
import ch.elexis.core.mail.MailAccount.TYPE;
import ch.elexis.core.mail.MailMessage;
import ch.elexis.core.mail.TaskUtil;
import ch.elexis.core.mail.ui.client.MailClientComponent;
import ch.elexis.core.model.tasks.TaskException;
import ch.elexis.core.tasks.model.ITask;
import ch.elexis.core.tasks.model.ITaskDescriptor;
import ch.elexis.core.tasks.model.TaskState;
import ch.elexis.data.Mandant;
/**
......@@ -62,11 +69,22 @@ public class SendMailNoUiHandler extends AbstractHandler implements IHandler {
if (attachments != null && !attachments.isEmpty()) {
message.setAttachments(attachments);
}
if (MailClientComponent.getMailClient().sendMail(mailAccount, message)) {
return null;
} else {
return MailClientComponent.getLastErrorMessage();
Optional<ITaskDescriptor> taskDescriptor =
TaskUtil.createSendMailTaskDescriptor(mailAccount.getId(), message);
if (taskDescriptor.isPresent()) {
try {
ITask task =
TaskUtil.executeTaskSync(taskDescriptor.get(), new NullProgressMonitor());
if (task.getState() == TaskState.COMPLETED) {
return null;
} else {
return MailClientComponent.getLastErrorMessage();
}
} catch (TaskException e) {
LoggerFactory.getLogger(TaskUtil.class).error("Error executing mail task", e);
}
}
return "Error executing mail task";
}
private MailAccount getMailAccount(String mandantId){
......
package ch.elexis.core.mail.ui.handlers;
import java.io.Serializable;
import java.util.Map;
import java.util.Optional;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.handlers.HandlerUtil;
import ch.elexis.core.mail.MailMessage;
import ch.elexis.core.mail.TaskUtil;
import ch.elexis.core.mail.ui.dialogs.SendMailDialog;
import ch.elexis.core.tasks.model.ITask;
import ch.elexis.core.tasks.model.ITaskDescriptor;
import ch.elexis.core.tasks.model.TaskState;
public class SendMailTaskHandler extends AbstractHandler implements IHandler {
@Override
public Object execute(ExecutionEvent event) throws ExecutionException{
String taskId = event.getParameter("ch.elexis.core.mail.ui.sendMailTaskDescriptorId");
Optional<ITaskDescriptor> taskDescriptor = TaskUtil.getTaskDescriptor(taskId);
if (taskDescriptor.isPresent()) {
MailMessage message =
MailMessage.fromJson(taskDescriptor.get().getRunContext().get("message"));
String accountId = (String) taskDescriptor.get().getRunContext().get("accountId");
if (message != null) {
SendMailDialog sendMailDialog =
new SendMailDialog(HandlerUtil.getActiveShell(event));
sendMailDialog.setMailMessage(message);
sendMailDialog.setAccountId(accountId);
sendMailDialog.disableOutbox();
if (sendMailDialog.open() == Dialog.OK) {
MailMessage sendMessage =
new MailMessage().to(sendMailDialog.getTo()).cc(sendMailDialog.getCc())
.subject(sendMailDialog.getSubject()).text(sendMailDialog.getText());
sendMessage.setAttachments(sendMailDialog.getAttachmentsString());
sendMessage.setDocuments(sendMailDialog.getDocumentsString());
Map<String, Serializable> runContext = taskDescriptor.get().getRunContext();
runContext.put("accountId", sendMailDialog.getAccount().getId());
runContext.put("message", sendMessage);
taskDescriptor.get().setRunContext(runContext);
ITaskDescriptor descriptor = TaskUtil.configureTaskDescriptor(taskDescriptor.get(),
sendMailDialog.getAccount().getId(), sendMessage);
ITask task = new SendMailTaskWithProgress()
.execute(HandlerUtil.getActiveShell(event), descriptor);
return Boolean.valueOf(task.getState() == TaskState.COMPLETED);
}
}
} else {
MessageDialog.openError(HandlerUtil.getActiveShell(event), "Fehler",
"Der mail Task konnte nicht geöffnet werden.");
}
return Boolean.FALSE;
}
}
package ch.elexis.core.mail.ui.handlers;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Shell;
import ch.elexis.core.mail.TaskUtil;
import ch.elexis.core.mail.ui.client.MailClientComponent;
import ch.elexis.core.model.tasks.TaskException;
import ch.elexis.core.tasks.model.ITask;
import ch.elexis.core.tasks.model.ITaskDescriptor;
public class SendMailTaskWithProgress {
private ITask task;
public ITask execute(Shell activeShell, ITaskDescriptor taskDescriptor){
try {
new ProgressMonitorDialog(activeShell).run(false, false,
new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException{
try {
monitor.beginTask("Send Mail ...", IProgressMonitor.UNKNOWN);
task = TaskUtil.executeTaskSync(taskDescriptor, monitor);
monitor.done();
} catch (TaskException e) {
MessageDialog.openError(activeShell, "Fehler",
"Versenden konnte nicht gestartet werden.");
}
}
});
} catch (InvocationTargetException | InterruptedException e) {
MessageDialog.openError(activeShell, "Fehler",
"Versenden konnte nicht gestartet werden.");
}
if (!task.isSucceeded()) {
String errorMessage = MailClientComponent.getLastErrorMessage();
if (errorMessage.isEmpty()) {
MessageDialog.openError(activeShell, "Fehler",
"Versenden konnte nicht gestartet werden.");
} else {
MessageDialog.openError(activeShell, "Fehler", errorMessage);
}
} else {
MessageDialog.openInformation(activeShell, "E-Mail versand",
"E-Mail erfolgreich versendet.");
}
return task;
}
}
......@@ -185,7 +185,15 @@ public class MailMessage implements Serializable {
this.attachmentsString = attachments;
}
public String getAttachmentsString(){
return attachmentsString;
}
public void setDocuments(String documents){
this.documentsString = documents;
}
public String getDocumentsString(){
return documentsString;
}
}
......@@ -28,6 +28,10 @@ public class TaskUtil {
TaskUtil.taskService = taskService;
}
public static Optional<ITaskDescriptor> getTaskDescriptor(String id){
return taskService.findTaskDescriptorByIdOrReferenceId(id);
}
/**
* Create a new {@link ITaskDescriptor} for sending the message using the account.
*
......@@ -45,20 +49,38 @@ public class TaskUtil {
try {
ITaskDescriptor descriptor =
taskService.createTaskDescriptor(sendMailRunnable.get());
Map<String, Serializable> runContext = descriptor.getRunContext();
runContext.put("accountId", accountId);
runContext.put("message", message);
descriptor.setRunContext(runContext);
taskService.saveTaskDescriptor(descriptor);
return Optional.of(descriptor);
return Optional.of(configureTaskDescriptor(descriptor, accountId, message));
} catch (TaskException e) {
LoggerFactory.getLogger(TaskUtil.class)
.error("Error creating send mail task descriptor", e);
LoggerFactory.getLogger(TaskUtil.class).error("Error creating mail task descriptor",
e);
}
}
return Optional.empty();
}
/**
* Configure the {@link ITaskDescriptor} run context with accountId and message.
*
* @param descriptor
* @param accountId
* @param message
* @return
*/
public static ITaskDescriptor configureTaskDescriptor(ITaskDescriptor descriptor,
String accountId, MailMessage message){
try {
Map<String, Serializable> runContext = descriptor.getRunContext();
runContext.put("accountId", accountId);
runContext.put("message", message);
descriptor.setRunContext(runContext);
taskService.saveTaskDescriptor(descriptor);
} catch (TaskException e) {
LoggerFactory.getLogger(TaskUtil.class).error("Error configuring mail task descriptor",
e);
}
return descriptor;
}
public static ITask executeTaskSync(ITaskDescriptor iTaskDescriptor,
IProgressMonitor progressMonitor) throws TaskException{
ITask task = taskService.trigger(iTaskDescriptor, progressMonitor, TaskTriggerType.MANUAL,
......
......@@ -251,12 +251,14 @@ public class Task extends AbstractIdDeleteModelAdapter<ch.elexis.core.jpa.entiti
throw new TaskException(TaskException.EXECUTION_REJECTED, "No task owner defined");
}
ContextServiceHolder.get().setActiveUser(owner);
IContact user_assignedContact = owner.getAssignedContact();
if (user_assignedContact != null && user_assignedContact.isMandator()) {
IMandator mandator = CoreModelServiceHolder.get()
.load(user_assignedContact.getId(), IMandator.class).orElse(null);
ContextServiceHolder.get().setActiveMandator(mandator);
if (isThreadLocalContextService()) {
ContextServiceHolder.get().setActiveUser(owner);
IContact user_assignedContact = owner.getAssignedContact();
if (user_assignedContact != null && user_assignedContact.isMandator()) {
IMandator mandator = CoreModelServiceHolder.get()
.load(user_assignedContact.getId(), IMandator.class).orElse(null);
ContextServiceHolder.get().setActiveMandator(mandator);
}
}
getEntity().setRunAt(System.currentTimeMillis());
......@@ -302,11 +304,18 @@ public class Task extends AbstractIdDeleteModelAdapter<ch.elexis.core.jpa.entiti
getEntity().setFinishedAt(System.currentTimeMillis());
setState(TaskState.FAILED);
} finally {
ContextServiceHolder.get().setActiveUser(null);
ContextServiceHolder.get().setActiveMandator(null);
if (isThreadLocalContextService()) {
ContextServiceHolder.get().setActiveUser(null);
ContextServiceHolder.get().setActiveMandator(null);
}
}
}
private boolean isThreadLocalContextService(){
return !ContextServiceHolder.get().getClass().getName()
.startsWith("ch.elexis.core.ui.services");
}
@Override
public boolean addXid(String domain, String id, boolean updateIfExists){
throw new UnsupportedOperationException();
......