Skip to content

Commit

Permalink
[26529] Automatisierte Datei Übertragung (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daksic28 authored Aug 16, 2024
1 parent a4ede58 commit e8b4377
Show file tree
Hide file tree
Showing 15 changed files with 801 additions and 2 deletions.
7 changes: 7 additions & 0 deletions bundles/ch.elexis.global_inbox.core/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
33 changes: 33 additions & 0 deletions bundles/ch.elexis.global_inbox.core/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ch.elexis.global_inbox.core</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ds.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=17
18 changes: 18 additions & 0 deletions bundles/ch.elexis.global_inbox.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Global Inbox Core
Bundle-SymbolicName: ch.elexis.global_inbox.core
Bundle-Version: 3.12.0.qualifier
Export-Package: ch.elexis.global_inbox.core.handler
Import-Package: ch.elexis.core.tasks.model,
ch.elexis.data
Require-Bundle: javax.inject,
ch.elexis.core,
at.medevit.elexis.inbox,
ch.elexis.core.ui;bundle-version="3.12.0",
ch.elexis.core.ui.tasks
Service-Component: OSGI-INF/ch.elexis.global_inbox.core.handler.IdentifiedRunnableFactoryImpl.xml,
OSGI-INF/ch.elexis.global_inbox.core.service.IdentifiedRunnableFactoryImplMover.xml
Automatic-Module-Name: ch.elexis.global.inbox.core
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-17
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" activate="activate" deactivate="deactivate" immediate="true" name="ch.elexis.global_inbox.core.service.IdentifiedRunnableFactoryImplMover">
<service>
<provide interface="ch.elexis.core.model.tasks.IIdentifiedRunnableFactory"/>
</service>
<reference bind="setModelService" interface="ch.elexis.core.services.IModelService" name="ModelService" target="(service.model.name=ch.elexis.core.tasks.model)"/>
<reference cardinality="1..1" field="accessControlService" interface="ch.elexis.core.services.IAccessControlService" name="accessControlService"/>
<reference bind="getModelService" interface="ch.elexis.core.services.IModelService" name="getModelService" target="(service.model.name=ch.elexis.core.tasks.model)"/>
<reference cardinality="1..1" field="taskService" interface="ch.elexis.core.tasks.model.ITaskService" name="taskService"/>
<reference cardinality="1..1" field="virtualFilsystemService" interface="ch.elexis.core.services.IVirtualFilesystemService" name="virtualFilsystemService"/>
<implementation class="ch.elexis.global_inbox.core.service.IdentifiedRunnableFactoryImplMover"/>
</scr:component>
5 changes: 5 additions & 0 deletions bundles/ch.elexis.global_inbox.core/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
OSGI-INF/ch.elexis.global_inbox.core.service.IdentifiedRunnableFactoryImplMover.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package ch.elexis.global_inbox.core.handler;

import java.io.IOException;
import java.io.Serializable;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.slf4j.Logger;

import ch.elexis.core.model.tasks.IIdentifiedRunnable;
import ch.elexis.core.model.tasks.TaskException;
import ch.elexis.core.services.IVirtualFilesystemService;
import ch.elexis.core.services.IVirtualFilesystemService.IVirtualFilesystemHandle;

import ch.elexis.global_inbox.core.util.Constants;

public class MoveFileIdentifiedRunnable implements IIdentifiedRunnable {

private IVirtualFilesystemService virtualFilesystemService;

public MoveFileIdentifiedRunnable(IVirtualFilesystemService virtualFilesystemService) {
this.virtualFilesystemService = virtualFilesystemService;
}

@Override
public String getId() {
return Constants.IMPORTFILETOOMNIVORE;
}

@Override
public Map<String, Serializable> run(Map<String, Serializable> context, IProgressMonitor progressMonitor,
Logger logger) throws TaskException {
String eventFilePath = (String) context.get(RunContextParameter.STRING_URL);
String destinationDir = (String) context.get("destinationDir");

if (eventFilePath == null || destinationDir == null) {
throw new TaskException(TaskException.EXECUTION_ERROR,
"Missing required run-context-parameter(s): [url, destinationDir]");
}

try {
IVirtualFilesystemHandle vfsHandle = virtualFilesystemService.of(eventFilePath);
Path sourcePath = Paths.get(vfsHandle.getURI());
Path targetDir = Paths.get(destinationDir);
if (!Files.exists(targetDir)) {
Files.createDirectories(targetDir);
} else if (!Files.isDirectory(targetDir)) {
throw new TaskException(TaskException.EXECUTION_ERROR, destinationDir + " is not a directory");
}

if (Files.isDirectory(sourcePath)) {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(sourcePath)) {
for (Path filePath : directoryStream) {
Path targetPath = targetDir.resolve(filePath.getFileName());
Files.move(filePath, targetPath);
logger.info("Moved {} to {}", filePath.toString(), targetPath.toString());
}
}
} else {
Path targetPath = targetDir.resolve(sourcePath.getFileName());
Files.move(sourcePath, targetPath);
logger.info("Moved {} to {}", sourcePath.toString(), targetPath.toString());
}
} catch (IOException e) {
throw new TaskException(TaskException.EXECUTION_ERROR, "Error moving files from [" + eventFilePath + "]",
e);
}

return null;
}

@Override
public String getLocalizedDescription() {
return "Move files from a directory to Omnivore directory";
}

@Override
public Map<String, Serializable> getDefaultRunContext() {
return Collections.singletonMap(RunContextParameter.STRING_URL,
RunContextParameter.VALUE_MISSING_REQUIRED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package ch.elexis.global_inbox.core.handler;

import ch.elexis.core.eenv.IElexisEnvironmentService;
import ch.elexis.core.exceptions.AccessControlException;
import ch.elexis.core.model.tasks.IIdentifiedRunnable;
import ch.elexis.core.model.tasks.IIdentifiedRunnable.RunContextParameter;
import ch.elexis.core.model.tasks.TaskException;
import ch.elexis.core.tasks.model.ITaskDescriptor;
import ch.elexis.core.tasks.model.ITaskService;
import ch.elexis.core.tasks.model.TaskTriggerType;
import ch.elexis.core.tasks.model.TaskTriggerTypeParameter;
import ch.elexis.core.services.IVirtualFilesystemService;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.slf4j.LoggerFactory;

public class TaskManagerHandler {

private ITaskService taskService;
private IVirtualFilesystemService virtualFilesystemService;

public TaskManagerHandler(ITaskService taskService, IVirtualFilesystemService virtualFilesystemService) {
this.taskService = taskService;
this.virtualFilesystemService = virtualFilesystemService;
}

public ITaskDescriptor getTaskDescriptorByReferenceId(String referenceId) {
Optional<ITaskDescriptor> taskDescriptorOpt = taskService.findTaskDescriptorByIdOrReferenceId(referenceId);
return taskDescriptorOpt.orElse(null);
}

public void createAndConfigureTask(String referenceId, String url, String destinationDir) {
try {
Optional<ITaskDescriptor> existingTaskDescriptorOpt = taskService
.findTaskDescriptorByIdOrReferenceId(referenceId);
if (existingTaskDescriptorOpt.isPresent()) {
ITaskDescriptor existingTaskDescriptor = existingTaskDescriptorOpt.get();
ensureNotDeletedById(existingTaskDescriptor.getReferenceId());
existingTaskDescriptor.setTriggerParameter(TaskTriggerTypeParameter.FILESYSTEM_CHANGE.URL, url);
existingTaskDescriptor.setRunContextParameter(RunContextParameter.STRING_URL, url);
existingTaskDescriptor.setRunContextParameter("destinationDir", destinationDir);
existingTaskDescriptor.setActive(true);
existingTaskDescriptor.setRunner(IElexisEnvironmentService.ES_STATION_ID_DEFAULT);
taskService.saveTaskDescriptor(existingTaskDescriptor);
} else {
IIdentifiedRunnable runnable = new MoveFileIdentifiedRunnable(virtualFilesystemService);
ITaskDescriptor newTaskDescriptor = taskService.createTaskDescriptor(runnable);
newTaskDescriptor.setReferenceId(referenceId);
newTaskDescriptor.setTriggerType(TaskTriggerType.FILESYSTEM_CHANGE);
newTaskDescriptor.setTriggerParameter(TaskTriggerTypeParameter.FILESYSTEM_CHANGE.URL, url);
newTaskDescriptor.setActive(true);
newTaskDescriptor.setRunner(IElexisEnvironmentService.ES_STATION_ID_DEFAULT);

Map<String, Serializable> runContext = new HashMap<>();
runContext.put(RunContextParameter.STRING_URL, url);
runContext.put("destinationDir", destinationDir);
newTaskDescriptor.setRunContext(runContext);

taskService.saveTaskDescriptor(newTaskDescriptor);
}
} catch (TaskException e) {
e.printStackTrace();
}
}

private void ensureNotDeletedById(String referenceId) {
Optional<ITaskDescriptor> taskDescriptorOpt = taskService.findTaskDescriptorByIdOrReferenceId(referenceId);
if (taskDescriptorOpt.isPresent()) {
ITaskDescriptor taskDescriptor = taskDescriptorOpt.get();
if (taskDescriptor.getReferenceId().equals(referenceId)) {
try {
if (taskDescriptor.isDeleted()) {
taskDescriptor.setDeleted(false);
taskService.saveTaskDescriptor(taskDescriptor);
}
} catch (TaskException e) {
e.printStackTrace();
}
}
}
}

public void deleteTaskDescriptorByReferenceId(String referenceId) {
Optional<ITaskDescriptor> taskDescriptorOpt = taskService.findTaskDescriptorByIdOrReferenceId(referenceId);
if (taskDescriptorOpt.isPresent()) {
ITaskDescriptor taskDescriptor = taskDescriptorOpt.get();
try {
taskDescriptor.setActive(false);
taskDescriptor.setDeleted(true);
taskService.saveTaskDescriptor(taskDescriptor);
} catch (AccessControlException e) {
LoggerFactory.getLogger(TaskManagerHandler.class).error("Berechtigungsfehler: " + e.getMessage());
} catch (TaskException e) {
e.printStackTrace();
}
}
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ch.elexis.global_inbox.core.service;

import java.util.ArrayList;
import java.util.List;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;

import ch.elexis.core.model.tasks.IIdentifiedRunnable;
import ch.elexis.core.model.tasks.IIdentifiedRunnableFactory;
import ch.elexis.core.services.IAccessControlService;
import ch.elexis.core.services.IModelService;
import ch.elexis.core.services.IVirtualFilesystemService;
import ch.elexis.core.tasks.internal.runnables.DeleteFileIdentifiedRunnable;
import ch.elexis.core.tasks.internal.runnables.LogResultContextIdentifiedRunnable;
import ch.elexis.core.tasks.internal.runnables.RemoveTaskLogEntriesRunnable;
import ch.elexis.core.tasks.model.ITaskService;
import ch.elexis.global_inbox.core.handler.MoveFileIdentifiedRunnable;


@Component(immediate = true)
public class IdentifiedRunnableFactoryImplMover implements IIdentifiedRunnableFactory {

@Reference
private ITaskService taskService;

@Reference
private IVirtualFilesystemService virtualFilsystemService;

private IModelService taskModelService;

@Reference(target = "(" + IModelService.SERVICEMODELNAME + "=ch.elexis.core.tasks.model)")
private void setModelService(IModelService modelService) {
taskModelService = modelService;
}

@Reference(target = "(" + IModelService.SERVICEMODELNAME + "=ch.elexis.core.tasks.model)")
public void getModelService(IModelService modelService) {
taskModelService = modelService;
}

@Reference
private IAccessControlService accessControlService;

@Activate
public void activate() {
taskService.bindIIdentifiedRunnableFactory(this);
}

@Deactivate
public void deactivate() {
taskService.unbindIIdentifiedRunnableFactory(this);
}

@Override
public List<IIdentifiedRunnable> getProvidedRunnables() {
List<IIdentifiedRunnable> ret = new ArrayList<>();
ret.add(new MoveFileIdentifiedRunnable(virtualFilsystemService));
return ret;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ch.elexis.global_inbox.core.util;

public class Constants {

public static final String IMPORTFILETOOMNIVORE = "importFileToOmnivore";
}
9 changes: 7 additions & 2 deletions bundles/ch.elexis.global_inbox/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ Require-Bundle: org.eclipse.jface,
org.eclipse.e4.core.services,
ch.elexis.core.services;bundle-version="1.0.0",
ch.elexis.core.ui.e4,
org.eclipse.core.databinding.observable
org.eclipse.core.databinding.observable,
ch.elexis.core.tasks,
ch.elexis.global_inbox.core
Bundle-RequiredExecutionEnvironment: JavaSE-21
Import-Package: javax.annotation;version="1.0.0";resolution:=optional,
Import-Package: ch.elexis.omnivore.model,
ch.elexis.omnivore.model.util,
ch.elexis.omnivore.ui.util,
javax.annotation;version="1.0.0";resolution:=optional,
javax.inject;version="1.0.0",
org.apache.commons.io,
org.apache.commons.lang3.time;version="3.7.0",
Expand Down
6 changes: 6 additions & 0 deletions bundles/ch.elexis.global_inbox/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
id="ch.elexis.global_inbox.preferencepage.titlecompletion"
name="Titel-Vervollständigung">
</page>
<page
category="ch.elexis.preferences.transporters"
class="ch.elexis.global_inbox.PreferencesServer"
id="ch.elexis.global_inbox.Prefs"
name="Automatiesiertes Eingangsfach">
</page>
</extension>
<extension
id="id1"
Expand Down
Loading

0 comments on commit e8b4377

Please sign in to comment.