]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/request/SingleObjectWithType.java
ae72985c4679b1dd691dde5c679613a899b239b2
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / SingleObjectWithType.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 org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.utils.NameUtils;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.exception.NoSingleResultException;
19
20 /**
21  * @author Tuukka Lehtonen
22  */
23 public final class SingleObjectWithType extends ResourceRead3<Resource> {
24
25     public SingleObjectWithType(Resource resource, Resource predicate, Resource type) {
26         super(resource, predicate, type);
27     }
28
29     @Override
30     public Resource perform(ReadGraph graph) throws DatabaseException {
31         Resource result = null;
32         for (Resource object : graph.getObjects(resource, resource2)) {
33             if (graph.isInstanceOf(object, resource3)) {
34                 if (result != null)
35                     throw new NoSingleResultException("More than 1 objects for relation "
36                             + NameUtils.getSafeName(graph, resource2) + " with type "
37                             + NameUtils.getSafeName(graph, resource3) + " at "
38                             + NameUtils.getSafeName(graph, resource));
39
40                 result = object;
41             }
42         }
43
44         if (result == null)
45             throw new NoSingleResultException("No objects for relation "
46                     + NameUtils.getSafeName(graph, resource2) + " with type "
47                     + NameUtils.getSafeName(graph, resource3) + " at "
48                     + NameUtils.getSafeName(graph, resource));
49
50         return result;
51     }
52
53 }