]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fileimport/src/org/simantics/fileimport/SimanticsResourceFileImport.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.fileimport / src / org / simantics / fileimport / SimanticsResourceFileImport.java
1 package org.simantics.fileimport;
2
3 import java.nio.file.Path;
4 import java.util.Collection;
5 import java.util.Optional;
6
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;
21
22 /**
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
25  * 
26  * @author Jani Simomaa
27  *
28  */
29 public abstract class SimanticsResourceFileImport implements IGenericFileImport {
30
31     @Override
32     final public Optional<String> performWithDefaultParent(Path file) throws Exception {
33         
34         Path dropins = Activator.getDropinsFolder();
35         
36         Path parts;
37         if (file.toAbsolutePath().toString().startsWith(dropins.toAbsolutePath().toString())) {
38                 parts = dropins.relativize(file);
39         } else {
40                 parts = file.getFileName();
41         }
42         
43         Resource parent = defaultParentResource();
44         if(parent == null)
45                 parent = resolveParent(null, parts);
46         
47         if (parent == null)
48             return Optional.empty();
49         
50         Optional<Resource> imported = perform(parent, file); 
51         if (imported.isPresent()) {
52             return Optional.of(serialize(imported.get()));
53         } else {
54             return Optional.empty();
55         }
56     }
57     
58     /**
59      * Performs the import for the given file
60      * 
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
64      * @throws Exception
65      */
66     
67     @Override
68     public void remove(String resourceId) throws Exception {
69         Optional<Resource> resource = deserialize(resourceId);
70         resource.ifPresent(res -> {
71             try {
72                 Simantics.sync(new WriteRequest() {
73
74                     @Override
75                     public void perform(WriteGraph graph) throws DatabaseException {
76                         RemoverUtil.remove(graph, resource.get());
77                     }
78                 });
79             } catch (Exception e) {
80                 throw new RuntimeDatabaseException(e);
81             }
82         });
83     }
84
85     public String serialize(Resource resource) {
86         return Long.toString(resource.getResourceId());
87     }
88
89     public Optional<Resource> deserialize(String serialized) throws Exception {
90         long resourceId = Long.valueOf(serialized);
91
92         Resource resource = Simantics.getSession().syncRequest(new Read<Resource>() {
93
94             @Override
95             public Resource perform(ReadGraph graph) throws DatabaseException {
96                 SerialisationSupport support = graph.getService(SerialisationSupport.class);
97                 Resource resource = support.getResource(resourceId);
98                 return resource;
99             }
100         });
101         return Optional.ofNullable(resource);
102     }
103     
104     private static Resource resolveParent(Resource parent, Path name) {
105         if (name.getParent() == null) {
106             return Simantics.getProjectResource();
107         } else {
108             name = name.getParent();
109             parent = resolveParent(parent, name);
110         }
111         final Resource newParent = parent;
112         final String folderName = name.getFileName().toString();
113
114         try {
115             return Simantics.getSession().syncRequest(new UniqueRead<Resource>() {
116
117                 @Override
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)) {
124                             return library;
125                         }
126                     }
127                     return null;
128                 }
129             });
130         } catch (DatabaseException e) {
131             e.printStackTrace();
132             return null;
133         }
134     }
135
136 }