]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/AddDocumentAction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / actions / 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.ui.actions;
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 import org.simantics.document.DocumentResource;
20 import org.simantics.document.ui.graphfile.DocumentVersionUtils;
21 import org.simantics.document.ui.graphfile.FileDocumentUtil;
22
23 /**
24  * Abstract base class for adding documents.
25  * 
26  * If import target is an document, a new version of a document is created.
27  * 
28  * Document version creation has two modes:
29  * FLAT: new version is added to the same library as the old document.
30  * TREE: new version replaces the old document in the library, and old document is moved under the new document.
31  * 
32  * 
33  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
34  *
35  */
36 public abstract class AddDocumentAction implements ActionFactory {
37         
38
39         
40         
41         private Resource relation;
42         
43         public AddDocumentAction(ReadGraph graph, String relationUri) throws DatabaseException {
44                 relation = graph.getResource(relationUri);
45         }
46         
47
48         protected Resource getRelation() {
49                 return relation;
50         }
51         
52         protected void linkDocument(WriteGraph graph, Resource target, Resource newDocument) throws DatabaseException{
53                 DocumentResource doc = DocumentResource.getInstance(graph);
54         
55                 if (graph.isInstanceOf(target, doc.Document)) {
56                         // adding a new revision
57                         DocumentVersionUtils.createNewVersion(graph, target, newDocument, relation);
58                         
59                         
60                 } else {
61                         graph.claim(target, relation, newDocument);
62                         FileDocumentUtil.createUniqueName(graph, newDocument);
63                 }
64         }
65
66 }