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;
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;
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;
}
}
+ 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());
+ }
+
}