Skip to content

Commit

Permalink
[26828] Logger hinzugefügt und Preferences Page angepasst auf die (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daksic28 authored Dec 18, 2024
1 parent b8fd2ff commit 52c827e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ protected IStatus run(IProgressMonitor monitor) {
File dir = null;
if (filepath == null) {
filepath = Constants.PREF_DIR_DEFAULT;
ConfigServiceHolder.get().setLocal(Constants.PREF_DIR, Constants.PREF_DIR_DEFAULT);
ConfigServiceHolder.get().set(Constants.PREF_DIR, Constants.PREF_DIR_DEFAULT);
}
try {
URI uri = new URI(filepath);
Path path = Paths.get(uri);
dir = path.toFile();
} catch (Exception e) {
log.error("Failed to convert filepath to directory. Filepath: {}", filepath, e);
return Status.CANCEL_STATUS;
}
addFilesInDirRecursive(dir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import ch.elexis.core.model.MimeType;
import ch.elexis.core.services.IDocumentStore;
import ch.elexis.core.services.INamedQuery;
import ch.elexis.core.services.LocalConfigService;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.core.services.holder.CoreModelServiceHolder;

Expand All @@ -35,7 +34,7 @@ public void setDocumentStore(IDocumentStore documentStore) {
ImportOmnivoreInboxUtil.omnivoreDocumentStore = documentStore;
}

private Logger logger = LoggerFactory.getLogger(getClass());
private static Logger logger = LoggerFactory.getLogger(ImportOmnivoreInboxUtil.class);

/**
* Try to import the file for the patient, will delete <code>file</code> if
Expand Down Expand Up @@ -115,23 +114,37 @@ private boolean isFileOpened(File file) {
}

public static String getDirectory(String defaultValue, String deviceName) {
String deviceDir = ConfigServiceHolder.get().getLocal(Constants.PREF_DEVICE_DIR_PREFIX + deviceName,
defaultValue);
return deviceDir != null ? deviceDir : defaultValue;
try {
String deviceDir = ConfigServiceHolder.getGlobal(Constants.PREF_DEVICE_DIR_PREFIX + deviceName,
defaultValue);
if (deviceDir == null) {
logger.warn("Directory for device [{}] is null. Using default value [{}].", deviceName, defaultValue);
}
return deviceDir != null ? deviceDir : defaultValue;
} catch (Exception ex) {
logger.error("Error while fetching directory for device [{}].", deviceName, ex);
return defaultValue;
}
}

public static String getCategory(File file) {
String category = LocalConfigService.get(Constants.PREF_LAST_SELECTED_CATEGORY, "default");// $NON-NLS-1$
File parent = file.getParentFile();
if (parent == null) {
return "Error in inbox path";
} else {
try {
String category = ConfigServiceHolder.getGlobal(Constants.PREF_LAST_SELECTED_CATEGORY, "default");
File parent = file.getParentFile();
if (parent == null) {
logger.warn("Parent directory for file [{}] is null.", file.getAbsolutePath());
return "Error in inbox path";
}

String fname = parent.getAbsolutePath();
if (fname.startsWith(category)) {
return parent.getName();
} else {
return category;
}
} catch (Exception ex) {
logger.error("Error while determining category for file [{}].", file.getAbsolutePath(), ex);
return "Error in category resolution";
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ch.elexis.global_inbox;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -36,19 +35,18 @@
import org.slf4j.LoggerFactory;

import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.global_inbox.core.util.Constants;
import ch.elexis.global_inbox.ui.Messages;
import ch.elexis.core.services.IVirtualFilesystemService;
import ch.elexis.core.services.IVirtualFilesystemService.IVirtualFilesystemHandle;
import ch.elexis.core.services.holder.ConfigServiceHolder;
import ch.elexis.core.services.holder.VirtualFilesystemServiceHolder;
import ch.elexis.core.tasks.model.ITaskService;
import ch.elexis.core.tasks.model.TaskTriggerTypeParameter;
import ch.elexis.core.ui.e4.dialog.VirtualFilesystemUriEditorDialog;
import ch.elexis.core.ui.icons.Images;
import ch.elexis.core.ui.preferences.SettingsPreferenceStore;
import ch.elexis.core.utils.OsgiServiceUtil;
import ch.elexis.global_inbox.core.handler.TaskManagerHandler;
import ch.elexis.global_inbox.core.util.Constants;
import ch.elexis.global_inbox.ui.Messages;
import ch.elexis.omnivore.model.IDocumentHandle;
import ch.elexis.omnivore.model.TransientCategory;
import ch.elexis.omnivore.model.util.CategoryUtil;
Expand Down Expand Up @@ -79,7 +77,8 @@ public void init(IWorkbench workbench) {
setPreferenceStore(new SettingsPreferenceStore(CoreHub.localCfg));
taskService = OsgiServiceUtil.getService(ITaskService.class).orElse(null);
taskManagerHandler = new TaskManagerHandler(taskService);
lastSelectedCategory = getPreferenceStore().getString(Constants.PREF_LAST_SELECTED_CATEGORY);
lastSelectedCategory = ConfigServiceHolder.getGlobal(Constants.PREF_LAST_SELECTED_CATEGORY, StringUtils.EMPTY);

}

public PreferencesServer() {
Expand Down Expand Up @@ -123,7 +122,7 @@ protected Control createContents(Composite parent) {
addButton.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
addButton.addListener(SWT.Selection, e -> {
addNewDevice(newDeviceText.getText(), contentComposite);
newDeviceText.setText("");
newDeviceText.setText(StringUtils.EMPTY);
});

Label comboLabel = new Label(contentComposite, SWT.NONE);
Expand Down Expand Up @@ -213,7 +212,7 @@ public void widgetSelected(SelectionEvent e) {
String selectedCategory = (String) selection.getFirstElement();
if (selectedCategory != null) {
lastSelectedCategory = selectedCategory;
getPreferenceStore().setValue(Constants.PREF_LAST_SELECTED_CATEGORY, lastSelectedCategory);
ConfigServiceHolder.get().set(Constants.PREF_LAST_SELECTED_CATEGORY, lastSelectedCategory);
}
});

Expand Down Expand Up @@ -299,7 +298,8 @@ private void updateCategoriesListForDevice(String device) {
List<String> allCategories = CategoryUtil.getCategoriesNames();
categoryListViewer.setInput(allCategories);
categoryListViewer.refresh();
String selectedCategory = getPreferenceStore().getString(Constants.PREF_CATEGORY_PREFIX + device);
String selectedCategory = ConfigServiceHolder.getGlobal(Constants.PREF_CATEGORY_PREFIX + device,
StringUtils.EMPTY);
if (selectedCategory != null && !selectedCategory.isEmpty()) {
categoryListViewer.setSelection(new org.eclipse.jface.viewers.StructuredSelection(selectedCategory));
lastSelectedCategory = selectedCategory;
Expand Down Expand Up @@ -327,7 +327,7 @@ private void renameCategory(String oldCategoryName, String newCategoryName) {
saveCategoriesForDevice(deviceCombo.getText(), categories);
}
lastSelectedCategory = newCategoryName;
getPreferenceStore().setValue(Constants.PREF_LAST_SELECTED_CATEGORY, newCategoryName);
ConfigServiceHolder.get().set(Constants.PREF_LAST_SELECTED_CATEGORY, newCategoryName);
updateCategoriesListForDevice(deviceCombo.getText());
}

Expand All @@ -339,18 +339,19 @@ private void removeCategory(String oldCategoryName, String newCategoryName) {
}

private List<String> getCategoriesForDevice(String device) {
String categoriesString = getPreferenceStore().getString(Constants.PREF_CATEGORY_PREFIX + device);
String categoriesString = ConfigServiceHolder.getGlobal(Constants.PREF_CATEGORY_PREFIX + device,
StringUtils.EMPTY);
return StringUtils.isNotBlank(categoriesString) ? new ArrayList<>(List.of(categoriesString.split(",")))
: new ArrayList<>();
}

private void saveCategoriesForDevice(String device, List<String> categories) {
String categoriesString = String.join(",", categories);
getPreferenceStore().setValue(Constants.PREF_CATEGORY_PREFIX + device, categoriesString);
ConfigServiceHolder.get().set(Constants.PREF_CATEGORY_PREFIX + device, categoriesString);
}

private String[][] getDeviceEntries() {
String devices = getPreferenceStore().getString(Constants.PREF_DEVICES);
String devices = ConfigServiceHolder.getGlobal(Constants.PREF_DEVICES, StringUtils.EMPTY);
if (StringUtils.isNotBlank(devices)) {
String[] deviceArray = devices.split(",");
String[][] deviceEntries = new String[deviceArray.length][2];
Expand All @@ -365,7 +366,7 @@ private String[][] getDeviceEntries() {

private void deleteSelectedDevice(String selectedDevice, Composite mainComposite) {
if (StringUtils.isNotBlank(selectedDevice)) {
String devices = getPreferenceStore().getString(Constants.PREF_DEVICES);
String devices = ConfigServiceHolder.getGlobal(Constants.PREF_DEVICES, StringUtils.EMPTY);
String[] deviceArray = devices.split(",");
StringBuilder newDevices = new StringBuilder();
for (String device : deviceArray) {
Expand All @@ -376,10 +377,10 @@ private void deleteSelectedDevice(String selectedDevice, Composite mainComposite
newDevices.append(device);
}
}
getPreferenceStore().setValue(Constants.PREF_DEVICES, newDevices.toString());
getPreferenceStore().setToDefault(Constants.PREF_DEVICE_DIR_PREFIX + selectedDevice);
getPreferenceStore().setToDefault(Constants.PREF_SELECTED_DEVICE);
getPreferenceStore().setToDefault(Constants.PREF_CATEGORY_PREFIX + selectedDevice);
ConfigServiceHolder.get().set(Constants.PREF_DEVICES, newDevices.toString());
ConfigServiceHolder.get().set(Constants.PREF_DEVICE_DIR_PREFIX + selectedDevice, StringUtils.EMPTY);
ConfigServiceHolder.get().set(Constants.PREF_SELECTED_DEVICE, StringUtils.EMPTY);
ConfigServiceHolder.get().set(Constants.PREF_CATEGORY_PREFIX + selectedDevice, StringUtils.EMPTY);
if (taskManagerHandler.getTaskDescriptorByReferenceId(selectedDevice) != null) {

taskManagerHandler.deleteTaskDescriptorByReferenceId(selectedDevice);
Expand All @@ -388,21 +389,21 @@ private void deleteSelectedDevice(String selectedDevice, Composite mainComposite
updateDeviceCombo();

if (selectedDevice.equals(lastSelectedCategory)) {
lastSelectedCategory = "";
getPreferenceStore().setToDefault(Constants.PREF_LAST_SELECTED_CATEGORY);
lastSelectedCategory = StringUtils.EMPTY;
ConfigServiceHolder.get().set(Constants.PREF_LAST_SELECTED_CATEGORY, StringUtils.EMPTY);
}
}
}

private void addNewDevice(String deviceName, Composite mainComposite) {
if (StringUtils.isNotBlank(deviceName)) {
String devices = getPreferenceStore().getString(Constants.PREF_DEVICES);
String devices = ConfigServiceHolder.getGlobal(Constants.PREF_DEVICES, StringUtils.EMPTY);
if (StringUtils.isNotBlank(devices)) {
devices += "," + deviceName;
} else {
devices = deviceName;
}
getPreferenceStore().setValue(Constants.PREF_DEVICES, devices);
ConfigServiceHolder.get().set(Constants.PREF_DEVICES, devices);
updateDeviceCombo();
}
}
Expand All @@ -422,21 +423,23 @@ private void updateDeviceCombo() {
private void updateDeviceFields() {
String selectedDevice = deviceCombo.getText();
if (StringUtils.isNotBlank(selectedDevice)) {
String dir = getPreferenceStore().getString(Constants.PREF_DEVICE_DIR_PREFIX + selectedDevice);
String dir = ConfigServiceHolder.getGlobal(Constants.PREF_DEVICE_DIR_PREFIX + selectedDevice,
StringUtils.EMPTY);
deviceDirText.setText(StringUtils.defaultString(dir));
updateCategoriesListForDevice(selectedDevice);
} else {
deviceDirText.setText("");
deviceDirText.setText(StringUtils.EMPTY);

}
}

private void loadDeviceData() {
String devices = getPreferenceStore().getString(Constants.PREF_DEVICES);
String devices = ConfigServiceHolder.getGlobal(Constants.PREF_DEVICES, StringUtils.EMPTY);
if (StringUtils.isNotBlank(devices)) {
String[] deviceArray = devices.split(",");
for (String device : deviceArray) {
String dir = getPreferenceStore().getString(Constants.PREF_DEVICE_DIR_PREFIX + device);
String dir = ConfigServiceHolder.getGlobal(Constants.PREF_DEVICE_DIR_PREFIX + device,
StringUtils.EMPTY);
if (StringUtils.isNotBlank(dir)) {
deviceDirMap.put(device, dir);
}
Expand All @@ -452,12 +455,12 @@ public boolean performOk() {
if (StringUtils.isNotBlank(selectedDevice)) {
String destinationDir = deviceDirText.getText();
if (lastSelectedCategory == null) {
lastSelectedCategory = "";
lastSelectedCategory = StringUtils.EMPTY;
}

getPreferenceStore().setValue(Constants.PREF_DEVICE_DIR_PREFIX + selectedDevice, deviceDirText.getText());
getPreferenceStore().setValue(Constants.PREF_SELECTED_DEVICE, selectedDevice);
getPreferenceStore().setValue(Constants.PREF_CATEGORY_PREFIX + selectedDevice, lastSelectedCategory);
ConfigServiceHolder.get().set(Constants.PREF_DEVICE_DIR_PREFIX + selectedDevice, deviceDirText.getText());
ConfigServiceHolder.get().set(Constants.PREF_SELECTED_DEVICE, selectedDevice);
ConfigServiceHolder.get().set(Constants.PREF_CATEGORY_PREFIX + selectedDevice, lastSelectedCategory);

if (StringUtils.isNotBlank(destinationDir)) {
taskManagerHandler.createAndConfigureTask(selectedDevice, destinationDir);
Expand All @@ -466,17 +469,16 @@ public boolean performOk() {
return super.performOk();
}


@Override
protected void performDefaults() {
super.performDefaults();
getPreferenceStore().setToDefault(Constants.PREF_SELECTED_DEVICE);
String devices = getPreferenceStore().getString(Constants.PREF_DEVICES);
ConfigServiceHolder.get().set(Constants.PREF_SELECTED_DEVICE, StringUtils.EMPTY);
String devices = ConfigServiceHolder.getGlobal(Constants.PREF_DEVICES, StringUtils.EMPTY);
if (StringUtils.isNotBlank(devices)) {
String[] deviceArray = devices.split(",");
for (String device : deviceArray) {
getPreferenceStore().setToDefault(Constants.PREF_DEVICE_DIR_PREFIX + device);
getPreferenceStore().setToDefault(Constants.PREF_CATEGORY_PREFIX + device);
ConfigServiceHolder.get().set(Constants.PREF_DEVICE_DIR_PREFIX + device, StringUtils.EMPTY);
ConfigServiceHolder.get().set(Constants.PREF_CATEGORY_PREFIX + device, StringUtils.EMPTY);
}
}
updateDeviceCombo();
Expand Down

0 comments on commit 52c827e

Please sign in to comment.