Skip to content
Snippets Groups Projects
Commit 718121de authored by Daniel's avatar Daniel
Browse files

FIX: Handle files on file system without File entity.

For #14.
parent e2161ece
No related branches found
No related tags found
No related merge requests found
......@@ -29,6 +29,7 @@ import caosdb.server.database.backend.implementation.MySQL.ConnectionException;
import caosdb.server.database.misc.TransactionBenchmark;
import caosdb.server.entity.Entity;
import caosdb.server.entity.FileProperties;
import caosdb.server.entity.Message;
import caosdb.server.entity.RetrieveEntity;
import caosdb.server.entity.container.TransactionContainer;
import caosdb.server.permissions.EntityPermission;
......@@ -149,17 +150,32 @@ public class FileSystemResource extends AbstractCaosDBServerResource {
return null;
}
/**
* Return an element for the given file on a file system.
*
* <p>If there is no File entity for this file, an element without `id` is returned, instead a
* corresponding message is added to the element.
*/
Element getFileElement(final String directory, final File file) throws Exception {
final Element celem = new Element("file");
celem.setAttribute(
"id",
getEntityID((directory.endsWith("/") ? directory : directory + "/") + file.getName()));
final String entId =
getEntityID((directory.endsWith("/") ? directory : directory + "/") + file.getName());
if (entId == null) {
new Message("Orphaned file").addToElement(celem);
} else {
celem.setAttribute("id", entId);
}
celem.setAttribute("name", file.getName());
return celem;
}
protected String getEntityID(final String path) throws Exception {
return getEntity(path).getId().toString();
final Entity fileEnt = getEntity(path);
if (fileEnt == null) {
return null;
}
return fileEnt.getId().toString();
}
private Entity getEntity(final String path) throws Exception {
......@@ -170,7 +186,12 @@ public class FileSystemResource extends AbstractCaosDBServerResource {
e.setFileProperties(fp);
c.add(e);
final Transaction<?> t = new RetrieveSparseEntityByPath(c);
t.execute();
try {
t.execute();
} catch (NullPointerException npe) {
// For example files in file system without corresponding File records.
return null;
}
final long t2 = System.currentTimeMillis();
getBenchmark().addMeasurement(this.getClass().getSimpleName() + ".getEntity", t2 - t1);
return e;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment