]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/UnescapedAssertedPropertyMapOfResource.java
Added resourceId and GUID to diagram DnD content
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / UnescapedAssertedPropertyMapOfResource.java
1 /*******************************************************************************
2  * Copyright (c) 2017 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.request;
13
14  import gnu.trove.map.hash.THashMap;
15
16 import java.util.Collection;
17 import java.util.Map;
18
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.Statement;
22 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
23 import org.simantics.db.common.request.ResourceRead;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.layer0.Layer0;
26 import org.simantics.utils.datastructures.Pair;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * @author Tuukka Lehtonen
32  * @since 1.27.0
33  */
34 public class UnescapedAssertedPropertyMapOfResource extends ResourceRead<Map<String, Pair<PropertyInfo, Resource>>> {
35
36         private static final Logger LOGGER = LoggerFactory.getLogger(UnescapedAssertedPropertyMapOfResource.class);
37
38         public UnescapedAssertedPropertyMapOfResource(Resource resource) {
39                 super(resource);
40         }
41
42         @Override
43         public Map<String,Pair<PropertyInfo, Resource>> perform(ReadGraph graph) throws DatabaseException {
44                 Layer0 L0 = Layer0.getInstance(graph);
45                 Collection<Statement> assertions = graph.getAssertedStatements(resource, L0.HasProperty); 
46                 THashMap<String, Pair<PropertyInfo, Resource>> result = new THashMap<>(assertions.size());
47                 for (Statement stm : assertions) {
48                         PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(stm.getPredicate()), TransientCacheAsyncListener.<PropertyInfo>instance());
49                         if (info != null && info.isHasProperty) {
50                                 String name = info.name;
51                                 // Use putIfAbsent because we want to prefer the results that are first,
52                                 // i.e. the ones deeper in the type inheritance chain.
53                                 if (result.putIfAbsent(name, Pair.make(info, stm.getObject())) != null) {
54                                         if (LOGGER.isDebugEnabled()) {
55                                                 LOGGER.debug(this + ": The database resource $" + resource.getResourceId()
56                                                                 + " asserts the multiple properties with the same name " + name + " (resource=$"
57                                                                 + info.predicate.getResourceId() + ").");
58                                         }
59                                 }
60                         }
61                 }
62                 return result;
63         }
64
65 }