/** * Apply constraints for query to select reports which have input parameter with class matching inputValueMetaClass */ public void applyPoliciesByEntityParameters(LoadContext lc, @Nullable MetaClass inputValueMetaClass) { if (inputValueMetaClass != null) { QueryTransformer transformer = queryTransformerFactory.transformer(lc.getQuery().getQueryString()); StringBuilder parameterTypeCondition = new StringBuilder("r.inputEntityTypesIdx like :type escape '\\'"); lc.getQuery().setParameter("type", wrapIdxParameterForSearch(inputValueMetaClass.getName())); List<MetaClass> ancestors = inputValueMetaClass.getAncestors(); for (int i = 0; i < ancestors.size(); i++) { MetaClass metaClass = ancestors.get(i); String paramName = "type" + (i + 1); parameterTypeCondition.append(" or r.inputEntityTypesIdx like :").append(paramName).append(" escape '\\'"); lc.getQuery().setParameter(paramName, wrapIdxParameterForSearch(metaClass.getName())); } transformer.addWhereAsIs(String.format("(%s)", parameterTypeCondition.toString())); lc.getQuery().setQueryString(transformer.getResult()); } }
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; }
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); }
@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; }
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); } }
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); }
@SuppressWarnings("unchecked") protected void fillDefaultEntities(Class entityClass) { MetaClass metaClass = metadata.getClassNN(entityClass); if (attribute.getObjectDefaultEntityId() != null) { LoadContext<Entity> lc = new LoadContext<>(entityClass).setView(View.MINIMAL); String pkName = referenceToEntitySupport.getPrimaryKeyForLoadingEntity(metaClass); lc.setQueryString(format("select e from %s e where e.%s = :entityId", metaClass.getName(), pkName)) .setParameter("entityId", attribute.getObjectDefaultEntityId()); Entity entity = dataManager.load(lc); if (entity != null) { defaultEntityField.setValue(entity); } else { defaultEntityField.setValue(null); } } }
@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); }
/** * 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<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 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()); }
@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); }); }
@Override public List<UserSubstitution> getSubstitutedUsers(UUID userId) { 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", userId); query.setParameter("currentDate", timeSource.currentTimestamp()); ctx.setView("app"); return dataManager.loadList(ctx); } }
protected List<ProcDefinition> findProcDefinitionsByModel(ProcModel model) { LoadContext<ProcDefinition> ctx = LoadContext.create(ProcDefinition.class); ctx.setQueryString( "select pd from bpm$ProcDefinition pd " + "where pd.model.id = :modelId order by pd.name, pd.deploymentDate desc") .setParameter("modelId", model.getId()); return dataManager.loadList(ctx); }
@Nullable protected ProcModel findModelByName(String modelName) { View view = new View(ProcModel.class) .addProperty("name") .addProperty("actModelId"); LoadContext<ProcModel> ctx = LoadContext.create(ProcModel.class).setView(view); ctx.setQueryString("select m from bpm$ProcModel m where m.name = :name") .setParameter("name", modelName); return dataManager.load(ctx); }
/** * Load filter entities from database and saves them in {@code filterEntities} collection. */ protected void loadFilterEntities() { LoadContext<FilterEntity> ctx = LoadContext.create(FilterEntity.class); ctx.setView("app"); ctx.setQueryString("select f from sec$Filter f left join f.user u " + "where f.componentId = :component and (u.id = :userId or u is null) order by f.name") .setParameter("component", ComponentsHelper.getFilterComponentPath(filter)) .setParameter("userId", userSessionSource.getUserSession().getCurrentOrSubstitutedUser().getId()); filterEntities = new ArrayList<>(dataService.loadList(ctx)); }
@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); }); }
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 Report loadReportInternal(String entityId) { checkCanReadEntity(metadata.getClassNN(Report.class)); LoadContext<Report> loadContext = new LoadContext<>(Report.class); loadContext.setView(ReportService.MAIN_VIEW_NAME) .setQueryString("select r from report$Report r where r.id = :id and r.restAccess = true") .setParameter("id", getReportIdFromString(entityId)); reportSecurityManager.applySecurityPolicies(loadContext, null, userSessionSource.getUserSession().getCurrentOrSubstitutedUser()); Report report = dataManager.load(loadContext); checkEntityIsNotNull(metadata.getClassNN(Report.class).getName(), entityId, report); return report; }
protected List<Entity> loadEntityList(String[] ids) { Metadata metadata = AppBeans.get(Metadata.class); MetaClass metaClass = metadata.getSession().getClassNN(javaClass); LoadContext ctx = new LoadContext(javaClass); LoadContext.Query query = ctx.setQueryString("select e from " + metaClass.getName() + " e where e.id in :ids"); query.setParameter("ids", Arrays.asList(ids)); DataManager dataManager = AppBeans.get(DataManager.class); return dataManager.loadList(ctx); }