Merge remote-tracking branch 'origin/svn'
[simantics/platform.git] / bundles / org.simantics.fileimport / src / org / simantics / fileimport / scl / DropinsSCL.java
1 package org.simantics.fileimport.scl;\r
2 \r
3 import java.io.IOException;\r
4 import java.nio.file.Path;\r
5 import java.util.HashMap;\r
6 import java.util.Map;\r
7 import java.util.Optional;\r
8 \r
9 import org.simantics.Simantics;\r
10 import org.simantics.databoard.util.Base64;\r
11 import org.simantics.db.ReadGraph;\r
12 import org.simantics.db.Resource;\r
13 import org.simantics.db.common.request.UniqueRead;\r
14 import org.simantics.db.exception.DatabaseException;\r
15 import org.simantics.db.service.SerialisationSupport;\r
16 import org.simantics.fileimport.Activator;\r
17 import org.simantics.fileimport.FileImportService;\r
18 import org.simantics.fileimport.dropins.FileImportDropins;\r
19 import org.simantics.layer0.Layer0;\r
20 import org.simantics.utils.FileUtils;\r
21 \r
22 /**\r
23  * SCL interface for Simantics File Import Functionality\r
24  * See <code>"Dropins/Core"</code> SCL module for more\r
25  * \r
26  * @author Jani Simomaa\r
27  *\r
28  */\r
29 public class DropinsSCL {\r
30 \r
31     public static void watchDropinsFolder() {\r
32         FileImportDropins.watchDropinsFolder();\r
33     }\r
34     \r
35     public static void unwatchDropinsFolder() {\r
36         FileImportDropins.unwatchDropinsFolder();\r
37     }\r
38     \r
39     public static void uploadToDropinsBase64(String base64, String fileName) {\r
40         // ensure that watcher is awake\r
41         FileImportDropins.watchDropinsFolder();\r
42         try {\r
43             Path rootFolder = Activator.getDropinsFolder();\r
44             byte[] bytes = Base64.decode(base64);\r
45             FileUtils.writeFile(rootFolder.resolve(fileName).toFile(), bytes);\r
46             \r
47         } catch (IOException e) {\r
48             e.printStackTrace();\r
49         }\r
50     }\r
51     \r
52     public static Map<String, Long> getUploadedFiles() throws DatabaseException {\r
53         Map<String, String> results = FileImportService.getPathsAndResources();\r
54         Map<String, Long> result = Simantics.getSession().syncRequest(new UniqueRead<Map<String, Long>>() {\r
55 \r
56             @Override\r
57             public Map<String, Long> perform(ReadGraph graph) throws DatabaseException {\r
58                 Map<String, Long> map = new HashMap<>();\r
59                 for (Map.Entry<String, String> entry : results.entrySet()) {\r
60                     String value = (String) entry.getValue();\r
61                     Long id = Long.valueOf(value);\r
62                     SerialisationSupport ss = graph.getService(SerialisationSupport.class);\r
63                     try {\r
64                         Resource r = ss.getResource(id);\r
65                         String name = graph.getRelatedValue(r, Layer0.getInstance(graph).HasName);\r
66                         map.put(name, id);\r
67                     } catch (DatabaseException e) {\r
68                         e.printStackTrace();\r
69                     }\r
70                 }\r
71                 return map;\r
72             }\r
73         });\r
74         return result;\r
75     }\r
76     \r
77     public static void removeFileForId(long id) {\r
78         FileImportService.removeFileForResource(id, Optional.empty());\r
79     }\r
80     \r
81 }\r