From 0dc14ff5755e9779c1a57e586056b00795927288 Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Mon, 31 Aug 2020 15:57:03 +0300 Subject: [PATCH] Utilities for listing district component types gitlab #94 Change-Id: I24f2b651176a7b9aa8b8871cf8e49473350d9193 --- .../district/network/DistrictNetworkUtil.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java index 2bb29660..85ebd06c 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java +++ b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java @@ -3,8 +3,12 @@ package org.simantics.district.network; import java.awt.geom.Rectangle2D; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -13,12 +17,16 @@ import org.simantics.databoard.Bindings; import org.simantics.datatypes.literal.RGB; import org.simantics.datatypes.literal.RGB.Integer; import org.simantics.db.ReadGraph; +import org.simantics.db.RequestProcessor; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; +import org.simantics.db.common.NamedResource; import org.simantics.db.common.procedure.adapter.TransientCacheListener; import org.simantics.db.common.request.BinaryRead; import org.simantics.db.common.request.IndexRoot; import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.request.PossibleChild; +import org.simantics.db.common.request.PossibleIndexRoot; import org.simantics.db.common.request.ResourceRead; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.BindingException; @@ -26,6 +34,8 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; import org.simantics.db.exception.ServiceException; import org.simantics.db.indexing.IndexUtils; +import org.simantics.db.layer0.QueryIndexUtils; +import org.simantics.db.layer0.request.PossibleActiveModel; import org.simantics.db.layer0.request.PossibleVariable; import org.simantics.db.layer0.variable.Variable; import org.simantics.diagram.stubs.DiagramResource; @@ -466,4 +476,53 @@ public class DistrictNetworkUtil { } } + public static class DistrictComponentListRequest extends ResourceRead> { + protected DistrictComponentListRequest(Resource model) { + super(model); + } + + @Override + public List perform(ReadGraph graph) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + + Resource model = this.resource; + + Set componentTypes = new HashSet<>(); + Collection mappings = QueryIndexUtils.searchByType(graph, model, DN.Mapping_Base); + for (Resource r : mappings) { + String componentType = graph.getPossibleRelatedValue2(r, DN.Mapping_ComponentType); + if (componentType != null) { + Resource root = graph.syncRequest(new PossibleIndexRoot(r)); + if (root != null) { + Resource type = graph.syncRequest(new PossibleChild(root, componentType)); + if (type != null) + componentTypes.add(type); + } + } + } + + List result = new ArrayList(componentTypes.size()); + for (Resource r : componentTypes) { + String name = graph.getPossibleRelatedValue(r, L0.HasName); + result.add(new NamedResource(name, r)); + } + + result.sort(Comparator.comparing(NamedResource::getName)); + return result; + } + } + + public static List getDistrictComponents() throws DatabaseException { + return getDistrictComponents(Simantics.getSession()); + } + + public static List getDistrictComponents(RequestProcessor session) throws DatabaseException { + Resource model = session.syncRequest(new PossibleActiveModel(Simantics.getProjectResource())); + if (model == null) + return Collections.emptyList(); + + return session.syncRequest(new DistrictComponentListRequest(model), TransientCacheListener.instance()); + } + } -- 2.47.1