1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common.request;
14 import java.util.Collection;
15 import java.util.HashSet;
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;
23 public class IndexRoot extends ResourceRead<Resource> {
25 public IndexRoot(Resource resource) {
30 public Resource perform(ReadGraph graph) throws DatabaseException {
32 Layer0 L0 = Layer0.getInstance(graph);
34 if (graph.isInstanceOf(resource, L0.IndexRoot))
37 Collection<Resource> parents = new HashSet<Resource>(graph.getObjects(resource, L0.IsOwnedBy));
38 Resource parent = parents.size() == 1 ? parents.iterator().next() : null;
41 if(resource.equals(parent))
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);
53 parent = graph.getPossibleObject(resource, L0.HasObjectInverse);
56 throw new DatabaseException("Index root was not found for " + NameUtils.getSafeName(graph, resource, true));
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));