public class NewFileDocument implements ActionFactory {
Resource relation;
String defaultName;
-
+
public NewFileDocument(ReadGraph graph, String relationUri, String defaultName) throws DatabaseException {
relation = graph.getResource(relationUri);
this.defaultName = defaultName;
}
-
+
@Override
public Runnable create(Object target) {
@Override
public void run() {
Simantics.getSession().asyncRequest(new WriteRequest() {
-
+
@Override
public void perform(WriteGraph graph) throws DatabaseException {
- graph.markUndoPoint();
-
- Layer0 l0 = Layer0.getInstance(graph);
-
- String name = NameUtils.findFreshName(graph, defaultName, resource, relation);
- DocumentResource doc = DocumentResource.getInstance(graph);
-
- Resource fileResource = graph.newResource();
- graph.claim(fileResource, l0.InstanceOf, doc.FileDocument);
- graph.claimLiteral(fileResource, l0.HasName, name);
- graph.claim(resource, relation, fileResource);
- try {
- GraphFileUtil.writeDataToGraph(graph, new byte[0], fileResource);
- } catch (IOException e) {
- throw new DatabaseException(e);
- }
+ create(graph, resource, relation, defaultName);
}
-
+
});
}
};
}
+
+ public static Resource create(WriteGraph graph, Resource resource, Resource relation, String defaultName) throws DatabaseException {
+
+ graph.markUndoPoint();
+
+ Layer0 l0 = Layer0.getInstance(graph);
+
+ String name = NameUtils.findFreshName(graph, defaultName, resource, relation);
+ DocumentResource doc = DocumentResource.getInstance(graph);
+
+ Resource fileResource = graph.newResource();
+ graph.claim(fileResource, l0.InstanceOf, doc.FileDocument);
+ graph.claimLiteral(fileResource, l0.HasName, name);
+ graph.claim(resource, relation, fileResource);
+ try {
+ GraphFileUtil.writeDataToGraph(graph, new byte[0], fileResource);
+ } catch (IOException e) {
+ throw new DatabaseException(e);
+ }
+
+ return fileResource;
+
+ }
+
}