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=67ff51dab9c029ae9ef7e1222bf83968058d220f;hp=2836b670e5f0e5b5198b2cce95e01d97e8d39a07;hb=664f37a026967c90a9a8a4ef3c5336ee426f67aa;hpb=86e91ef31d25e4d5950cae130754846d15119c61 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 2836b670e..67ff51dab 100644 --- a/bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java +++ b/bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java @@ -1,111 +1,136 @@ -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; - -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(); - } - } - - public abstract Optional perform(Resource parent, Path file); - - @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 performWithDefaultParent(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 = defaultParentResource(); + if(parent == null) + 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; + } + } + +}