]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/IndexRoot.java
Automatically import also SCLMain modules of dependent index roots
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / IndexRoot.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.db.common.request;
13
14 import java.util.Collection;
15 import java.util.HashSet;
16
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.utils.NameUtils;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.layer0.Layer0;
22
23 public class IndexRoot extends ResourceRead<Resource> {
24
25     public IndexRoot(Resource resource) {
26         super(resource);
27     }
28
29     @Override
30     public Resource perform(ReadGraph graph) throws DatabaseException {
31
32         Layer0 L0 = Layer0.getInstance(graph);
33         
34         if (graph.isInstanceOf(resource, L0.IndexRoot))
35             return resource;
36         
37         Collection<Resource> parents = new HashSet<Resource>(graph.getObjects(resource, L0.IsOwnedBy));
38         Resource parent = parents.size() == 1 ? parents.iterator().next() : null;
39         
40         // What is this?
41         if(resource.equals(parent))
42                 parent = null;
43
44         if (parent == null) {
45
46                 if(parents.size() > 1) {
47                         // Maybe we have a case where there is ConsistsOf and HasProperty that is incorrectly classified as IsComposedOf
48                         parent = graph.getPossibleObject(resource, L0.PartOf);
49                 } 
50
51                 // Assertion object?
52                 if(parent == null)
53                         parent = graph.getPossibleObject(resource, L0.HasObjectInverse);
54                 
55                 if(parent == null)
56                         throw new DatabaseException("Index root was not found for " + NameUtils.getSafeName(graph, resource, true));
57                 
58         } 
59         
60         try {
61                 return graph.syncRequest(new IndexRoot(parent));
62         } catch (DatabaseException e) {
63                 throw new DatabaseException("Index root was not found for " + NameUtils.getSafeName(graph, resource, true));
64         }
65
66     }
67
68 }