Search in sources :

Example 6 with TaskAttribute

use of io.vertigo.dynamo.task.metamodel.TaskAttribute in project vertigo by KleeGroup.

the class WhereInPreProcessor method doEvaluate.

private String doEvaluate(final String sqlQuery) {
    final StringBuilder buildQuery = new StringBuilder(sqlQuery.length());
    int lastMatchOffset = 0;
    final Matcher matcher = JAVA_PATTERN.matcher(sqlQuery);
    while (matcher.find()) {
        // The first matched char is a blank, we keep it (matcher.start+1)
        buildQuery.append(sqlQuery.substring(lastMatchOffset, matcher.start() + 1));
        lastMatchOffset = matcher.end();
        final String fkFieldName = matcher.group(MASTER_TABLE_FK_GROUP);
        final String pkFieldName = matcher.group(DTC_INPUT_PK_GROUP);
        final String inputParamName = matcher.group(DTC_INPUTNAME_GROUP);
        // null if not found
        final boolean isNotIn = matcher.group(OPTIONNAL_NOT_GROUP) != null;
        final TaskAttribute attribute = obtainInTaskAttribute(inputParamName);
        Assertion.checkState(attribute.getDomain().isMultiple(), "Attribute {0} can't be use in WherInPreProcessor. Check it was declared as IN and is DtList type.", inputParamName);
        // -----
        final List<?> listObject = (List<?>) inTaskAttributes.get(attribute);
        if (listObject.isEmpty()) {
            // where XX not in <<empty>> => always true
            // where XX in <<empty>> => always false
            buildQuery.append(isNotIn ? "1=1" : "1=2");
        } else {
            // -----
            final boolean moreThanOneWhereIn = listObject.size() > NB_MAX_WHERE_IN_ITEM;
            if (moreThanOneWhereIn) {
                buildQuery.append("( ");
            }
            appendValuesToSqlQuery(buildQuery, fkFieldName, pkFieldName, inputParamName, isNotIn, listObject, attribute.getDomain().getScope().isPrimitive(), moreThanOneWhereIn);
            if (moreThanOneWhereIn) {
                buildQuery.append(')');
            }
        }
    }
    Assertion.checkState(lastMatchOffset > 0, "WhereInPreProcessor not applied. Keywords found but query doesn't match. Check syntaxe : XXX_ID <<not>> in (#YYY.ROWNUM.ZZZ_ID#) of {0}", sqlQuery);
    buildQuery.append(sqlQuery.substring(lastMatchOffset));
    return buildQuery.toString();
}
Also used : TaskAttribute(io.vertigo.dynamo.task.metamodel.TaskAttribute) Matcher(java.util.regex.Matcher) List(java.util.List)

Example 7 with TaskAttribute

use of io.vertigo.dynamo.task.metamodel.TaskAttribute in project vertigo by KleeGroup.

the class AbstractTaskEngineSQL method preProcessQuery.

/**
 * Permet de créer du SQL dynamiquement interprété.
 * Les paramètres IN de la tache peuvent être invoqués pour construire
 * la requête SQL.
 * Exemple :
 * request = " Select *
 *       From PRODUIT
 *       <%if (dtoProduitCritere.getPrdLibelle()!=null) {%>
 *               Where PRD_LIBELLE like #DTO_PRODUIT_CRITERE.PRD_LIBELLE#||'%%'
 *       <%}%> order by <%=1%>";
 * @param sqlQuery Requete à évaluer
 * @return Requete évaluée
 */
protected final String preProcessQuery(final String sqlQuery) {
    final Collection<TaskAttribute> inAttributes = getTaskDefinition().getInAttributes();
    final Map<TaskAttribute, Object> inTaskAttributes = new HashMap<>(inAttributes.size());
    for (final TaskAttribute taskAttribute : inAttributes) {
        inTaskAttributes.put(taskAttribute, getValue(taskAttribute.getName()));
    }
    // -----
    final ScriptPreProcessor scriptPreProcessor = new ScriptPreProcessor(scriptManager, inTaskAttributes, SeparatorType.CLASSIC);
    final TrimPreProcessor trimPreProcessor = new TrimPreProcessor(SeparatorType.BEGIN_SEPARATOR_CLASSIC, SeparatorType.END_SEPARATOR_CLASSIC);
    final WhereInPreProcessor whereInPreProcessor = new WhereInPreProcessor(inTaskAttributes);
    // --
    String sql = sqlQuery;
    sql = scriptPreProcessor.evaluate(sql);
    sql = trimPreProcessor.evaluate(sql);
    sql = whereInPreProcessor.evaluate(sql);
    return sql;
}
Also used : TaskAttribute(io.vertigo.dynamo.task.metamodel.TaskAttribute) HashMap(java.util.HashMap)

Example 8 with TaskAttribute

use of io.vertigo.dynamo.task.metamodel.TaskAttribute in project vertigo by KleeGroup.

the class ScriptPreProcessor method createParameters.

private static List<ExpressionParameter> createParameters(final ScriptManager scriptManager, final Map<TaskAttribute, Object> inTaskAttributes) {
    Assertion.checkNotNull(scriptManager);
    Assertion.checkNotNull(inTaskAttributes);
    // -----
    final List<ExpressionParameter> tmpParameters = new ArrayList<>(inTaskAttributes.size());
    // ==========Initialisation des types et noms de paramètre==============
    ExpressionParameter scriptEvaluatorParameter;
    for (final Entry<TaskAttribute, Object> entry : inTaskAttributes.entrySet()) {
        final TaskAttribute taskAttribute = entry.getKey();
        final Class<?> clazz = taskAttribute.getDomain().getTargetJavaClass();
        scriptEvaluatorParameter = new ExpressionParameter(StringUtil.constToLowerCamelCase(taskAttribute.getName()), clazz, entry.getValue());
        tmpParameters.add(scriptEvaluatorParameter);
    }
    return tmpParameters;
}
Also used : TaskAttribute(io.vertigo.dynamo.task.metamodel.TaskAttribute) ArrayList(java.util.ArrayList) ExpressionParameter(io.vertigo.commons.script.ExpressionParameter)

Example 9 with TaskAttribute

use of io.vertigo.dynamo.task.metamodel.TaskAttribute in project vertigo by KleeGroup.

the class TaskBuilder method addValue.

/**
 * adds a value to an attribute.
 *
 * @param attributeName the name of the attribute
 * @param value the values
 * @return this builder
 */
public TaskBuilder addValue(final String attributeName, final Object value) {
    final TaskAttribute taskAttribute = taskDefinition.getInAttribute(attributeName);
    taskAttributesBuilder.putNullable(taskAttribute, value);
    return this;
}
Also used : TaskAttribute(io.vertigo.dynamo.task.metamodel.TaskAttribute)

Aggregations

TaskAttribute (io.vertigo.dynamo.task.metamodel.TaskAttribute)9 TaskDefinition (io.vertigo.dynamo.task.metamodel.TaskDefinition)2 List (java.util.List)2 ExpressionParameter (io.vertigo.commons.script.ExpressionParameter)1 ScriptManager (io.vertigo.commons.script.ScriptManager)1 VTransactionManager (io.vertigo.commons.transaction.VTransactionManager)1 SqlDataBaseManager (io.vertigo.database.sql.SqlDataBaseManager)1 SqlConnection (io.vertigo.database.sql.connection.SqlConnection)1 SqlStatement (io.vertigo.database.sql.statement.SqlStatement)1 SqlStatementBuilder (io.vertigo.database.sql.statement.SqlStatementBuilder)1 StoreManager (io.vertigo.dynamo.store.StoreManager)1 Assertion (io.vertigo.lang.Assertion)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 OptionalInt (java.util.OptionalInt)1 Matcher (java.util.regex.Matcher)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1