Search in sources :

Example 6 with ResultSetMap

use of com.dexels.navajo.adapter.sqlmap.ResultSetMap in project navajo by Dexels.

the class QueryMap method store.

@Override
public void store() throws MappableException, UserException {
    // Construct Navajo message.
    try {
        Message recordSet = NavajoFactory.getInstance().createMessage(outputDoc, "RecordSet", Message.MSG_TYPE_ARRAY);
        try {
            outputDoc.addMessage(recordSet);
        } catch (NavajoException ex) {
            throw new UserException(-1, ex.getMessage(), ex);
        }
        ResultSetMap[] resultSet = getResultSet();
        for (int i = 0; i < resultSet.length; i++) {
            Message record = NavajoFactory.getInstance().createMessage(outputDoc, "RecordSet", Message.MSG_TYPE_ARRAY_ELEMENT);
            recordSet.addElement(record);
            RecordMap[] columns = resultSet[i].getRecords();
            for (int j = 0; j < columns.length; j++) {
                try {
                    Object value = columns[j].getRecordValue();
                    String type = (value != null ? MappingUtils.determineNavajoType(value) : "unknown");
                    Property prop = NavajoFactory.getInstance().createProperty(outputDoc, columns[j].recordName, type, null, 0, "", Property.DIR_IN);
                    prop.setAnyValue(value);
                    record.addProperty(prop);
                } catch (Exception ex1) {
                    throw new UserException(-1, ex1.getMessage(), ex1);
                }
            }
        }
    } finally {
        super.store();
    }
}
Also used : RecordMap(com.dexels.navajo.adapter.sqlmap.RecordMap) Message(com.dexels.navajo.document.Message) NavajoException(com.dexels.navajo.document.NavajoException) ResultSetMap(com.dexels.navajo.adapter.sqlmap.ResultSetMap) UserException(com.dexels.navajo.script.api.UserException) Property(com.dexels.navajo.document.Property) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) NavajoException(com.dexels.navajo.document.NavajoException)

Example 7 with ResultSetMap

use of com.dexels.navajo.adapter.sqlmap.ResultSetMap in project navajo by Dexels.

the class SQLMap method getResultSet.

protected ResultSetMap[] getResultSet(boolean updateOnly) throws UserException {
    requestCount++;
    ResultSet rs = null;
    long start = 0;
    if (debug || timeAlert > 0) {
        start = System.currentTimeMillis();
    }
    try {
        if (resultSet == null) {
            rs = getDBResultSet(updateOnly);
        }
        if (debug) {
            Access.writeToConsole(myAccess, "SQLMAP, QUERY HAS BEEN EXECUTED, RETRIEVING RESULTSET\n");
        }
        if (rs != null) {
            int columns = 0;
            ResultSetMetaData meta = null;
            try {
                meta = rs.getMetaData();
                columns = meta.getColumnCount();
            } catch (Exception e) {
                throw new UserException(-1, "Error getting metadata / columns", e);
            }
            ArrayList dummy = new ArrayList();
            int index = 1;
            remainCount = 0;
            rowCount = 0;
            while (rs.next()) {
                if ((index >= startIndex) && (index <= endIndex)) {
                    ResultSetMap rm = getResultSetMap(meta, columns, rs);
                    dummy.add(rm);
                    viewCount++;
                }
                // else if (index >= startIndex) {
                // remainCount++;
                // }
                rowCount++;
                index++;
            }
            if (debug) {
                Access.writeToConsole(myAccess, "GOT RESULTSET\n");
            }
            resultSet = new ResultSetMap[dummy.size()];
            resultSet = (ResultSetMap[]) dummy.toArray(resultSet);
        }
    } catch (SQLException sqle) {
        logger.error("The following query failed: {}", this.getQuery());
        AuditLog.log("SQLMap", sqle.getMessage(), sqle, Level.SEVERE, (myAccess != null ? (myAccess != null ? myAccess.accessID : "unknown access") : "unknown access"));
        throw new UserException(-1, sqle.getMessage(), sqle);
    } catch (Exception sqle) {
        AuditLog.log("SQLMap", sqle.getMessage(), sqle, Level.SEVERE, (myAccess != null ? (myAccess != null ? myAccess.accessID : "unknown access") : "unknown access"));
        throw new UserException(-1, sqle.getMessage(), sqle);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace(Access.getConsoleWriter(myAccess));
            }
            rs = null;
        }
        this.resetAll();
    }
    if (debug || timeAlert > 0) {
        long end = System.currentTimeMillis();
        double total = (end - start) / 1000.0;
        if (timeAlert > 0 && (int) (end - start) > timeAlert) {
            AuditLogEvent ale = new AuditLogEvent("SQLMAPTIMEALERT", "Query took " + (end - start) + " millis:\n" + (query != null ? query : update), Level.WARNING);
            ale.setAccessId(myAccess.accessID);
            NavajoEventRegistry.getInstance().publishEvent(ale);
        }
    // Log total if needed....
    // totaltiming += total;
    }
    return resultSet;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) AuditLogEvent(com.dexels.navajo.events.types.AuditLogEvent) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) ResultSetMap(com.dexels.navajo.adapter.sqlmap.ResultSetMap) UserException(com.dexels.navajo.script.api.UserException) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) SQLException(java.sql.SQLException)

Aggregations

ResultSetMap (com.dexels.navajo.adapter.sqlmap.ResultSetMap)7 ArrayList (java.util.ArrayList)5 MappableException (com.dexels.navajo.script.api.MappableException)4 UserException (com.dexels.navajo.script.api.UserException)4 ResultSet (java.sql.ResultSet)3 ResultSetMetaData (java.sql.ResultSetMetaData)3 SQLException (java.sql.SQLException)3 NavajoException (com.dexels.navajo.document.NavajoException)2 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)2 JDBCMappable (com.dexels.navajo.jdbc.JDBCMappable)2 IOException (java.io.IOException)2 RecordMap (com.dexels.navajo.adapter.sqlmap.RecordMap)1 Message (com.dexels.navajo.document.Message)1 Property (com.dexels.navajo.document.Property)1 Binary (com.dexels.navajo.document.types.Binary)1 AuditLogEvent (com.dexels.navajo.events.types.AuditLogEvent)1 File (java.io.File)1 PDFMergerUtility (org.apache.pdfbox.util.PDFMergerUtility)1