/** * @param queryString JPQL query string. Only named parameters are supported. * @return query definition object */ @Override public Query setQueryString(String queryString) { final Query query = new Query(queryString); setQuery(query); return query; }
/** * Creates a copy of this LoadContext instance. */ public LoadContext<?> copy() { LoadContext<?> ctx; try { ctx = getClass().newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException("Error copying LoadContext", e); } ctx.metaClass = metaClass; ctx.setQuery(query != null ? query.copy() : null); ctx.view = view; ctx.id = id; ctx.softDeletion = softDeletion; ctx.prevQueries.addAll(prevQueries.stream().map(Query::copy).collect(Collectors.toList())); ctx.queryKey = queryKey; if (dbHints != null) { ctx.getDbHints().putAll(dbHints); } ctx.loadDynamicAttributes = loadDynamicAttributes; ctx.authorizationRequired = authorizationRequired; ctx.joinTransaction = joinTransaction; return ctx; }
protected User loadUser(UUID userId) { LoadContext<User> loadContext = new LoadContext<>(User.class); LoadContext.Query query = new LoadContext.Query("select u from sec$User u where u.id = :userId"); query.setParameter("userId", userId); loadContext.setQuery(query); List<User> users = dataService.loadList(loadContext); return users.isEmpty() ? null : users.get(0); }
protected Collection<? extends Entity> reloadEntities(Collection<? extends Entity> entities, View view) { List<Object> ids = entities.stream() .map(Entity::getId) .collect(Collectors.toList()); MetaClass metaClass = metadata.getClassNN(view.getEntityClass()); LoadContext.Query query = LoadContext.createQuery("select e from " + metaClass.getName() + " e where e.id in :ids") .setParameter("ids", ids); LoadContext<? extends Entity> ctx = LoadContext.create(view.getEntityClass()) .setQuery(query) .setView(view); return dataManager.loadList(ctx); }
protected List<com.haulmont.cuba.core.entity.Config> loadDbContent() { LoadContext<com.haulmont.cuba.core.entity.Config> loadContext = LoadContext.create(com.haulmont.cuba.core.entity.Config.class).setQuery( LoadContext.createQuery("select c from sys$Config c") ).setView("appProperties"); return dataManager.loadList(loadContext); }
public EntitiesSearchResult loadEntitiesList(String entityName, @Nullable String viewName, @Nullable Integer limit, @Nullable Integer offset, @Nullable String sort, @Nullable Boolean returnNulls, @Nullable Boolean returnCount, @Nullable Boolean dynamicAttributes, @Nullable String modelVersion) { entityName = restControllerUtils.transformEntityNameIfRequired(entityName, modelVersion, JsonTransformationDirection.FROM_VERSION); MetaClass metaClass = restControllerUtils.getMetaClass(entityName); checkCanReadEntity(metaClass); String queryString = "select e from " + entityName + " e"; String json = _loadEntitiesList(queryString, viewName, limit, offset, sort, returnNulls, dynamicAttributes, modelVersion, metaClass, new HashMap<>()); json = restControllerUtils.transformJsonIfRequired(entityName, modelVersion, JsonTransformationDirection.TO_VERSION, json); Long count = null; if (BooleanUtils.isTrue(returnCount)) { LoadContext ctx = LoadContext.create(metaClass.getJavaClass()) .setQuery(LoadContext.createQuery(queryString)); count = dataManager.getCount(ctx); } return new EntitiesSearchResult(json, count); }
LoadContext<E> createLoadContext() { LoadContext<E> loadContext = LoadContext.create(loader.entityClass); loader.initCommonLoadContextParameters(loadContext); LoadContext.Query query = LoadContext.createQuery(queryString); for (Map.Entry<String, Object> entry : parameters.entrySet()) { if (noConversionParams.contains(entry.getKey())) query.setParameter(entry.getKey(), entry.getValue(), false); else query.setParameter(entry.getKey(), entry.getValue()); } loadContext.setQuery(query); loadContext.getQuery().setCondition(condition); loadContext.getQuery().setFirstResult(firstResult); loadContext.getQuery().setMaxResults(maxResults); loadContext.getQuery().setCacheable(cacheable); return loadContext; }
@Nullable protected Entity findEntity(String entityName, Object entityId) { Entity entity = null; if (entityId != null && !Strings.isNullOrEmpty(entityName)) { MetaClass metaClass = metadata.getClass(entityName); if (metaClass != null) { LoadContext ctx = new LoadContext(metaClass).setQuery( LoadContext.createQuery(String.format("select e from %s e where e.%s = :entityId", entityName, referenceToEntitySupport.getPrimaryKeyForLoadingEntity(metaClass))) .setParameter("entityId", entityId)) .setView(View.MINIMAL); entity = dataManager.load(ctx); } } return entity; }
private List<Tenant> createOptionList() { return dataManager.loadList(LoadContext.create(Tenant.class) .setQuery(LoadContext.createQuery("select e from cubasdbmt$Tenant e")) .setView("tenant-with-group")); }
protected List<ProcDefinition> getProcDefinitionsByName(String name) { LoadContext ctx = new LoadContext(ProcDefinition.class); ctx.setQuery(new LoadContext.Query("select pd from bpm$ProcDefinition pd where pd.name = :name") .setParameter("name", name)); return dataManager.loadList(ctx); }
protected void loadEntities(MetaClass metaClass) { LoadContext loadContext = LoadContext.create(metaClass.getJavaClass()) .setQuery(LoadContext.createQuery(format("select e from %s e", metaClass.getName()))); List entities = dataManager.loadList(loadContext); entitiesLookup.setOptionsList(entities); }
LoadContext<E> createLoadContext() { LoadContext<E> loadContext = LoadContext.create(entityClass); initCommonLoadContextParameters(loadContext); String entityName = AppBeans.get(Metadata.class).getClassNN(entityClass).getName(); String queryString = String.format("select e from %s e", entityName); loadContext.setQuery(LoadContext.createQuery(queryString)); return loadContext; }
protected User loadUser(UUID userId, User user) { if (user.getId().equals(userId)) return user; LoadContext loadContext = new LoadContext(UserSubstitution.class); LoadContext.Query query = new LoadContext.Query("select su from sec$UserSubstitution us join us.user u " + "join us.substitutedUser su where u.id = :id and su.id = :userId and " + "(us.endDate is null or us.endDate >= :currentDate) and (us.startDate is null or us.startDate <= :currentDate)"); query.setParameter("id", user.getId()); query.setParameter("userId", userId); query.setParameter("currentDate", timeSource.currentTimestamp()); loadContext.setQuery(query); List<User> users = dataService.loadList(loadContext); return users.isEmpty() ? null : users.get(0); }
/** * For entities with composite keys there will be a value of the 'uuid' property in the {@code entityId} parameter. */ protected Entity reloadEntity(MetaClass metaClass, Object entityId) { String ftsPrimaryKeyName = ftsService.getPrimaryKeyPropertyForFts(metaClass).getName(); String queryStr = String.format("select e from %s e where e.%s = :id", metaClass.getName(), ftsPrimaryKeyName); LoadContext lc = new LoadContext(metaClass) .setView(View.MINIMAL) .setQuery(LoadContext.createQuery(queryStr).setParameter("id", entityId)); List list = getDsContext().getDataSupplier().loadList(lc); return list.isEmpty() ? null : (Entity) list.get(0); } }
protected List<Entity> loadItemsWithDirectKey(LoadDescriptor ld) { LoadContext.Query query = new LoadContext.Query( String.format("select e from %s e where e.%s in :ids", ld.getMetaClass(), metadataTools.getPrimaryKeyName(ld.getMetaClass()))); List<Object> ids = ld.getSelectedItems().stream() .map(Entity::getId) .collect(Collectors.toList()); query.setParameter("ids", ids); LoadContext<Entity> lc = new LoadContext<>(ld.getMetaClass()); lc.setSoftDeletion(false); lc.setQuery(query); lc.setView(ld.getView()); lc.setLoadDynamicAttributes(ld.isLoadDynamicAttributes()); return dataManager.secure().loadList(lc); } }
@Override public List<SessionLogEntry> getAllSessionLogRecords(UUID userSessionId) { return authentication.withSystemUser(() -> { LoadContext<SessionLogEntry> loadContext = LoadContext.create(SessionLogEntry.class) .setView(SessionLogEntry.DEFAULT_VIEW) .setQuery( createQuery("select e from sec$SessionLogEntry e where e.sessionId = :sid order by e.startedTs asc") .setParameter("sid", userSessionId) ); return dataManager.loadList(loadContext); }); }
@Override public SessionLogEntry getLastSessionLogRecord(UUID userSessionId) { return authentication.withSystemUser(() -> { LoadContext<SessionLogEntry> loadContext = LoadContext.create(SessionLogEntry.class) .setView(SessionLogEntry.DEFAULT_VIEW) .setQuery( createQuery("select e from sec$SessionLogEntry e where e.sessionId = :sid order by e.startedTs desc") .setParameter("sid", userSessionId) .setMaxResults(1) ); return dataManager.load(loadContext); }); }
protected Dashboard loadDashboardByCode(String code) { LoadContext<PersistentDashboard> loadContext = LoadContext.create(PersistentDashboard.class) .setQuery(LoadContext.createQuery("select d from dashboard$PersistentDashboard d where d.code = :code") .setParameter("code", code)) .setView("_local"); PersistentDashboard entity = dataManager.load(loadContext); if (entity == null || entity.getDashboardModel() == null) { return null; } return jsonConverter.dashboardFromJson(entity.getDashboardModel()); }
protected void assignRoleUsers(Role role, Collection<User> items) { if (items == null) return; List<Entity> toCommit = new ArrayList<>(); for (User user : items) { LoadContext<UserRole> ctx = LoadContext.create(UserRole.class) .setView("user.edit") .setQuery(new LoadContext.Query("select ur from sec$UserRole ur where ur.user.id = :userId") .setParameter("userId", user.getId()) ); List<UserRole> userRoles = dataManager.loadList(ctx); boolean roleExist = false; for (UserRole userRole : userRoles) { if (role.equals(userRole.getRole())) { roleExist = true; break; } } if (!roleExist) { UserRole ur = metadata.create(UserRole.class); ur.setUser(user); ur.setRole(role); toCommit.add(ur); } } if (!toCommit.isEmpty()) { dataManager.commit(new CommitContext(toCommit)); } showNotification(getMessage("rolesAssigned.msg")); }
@Override public EntitySnapshot getLastEntitySnapshot(Entity entity) { checkCompositePrimaryKey(entity); MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(entity.getMetaClass()); View view = metadata.getViewRepository().getView(EntitySnapshot.class, "entitySnapshot.browse"); LoadContext<EntitySnapshot> lx = LoadContext.create(EntitySnapshot.class).setQuery(LoadContext .createQuery(format("select e from sys$EntitySnapshot e where e.entityMetaClass = :metaClass and" + " e.entity.%s = :entityId order by e.snapshotDate desc", referenceToEntitySupport.getReferenceIdPropertyName(metaClass))) .setParameter("metaClass", metaClass.getName()) .setParameter("entityId", referenceToEntitySupport.getReferenceId(entity)) .setMaxResults(1)) .setView(view); return dataManager.load(lx); }