use of io.vertigo.dynamo.criteria.CriteriaCtx in project vertigo by KleeGroup.
the class JpaDataStorePlugin method doFindByCriteria.
private <E extends Entity> DtList<E> doFindByCriteria(final ProcessAnalyticsTracer tracer, final DtDefinition dtDefinition, final Criteria<E> criteria, final Integer maxRows) {
final Class<E> resultClass = (Class<E>) ClassUtil.classForName(dtDefinition.getClassCanonicalName());
final Tuples.Tuple2<String, CriteriaCtx> tuple = criteria.toSql(sqlDataBase.getSqlDialect());
final String tableName = getTableName(dtDefinition);
final String request = createLoadAllLikeQuery(tableName, tuple.getVal1());
final CriteriaCtx ctx = tuple.getVal2();
final TypedQuery<E> q = getEntityManager().createQuery(request, resultClass);
// IN, obligatoire
for (final String attributeName : ctx.getAttributeNames()) {
q.setParameter(attributeName, ctx.getAttributeValue(attributeName));
}
if (maxRows != null) {
q.setMaxResults(maxRows);
}
final List<E> results = q.getResultList();
final DtList<E> dtc = new DtList<>(dtDefinition);
dtc.addAll(results);
tracer.setMeasure("nbSelectedRow", dtc.size());
return dtc;
}
use of io.vertigo.dynamo.criteria.CriteriaCtx in project vertigo by KleeGroup.
the class SqlDataStorePlugin method findByCriteria.
/**
* {@inheritDoc}
*/
@Override
public <E extends Entity> DtList<E> findByCriteria(final DtDefinition dtDefinition, final Criteria<E> criteria, final Integer maxRows) {
Assertion.checkNotNull(dtDefinition);
Assertion.checkNotNull(criteria);
// ---
final String tableName = getTableName(dtDefinition);
final String requestedFields = getRequestedFields(dtDefinition);
final String taskName = getListTaskName(tableName);
final Tuples.Tuple2<String, CriteriaCtx> tuple = criteria.toSql(sqlDialect);
final String where = tuple.getVal1();
final String request = createLoadAllLikeQuery(tableName, requestedFields, where, maxRows);
final TaskDefinitionBuilder taskDefinitionBuilder = TaskDefinition.builder(taskName).withEngine(TaskEngineSelect.class).withDataSpace(dataSpace).withRequest(request);
final CriteriaCtx ctx = tuple.getVal2();
// IN, Optional
for (final String attributeName : ctx.getAttributeNames()) {
taskDefinitionBuilder.addInOptional(attributeName, dtDefinition.getField(ctx.getDtFieldName(attributeName)).getDomain());
}
// OUT, obligatoire
final TaskDefinition taskDefinition = taskDefinitionBuilder.withOutRequired("dtc", Home.getApp().getDefinitionSpace().resolve(DOMAIN_PREFIX + SEPARATOR + dtDefinition.getName() + "_DTC", Domain.class)).build();
final TaskBuilder taskBuilder = Task.builder(taskDefinition);
for (final String attributeName : ctx.getAttributeNames()) {
taskBuilder.addValue(attributeName, ctx.getAttributeValue(attributeName));
}
return taskManager.execute(taskBuilder.build()).getResult();
}
use of io.vertigo.dynamo.criteria.CriteriaCtx in project vertigo by KleeGroup.
the class VSecurityManagerTest method testSecuritySqlOnEntity.
@Test
public void testSecuritySqlOnEntity() {
final Record recordTooExpensive = createRecord();
recordTooExpensive.setAmount(10000d);
final Record recordOtherUser = createRecord();
recordOtherUser.setUtiIdOwner(2000L);
final Record recordOtherUserAndTooExpensive = createRecord();
recordOtherUserAndTooExpensive.setUtiIdOwner(2000L);
recordOtherUserAndTooExpensive.setAmount(10000d);
final Authorization recordRead = getAuthorization(RecordAuthorizations.ATZ_RECORD$READ);
final UserSession userSession = securityManager.<TestUserSession>createUserSession();
try {
securityManager.startCurrentUserSession(userSession);
authorizationManager.obtainUserAuthorizations().withSecurityKeys("utiId", DEFAULT_UTI_ID).withSecurityKeys("typId", DEFAULT_TYPE_ID).withSecurityKeys("montantMax", DEFAULT_MONTANT_MAX).addAuthorization(recordRead);
final boolean canReadRecord = authorizationManager.hasAuthorization(RecordAuthorizations.ATZ_RECORD$READ);
Assert.assertTrue(canReadRecord);
final SqlDialect sqlDialect = new PostgreSqlDataBase().getSqlDialect();
final Tuple2<String, CriteriaCtx> readRecordSql = authorizationManager.getCriteriaSecurity(Record.class, RecordOperations.READ).toSql(sqlDialect);
// read -> MONTANT<=${montantMax} or UTI_ID_OWNER=${utiId}
Assert.assertEquals("( AMOUNT <= #AMOUNT_0# OR UTI_ID_OWNER = #UTI_ID_OWNER_1# ) ", readRecordSql.getVal1());
Assert.assertEquals(100.0, readRecordSql.getVal2().getAttributeValue("AMOUNT_0"));
Assert.assertEquals(1000L, readRecordSql.getVal2().getAttributeValue("UTI_ID_OWNER_1"));
} finally {
securityManager.stopCurrentUserSession();
}
}
Aggregations