Skip to content

Commit

Permalink
[26529] Mover weggenommen direct OmnivorImport (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daksic28 authored Nov 6, 2024
1 parent d38b643 commit efe9c72
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 216 deletions.
16 changes: 5 additions & 11 deletions bundles/ch.elexis.global_inbox.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@ Bundle-SymbolicName: ch.elexis.global_inbox.core
Bundle-Version: 3.13.0.qualifier
Export-Package: ch.elexis.global_inbox.core.handler,
ch.elexis.global_inbox.core.util
Require-Bundle: javax.inject,
ch.elexis.core,
at.medevit.elexis.inbox,
ch.elexis.core.documents,
ch.elexis.core.findings,
org.apache.commons.commons-io,
ch.elexis.core.importer.div,
ch.elexis.omnivore.model,
ch.elexis.core.tasks,
ch.elexis.core.services
Service-Component: OSGI-INF/ch.elexis.global_inbox.core.service.IdentifiedRunnableFactoryImplMover.xml
Require-Bundle: ch.elexis.core.tasks,
ch.elexis.core.services,
ch.elexis.core.data
Service-Component: OSGI-INF/ch.elexis.global_inbox.core.service.IdentifiedRunnableFactoryImplMover.xml,
OSGI-INF/ch.elexis.global_inbox.core.util.ImportOmnivoreInboxUtil.xml
Automatic-Module-Name: ch.elexis.global.inbox.core
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: JavaSE-21
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
<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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="ch.elexis.global_inbox.core.util.ImportOmnivoreInboxUtil">
<reference bind="setDocumentStore" interface="ch.elexis.core.services.IDocumentStore" name="DocumentStore" target="(storeid=ch.elexis.data.store.omnivore)"/>
<implementation class="ch.elexis.global_inbox.core.util.ImportOmnivoreInboxUtil"/>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.elexis.core.services.IConfigService.ILocalLock;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.global_inbox.core.util.Constants;
import ch.elexis.global_inbox.core.util.ImportOmnivoreInboxUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
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 java.util.concurrent.ConcurrentHashMap;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
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.LocalConfigService;
import ch.elexis.core.services.IVirtualFilesystemService.IVirtualFilesystemHandle;
import ch.elexis.global_inbox.core.util.Constants;

public class ImportOmnivoreIdentifiedRunnable implements IIdentifiedRunnable {

private IVirtualFilesystemService virtualFilesystemService;

private String eventFilePath;
private String destinationDir;
private String deviceName;
private Map<String, String> lastContextState = new ConcurrentHashMap<>();

public ImportOmnivoreIdentifiedRunnable(IVirtualFilesystemService virtualFilesystemService) {
this.virtualFilesystemService = virtualFilesystemService;
}
private String deviceName;

@Override
public String getId() {
Expand All @@ -39,38 +27,29 @@ public String getId() {

@Override
public String getLocalizedDescription() {
return "Move files from a directory to Omnivore directory and then import them";
return "Monitor a directory and import incoming files into the Omnivore";
}

@Override
public synchronized Map<String, Serializable> run(Map<String, Serializable> context,
IProgressMonitor progressMonitor, Logger logger) throws TaskException {
eventFilePath = null;
destinationDir = null;

deviceName = null;
for (Map.Entry<String, Serializable> entry : context.entrySet()) {
String key = entry.getKey();
Serializable value = entry.getValue();
if ("url".equals(key)) {
eventFilePath = (String) value;
} else if ("destinationDir".equals(key)) {
destinationDir = (String) value;
} else if ("referenceId".equals(key)) {
deviceName = (String) value;
}
}
if (eventFilePath == null || destinationDir == null || deviceName == null) {
if (eventFilePath == null || deviceName == null) {
throw new TaskException(TaskException.EXECUTION_ERROR,
"Missing required run-context-parameter(s): [url, destinationDir, referenceId]");
}
String destinationDirPreference = LocalConfigService.get(Constants.PREF_LAST_SELECTED_CATEGORY, null);
if (destinationDirPreference != null && !destinationDirPreference.isEmpty()) {
int lastSlashIndex = destinationDir.lastIndexOf('\\');
if (lastSlashIndex != -1) {
destinationDir = destinationDir.substring(0, lastSlashIndex + 1) + destinationDirPreference;
}
}
moveFiles(progressMonitor, logger);

try {
IStatus status = new ImportOmnivore(deviceName).run(progressMonitor);
if (!status.isOK()) {
Expand All @@ -90,59 +69,4 @@ public Map<String, Serializable> getDefaultRunContext() {
return Collections.singletonMap("url", (Serializable) "DefaultURLValue");
}

public boolean shouldRestartTask(Map<String, Serializable> context) {
String newEventFilePath = (String) context.get("url");
String newDestinationDir = (String) context.get("destinationDir");
String referenceId = (String) context.get("referenceId");
String lastEventFilePath = lastContextState.get(referenceId + "_url");
String lastDestinationDir = lastContextState.get(referenceId + "_destinationDir");
return !newEventFilePath.equals(lastEventFilePath) || !newDestinationDir.equals(lastDestinationDir);
}

public void moveFiles(IProgressMonitor monitor, Logger logger) throws TaskException {
try {
IVirtualFilesystemHandle vfseventFilePathHandle = virtualFilesystemService.of(eventFilePath);
IVirtualFilesystemHandle vfsdestinationDirHandle = virtualFilesystemService.of(destinationDir);
Path sourcePath = Paths.get(vfseventFilePathHandle.getURI());
Path targetDir = Paths.get(vfsdestinationDirHandle.getURI());
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) {
moveSingleFile(filePath, targetDir, logger);
}
}
} else {
moveSingleFile(sourcePath, targetDir, logger);
}
} catch (IOException e) {
throw new TaskException(TaskException.EXECUTION_ERROR, "Error moving files from [" + eventFilePath + "]");
}
}

private void moveSingleFile(Path sourcePath, Path targetDir, Logger logger) throws IOException, TaskException {
Path targetPath = targetDir.resolve(sourcePath.getFileName());
if (Files.exists(targetPath)) {
String fileName = sourcePath.getFileName().toString();
String newFileName = appendCopyToFileName(fileName);
targetPath = targetDir.resolve(newFileName);
}
Files.copy(sourcePath, targetPath);
Files.delete(sourcePath);
logger.info("Moved {} to {}", sourcePath.toString(), targetPath.toString());
}

private String appendCopyToFileName(String fileName) {
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex == -1) {
return fileName + "_copy";
} else {
return fileName.substring(0, dotIndex) + "_copy" + fileName.substring(dotIndex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
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.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -22,31 +20,28 @@
public class TaskManagerHandler {

private ITaskService taskService;
private IVirtualFilesystemService virtualFilesystemService;

public TaskManagerHandler(ITaskService taskService, IVirtualFilesystemService virtualFilesystemService) {
public TaskManagerHandler(ITaskService taskService) {
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) {
public void createAndConfigureTask(String referenceId, String destinationDir) {
try {
Optional<ITaskDescriptor> existingTaskDescriptorOpt = taskService
.findTaskDescriptorByIdOrReferenceId(referenceId);
ITaskDescriptor taskDescriptor;
ImportOmnivoreIdentifiedRunnable test = new ImportOmnivoreIdentifiedRunnable(virtualFilesystemService);
ImportOmnivoreIdentifiedRunnable test = new ImportOmnivoreIdentifiedRunnable();
IIdentifiedRunnable runnable = test;

if (existingTaskDescriptorOpt.isPresent()) {
taskDescriptor = existingTaskDescriptorOpt.get();
ensureNotDeletedById(taskDescriptor.getReferenceId());
taskDescriptor.setTriggerParameter(TaskTriggerTypeParameter.FILESYSTEM_CHANGE.URL, url);
taskDescriptor.setRunContextParameter(RunContextParameter.STRING_URL, url);
taskDescriptor.setTriggerParameter(TaskTriggerTypeParameter.FILESYSTEM_CHANGE.URL, destinationDir);
taskDescriptor.setRunContextParameter(RunContextParameter.STRING_URL, destinationDir);
taskDescriptor.setRunContextParameter("destinationDir", destinationDir);
taskDescriptor.setRunContextParameter("referenceId", referenceId);
taskDescriptor.setActive(true);
Expand All @@ -57,15 +52,14 @@ public void createAndConfigureTask(String referenceId, String url, String destin
taskDescriptor = taskService.createTaskDescriptor(runnable);
taskDescriptor.setReferenceId(referenceId);
taskDescriptor.setTriggerType(TaskTriggerType.FILESYSTEM_CHANGE);
taskDescriptor.setTriggerParameter(TaskTriggerTypeParameter.FILESYSTEM_CHANGE.URL, url);
taskDescriptor.setTriggerParameter(TaskTriggerTypeParameter.CRON.SCHEMA, destinationDir);
taskDescriptor.setActive(true);
taskDescriptor.setRunner(IElexisEnvironmentService.ES_STATION_ID_DEFAULT);
Map<String, Serializable> runContext = new HashMap<>();
runContext.put(RunContextParameter.STRING_URL, url);
runContext.put(RunContextParameter.STRING_URL, destinationDir);
runContext.put("destinationDir", destinationDir);
runContext.put("referenceId", referenceId);
taskDescriptor.setRunContext(runContext);

taskService.saveTaskDescriptor(taskDescriptor);
}
} catch (TaskException e) {
Expand Down Expand Up @@ -99,13 +93,11 @@ public void deleteTaskDescriptorByReferenceId(String referenceId) {
taskDescriptor.setDeleted(true);
taskService.setActive(taskDescriptor, false);
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
Expand Up @@ -11,7 +11,6 @@
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.model.ITaskService;
import ch.elexis.global_inbox.core.handler.ImportOmnivoreIdentifiedRunnable;

Expand All @@ -22,9 +21,6 @@ public class IdentifiedRunnableFactoryImplMover implements IIdentifiedRunnableFa
@Reference
private ITaskService taskService;

@Reference
private IVirtualFilesystemService virtualFilsystemService;

@Reference(target = "(" + IModelService.SERVICEMODELNAME + "=ch.elexis.core.tasks.model)")
private void setModelService(IModelService modelService) {
}
Expand All @@ -49,7 +45,7 @@ public void deactivate() {
@Override
public List<IIdentifiedRunnable> getProvidedRunnables() {
List<IIdentifiedRunnable> ret = new ArrayList<>();
ret.add(new ImportOmnivoreIdentifiedRunnable(virtualFilsystemService));
ret.add(new ImportOmnivoreIdentifiedRunnable());
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.apache.commons.lang3.StringUtils;

import ch.elexis.core.model.IDocument;

public class Constants {

Expand All @@ -13,9 +12,7 @@ public class Constants {
public static final String PREF_DIR_DEFAULT = StringUtils.EMPTY; // $NON-NLS-1$
public static final String PREF_LAST_SELECTED_CATEGORY = PREFERENCE_BRANCH_SERVER + "last_selected_category"; //$NON-NLS-1$
public static final String PREF_DEVICE_DIR_PREFIX = PREFERENCE_BRANCH_SERVER + "device_dir_"; //$NON-NLS-1$
public static final String PREF_TITLE_COMPLETION = PREFERENCE_BRANCH_SERVER + "titleCompletions"; //$NON-NLS-1$
public static final String PREF_DEVICES = PREFERENCE_BRANCH_SERVER + "devices"; //$NON-NLS-1$
public static final String PREF_SELECTED_DEVICE = PREFERENCE_BRANCH_SERVER + "selectedDevice"; //$NON-NLS-1$
public static final String PREF_OMNIVORE_DIR_STRUCTURE = PREFERENCE_BRANCH_SERVER + "omnivore_dir_structure"; //$NON-NLS-1$
public static final String PREF_CATEGORY_PREFIX = PREFERENCE_BRANCH_SERVER + "categories_"; //$NON-NLS-1$
}
Loading

0 comments on commit efe9c72

Please sign in to comment.