1 package org.simantics.fileimport;
\r
3 import java.nio.file.Path;
\r
4 import java.util.Collection;
\r
5 import java.util.Optional;
\r
7 import org.simantics.Simantics;
\r
8 import org.simantics.databoard.Bindings;
\r
9 import org.simantics.db.ReadGraph;
\r
10 import org.simantics.db.Resource;
\r
11 import org.simantics.db.WriteGraph;
\r
12 import org.simantics.db.common.request.ObjectsWithType;
\r
13 import org.simantics.db.common.request.UniqueRead;
\r
14 import org.simantics.db.common.request.WriteRequest;
\r
15 import org.simantics.db.exception.DatabaseException;
\r
16 import org.simantics.db.exception.RuntimeDatabaseException;
\r
17 import org.simantics.db.layer0.util.RemoverUtil;
\r
18 import org.simantics.db.request.Read;
\r
19 import org.simantics.db.service.SerialisationSupport;
\r
20 import org.simantics.layer0.Layer0;
\r
23 * Most of the implementations should extend this class which handles the storing of
\r
24 * the identifier of the imported entity and the removing of the entity
\r
26 * @author Jani Simomaa
\r
29 public abstract class SimanticsResourceFileImport implements IGenericFileImport {
\r
32 final public Optional<String> perform(Path file) throws Exception {
\r
34 Path dropins = Activator.getDropinsFolder();
\r
37 if (file.toAbsolutePath().toString().startsWith(dropins.toAbsolutePath().toString())) {
\r
38 parts = dropins.relativize(file);
\r
40 parts = file.getFileName();
\r
43 Resource parent = resolveParent(null, parts);
\r
45 return Optional.empty();
\r
46 Optional<Resource> imported = perform(parent, file);
\r
47 if (imported.isPresent()) {
\r
48 return Optional.of(serialize(imported.get()));
\r
50 return Optional.empty();
\r
55 * Performs the import for the given file
\r
57 * @param parent Resource parent of the imported entity in Simantics database
\r
58 * @param file Path file location of file
\r
59 * @return Optional Resource of the imported entity in Simantics database
\r
62 public abstract Optional<Resource> perform(Resource parent, Path file) throws Exception;
\r
65 public void remove(String resourceId) throws Exception {
\r
66 Optional<Resource> resource = deserialize(resourceId);
\r
67 resource.ifPresent(res -> {
\r
69 Simantics.sync(new WriteRequest() {
\r
72 public void perform(WriteGraph graph) throws DatabaseException {
\r
73 RemoverUtil.remove(graph, resource.get());
\r
76 } catch (Exception e) {
\r
77 throw new RuntimeDatabaseException(e);
\r
82 public String serialize(Resource resource) {
\r
83 return Long.toString(resource.getResourceId());
\r
86 public Optional<Resource> deserialize(String serialized) throws Exception {
\r
87 long resourceId = Long.valueOf(serialized);
\r
89 Resource resource = Simantics.getSession().syncRequest(new Read<Resource>() {
\r
92 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
93 SerialisationSupport support = graph.getService(SerialisationSupport.class);
\r
94 Resource resource = support.getResource(resourceId);
\r
98 return Optional.ofNullable(resource);
\r
101 private static Resource resolveParent(Resource parent, Path name) {
\r
102 if (name.getParent() == null) {
\r
103 return Simantics.getProjectResource();
\r
105 name = name.getParent();
\r
106 parent = resolveParent(parent, name);
\r
108 final Resource newParent = parent;
\r
109 final String folderName = name.getFileName().toString();
\r
112 return Simantics.getSession().syncRequest(new UniqueRead<Resource>() {
\r
115 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
116 Layer0 L0 = Layer0.getInstance(graph);
\r
117 Collection<Resource> libraries = graph.sync(new ObjectsWithType(newParent, L0.ConsistsOf, L0.Library));
\r
118 for (Resource library : libraries) {
\r
119 String libraryName = graph.getRelatedValue2(library, L0.HasName, Bindings.STRING);
\r
120 if (libraryName.equals(folderName)) {
\r
127 } catch (DatabaseException e) {
\r
128 e.printStackTrace();
\r