Variable optimizations for documents (Simupedia)
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / queries / StructuralChildMapOfResource.java
1 /*******************************************************************************
2  * Copyright (c) 2018 Association for Decentralized Information Management in
3  * 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.structural2.queries;
13
14 import java.util.Map;
15
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.request.ResourceRead;
19 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.structural.stubs.StructuralResource2;
22
23 /**
24  * @author Antti Villberg
25  * @since 1.36.0
26  */
27 public class StructuralChildMapOfResource extends ResourceRead<Map<String, Resource>> {
28
29     public StructuralChildMapOfResource(Resource resource) {
30         super(resource);
31     }
32
33     @Override
34     public Map<String, Resource> perform(ReadGraph graph) throws DatabaseException {
35         StructuralResource2 STR = StructuralResource2.getInstance(graph);
36         Resource type = graph.getPossibleType(resource, STR.Component);
37         if (type != null) {
38             Resource definition = graph.getPossibleObject(type, STR.IsDefinedBy);
39             if (definition != null) {
40                 Map<String, Resource> map = graph.syncRequest(new UnescapedChildMapOfResource(definition));
41                 if (!map.isEmpty())
42                     return map;
43             }
44         }
45         return graph.syncRequest(new UnescapedChildMapOfResource(resource));
46     }
47
48 }