]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document/src/org/simantics/document/AddDocumentAction.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.document / src / org / simantics / document / AddDocumentAction.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.document;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.adapter.ActionFactory;
19
20 /**
21  * Abstract base class for adding documents.
22  * 
23  * If import target is an document, a new version of a document is created.
24  * 
25  * Document version creation has two modes:
26  * FLAT: new version is added to the same library as the old document.
27  * TREE: new version replaces the old document in the library, and old document is moved under the new document.
28  * 
29  * 
30  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
31  *
32  */
33 public abstract class AddDocumentAction implements ActionFactory {
34         
35         private Resource relation;
36         
37         public AddDocumentAction(ReadGraph graph, String relationUri) throws DatabaseException {
38                 relation = graph.getResource(relationUri);
39         }
40         
41
42         protected Resource getRelation() {
43                 return relation;
44         }
45         
46         protected void linkDocument(WriteGraph graph, Resource target, Resource newDocument) throws DatabaseException{
47                 DocumentResource doc = DocumentResource.getInstance(graph);
48         
49                 if (graph.isInstanceOf(target, doc.Document)) {
50                         // adding a new revision
51                         DocumentVersionUtils.createNewVersion(graph, target, newDocument, relation);
52                         
53                         
54                 } else {
55                         graph.claim(target, relation, newDocument);
56                         FileDocumentUtil.createUniqueName(graph, newDocument);
57                 }
58         }
59
60 }