X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.fileimport%2Fsrc%2Forg%2Fsimantics%2Ffileimport%2FSimanticsResourceFileImport.java;h=4d2bcbe69105ff9a402931446718ef5e70a4c7b8;hp=70439ef1db950a3861a2c7452a26d42f936882b5;hb=0d82e7808541ed486f6027a28038d44d2d93711a;hpb=749a1ea2c0a66ae41aad617d1fd81b61cbdb7685 diff --git a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java index 70439ef1d..4d2bcbe69 100644 --- a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java +++ b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java @@ -1,126 +1,134 @@ -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 perform(Path file) throws Exception { - - Path dropins = Activator.getDropinsFolder(); - Path parts = dropins.relativize(file); - Resource parent = resolveParent(null, parts); - if (parent == null) - return Optional.empty(); - Optional 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 - */ - public abstract Optional perform(Resource parent, Path file) throws Exception; - - @Override - public void remove(String resourceId) throws Exception { - Optional 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 deserialize(String serialized) throws Exception { - long resourceId = Long.valueOf(serialized); - - Resource resource = Simantics.getSession().syncRequest(new Read() { - - @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() { - - @Override - public Resource perform(ReadGraph graph) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - Collection 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; - } - } - -} +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 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 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 = 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 deserialize(String serialized) throws Exception { + long resourceId = Long.valueOf(serialized); + + Resource resource = Simantics.getSession().syncRequest(new Read() { + + @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() { + + @Override + public Resource perform(ReadGraph graph) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + Collection 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(); + +}