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