Search in sources :

Example 6 with DataService

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"));
}
Also used : Role(com.haulmont.cuba.security.entity.Role) Entity(com.haulmont.cuba.core.entity.Entity) Permission(com.haulmont.cuba.security.entity.Permission) DataService(com.haulmont.cuba.core.app.DataService) Test(org.junit.Test)

Example 7 with DataService

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);
    }
}
Also used : LoginWorker(com.haulmont.cuba.security.app.LoginWorker) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) Server(com.haulmont.cuba.core.entity.Server) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) UserSession(com.haulmont.cuba.security.global.UserSession) DataService(com.haulmont.cuba.core.app.DataService) Test(org.junit.Test)

Example 8 with DataService

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);
}
Also used : UserSubstitution(com.haulmont.cuba.security.entity.UserSubstitution) DataService(com.haulmont.cuba.core.app.DataService)

Example 9 with DataService

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;
    }
}
Also used : CategoryAttribute(com.haulmont.cuba.core.entity.CategoryAttribute) DataService(com.haulmont.cuba.core.app.DataService)

Example 10 with DataService

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());
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) User(com.haulmont.cuba.security.entity.User) LoadContext(com.haulmont.cuba.core.global.LoadContext) DataService(com.haulmont.cuba.core.app.DataService) Test(org.junit.Test)

Aggregations

DataService (com.haulmont.cuba.core.app.DataService)12 Entity (com.haulmont.cuba.core.entity.Entity)6 Test (org.junit.Test)5 LoadContext (com.haulmont.cuba.core.global.LoadContext)4 User (com.haulmont.cuba.security.entity.User)3 UserSession (com.haulmont.cuba.security.global.UserSession)2 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)1 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)1 Server (com.haulmont.cuba.core.entity.Server)1 Frame (com.haulmont.cuba.gui.components.Frame)1 Window (com.haulmont.cuba.gui.components.Window)1 LoginWorker (com.haulmont.cuba.security.app.LoginWorker)1 Permission (com.haulmont.cuba.security.entity.Permission)1 Presentation (com.haulmont.cuba.security.entity.Presentation)1 Role (com.haulmont.cuba.security.entity.Role)1 ScreenHistoryEntity (com.haulmont.cuba.security.entity.ScreenHistoryEntity)1 UserSubstitution (com.haulmont.cuba.security.entity.UserSubstitution)1 TestUserSessionSource (com.haulmont.cuba.testsupport.TestUserSessionSource)1