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.db.common.request;
\r
14 import java.util.Collection;
\r
15 import java.util.HashSet;
\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
23 public class IndexRoot extends ResourceRead<Resource> {
\r
25 public IndexRoot(Resource resource) {
\r
30 public Resource perform(ReadGraph graph) throws DatabaseException {
\r
32 Layer0 L0 = Layer0.getInstance(graph);
\r
34 if (graph.isInstanceOf(resource, L0.IndexRoot))
\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
41 if(resource.equals(parent))
\r
44 if (parent == null) {
\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
51 // Assertion object?
\r
53 parent = graph.getPossibleObject(resource, L0.HasObjectInverse);
\r
56 throw new DatabaseException("Index root was not found for " + NameUtils.getSafeName(graph, resource, true));
\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