public void restrictByPreviousResults(UUID sessionId, int queryKey) { QueryTransformer transformer = QueryTransformerFactory.createTransformer(queryString); MetaClass metaClass = metadata.getClassNN(entityName); MetaProperty primaryKey = metadata.getTools().getPrimaryKeyProperty(metaClass); if (primaryKey == null) throw new IllegalStateException(String.format("Entity %s has no primary key", entityName)); Class type = primaryKey.getJavaType(); String entityIdField; if (UUID.class.equals(type)) { entityIdField = "entityId"; } else if (Long.class.equals(type)) { entityIdField = "longEntityId"; } else if (Integer.class.equals(type)) { entityIdField = "intEntityId"; } else if (String.class.equals(type)) { entityIdField = "stringEntityId"; } else { throw new IllegalStateException( String.format("Unsupported primary key type: %s for %s", type.getSimpleName(), entityName)); } transformer.addJoinAndWhere( ", sys$QueryResult _qr", String.format("_qr.%s = {E}.%s and _qr.sessionId = :_qr_sessionId and _qr.queryKey = %s", entityIdField, primaryKey.getName(), queryKey) ); queryString = transformer.getResult(); this.queryParams.put("_qr_sessionId", sessionId); }
public String processQuery(String query, Map<String, Object> paramValues) { Set<String> params = new HashSet<>(); for (Map.Entry<String, Object> entry : paramValues.entrySet()) { if (paramValueIsOk(entry.getValue())) params.add(entry.getKey()); } query = TemplateHelper.processTemplate(query, paramValues); if (isActual(root, params)) { Condition refined = refine(root, params); if (refined != null) { QueryTransformer transformer = QueryTransformerFactory.createTransformer(query); String where = new FilterJpqlGenerator().generateJpql(refined); if (!StringUtils.isBlank(where)) { Set<String> joins = refined.getJoins(); if (!joins.isEmpty()) { String joinsStr = joins.stream().collect(Collectors.joining(" ")); transformer.addJoinAndWhere(joinsStr, where); } else { transformer.addWhere(where); } } return transformer.getResult(); } } return query; }
/** * Returns a JPQL query modified according to the given tree of conditions. * @param query JPQL query * @param condition root condition. If null, the query is returned as is. */ public String processQuery(String query, @Nullable Condition condition) { if (condition == null) { return query; } QueryTransformer transformer = queryTransformerFactory.transformer(query); String joins = generateJoins(condition); String where = generateWhere(condition); if (!Strings.isNullOrEmpty(joins)) { transformer.addJoinAndWhere(joins, where); } else { transformer.addWhere(where); } return transformer.getResult(); }
protected void processConstraint(QueryTransformer transformer, ConstraintData constraint, String entityName) { String join = constraint.getJoin(); String where = constraint.getWhereClause(); try { if (StringUtils.isBlank(join)) { if (!StringUtils.isBlank(where)) { transformer.addWhere(where); } } else { transformer.addJoinAndWhere(join, where); } } catch (JpqlSyntaxException e) { log.error("Syntax errors found in constraint's JPQL expressions. Entity [{}]. Constraint ID [{}].", entityName, constraint.getId(), e); throw new RowLevelSecurityException( "Syntax errors found in constraint's JPQL expressions. Please see the logs.", entityName); } catch (Exception e) { log.error("An error occurred when applying security constraint. Entity [{}]. Constraint ID [{}].", entityName, constraint.getId(), e); throw new RowLevelSecurityException( "An error occurred when applying security constraint. Please see the logs.", entityName); } }
QueryTransformer transformer = QueryTransformerFactory.createTransformer(baseQueryString); if (StringUtils.isNotBlank(constraint.getJoinClause())) { transformer.addJoinAndWhere(constraint.getJoinClause(), constraint.getWhereClause()); } else { transformer.addWhere(constraint.getWhereClause());