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