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