diff --git a/modules/core/test/com/haulmont/cuba/core/DataManagerTest.java b/modules/core/test/com/haulmont/cuba/core/DataManagerTest.java index 8d868f34c9..01228e7f55 100644 --- a/modules/core/test/com/haulmont/cuba/core/DataManagerTest.java +++ b/modules/core/test/com/haulmont/cuba/core/DataManagerTest.java @@ -8,7 +8,7 @@ import com.haulmont.bali.db.QueryRunner; import com.haulmont.cuba.core.entity.Entity; import com.haulmont.cuba.core.entity.Server; import com.haulmont.cuba.core.global.*; -import com.haulmont.cuba.security.entity.Group; +import com.haulmont.cuba.security.entity.User; import java.util.Collections; import java.util.List; @@ -34,8 +34,7 @@ public class DataManagerTest extends CubaTestCase { dataManager.commit(new CommitContext(Collections.singleton(server))); - final LoadContext loadContext = new LoadContext<>(Server.class); - loadContext.setId(id); + LoadContext loadContext = LoadContext.create(Server.class).setId(id); server = dataManager.load(loadContext); assertEquals("localhost", server.getName()); @@ -52,8 +51,7 @@ public class DataManagerTest extends CubaTestCase { dataManager.commit(new CommitContext(Collections.singleton(server))); - final LoadContext loadContext = new LoadContext<>(Server.class); - loadContext.setId(id); + LoadContext loadContext = LoadContext.create(Server.class).setId(id); server = dataManager.load(loadContext); assertEquals("localhost", server.getName()); @@ -66,8 +64,7 @@ public class DataManagerTest extends CubaTestCase { dataManager.commit(new CommitContext(Collections.singleton(server))); - final LoadContext loadContext = - new LoadContext(Server.class); + LoadContext loadContext = LoadContext.create(Server.class); loadContext.setQueryString("select s from " + PersistenceHelper.getEntityName(Server.class) + " s"); List list = dataManager.loadList(loadContext); @@ -82,19 +79,18 @@ public class DataManagerTest extends CubaTestCase { dataManager.commit(new CommitContext(Collections.singleton(server))); - LoadContext loadContext = new LoadContext(Server.class); - loadContext.setId(id); + LoadContext loadContext = LoadContext.create(Server.class).setId(id); List list = dataManager.loadList(loadContext); assertTrue(list.size() == 1); } public void testAssociatedResult() throws Exception { - LoadContext loadContext = new LoadContext(Group.class); + LoadContext loadContext = LoadContext.create(User.class); loadContext.setQueryString("select u.group from sec$User u where u.id = :userId") .setParameter("userId", UUID.fromString("60885987-1b61-4247-94c7-dff348347f93")); - List list = dataManager.loadList(loadContext); + List list = dataManager.loadList(loadContext); assertTrue(list.size() == 1); } @@ -105,8 +101,7 @@ public class DataManagerTest extends CubaTestCase { dataManager.commit(new CommitContext(Collections.singleton(server))); - final LoadContext loadContext = - new LoadContext(Server.class); + LoadContext loadContext = LoadContext.create(Server.class); loadContext.setQueryString("select s from sys$Server s where s.name like :name") .setParameter("name", "(?i)%loc%host%"); @@ -115,9 +110,8 @@ public class DataManagerTest extends CubaTestCase { } public void testUnexistingQueryParameters() throws Exception { - LoadContext loadContext = new LoadContext(Server.class); - loadContext.setQueryString("select u from sec$User u where u.login = :login") - .setParameter("name", "admin"); + LoadContext loadContext = LoadContext.create(User.class).setQuery( + LoadContext.createQuery("select u from sec$User u where u.login = :login").setParameter("name", "admin")); try { dataManager.loadList(loadContext); @@ -126,10 +120,9 @@ public class DataManagerTest extends CubaTestCase { // ok } - loadContext = new LoadContext(Server.class); - loadContext.setQueryString("select u from sec$User u where u.login = :login") - .setParameter("login", "admin"); - List list = dataManager.loadList(loadContext); + loadContext = LoadContext.create(User.class).setQuery( + LoadContext.createQuery("select u from sec$User u where u.login = :login").setParameter("login", "admin")); + List list = dataManager.loadList(loadContext); assertEquals(1, list.size()); } } diff --git a/modules/global/src/com/haulmont/cuba/core/global/LoadContext.java b/modules/global/src/com/haulmont/cuba/core/global/LoadContext.java index e2de52605e..b956c3e25a 100644 --- a/modules/global/src/com/haulmont/cuba/core/global/LoadContext.java +++ b/modules/global/src/com/haulmont/cuba/core/global/LoadContext.java @@ -11,8 +11,16 @@ import java.io.Serializable; import java.util.*; /** - * Class that defines parameters for loading entities from the database. - *

Used by {@link DataManager}.

+ * Class that defines parameters for loading entities from the database via {@link DataManager}. + *

Typical usage: + *

+    LoadContext<User> context = LoadContext.create(User.class).setQuery(
+            LoadContext.createQuery("select u from sec$User u where u.login like :login")
+                    .setParameter("login", "a%")
+                    .setMaxResults(10))
+            .setView("user.browse");
+    List<User> users = dataManager.loadList(context);
+ * 
* * @author krivopustov * @version $Id$ @@ -31,6 +39,24 @@ public class LoadContext implements Serializable { protected Map dbHints = new HashMap<>(); protected boolean loadDynamicAttributes; + /** + * Factory method to create a LoadContext instance. + * + * @param entityClass class of the loaded entities + */ + public static LoadContext create(Class entityClass) { + return new LoadContext(entityClass); + } + + /** + * Factory method to create a LoadContext.Query instance for passing into {@link #setQuery(Query)} method. + * + * @param queryString JPQL query string. Only named parameters are supported. + */ + public static LoadContext.Query createQuery(String queryString) { + return new LoadContext.Query(queryString); + } + /** * @param metaClass metaclass of the loaded entities */