From: Tuukka Lehtonen Date: Mon, 11 Sep 2017 08:49:03 +0000 (+0300) Subject: Added ListUtils.create alternatives that work with WriteOnlyGraph X-Git-Tag: v1.31.0~188 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F47%2F947%2F2;p=simantics%2Fplatform.git Added ListUtils.create alternatives that work with WriteOnlyGraph This allows those functions to work with DelayedWrites as well. refs #7477 Change-Id: I88a9de053783f05db85ecad8d1c6290e4ebc8258 --- diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java index 4d6518524..3631de4b3 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/ListUtils.java @@ -1,6 +1,7 @@ package org.simantics.db.common.utils; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Set; @@ -84,7 +85,19 @@ public class ListUtils { insertBetween(g, L0, list, list, list, elements); return list; } - + + public static Resource create(WriteOnlyGraph g, Resource type, Resource elementPredicate, Resource elementPredicateInverse, Iterable elements) throws DatabaseException { + Layer0 L0 = g.getService(Layer0.class); + Resource list = g.newResource(); + g.claim(list, L0.InstanceOf, null, type); + createExisting(g, list, elementPredicate, elementPredicateInverse, elements); + return list; + } + + public static Resource create(WriteOnlyGraph g, Resource type, Resource elementPredicate, Resource elementPredicateInverse, Resource... elements) throws DatabaseException { + return create(g, type, elementPredicate, elementPredicateInverse, Arrays.asList(elements)); + } + /** * Inserts given {@code elements} into the front of the list. */ @@ -92,33 +105,41 @@ public class ListUtils { if (!elements.iterator().hasNext()) return; Layer0 L0 = Layer0.getInstance(g); - - Resource first = g.getSingleObject(list, L0.List_Next); - g.deny(list, L0.List_Next, L0.List_Previous, first); + + Resource first = g.getSingleObject(list, L0.List_Next); + g.deny(list, L0.List_Next, L0.List_Previous, first); insertBetween(g, L0, list, list, first, elements); } - + public static void createExisting(WriteOnlyGraph g, Resource list, Iterable elements) throws DatabaseException { - createExisting(g, list, false, elements); + createExisting(g, list, false, elements); } public static void createExisting(WriteOnlyGraph g, Resource list, boolean withInverses, Iterable elements) throws DatabaseException { - Layer0 L0 = g.getService(Layer0.class); - Resource before = list; Resource elementPredicate = withInverses ? L0.List_ElementWithInverse : L0.List_Element; + Resource elementPredicateInverse = withInverses ? L0.List_ElementWithInverse_Inverse : null; + createExisting(g, list, elementPredicate, elementPredicateInverse, elements); + } + + public static void createExisting(WriteOnlyGraph g, Resource list, Resource elementPredicate, Resource elementPredicateInverse, Iterable elements) throws DatabaseException { + Layer0 L0 = g.getService(Layer0.class); + Resource before = list; for(Resource item : elements) { Resource cur = g.newResource(); g.claim(cur, L0.InstanceOf, null, L0.List_Entry); g.claim(cur, L0.IsOwnedBy, L0.IsComposedOf, list); g.claim(before, L0.List_Next, L0.List_Previous, cur); - g.claim(cur, elementPredicate, null, item); + g.claim(cur, elementPredicate, elementPredicateInverse, item); before = cur; } g.claim(before, L0.List_Next, L0.List_Previous, list); - } - + + public static void createExisting(WriteOnlyGraph g, Resource list, Resource elementPredicate, Resource elementPredicateInverse, Resource... elements) throws DatabaseException { + createExisting(g, list, elementPredicate, elementPredicateInverse, Arrays.asList(elements)); + } + /** * Inserts given {@code elements} into the back of the list. */ @@ -308,14 +329,13 @@ public class ListUtils { Layer0 L0 = Layer0.getInstance(g); Resource predicate = g.getPossibleObject(list, L0.List_ElementPredicate); if(predicate != null) return predicate; - return g.isInstanceOf(list, L0.ListWithInverses) ? - L0.List_ElementWithInverse : L0.List_Element; + return g.isInstanceOf(list, L0.ListWithInverses) ? + L0.List_ElementWithInverse : L0.List_Element; } public static Resource getListElementList(ReadGraph g, Resource element) throws DatabaseException { Layer0 L0 = Layer0.getInstance(g); - return g.getSingleObject(element, L0.IsOwnedBy); + return g.getSingleObject(element, L0.IsOwnedBy); } - - + }