use of com.haulmont.cuba.core.app.DataService in project cuba by cuba-platform.
the class UpdateDetachedTest method testDataService.
@Test
public void testDataService() throws Exception {
Permission p;
DataService ds = AppBeans.get(DataService.NAME);
LoadContext<Permission> ctx = new LoadContext<>(Permission.class);
ctx.setId(permissionId);
ctx.setView(new View(Permission.class).addProperty("target").addProperty("role", new View(Role.class).addProperty("name")));
p = ds.load(ctx);
assertNotNull(p);
p.setTarget("newTarget");
CommitContext commitCtx = new CommitContext(Collections.singleton(p));
Set<Entity> entities = ds.commit(commitCtx);
Permission result = null;
for (Entity entity : entities) {
if (entity.equals(p))
result = (Permission) entity;
}
result = reserialize(result);
assertTrue(PersistenceHelper.isDetached(result));
assertNotNull(result.getRole());
assertTrue(PersistenceHelper.isDetached(result.getRole()));
assertTrue(PersistenceHelper.isLoaded(result, "role"));
}
use of com.haulmont.cuba.core.app.DataService in project cuba by cuba-platform.
the class DataManagerSecurityTest method test.
@Test
public void test() throws Exception {
LoginWorker lw = AppBeans.get(LoginWorker.NAME);
UserSession userSession = lw.login(USER_NAME, passwordEncryption.getPlainHash(USER_PASSW), Locale.getDefault());
assertNotNull(userSession);
UserSessionSource uss = AppBeans.get(UserSessionSource.class);
UserSession savedUserSession = uss.getUserSession();
((TestUserSessionSource) uss).setUserSession(userSession);
try {
DataManager dm = AppBeans.get(DataManager.NAME);
LoadContext<Server> loadContext = LoadContext.create(Server.class).setQuery(new LoadContext.Query("select s from sys$Server s"));
List<Server> list = dm.loadList(loadContext);
assertFalse("Permission took effect when calling DataManager inside middleware", list.isEmpty());
DataService ds = AppBeans.get(DataService.NAME);
loadContext = LoadContext.create(Server.class).setQuery(new LoadContext.Query("select s from sys$Server s"));
list = ds.loadList(loadContext);
assertTrue("Permission did not take effect when calling DataService", list.isEmpty());
} finally {
((TestUserSessionSource) uss).setUserSession(savedUserSession);
}
}
use of com.haulmont.cuba.core.app.DataService in project cuba by cuba-platform.
the class WebUserIndicator method getUserSubstitutions.
protected List<UserSubstitution> getUserSubstitutions() {
TimeSource timeSource = AppBeans.get(TimeSource.NAME);
DataService dataService = AppBeans.get(DataService.NAME);
UserSessionSource uss = AppBeans.get(UserSessionSource.NAME);
LoadContext<UserSubstitution> ctx = new LoadContext<>(UserSubstitution.class);
LoadContext.Query query = ctx.setQueryString("select us from sec$UserSubstitution us " + "where us.user.id = :userId and (us.endDate is null or us.endDate >= :currentDate) " + "and (us.startDate is null or us.startDate <= :currentDate) " + "and (us.substitutedUser.active = true or us.substitutedUser.active is null) order by us.substitutedUser.name");
query.setParameter("userId", uss.getUserSession().getUser().getId());
query.setParameter("currentDate", timeSource.currentTimestamp());
ctx.setView("app");
return dataService.loadList(ctx);
}
use of com.haulmont.cuba.core.app.DataService in project cuba by cuba-platform.
the class Param method createRuntimeEnumLookup.
protected Component createRuntimeEnumLookup(final ValueProperty valueProperty) {
if (javaClass == Boolean.class) {
return createBooleanField(valueProperty);
}
DataService dataService = AppBeans.get(DataService.NAME);
LoadContext<CategoryAttribute> context = new LoadContext<>(CategoryAttribute.class);
LoadContext.Query q = context.setQueryString("select a from sys$CategoryAttribute a where a.id = :id");
context.setView("_local");
q.setParameter("id", categoryAttrId);
CategoryAttribute categoryAttribute = dataService.load(context);
if (categoryAttribute == null) {
throw new EntityAccessException(CategoryAttribute.class, categoryAttrId);
}
runtimeEnum = new LinkedList<>();
String enumerationString = categoryAttribute.getEnumeration();
String[] array = StringUtils.split(enumerationString, ',');
for (String s : array) {
String trimmedValue = StringUtils.trimToNull(s);
if (trimmedValue != null) {
runtimeEnum.add(trimmedValue);
}
}
if (inExpr) {
ListEditor listEditor = componentsFactory.createComponent(ListEditor.class);
listEditor.setItemType(ListEditor.ItemType.STRING);
listEditor.setOptionsMap(categoryAttribute.getLocalizedEnumerationMap());
initListEditor(listEditor, valueProperty);
return listEditor;
} else {
LookupField lookup = componentsFactory.createComponent(LookupField.class);
lookup.setOptionsMap(categoryAttribute.getLocalizedEnumerationMap());
lookup.addValueChangeListener(e -> {
_setValue(e.getValue(), valueProperty);
});
lookup.setValue(_getValue(valueProperty));
return lookup;
}
}
use of com.haulmont.cuba.core.app.DataService in project cuba by cuba-platform.
the class QueryResultTest method testFirstQuery.
@Test
public void testFirstQuery() throws SQLException {
DataService dataService = AppBeans.get(DataService.class);
LoadContext context = new LoadContext(User.class).setView(View.LOCAL);
context.setQueryString("select u from sec$User u where u.name like :name").setParameter("name", "A-%");
List<Entity> entities = dataService.loadList(context);
assertEquals(20, entities.size());
List<Map<String, Object>> queryResults = getQueryResults();
assertEquals(0, queryResults.size());
}
Aggregations