1 /*******************************************************************************
2 * Copyright (c) 2017 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.request;
14 import gnu.trove.map.hash.THashMap;
16 import java.util.Collection;
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;
31 * @author Tuukka Lehtonen
34 public class UnescapedAssertedPropertyMapOfResource extends ResourceRead<Map<String, Pair<PropertyInfo, Resource>>> {
36 private static final Logger LOGGER = LoggerFactory.getLogger(UnescapedAssertedPropertyMapOfResource.class);
38 public UnescapedAssertedPropertyMapOfResource(Resource resource) {
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() + ").");