]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/queries/ConnectionPointMapOfResource.java
More SCL functions for experiment and run handling
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / queries / ConnectionPointMapOfResource.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.structural2.queries;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.Map;
17
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
21 import org.simantics.db.common.request.TransientResourceRead;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.request.PropertyInfo;
24 import org.simantics.db.request.RequestFlags;
25 import org.simantics.db.service.QueryControl;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import gnu.trove.map.hash.THashMap;
30
31 public class ConnectionPointMapOfResource extends TransientResourceRead<Map<String, Resource>> {
32
33     private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionPointMapOfResource.class);
34
35         public ConnectionPointMapOfResource(ReadGraph graph, Resource resource) throws DatabaseException {
36             super(graph, resource);
37         }
38         
39         public ConnectionPointMapOfResource(ReadGraph graph, QueryControl qc, Resource resource) throws DatabaseException {
40                 super(graph, qc, resource);
41         }
42
43         @Override
44         public Map<String,Resource> perform(ReadGraph graph, Resource resource) throws DatabaseException {
45             
46                 Collection<Resource> predicates = graph.getPredicates(resource);
47                 
48                 THashMap<String,Resource> result = null;
49                 
50                 for(Resource predicate : predicates) {
51                         
52                         PropertyInfo info = graph.syncRequest(new PossibleConnectionPointInfo(predicate), TransientCacheAsyncListener.<PropertyInfo>instance());
53                         if(info != null) {
54                                 if (result == null) result = new THashMap<String,Resource>(predicates.size());
55                                 if (result.put(info.name, predicate) != null)
56                                     LOGGER.error("The database contains siblings with the same name " + info.name + " (resource=$" + resource.getResourceId() + ").");
57                         }                               
58                         
59                 }
60
61                 if(result != null) return result;
62                 
63                 else return Collections.emptyMap();
64                         
65         }
66         
67         @Override
68         public int getType() {
69                 return RequestFlags.INVALIDATE;
70         }
71         
72 }