Merge commit '8e4e41fa135641b23f68e205832e0696951c5f63'
[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.DirectoryStream;\r
5 import java.nio.file.Files;\r
6 import java.nio.file.Path;\r
7 import java.util.HashMap;\r
8 import java.util.Iterator;\r
9 import java.util.Map;\r
10 import java.util.Optional;\r
11 \r
12 import org.simantics.Simantics;\r
13 import org.simantics.databoard.util.Base64;\r
14 import org.simantics.db.ReadGraph;\r
15 import org.simantics.db.Resource;\r
16 import org.simantics.db.common.request.UniqueRead;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.db.service.SerialisationSupport;\r
19 import org.simantics.fileimport.Activator;\r
20 import org.simantics.fileimport.FileImportService;\r
21 import org.simantics.fileimport.dropins.FileImportDropins;\r
22 import org.simantics.layer0.Layer0;\r
23 import org.simantics.utils.FileUtils;\r
24 \r
25 /**\r
26  * SCL interface for Simantics File Import Functionality\r
27  * See <code>"Dropins/Core"</code> SCL module for more\r
28  * \r
29  * @author Jani Simomaa\r
30  *\r
31  */\r
32 public class DropinsSCL {\r
33 \r
34     public static void watchDropinsFolder() {\r
35         FileImportDropins.watchDropinsFolder();\r
36     }\r
37 \r
38     public static void unwatchDropinsFolder() {\r
39         FileImportDropins.unwatchDropinsFolder();\r
40     }\r
41 \r
42     public static void uploadToDropinsBase64(String base64, String fileName) {\r
43         // ensure that watcher is awake\r
44         FileImportDropins.watchDropinsFolder();\r
45         try {\r
46             Path rootFolder = Activator.getDropinsFolder();\r
47             Path newFile = rootFolder.resolve(fileName);\r
48             if (Files.exists(newFile)) {\r
49                 newFile = findFreshFileName(rootFolder, fileName);\r
50             }\r
51             byte[] bytes = Base64.decode(base64);\r
52             FileUtils.writeFile(newFile.toFile(), bytes);\r
53         } catch (IOException e) {\r
54             e.printStackTrace();\r
55         }\r
56     }\r
57 \r
58     private static Path findFreshFileName(Path root, String fileName) throws IOException {\r
59         int ending = fileName.lastIndexOf('.');\r
60         String glob = fileName;\r
61         String suffix = "";\r
62         if (ending > -1) {\r
63             glob = fileName.substring(0, ending);\r
64             suffix = fileName.substring(ending);\r
65         }\r
66         int i = 0;\r
67         try (DirectoryStream<Path> stream = Files.newDirectoryStream(root, glob)) {\r
68             Iterator<Path> iter = stream.iterator();\r
69             while (iter.hasNext()) {\r
70                 iter.next();\r
71                 i++;\r
72             }\r
73         }\r
74         String newFileName = glob + "_" + i + suffix;\r
75         return root.resolve(newFileName);\r
76     }\r
77 \r
78     public static Map<String, Long> getUploadedFiles() throws DatabaseException {\r
79         Map<String, String> results = FileImportService.getPathsAndResources();\r
80         Map<String, Long> result = Simantics.getSession().syncRequest(new UniqueRead<Map<String, Long>>() {\r
81 \r
82             @Override\r
83             public Map<String, Long> perform(ReadGraph graph) throws DatabaseException {\r
84                 Map<String, Long> map = new HashMap<>();\r
85                 for (Map.Entry<String, String> entry : results.entrySet()) {\r
86                     String value = (String) entry.getValue();\r
87                     Long id = Long.valueOf(value);\r
88                     SerialisationSupport ss = graph.getService(SerialisationSupport.class);\r
89                     try {\r
90                         Resource r = ss.getResource(id);\r
91                         String name = graph.getRelatedValue(r, Layer0.getInstance(graph).HasName);\r
92                         map.put(name, id);\r
93                     } catch (DatabaseException e) {\r
94                         e.printStackTrace();\r
95                     }\r
96                 }\r
97                 return map;\r
98             }\r
99         });\r
100         return result;\r
101     }\r
102 \r
103     public static void removeFileForId(long id) {\r
104         FileImportService.removeFileForResource(id, Optional.empty());\r
105     }\r
106 \r
107 }\r