Sync git svn branch with SVN repository r33189.
[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         Path parts = dropins.relativize(file);\r
36         Resource parent = resolveParent(null, parts);\r
37         if (parent == null)\r
38             return Optional.empty();\r
39         Optional<Resource> imported = perform(parent, file); \r
40         if (imported.isPresent()) {\r
41             return Optional.of(serialize(imported.get()));\r
42         } else {\r
43             return Optional.empty();\r
44         }\r
45     }\r
46     \r
47     /**\r
48      * Performs the import for the given file\r
49      * \r
50      * @param parent Resource parent of the imported entity in Simantics database\r
51      * @param file Path file location of file\r
52      * @return Optional Resource of the imported entity in Simantics database\r
53      * @throws Exception\r
54      */\r
55     public abstract Optional<Resource> perform(Resource parent, Path file) throws Exception;\r
56     \r
57     @Override\r
58     public void remove(String resourceId) throws Exception {\r
59         Optional<Resource> resource = deserialize(resourceId);\r
60         resource.ifPresent(res -> {\r
61             try {\r
62                 Simantics.sync(new WriteRequest() {\r
63 \r
64                     @Override\r
65                     public void perform(WriteGraph graph) throws DatabaseException {\r
66                         RemoverUtil.remove(graph, resource.get());\r
67                     }\r
68                 });\r
69             } catch (Exception e) {\r
70                 throw new RuntimeDatabaseException(e);\r
71             }\r
72         });\r
73     }\r
74 \r
75     public String serialize(Resource resource) {\r
76         return Long.toString(resource.getResourceId());\r
77     }\r
78 \r
79     public Optional<Resource> deserialize(String serialized) throws Exception {\r
80         long resourceId = Long.valueOf(serialized);\r
81 \r
82         Resource resource = Simantics.getSession().syncRequest(new Read<Resource>() {\r
83 \r
84             @Override\r
85             public Resource perform(ReadGraph graph) throws DatabaseException {\r
86                 SerialisationSupport support = graph.getService(SerialisationSupport.class);\r
87                 Resource resource = support.getResource(resourceId);\r
88                 return resource;\r
89             }\r
90         });\r
91         return Optional.ofNullable(resource);\r
92     }\r
93     \r
94     private static Resource resolveParent(Resource parent, Path name) {\r
95         if (name.getParent() == null) {\r
96             return Simantics.getProjectResource();\r
97         } else {\r
98             name = name.getParent();\r
99             parent = resolveParent(parent, name);\r
100         }\r
101         final Resource newParent = parent;\r
102         final String folderName = name.getFileName().toString();\r
103 \r
104         try {\r
105             return Simantics.getSession().syncRequest(new UniqueRead<Resource>() {\r
106 \r
107                 @Override\r
108                 public Resource perform(ReadGraph graph) throws DatabaseException {\r
109                     Layer0 L0 = Layer0.getInstance(graph);\r
110                     Collection<Resource> libraries = graph.sync(new ObjectsWithType(newParent, L0.ConsistsOf, L0.Library));\r
111                     for (Resource library : libraries) {\r
112                         String libraryName = graph.getRelatedValue2(library, L0.HasName, Bindings.STRING);\r
113                         if (libraryName.equals(folderName)) {\r
114                             return library;\r
115                         }\r
116                     }\r
117                     return null;\r
118                 }\r
119             });\r
120         } catch (DatabaseException e) {\r
121             e.printStackTrace();\r
122             return null;\r
123         }\r
124     }\r
125 \r
126 }\r