Search in sources :

Example 1 with CriteriaCtx

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;
}
Also used : CriteriaCtx(io.vertigo.dynamo.criteria.CriteriaCtx) Tuples(io.vertigo.lang.Tuples) DtList(io.vertigo.dynamo.domain.model.DtList)

Example 2 with CriteriaCtx

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();
}
Also used : TaskBuilder(io.vertigo.dynamo.task.model.TaskBuilder) TaskDefinition(io.vertigo.dynamo.task.metamodel.TaskDefinition) CriteriaCtx(io.vertigo.dynamo.criteria.CriteriaCtx) Tuples(io.vertigo.lang.Tuples) TaskDefinitionBuilder(io.vertigo.dynamo.task.metamodel.TaskDefinitionBuilder)

Example 3 with CriteriaCtx

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();
    }
}
Also used : Authorization(io.vertigo.account.authorization.metamodel.Authorization) CriteriaCtx(io.vertigo.dynamo.criteria.CriteriaCtx) TestUserSession(io.vertigo.account.data.TestUserSession) UserSession(io.vertigo.persona.security.UserSession) SqlDialect(io.vertigo.database.sql.vendor.SqlDialect) Record(io.vertigo.account.authorization.model.Record) TestUserSession(io.vertigo.account.data.TestUserSession) PostgreSqlDataBase(io.vertigo.database.impl.sql.vendor.postgresql.PostgreSqlDataBase) Test(org.junit.Test)

Aggregations

CriteriaCtx (io.vertigo.dynamo.criteria.CriteriaCtx)3 Tuples (io.vertigo.lang.Tuples)2 Authorization (io.vertigo.account.authorization.metamodel.Authorization)1 Record (io.vertigo.account.authorization.model.Record)1 TestUserSession (io.vertigo.account.data.TestUserSession)1 PostgreSqlDataBase (io.vertigo.database.impl.sql.vendor.postgresql.PostgreSqlDataBase)1 SqlDialect (io.vertigo.database.sql.vendor.SqlDialect)1 DtList (io.vertigo.dynamo.domain.model.DtList)1 TaskDefinition (io.vertigo.dynamo.task.metamodel.TaskDefinition)1 TaskDefinitionBuilder (io.vertigo.dynamo.task.metamodel.TaskDefinitionBuilder)1 TaskBuilder (io.vertigo.dynamo.task.model.TaskBuilder)1 UserSession (io.vertigo.persona.security.UserSession)1 Test (org.junit.Test)1