--- /dev/null
+package org.simantics.district.selection;
+
+import java.util.List;
+import java.util.UUID;
+
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.QueryIndexUtils;
+import org.simantics.db.layer0.request.PossibleActiveModel;
+import org.simantics.db.request.WriteResult;
+import org.simantics.layer0.Layer0;
+
+public class ElementSelectionUtils {
+ public static Resource ensureSelectionLibrary(RequestProcessor graph) throws DatabaseException {
+ return graph.syncRequest(new WriteResult<Resource>() {
+ @Override
+ public Resource perform(WriteGraph graph) throws DatabaseException {
+ ElementSelectionResource ES = ElementSelectionResource.getInstance(graph);
+
+ Resource model = graph.sync(new PossibleActiveModel(Simantics.getProjectResource()));
+ if (model == null) return null;
+
+ List<Resource> libs = QueryIndexUtils.searchByTypeShallow(graph, model, ES.SelectionLibrary);
+ if (libs.isEmpty()) {
+ Resource lib = graph.newResource();
+ Layer0 L0 = Layer0.getInstance(graph);
+ graph.claim(lib, L0.InstanceOf, ES.SelectionLibrary);
+ graph.claim(model, L0.ConsistsOf, lib);
+ graph.claimLiteral(lib, L0.HasName, L0.String, UUID.randomUUID().toString(), Bindings.STRING);
+ return lib;
+ }
+ else {
+ return libs.get(0);
+ }
+ }
+ });
+ }
+
+}