--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.document.ui.actions;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.ActionFactory;\r
+import org.simantics.document.DocumentResource;\r
+import org.simantics.document.ui.graphfile.DocumentVersionUtils;\r
+import org.simantics.document.ui.graphfile.FileDocumentUtil;\r
+\r
+/**\r
+ * Abstract base class for adding documents.\r
+ * \r
+ * If import target is an document, a new version of a document is created.\r
+ * \r
+ * Document version creation has two modes:\r
+ * FLAT: new version is added to the same library as the old document.\r
+ * TREE: new version replaces the old document in the library, and old document is moved under the new document.\r
+ * \r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ *\r
+ */\r
+public abstract class AddDocumentAction implements ActionFactory {\r
+ \r
+\r
+ \r
+ \r
+ private Resource relation;\r
+ \r
+ public AddDocumentAction(ReadGraph graph, String relationUri) throws DatabaseException {\r
+ relation = graph.getResource(relationUri);\r
+ }\r
+ \r
+\r
+ protected Resource getRelation() {\r
+ return relation;\r
+ }\r
+ \r
+ protected void linkDocument(WriteGraph graph, Resource target, Resource newDocument) throws DatabaseException{\r
+ DocumentResource doc = DocumentResource.getInstance(graph);\r
+ \r
+ if (graph.isInstanceOf(target, doc.Document)) {\r
+ // adding a new revision\r
+ DocumentVersionUtils.createNewVersion(graph, target, newDocument, relation);\r
+ \r
+ \r
+ } else {\r
+ graph.claim(target, relation, newDocument);\r
+ FileDocumentUtil.createUniqueName(graph, newDocument);\r
+ }\r
+ }\r
+\r
+}\r