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> perform(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 = resolveParent(null, parts);
45 return Optional.empty();
46 Optional<Resource> imported = perform(parent, file);
47 if (imported.isPresent()) {
48 return Optional.of(serialize(imported.get()));
50 return Optional.empty();
55 * Performs the import for the given file
57 * @param parent Resource parent of the imported entity in Simantics database
58 * @param file Path file location of file
59 * @return Optional Resource of the imported entity in Simantics database
64 public void remove(String resourceId) throws Exception {
65 Optional<Resource> resource = deserialize(resourceId);
66 resource.ifPresent(res -> {
68 Simantics.sync(new WriteRequest() {
71 public void perform(WriteGraph graph) throws DatabaseException {
72 RemoverUtil.remove(graph, resource.get());
75 } catch (Exception e) {
76 throw new RuntimeDatabaseException(e);
81 public String serialize(Resource resource) {
82 return Long.toString(resource.getResourceId());
85 public Optional<Resource> deserialize(String serialized) throws Exception {
86 long resourceId = Long.valueOf(serialized);
88 Resource resource = Simantics.getSession().syncRequest(new Read<Resource>() {
91 public Resource perform(ReadGraph graph) throws DatabaseException {
92 SerialisationSupport support = graph.getService(SerialisationSupport.class);
93 Resource resource = support.getResource(resourceId);
97 return Optional.ofNullable(resource);
100 private static Resource resolveParent(Resource parent, Path name) {
101 if (name.getParent() == null) {
102 return Simantics.getProjectResource();
104 name = name.getParent();
105 parent = resolveParent(parent, name);
107 final Resource newParent = parent;
108 final String folderName = name.getFileName().toString();
111 return Simantics.getSession().syncRequest(new UniqueRead<Resource>() {
114 public Resource perform(ReadGraph graph) throws DatabaseException {
115 Layer0 L0 = Layer0.getInstance(graph);
116 Collection<Resource> libraries = graph.sync(new ObjectsWithType(newParent, L0.ConsistsOf, L0.Library));
117 for (Resource library : libraries) {
118 String libraryName = graph.getRelatedValue2(library, L0.HasName, Bindings.STRING);
119 if (libraryName.equals(folderName)) {
126 } catch (DatabaseException e) {
132 public abstract Resource defaultParentResource();