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