Merge commit '0b471805f017da83d715a0d8409f53bdd009d31e'
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / actions / NewFileDocument.java
1 package org.simantics.document.ui.actions;\r
2 \r
3 import java.io.IOException;\r
4 \r
5 import org.simantics.db.ReadGraph;\r
6 import org.simantics.db.Resource;\r
7 import org.simantics.db.WriteGraph;\r
8 import org.simantics.db.common.request.WriteRequest;\r
9 import org.simantics.db.common.utils.NameUtils;\r
10 import org.simantics.db.exception.DatabaseException;\r
11 import org.simantics.db.layer0.adapter.ActionFactory;\r
12 import org.simantics.document.DocumentResource;\r
13 import org.simantics.graphfile.util.GraphFileUtil;\r
14 import org.simantics.layer0.Layer0;\r
15 import org.simantics.ui.SimanticsUI;\r
16 \r
17 public class NewFileDocument implements ActionFactory {\r
18         Resource relation;\r
19         String defaultName;\r
20         \r
21         public NewFileDocument(ReadGraph graph, String relationUri, String defaultName) throws DatabaseException {\r
22                 relation = graph.getResource(relationUri);\r
23                 this.defaultName = defaultName;\r
24         }\r
25         \r
26         @Override\r
27         public Runnable create(Object target) {\r
28 \r
29                 if(!(target instanceof Resource))\r
30                         return null;\r
31 \r
32                 final Resource resource = (Resource)target;\r
33 \r
34                 return new Runnable() {\r
35                         @Override\r
36                         public void run() {\r
37                                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
38                                         \r
39                                         @Override\r
40                                         public void perform(WriteGraph graph) throws DatabaseException {\r
41                                             graph.markUndoPoint();\r
42                                             \r
43                                                 Layer0 l0 = Layer0.getInstance(graph);\r
44 \r
45                             String name = NameUtils.findFreshName(graph, defaultName, resource, relation);\r
46                                 DocumentResource doc = DocumentResource.getInstance(graph);\r
47                                 \r
48                                 Resource fileResource = graph.newResource();\r
49                                 graph.claim(fileResource, l0.InstanceOf, doc.FileDocument);\r
50                             graph.claimLiteral(fileResource, l0.HasName, name);\r
51                             graph.claim(resource, relation, fileResource);\r
52                             try {\r
53                                 GraphFileUtil.writeDataToGraph(graph, new byte[0], fileResource);\r
54                             } catch (IOException e) {\r
55                                 throw new DatabaseException(e);\r
56                             }\r
57                                         }\r
58                                         \r
59                                 });\r
60                         }\r
61                 };\r
62         }\r
63 }\r