Search in sources :

Example 56 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class ToBinaryFromPath method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    Object o = getOperand(0);
    String s = (String) o;
    try {
        java.io.File u = new java.io.File(s);
        Binary b = new Binary(u);
        return b;
    } catch (MalformedURLException e) {
        throw new TMLExpressionException("Bad url in function ToBinaryFromPath: " + s, e);
    } catch (IOException e) {
        throw new TMLExpressionException("Error opening url in function ToBinaryFromPath: " + s, e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Binary(com.dexels.navajo.document.types.Binary) IOException(java.io.IOException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 57 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class GetSequenceValue method evaluate.

@Override
public Object evaluate() {
    Integer transactionContext = -1;
    String datasource = null;
    String sequencename = null;
    Object o1 = operand(0).value;
    String sql = "";
    if (o1 instanceof Integer) {
        // TransactionContext set.
        transactionContext = (Integer) o1;
        Object o2 = operand(1).value;
        if (!(o2 instanceof String))
            throw new TMLExpressionException(this, "Invalid argument: " + o2);
        sequencename = (String) o2;
    } else if (o1 instanceof String) {
        // No TransactionContext set.
        sql = (String) o1;
        datasource = sql.substring(0, sql.indexOf(SingleValueQuery.DATASOURCEDELIMITER));
        sequencename = sql.substring(sql.indexOf(SingleValueQuery.DATASOURCEDELIMITER) + 1);
    } else
        throw new TMLExpressionException(this, "Invalid argument: " + o1);
    // Use SingleValueQuery to execute the statement
    SingleValueQuery query = new SingleValueQuery();
    query.reset();
    query.setAccess(getAccess());
    String dbIdentifier = getDbIdentifier(datasource, transactionContext);
    if (sequencename != null) {
        if (SQLMapConstants.POSTGRESDB.equals(dbIdentifier) || SQLMapConstants.ENTERPRISEDB.equals(dbIdentifier)) {
            sql = "SELECT nextval('" + sequencename + "')";
        } else {
            sql = "SELECT " + sequencename + ".nextval FROM dual";
        }
    }
    // Reuse the query object for the actual work
    query.reset();
    if (transactionContext != -1) {
        query.insertIntegerOperand(transactionContext);
    }
    if (datasource != null) {
        sql = datasource + ":" + sql;
    }
    query.insertStringOperand(sql);
    return query.evaluate();
}
Also used : TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 58 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class SingleValueQuery method evaluateQuery.

protected final JDBCMappable evaluateQuery() {
    String query = "";
    JDBCMappable sql = null;
    int transactionContext = -1;
    // String read query.
    Object o1 = operand(0).value;
    if (o1 instanceof Integer) {
        // TransactionContext set.
        transactionContext = ((Integer) o1).intValue();
        Object o2 = operand(1).value;
        if (!(o2 instanceof String))
            throw new TMLExpressionException(this, "Invalid argument: " + o2);
        query = (String) o2;
    } else if (o1 instanceof String) {
        // No TransactionContext set.
        query = (String) o1;
    } else {
        throw new TMLExpressionException(this, "Invalid argument: " + o1);
    }
    StringTokenizer tokens = new StringTokenizer(query, "?");
    int parameterCount = tokens.countTokens() - 1;
    if (query.endsWith("?"))
        parameterCount++;
    String datasource = "";
    String user = "";
    try {
        sql = JDBCFactory.getJDBCMap(getAccess());
        if (query.indexOf(DATASOURCEDELIMITER) != -1) {
            // Contains datasource specification
            datasource = query.substring(0, query.indexOf(DATASOURCEDELIMITER));
            query = query.substring(query.indexOf(DATASOURCEDELIMITER) + 1);
            if (datasource.indexOf(USERDELIMITER) != -1) {
                user = datasource.substring(0, datasource.indexOf(USERDELIMITER));
                datasource = datasource.substring(datasource.indexOf(USERDELIMITER) + 1);
            }
            if (datasource != null) {
                if (datasource.trim().equals("")) {
                    logger.warn("Ignoring empty datasource - using default!");
                } else {
                    sql.setDatasource(datasource);
                }
            }
            if (user != null && !user.trim().equals("")) {
                sql.setUsername(user);
            }
        }
        if (transactionContext != -1) {
            sql.setTransactionContext(transactionContext);
        }
        sql.setQuery(query);
        int offset = (transactionContext == -1) ? 1 : 2;
        for (int i = 0; i < parameterCount; i++) {
            Object o = operand(i + offset).value;
            sql.setParameter(o);
        }
    } catch (Exception e) {
        sql.kill();
        throw new TMLExpressionException(this, "Fatal error: " + e.getMessage() + ", query = " + query, e);
    }
    return sql;
}
Also used : StringTokenizer(java.util.StringTokenizer) JDBCMappable(com.dexels.navajo.jdbc.JDBCMappable) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 59 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class MultipleValueQuery method evaluate.

@Override
public final Object evaluate() {
    JDBCMappable sql = evaluateQuery();
    ArrayList result = new ArrayList();
    try {
        ResultSetMap[] resultSet = sql.getResultSet();
        if (resultSet.length > 0) {
            for (int i = 0; i < resultSet.length; i++) {
                result.add(resultSet[i].getColumnValue(Integer.valueOf(0)));
            }
        }
    } catch (Exception e) {
        sql.kill();
        throw new TMLExpressionException(this, "Fatal error: " + e.getMessage(), e);
    } finally {
        try {
            sql.store();
        } catch (Exception e1) {
            logger.error("Error: ", e1);
        }
    }
    return result;
}
Also used : JDBCMappable(com.dexels.navajo.jdbc.JDBCMappable) ArrayList(java.util.ArrayList) ResultSetMap(com.dexels.navajo.adapter.sqlmap.ResultSetMap) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 60 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class Age method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    final Object o = this.getOperands().get(0);
    if (o == null | !(o instanceof java.util.Date)) {
        throw new TMLExpressionException("Age: could not return value for input: " + o);
    }
    final java.util.Date datum = (java.util.Date) o;
    final Calendar cal = Calendar.getInstance();
    cal.setTime(datum);
    /* if a second operand is provided, calculate the age from that
        date, otherwise calculate the age as of today */
    Calendar asofCal = Calendar.getInstance();
    asofCal.setTime(new java.util.Date());
    if (this.getOperands().size() > 1) {
        final Object p = this.getOperands().get(1);
        if ((p != null) && (p instanceof java.util.Date)) {
            asofCal.setTime((java.util.Date) p);
        }
    }
    int yToday = asofCal.get(Calendar.YEAR);
    int dToday = cal.get(Calendar.YEAR);
    int result = yToday - dToday;
    if ((cal.get(Calendar.MONTH) > asofCal.get(Calendar.MONTH))) {
        result--;
    } else if ((cal.get(Calendar.MONTH) == asofCal.get(Calendar.MONTH)) && (cal.get(Calendar.DAY_OF_MONTH) > asofCal.get(Calendar.DAY_OF_MONTH))) {
        result--;
    }
    return Integer.valueOf(result);
}
Also used : Calendar(java.util.Calendar) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Aggregations

TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)129 Message (com.dexels.navajo.document.Message)26 Navajo (com.dexels.navajo.document.Navajo)26 Binary (com.dexels.navajo.document.types.Binary)23 Property (com.dexels.navajo.document.Property)17 ArrayList (java.util.ArrayList)16 Operand (com.dexels.navajo.document.Operand)15 List (java.util.List)13 NavajoException (com.dexels.navajo.document.NavajoException)12 IOException (java.io.IOException)12 Selection (com.dexels.navajo.document.Selection)11 Access (com.dexels.navajo.script.api.Access)10 Calendar (java.util.Calendar)10 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)9 Test (org.junit.Test)9 Money (com.dexels.navajo.document.types.Money)8 SystemException (com.dexels.navajo.script.api.SystemException)8 StringTokenizer (java.util.StringTokenizer)8 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)7 ClockTime (com.dexels.navajo.document.types.ClockTime)7