Commit 3c1f49fe authored by Thomas Huster's avatar Thomas Huster

[17215] fixed DocumentStore and LocalDocumentService document handling

parent df6d2aa0
Pipeline #12128 passed with stages
in 4 minutes and 44 seconds
......@@ -59,7 +59,7 @@ public class LocalDocumentService implements ILocalDocumentService {
}
}
ILoadHandler loadHandler = registeredLoadHandler.get(documentSource.getClass());
ILoadHandler loadHandler = getRegisteredLoadHandler(documentSource.getClass());
if (loadHandler == null) {
throw new IllegalStateException("No load handler for [" + documentSource + "]");
}
......@@ -77,6 +77,20 @@ public class LocalDocumentService implements ILocalDocumentService {
return Optional.empty();
}
private ILoadHandler getRegisteredLoadHandler(Class<? extends Object> clazz){
ILoadHandler matchingHandler = registeredLoadHandler.get(clazz);
if (matchingHandler == null) {
Class<?>[] interfaces = clazz.getInterfaces();
for (Class<?> interfaze : interfaces) {
matchingHandler = registeredLoadHandler.get(interfaze);
if(matchingHandler != null) {
break;
}
}
}
return matchingHandler;
}
@Override
public void remove(Object documentSource, IConflictHandler conflictHandler){
// try to delete the file
......@@ -364,9 +378,22 @@ public class LocalDocumentService implements ILocalDocumentService {
registeredLoadHandler.put(clazz, saveHandler);
}
private ISaveHandler getRegisteredSaveHandler(Class<? extends Object> clazz){
ISaveHandler matchingHandler = registeredSaveHandler.get(clazz);
if (matchingHandler == null) {
Class<?>[] interfaces = clazz.getInterfaces();
for (Class<?> interfaze : interfaces) {
matchingHandler = registeredSaveHandler.get(interfaze);
if (matchingHandler != null) {
break;
}
}
}
return matchingHandler;
}
@Override
public boolean save(Object documentSource) throws IllegalStateException{
ISaveHandler saveHandler = registeredSaveHandler.get(documentSource.getClass());
ISaveHandler saveHandler = getRegisteredSaveHandler(documentSource.getClass());
if(saveHandler != null) {
return saveHandler.save(documentSource, this);
}
......
......@@ -346,9 +346,13 @@ public class DocumentStore {
}
public Optional<Identifiable> getPersistenceObject(IDocument document){
Optional<Object> po = getService(document.getStoreId()).getPersistenceObject(document);
if (po.isPresent()) {
return Optional.of((Identifiable) po.get());
if (document instanceof Identifiable) {
return Optional.of(document);
} else {
Optional<Object> po = getService(document.getStoreId()).getPersistenceObject(document);
if (po.isPresent()) {
return Optional.of((Identifiable) po.get());
}
}
return Optional.empty();
}
......
......@@ -10,6 +10,7 @@ import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.LoggerFactory;
import ch.elexis.core.model.IDocumentLetter;
import ch.elexis.core.services.ILocalDocumentService;
import ch.elexis.core.services.ILocalDocumentService.ILoadHandler;
import ch.elexis.core.services.ILocalDocumentService.ISaveHandler;
......@@ -74,6 +75,40 @@ public class LocalDocumentServiceHolder {
return null;
}
});
service.registerSaveHandler(IDocumentLetter.class, new ISaveHandler() {
@Override
public boolean save(Object documentSource, ILocalDocumentService service){
IDocumentLetter letter = (IDocumentLetter) documentSource;
Optional<InputStream> content = service.getContent(letter);
if (content.isPresent()) {
try {
letter.setContent(content.get());
return true;
} finally {
try {
content.get().close();
} catch (IOException e) {
// ignore
}
}
}
return false;
}
});
service.registerLoadHandler(IDocumentLetter.class, new ILoadHandler() {
@Override
public InputStream load(Object documentSource){
IDocumentLetter letter = (IDocumentLetter) documentSource;
try {
return letter.getContent();
} catch (Exception e) {
LoggerFactory.getLogger(getClass()).error("Error loading document", e);
}
return null;
}
});
}
public static void unbind(ILocalDocumentService service){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment