]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/genericrelation/DependencyResources.java
1702af0f38e6182832673997ab7b55588ae5f335
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / genericrelation / DependencyResources.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.layer0.genericrelation;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.common.utils.Logger;
17 import org.simantics.db.exception.DatabaseException;
18 import org.simantics.db.layer0.adapter.GenericRelationIndex;
19 import org.simantics.db.service.QueryControl;
20 import org.simantics.operation.Layer0X;
21 import org.simantics.scl.runtime.function.FunctionImpl4;
22 import org.simantics.scl.runtime.function.UnsaturatedFunction2;
23
24 /**
25  * dependencyResources:
26  *      (ReadGraph, Resource model, String query) -> List<Resource>
27  *      (ReadGraph, Resource model, String query, Integer maxResults) -> List<Resource>
28  * 
29  * @author Antti Villberg
30  */
31 public class DependencyResources extends FunctionImpl4<ReadGraph, Resource, String, Integer, Object> {
32
33     protected Resource getIndexRelation(ReadGraph graph) {
34         return Layer0X.getInstance(graph).DependenciesRelation;
35     }
36
37     protected String getBindingPattern() {
38         return Dependencies.getBindingPattern();
39     }
40
41     @Override
42     public Object apply(ReadGraph p0, Resource p1) {
43         return new UnsaturatedFunction2(this, p0, p1);
44     }
45
46     @Override
47     public Object apply(ReadGraph p0, Resource p1, String p2) {
48         return apply(p0, p1, p2, Integer.MAX_VALUE);
49     }
50
51     @Override
52     public Object apply(ReadGraph graph, final Resource model, final String query, Integer _maxResults) {
53         
54         try {
55
56             final GenericRelationIndex index = graph.adapt(getIndexRelation(graph), GenericRelationIndex.class);
57
58             // Listen
59             graph.syncRequest(new ExternalRequest(index, model));
60             
61             final int maxResults = _maxResults != null ? _maxResults : Integer.MAX_VALUE;
62             
63             QueryControl qc = graph.getService(QueryControl.class);
64             return index.queryResources(qc.getIndependentGraph(graph), query, getBindingPattern(), new Object[] { model }, maxResults);
65             
66         } catch (DatabaseException e) {
67             Logger.defaultLogError(e);
68             return null;
69         }
70     }
71
72 }