]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Utilities for listing district component types 14/4414/2
authorReino Ruusu <reino.ruusu@semantum.fi>
Mon, 31 Aug 2020 12:57:03 +0000 (15:57 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 1 Sep 2020 06:15:26 +0000 (06:15 +0000)
gitlab #94

Change-Id: I24f2b651176a7b9aa8b8871cf8e49473350d9193

org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java

index 2bb29660930b82c9a30e010a118538fe73230c96..85ebd06c488ba27a1c4912dbd3eb40437a57eab1 100644 (file)
@@ -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<List<NamedResource>> {
+        protected DistrictComponentListRequest(Resource model) {
+            super(model);
+        }
+
+        @Override
+        public List<NamedResource> perform(ReadGraph graph) throws DatabaseException {
+            Layer0 L0 = Layer0.getInstance(graph);
+            DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+            
+            Resource model = this.resource;
+            
+            Set<Resource> componentTypes = new HashSet<>();
+            Collection<Resource> 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<NamedResource> result = new ArrayList<NamedResource>(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<NamedResource> getDistrictComponents() throws DatabaseException {
+        return getDistrictComponents(Simantics.getSession());
+    }
+
+    public static List<NamedResource> 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());
+    }
+
 }