Search in sources :

Example 1 with LoadContext

use of com.haulmont.cuba.core.global.LoadContext in project cuba by cuba-platform.

the class ConverterHelper method fetchDynamicAttributes.

public static void fetchDynamicAttributes(Entity entity) {
    if (entity instanceof BaseGenericIdEntity) {
        LoadContext<BaseGenericIdEntity> loadContext = new LoadContext<>(entity.getMetaClass());
        loadContext.setId(entity.getId()).setLoadDynamicAttributes(true);
        DataService dataService = AppBeans.get(DataService.NAME, DataService.class);
        BaseGenericIdEntity reloaded = dataService.load(loadContext);
        if (reloaded != null) {
            ((BaseGenericIdEntity) entity).setDynamicAttributes(reloaded.getDynamicAttributes());
        } else {
            ((BaseGenericIdEntity) entity).setDynamicAttributes(new HashMap<>());
        }
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) LoadContext(com.haulmont.cuba.core.global.LoadContext) DataService(com.haulmont.cuba.core.app.DataService)

Example 2 with LoadContext

use of com.haulmont.cuba.core.global.LoadContext in project cuba by cuba-platform.

the class RestFileDownloadController method download.

@RequestMapping(value = "/api/download", method = RequestMethod.GET)
public ModelAndView download(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserSession userSession = getSession(request, response);
    if (userSession == null) {
        error(response);
        return null;
    }
    AppContext.setSecurityContext(new SecurityContext(userSession));
    try {
        UUID fileId;
        try {
            fileId = UUID.fromString(request.getParameter("f"));
        } catch (Exception e) {
            log.error(e.toString());
            error(response);
            return null;
        }
        FileDescriptor fd = dataService.load(new LoadContext<>(FileDescriptor.class).setId(fileId));
        if (fd == null) {
            log.warn("Unable to find file with id " + fileId);
            error(response);
            return null;
        }
        String fileName = URLEncodeUtils.encodeUtf8(fd.getName());
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Content-Type", getContentType(fd));
        response.setHeader("Pragma", "no-cache");
        boolean attach = Boolean.valueOf(request.getParameter("a"));
        response.setHeader("Content-Disposition", (attach ? "attachment" : "inline") + "; filename=" + fileName);
        writeResponse(response, userSession, fd);
    } finally {
        AppContext.setSecurityContext(null);
    }
    return null;
}
Also used : UserSession(com.haulmont.cuba.security.global.UserSession) SecurityContext(com.haulmont.cuba.core.sys.SecurityContext) LoadContext(com.haulmont.cuba.core.global.LoadContext) UUID(java.util.UUID) IOException(java.io.IOException) NoUserSessionException(com.haulmont.cuba.security.global.NoUserSessionException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with LoadContext

use of com.haulmont.cuba.core.global.LoadContext in project cuba by cuba-platform.

the class FileDownloadController method getFileDescriptor.

protected FileDescriptor getFileDescriptor(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UUID fileId;
    try {
        fileId = UUID.fromString(request.getParameter("f"));
    } catch (Exception e) {
        log.error("Error parsing fileId from URL param", e);
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    FileDescriptor fileDescriptor = dataService.load(new LoadContext<>(FileDescriptor.class).setId(fileId));
    if (fileDescriptor == null)
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return fileDescriptor;
}
Also used : LoadContext(com.haulmont.cuba.core.global.LoadContext) UUID(java.util.UUID) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) IOException(java.io.IOException) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 4 with LoadContext

use of com.haulmont.cuba.core.global.LoadContext in project cuba by cuba-platform.

the class QueryConditionsUsageTest method test.

@Test
public void test() {
    LogicalCondition condition = and().add(where("u.login like :login")).add(where("u.userRoles ur", "ur.role.name = :roleName")).add(or().add(where("u.foo = :foo")).add(where("u.bar = :bar")));
    LoadContext.Query query = LoadContext.createQuery("select u from sec$User u").setCondition(condition).setParameter("login", "admin");
    LoadContext<User> loadContext = LoadContext.create(User.class).setQuery(query);
    List<User> users = dataManager.loadList(loadContext);
    assertEquals(1, users.size());
    Optional<User> userOpt = dataManager.load(User.class).query("select u from sec$User u").condition(condition).parameter("login", "admin").optional();
    assertTrue(userOpt.isPresent());
}
Also used : User(com.haulmont.cuba.security.entity.User) LoadContext(com.haulmont.cuba.core.global.LoadContext) LogicalCondition(com.haulmont.cuba.core.global.queryconditions.LogicalCondition) Test(org.junit.jupiter.api.Test)

Example 5 with LoadContext

use of com.haulmont.cuba.core.global.LoadContext in project cuba by cuba-platform.

the class QueryResultTest method testSecondQuery.

@Test
public void testSecondQuery() 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.email like :email").setParameter("email", "%aaa.com");
    LoadContext.Query prevQuery = new LoadContext.Query("select u from sec$User u where u.name like :name").setParameter("name", "A-%");
    context.getPrevQueries().add(prevQuery);
    context.setQueryKey(111);
    List<Entity> entities = dataService.loadList(context);
    assertEquals(10, entities.size());
    List<Map<String, Object>> queryResults = getQueryResults();
    assertEquals(20, 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.jupiter.api.Test)

Aggregations

LoadContext (com.haulmont.cuba.core.global.LoadContext)16 User (com.haulmont.cuba.security.entity.User)6 DataService (com.haulmont.cuba.core.app.DataService)4 Entity (com.haulmont.cuba.core.entity.Entity)4 DataManager (com.haulmont.cuba.core.global.DataManager)4 Test (org.junit.jupiter.api.Test)4 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)2 IOException (java.io.IOException)2 UUID (java.util.UUID)2 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)1 JmxInstance (com.haulmont.cuba.core.entity.JmxInstance)1 EntityLoadInfo (com.haulmont.cuba.core.global.EntityLoadInfo)1 FileStorageException (com.haulmont.cuba.core.global.FileStorageException)1 View (com.haulmont.cuba.core.global.View)1 LogicalCondition (com.haulmont.cuba.core.global.queryconditions.LogicalCondition)1 SecurityContext (com.haulmont.cuba.core.sys.SecurityContext)1 GuiDevelopmentException (com.haulmont.cuba.gui.GuiDevelopmentException)1 Group (com.haulmont.cuba.security.entity.Group)1 NoUserSessionException (com.haulmont.cuba.security.global.NoUserSessionException)1 UserSession (com.haulmont.cuba.security.global.UserSession)1