]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/StandardGraphResourcePropertyVariable.java
New implementation NearestOwnerFinder of CommonDBUtils.getNearestOwner
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / StandardGraphResourcePropertyVariable.java
1 package org.simantics.db.layer0.variable;
2
3 import org.simantics.databoard.binding.Binding;
4 import org.simantics.databoard.type.Datatype;
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.Resource;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.exception.NoSingleResultException;
9 import org.simantics.db.layer0.exception.MissingVariableValueException;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 public class StandardGraphResourcePropertyVariable extends StandardGraphPropertyVariable {
14
15     private static final Logger LOGGER = LoggerFactory.getLogger(StandardGraphResourcePropertyVariable.class);
16
17         public StandardGraphResourcePropertyVariable(ReadGraph graph, Variable parent, Resource parentResource, Resource property) throws DatabaseException {
18             super(graph, parent, null, parentResource, property);
19         }
20
21         @Override
22         public <T> T getValue(ReadGraph graph) throws DatabaseException {
23                 if (parentResource == null)
24                         throw new MissingVariableValueException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");
25                 try {
26                 return (T)graph.getSingleObject(parentResource, property.predicate);
27                 } catch (NoSingleResultException e) {
28                         LOGGER.error("No object for " + parentResource + " " + property);
29                         throw new MissingVariableValueException(getPossibleURI(graph), e);
30                 } 
31         }
32
33         @Override
34         public <T> T getValue(ReadGraph graph, Binding binding) throws DatabaseException {
35                 if (parentResource == null)
36                         throw new MissingVariableValueException("Variable is not represented by any resource (URI=" + getPossibleURI(graph) + ").");
37                 try {
38             return (T)graph.getSingleObject(parentResource, property.predicate);
39                 } catch (NoSingleResultException e) {
40                         LOGGER.error("No object for " + parentResource + " " + property);
41                         throw new MissingVariableValueException(getPossibleURI(graph), e);
42                 } 
43         }
44         
45         @Override
46         public Datatype getDatatype(ReadGraph graph) throws DatabaseException {
47             return null;
48         }
49         
50 }