]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/PossibleIndexRoot.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / PossibleIndexRoot.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.exception.DatabaseException;\r
20 import org.simantics.layer0.Layer0;\r
21 \r
22 public class PossibleIndexRoot extends ResourceRead<Resource> {\r
23 \r
24     public PossibleIndexRoot(Resource resource) {\r
25         super(resource);\r
26     }\r
27 \r
28     @Override\r
29     public Resource perform(ReadGraph graph) throws DatabaseException {\r
30 \r
31         Layer0 L0 = Layer0.getInstance(graph);\r
32         \r
33         if (graph.isInstanceOf(resource, L0.IndexRoot))\r
34             return resource;\r
35         \r
36         Collection<Resource> parents = new HashSet<Resource>(graph.getObjects(resource, L0.IsOwnedBy));\r
37         Resource parent = parents.size() == 1 ? parents.iterator().next() : null;\r
38 \r
39         // What is this?\r
40         if(resource.equals(parent))\r
41                 parent = null;\r
42 \r
43         if (parent == null) {\r
44                 \r
45                 if(parents.size() > 1) {\r
46                         // Maybe we have a case where there is ConsistsOf and HasProperty that is incorrectly classified as IsComposedOf\r
47                         parent = graph.getPossibleObject(resource, L0.PartOf);\r
48                 } \r
49 \r
50                 // Assertion object?\r
51                 if(parent == null)\r
52                         parent = graph.getPossibleObject(resource, L0.HasObjectInverse);\r
53                 \r
54                 if(parent == null) return null;\r
55                 \r
56         }\r
57         \r
58         return graph.syncRequest(new PossibleIndexRoot(parent));\r
59 \r
60     }\r
61 \r
62 }\r