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<>());
}
}
}
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;
}
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;
}
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());
}
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());
}
Aggregations