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