1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics;
\r
14 import org.simantics.db.ReadGraph;
\r
15 import org.simantics.db.Resource;
\r
16 import org.simantics.db.WriteOnlyGraph;
\r
17 import org.simantics.db.common.WriteBindings;
\r
18 import org.simantics.db.exception.DatabaseException;
\r
19 import org.simantics.db.exception.RuntimeDatabaseException;
\r
20 import org.simantics.db.service.SerialisationSupport;
\r
21 import org.simantics.graph.db.AbstractImportAdvisor2;
\r
22 import org.simantics.graph.db.TransferableGraphImporter;
\r
23 import org.simantics.graph.representation.Root;
\r
24 import org.simantics.layer0.Layer0;
\r
25 import org.simantics.project.management.DatabaseManagement;
\r
26 import org.simantics.project.management.GraphBundle;
\r
28 public class OntologyImportAdvisor extends AbstractImportAdvisor2 {
\r
30 final private GraphBundle tg;
\r
31 final private DatabaseManagement mgmt;
\r
32 final private String rootName;
\r
33 private Resource ontology;
\r
34 private Resource ontologyName;
\r
36 public OntologyImportAdvisor(GraphBundle tg, DatabaseManagement mgmt) {
\r
39 if(tg.getName().contains("@")) {
\r
40 rootName = tg.getName().substring(tg.getName().lastIndexOf("/")+1);
\r
42 rootName = tg.getName().substring(tg.getName().lastIndexOf("/")+1) + "-" + tg.getMajor() + "." + tg.getMinor();
\r
47 public Resource getTarget() {
\r
52 public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
\r
57 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
\r
58 return createRoot(graph, root, null);
\r
62 public Resource createRoot(WriteOnlyGraph graph, Root root, Resource resource) throws DatabaseException {
\r
63 if(resource == null) resource = graph.newResource();
\r
64 addRootInfo(root, root.name, resource);
\r
69 public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
\r
70 ontology = graph.newResource(graph.getRootLibrary());
\r
71 ontologyName = graph.newResource(graph.getRootLibrary());
\r
72 graph.newClusterSet(ontology);
\r
73 graph.setClusterSet4NewResource(ontology);
\r
77 public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
\r
80 long[] ids = process.getResourceIds(graph.getService(SerialisationSupport.class));
\r
81 tg.setResourceArray(ids);
\r
82 mgmt.createGraphBundle(graph, tg);
\r
83 //graph.flushCluster();
\r
84 } catch (DatabaseException e) {
\r
85 throw new RuntimeDatabaseException(e);
\r
91 public boolean allowImmutableModifications() {
\r
96 public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent,
\r
97 String name) throws DatabaseException {
\r
98 if(rootName.equals(name)) {
\r
100 Layer0 L0 = graph.getService(Layer0.class);
\r
101 graph.claim(ontologyName, L0.InstanceOf, null, L0.String);
\r
102 graph.claimValue(ontologyName, name, WriteBindings.STRING);
\r
103 graph.claim(ontology, L0.HasName, L0.NameOf, ontologyName);
\r
108 return process.createChild(graph, parent, null, name);
\r
114 public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent, Resource child,
\r
115 String name) throws DatabaseException {
\r
117 if(rootName.equals(name)) {
\r
119 Layer0 L0 = graph.getService(Layer0.class);
\r
120 graph.claim(ontologyName, L0.InstanceOf, null, L0.String);
\r
121 graph.claimValue(ontologyName, name, WriteBindings.STRING);
\r
122 graph.claim(ontology, L0.HasName, L0.NameOf, ontologyName);
\r
127 return process.createChild(graph, parent, null, name);
\r