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