Search in sources :

Example 1 with JDBCMappable

use of com.dexels.navajo.jdbc.JDBCMappable 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 2 with JDBCMappable

use of com.dexels.navajo.jdbc.JDBCMappable 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 3 with JDBCMappable

use of com.dexels.navajo.jdbc.JDBCMappable in project navajo by Dexels.

the class MergePDFsFromDatasource method evaluate.

@SuppressWarnings("unchecked")
@Override
public Object evaluate() throws TMLExpressionException {
    if (getOperands().size() != 7) {
        throw new TMLExpressionException("Invalid number of operands.");
    }
    // List items contains all the ids of the document
    ArrayList<String> items = null;
    if (getOperand(0) != null) {
        if (getOperand(0) instanceof ArrayList) {
            items = (ArrayList<String>) getOperand(0);
        } else if (getOperand(0) instanceof String) {
            items = (ArrayList<String>) Arrays.stream(((String) getOperand(0)).split(";")).map(o -> o.toString()).collect(Collectors.toList());
        }
    }
    int transactionContext = -1;
    if (getOperand(1) != null && getOperand(1) instanceof Integer) {
        transactionContext = ((Integer) getOperand(1)).intValue();
    }
    String datasource = null;
    if (getOperand(2) != null && getOperand(2) instanceof String) {
        datasource = (String) getOperand(2);
    }
    String username = null;
    if (getOperand(3) != null && getOperand(3) instanceof String) {
        username = (String) getOperand(3);
    }
    String tableId = null;
    if (getOperand(4) != null && getOperand(4) instanceof String) {
        tableId = (String) getOperand(4);
    }
    String objectType = null;
    if (getOperand(5) != null && getOperand(5) instanceof String) {
        objectType = (String) getOperand(5);
    }
    String binaryColumnName = null;
    if (getOperand(6) != null && getOperand(6) instanceof String) {
        binaryColumnName = (String) getOperand(6);
    }
    // Max in operator in oracle takes 1000 ites, but we need more ::
    int numOfItems = items.size();
    String queryPart = "";
    if (numOfItems == 1) {
        queryPart = " in " + items.toString().replace("[", "('").replace("]", "')").replace(" ", "");
    } else {
        int startPosition = 0;
        int endPosition = 999;
        if (startPosition + endPosition >= numOfItems) {
            endPosition = numOfItems - 1;
        } else {
            endPosition = startPosition + 999;
        }
        while (endPosition <= numOfItems && startPosition != endPosition) {
            queryPart = queryPart + " or " + tableId + " in " + items.subList(startPosition, endPosition).toString().replace(",", "','").replace("[", "('").replace("]", "')").replace(" ", "");
            startPosition = endPosition;
            if (endPosition + 999 >= numOfItems) {
                endPosition = numOfItems;
            } else {
                endPosition = startPosition + 999;
            }
        }
        queryPart = queryPart.substring(4).substring(tableId.length());
    }
    String query = "select * FROM document where (" + tableId + queryPart + ") AND objectType  = '" + objectType + "'";
    JDBCMappable sql = null;
    ArrayList<Object> result = new ArrayList<>();
    try {
        sql = JDBCFactory.getJDBCMap(getAccess());
        if (transactionContext != -1) {
            sql.setTransactionContext(transactionContext);
        } else {
            sql.setDatasource(datasource);
            sql.setUsername(username);
        }
        sql.setQuery(query);
        System.out.println(query);
        ResultSetMap[] resultSet = sql.getResultSet();
        if (resultSet.length > 0) {
            for (int i = 0; i < resultSet.length; i++) {
                result.add(resultSet[i].getColumnValue(0));
            }
        }
        int dataPosition = -1;
        // We got them all, now MERGE :D
        if (result.size() > 0) {
            // First find DATA position
            for (int i = 0; i < resultSet[0].getValuesSize(); i++) {
                if (resultSet[0].getColumnName(i).equalsIgnoreCase(binaryColumnName)) {
                    dataPosition = i;
                    break;
                }
            }
            // Then combine
            try {
                PDFMergerUtility merger = new PDFMergerUtility();
                File tempFile = File.createTempFile("pdfmerge", "pdf");
                String fileName = tempFile.getCanonicalPath();
                merger.setDestinationFileName(fileName);
                // Logic to short by items.
                for (String item : items) {
                    for (int i = 0; i < resultSet.length; i++) {
                        if (resultSet[i].getColumnValue(tableId).toString().equals(item)) {
                            // FOUND
                            merger.addSource(((Binary) resultSet[i].getColumnValue(dataPosition)).getFile());
                            break;
                        }
                    }
                }
                merger.mergeDocuments();
                Binary resultPDF = new Binary(new File(fileName), false);
                tempFile.delete();
                return resultPDF;
            } catch (IOException e) {
                throw new TMLExpressionException(this, e.getMessage(), e);
            }
        }
    } catch (Exception e) {
        sql.kill();
        throw new TMLExpressionException(this, "Fatal error: " + e.getMessage() + ", query = " + query, e);
    } finally {
        sql.kill();
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) ResultSetMap(com.dexels.navajo.adapter.sqlmap.ResultSetMap) PDFMergerUtility(org.apache.pdfbox.util.PDFMergerUtility) IOException(java.io.IOException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) IOException(java.io.IOException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) JDBCMappable(com.dexels.navajo.jdbc.JDBCMappable) Binary(com.dexels.navajo.document.types.Binary) File(java.io.File)

Example 4 with JDBCMappable

use of com.dexels.navajo.jdbc.JDBCMappable in project navajo by Dexels.

the class SingleValueQuery method evaluate.

@Override
public Object evaluate() {
    JDBCMappable sql = evaluateQuery();
    setDbIdentifier(sql.getDbIdentifier());
    Object result = null;
    try {
        if (sql.getRowCount() > 0) {
            result = sql.getColumnValue(Integer.valueOf(0));
        } else {
        }
    } catch (Exception e) {
        sql.kill();
        throw new TMLExpressionException(this, "Fatal error: " + e.getMessage() + ", query = " + sql.getQuery(), e);
    } finally {
        try {
            sql.store();
        } catch (Exception e1) {
            logger.error("Fatal error: query = " + sql.getQuery(), e1);
        }
    }
    return result;
}
Also used : JDBCMappable(com.dexels.navajo.jdbc.JDBCMappable) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Aggregations

TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)4 JDBCMappable (com.dexels.navajo.jdbc.JDBCMappable)4 ResultSetMap (com.dexels.navajo.adapter.sqlmap.ResultSetMap)2 ArrayList (java.util.ArrayList)2 Binary (com.dexels.navajo.document.types.Binary)1 File (java.io.File)1 IOException (java.io.IOException)1 StringTokenizer (java.util.StringTokenizer)1 PDFMergerUtility (org.apache.pdfbox.util.PDFMergerUtility)1