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