-package org.simantics.fileimport;\r
-\r
-import java.nio.file.Path;\r
-import java.util.Collection;\r
-import java.util.Optional;\r
-\r
-import org.simantics.Simantics;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.ObjectsWithType;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.RuntimeDatabaseException;\r
-import org.simantics.db.layer0.util.RemoverUtil;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.db.service.SerialisationSupport;\r
-import org.simantics.layer0.Layer0;\r
-\r
-/**\r
- * Most of the implementations should extend this class which handles the storing of\r
- * the identifier of the imported entity and the removing of the entity\r
- * \r
- * @author Jani Simomaa\r
- *\r
- */\r
-public abstract class SimanticsResourceFileImport implements IGenericFileImport {\r
-\r
- @Override\r
- final public Optional<String> perform(Path file) throws Exception {\r
- \r
- Path dropins = Activator.getDropinsFolder();\r
- Path parts = dropins.relativize(file);\r
- Resource parent = resolveParent(null, parts);\r
- if (parent == null)\r
- return Optional.empty();\r
- Optional<Resource> imported = perform(parent, file); \r
- if (imported.isPresent()) {\r
- return Optional.of(serialize(imported.get()));\r
- } else {\r
- return Optional.empty();\r
- }\r
- }\r
- \r
- /**\r
- * Performs the import for the given file\r
- * \r
- * @param parent Resource parent of the imported entity in Simantics database\r
- * @param file Path file location of file\r
- * @return Optional Resource of the imported entity in Simantics database\r
- * @throws Exception\r
- */\r
- public abstract Optional<Resource> perform(Resource parent, Path file) throws Exception;\r
- \r
- @Override\r
- public void remove(String resourceId) throws Exception {\r
- Optional<Resource> resource = deserialize(resourceId);\r
- resource.ifPresent(res -> {\r
- try {\r
- Simantics.sync(new WriteRequest() {\r
-\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- RemoverUtil.remove(graph, resource.get());\r
- }\r
- });\r
- } catch (Exception e) {\r
- throw new RuntimeDatabaseException(e);\r
- }\r
- });\r
- }\r
-\r
- public String serialize(Resource resource) {\r
- return Long.toString(resource.getResourceId());\r
- }\r
-\r
- public Optional<Resource> deserialize(String serialized) throws Exception {\r
- long resourceId = Long.valueOf(serialized);\r
-\r
- Resource resource = Simantics.getSession().syncRequest(new Read<Resource>() {\r
-\r
- @Override\r
- public Resource perform(ReadGraph graph) throws DatabaseException {\r
- SerialisationSupport support = graph.getService(SerialisationSupport.class);\r
- Resource resource = support.getResource(resourceId);\r
- return resource;\r
- }\r
- });\r
- return Optional.ofNullable(resource);\r
- }\r
- \r
- private static Resource resolveParent(Resource parent, Path name) {\r
- if (name.getParent() == null) {\r
- return Simantics.getProjectResource();\r
- } else {\r
- name = name.getParent();\r
- parent = resolveParent(parent, name);\r
- }\r
- final Resource newParent = parent;\r
- final String folderName = name.getFileName().toString();\r
-\r
- try {\r
- return Simantics.getSession().syncRequest(new UniqueRead<Resource>() {\r
-\r
- @Override\r
- public Resource perform(ReadGraph graph) throws DatabaseException {\r
- Layer0 L0 = Layer0.getInstance(graph);\r
- Collection<Resource> libraries = graph.sync(new ObjectsWithType(newParent, L0.ConsistsOf, L0.Library));\r
- for (Resource library : libraries) {\r
- String libraryName = graph.getRelatedValue2(library, L0.HasName, Bindings.STRING);\r
- if (libraryName.equals(folderName)) {\r
- return library;\r
- }\r
- }\r
- return null;\r
- }\r
- });\r
- } catch (DatabaseException e) {\r
- e.printStackTrace();\r
- return null;\r
- }\r
- }\r
-\r
-}\r
+package org.simantics.fileimport;
+
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.Optional;
+
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.RuntimeDatabaseException;
+import org.simantics.db.layer0.util.RemoverUtil;
+import org.simantics.db.request.Read;
+import org.simantics.db.service.SerialisationSupport;
+import org.simantics.layer0.Layer0;
+
+/**
+ * Most of the implementations should extend this class which handles the storing of
+ * the identifier of the imported entity and the removing of the entity
+ *
+ * @author Jani Simomaa
+ *
+ */
+public abstract class SimanticsResourceFileImport implements IGenericFileImport {
+
+ @Override
+ final public Optional<String> perform(Path file) throws Exception {
+
+ Path dropins = Activator.getDropinsFolder();
+
+ Path parts;
+ if (file.toAbsolutePath().toString().startsWith(dropins.toAbsolutePath().toString())) {
+ parts = dropins.relativize(file);
+ } else {
+ parts = file.getFileName();
+ }
+
+ Resource parent = resolveParent(null, parts);
+ if (parent == null)
+ return Optional.empty();
+ Optional<Resource> imported = perform(parent, file);
+ if (imported.isPresent()) {
+ return Optional.of(serialize(imported.get()));
+ } else {
+ return Optional.empty();
+ }
+ }
+
+ /**
+ * Performs the import for the given file
+ *
+ * @param parent Resource parent of the imported entity in Simantics database
+ * @param file Path file location of file
+ * @return Optional Resource of the imported entity in Simantics database
+ * @throws Exception
+ */
+
+ @Override
+ public void remove(String resourceId) throws Exception {
+ Optional<Resource> resource = deserialize(resourceId);
+ resource.ifPresent(res -> {
+ try {
+ Simantics.sync(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ RemoverUtil.remove(graph, resource.get());
+ }
+ });
+ } catch (Exception e) {
+ throw new RuntimeDatabaseException(e);
+ }
+ });
+ }
+
+ public String serialize(Resource resource) {
+ return Long.toString(resource.getResourceId());
+ }
+
+ public Optional<Resource> deserialize(String serialized) throws Exception {
+ long resourceId = Long.valueOf(serialized);
+
+ Resource resource = Simantics.getSession().syncRequest(new Read<Resource>() {
+
+ @Override
+ public Resource perform(ReadGraph graph) throws DatabaseException {
+ SerialisationSupport support = graph.getService(SerialisationSupport.class);
+ Resource resource = support.getResource(resourceId);
+ return resource;
+ }
+ });
+ return Optional.ofNullable(resource);
+ }
+
+ private static Resource resolveParent(Resource parent, Path name) {
+ if (name.getParent() == null) {
+ return Simantics.getProjectResource();
+ } else {
+ name = name.getParent();
+ parent = resolveParent(parent, name);
+ }
+ final Resource newParent = parent;
+ final String folderName = name.getFileName().toString();
+
+ try {
+ return Simantics.getSession().syncRequest(new UniqueRead<Resource>() {
+
+ @Override
+ public Resource perform(ReadGraph graph) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ Collection<Resource> libraries = graph.sync(new ObjectsWithType(newParent, L0.ConsistsOf, L0.Library));
+ for (Resource library : libraries) {
+ String libraryName = graph.getRelatedValue2(library, L0.HasName, Bindings.STRING);
+ if (libraryName.equals(folderName)) {
+ return library;
+ }
+ }
+ return null;
+ }
+ });
+ } catch (DatabaseException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ public abstract Resource defaultParentResource();
+
+}