Search in sources :

Example 26 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class JDBCMap method getRecords.

/*
	 * (non-Javadoc)
	 * 
	 * @see com.dexels.navajo.adapter.JDBCMappable#getRecords()
	 */
@Override
public Binary getRecords() throws UserException {
    ResultSet rs = null;
    File tempFile;
    try {
        tempFile = File.createTempFile("sqlmap_records", "navajo");
    } catch (IOException e1) {
        throw new UserException("Temp file faillure", e1);
    }
    try (FileOutputStream fos = new FileOutputStream(tempFile);
        OutputStreamWriter fw = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
        Binary b = null;
        rs = getDBResultSet(false);
        int columns = 0;
        ResultSetMetaData meta = null;
        try {
            meta = rs.getMetaData();
            columns = meta.getColumnCount();
            if (this.showHeader) {
                for (int j = 0; j < columns; j++) {
                    String column = meta.getColumnLabel(j + 1);
                    if (j == 0) {
                        fw.write(column);
                    } else {
                        fw.write(this.separator + column);
                    }
                }
                fw.write("\n");
            }
        } catch (Exception e) {
            logger.error("Error writing output binary", e);
        }
        while (rs.next()) {
            for (int j = 1; j <= columns; j++) {
                String value = (rs.getObject(j) != null ? rs.getString(j) + "" : "");
                if (j == 1) {
                    fw.write(value);
                } else {
                    fw.write(this.separator + value);
                }
            }
            fw.write("\n");
        }
        fw.flush();
        b = new Binary(tempFile, false);
        return b;
    } catch (Exception ioe) {
        throw new UserException(-1, ioe.getMessage(), ioe);
    } finally {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
                resetAll();
            } catch (SQLException e) {
                logger.error("Error writing output binary", e);
            }
        }
        if (tempFile != null) {
            try {
                Files.deleteIfExists(tempFile.toPath());
            } catch (Exception ioe2) {
                logger.error("Error writing output binary", ioe2);
            }
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) FileOutputStream(java.io.FileOutputStream) ResultSet(java.sql.ResultSet) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) UserException(com.dexels.navajo.script.api.UserException) Binary(com.dexels.navajo.document.types.Binary) File(java.io.File) UserException(com.dexels.navajo.script.api.UserException) SQLException(java.sql.SQLException) MappableException(com.dexels.navajo.script.api.MappableException) IOException(java.io.IOException)

Example 27 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class JDBCMap method resetAll.

protected void resetAll() throws UserException {
    this.query = this.update = null;
    try {
        if (this.statement != null) {
            this.statement.close();
            this.statement = null;
        }
    } catch (Exception e) {
        AuditLog.log("SQLMap", e.getMessage(), Level.SEVERE, (myAccess != null ? myAccess.accessID : "unknown access"));
        throw new UserException(-1, e.getMessage());
    }
}
Also used : UserException(com.dexels.navajo.script.api.UserException) UserException(com.dexels.navajo.script.api.UserException) SQLException(java.sql.SQLException) MappableException(com.dexels.navajo.script.api.MappableException) IOException(java.io.IOException)

Example 28 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class GrusProviderImpl method getDatabaseIdentifier.

@Override
public String getDatabaseIdentifier(String instance, String name) throws UserException {
    DataSource dataSourceInstance = null;
    dataSourceInstance = getInstanceDataSource(instance, name);
    Map<String, Object> settings = settingsMap.get(dataSourceInstance);
    if (settings == null && dataSourceInstance == null) {
        settings = defaultSettingsMap.get(name);
    }
    if (settings == null) {
        throw new UserException(-1, "Could not find settings for tenant-less datasource: " + name + " available (tenant-less) datasources: " + defaultSettingsMap.keySet());
    }
    String componentName = (String) settings.get("component.name");
    if (componentName.endsWith("oracle")) {
        return SQLMapConstants.ORACLEDB;
    } else if (componentName.endsWith("mysql")) {
        return SQLMapConstants.MYSQLDB;
    } else if (componentName.endsWith("postgresql")) {
        return SQLMapConstants.POSTGRESDB;
    }
    return null;
}
Also used : UserException(com.dexels.navajo.script.api.UserException) DataSource(javax.sql.DataSource)

Example 29 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class GrusProviderImpl method requestConnection.

@Override
public GrusConnection requestConnection(String instance, String name) throws UserException {
    DataSource dataSourceInstance = null;
    dataSourceInstance = getInstanceDataSource(instance, name);
    Map<String, Object> settings = settingsMap.get(dataSourceInstance);
    if (settings == null && dataSourceInstance == null) {
        settings = defaultSettingsMap.get(name);
        if (settings == null) {
            throw new UserException(-1, "Could not find settings for tenant-less datasource: " + name + " available (tenant-less) datasources: " + defaultSettingsMap.keySet());
        }
        dataSourceInstance = defaultDataSources.get(name);
    }
    int id = connectionCounter.getAndIncrement();
    GrusConnection gc;
    try {
        gc = new GrusDataSource(id, dataSourceInstance, settings, this);
    } catch (Exception e) {
        throw new UserException(-1, "Could not create datasource connection for: " + instance + " and name: " + name, e);
    }
    grusIds.put((long) id, gc);
    Map<DataSource, Integer> currentMap = userThreadLocal.get();
    if (currentMap == null) {
        currentMap = new HashMap<>();
    }
    currentMap.put(dataSourceInstance, id);
    userThreadLocal.set(currentMap);
    return gc;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GrusConnection(org.dexels.grus.GrusConnection) UserException(com.dexels.navajo.script.api.UserException) UserException(com.dexels.navajo.script.api.UserException) DataSource(javax.sql.DataSource)

Example 30 with UserException

use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.

the class GrusProviderImpl method getInstanceDataSourceSettings.

@Override
public Map<String, Object> getInstanceDataSourceSettings(String instance, String name) throws UserException {
    DataSource dataSourceInstance = null;
    dataSourceInstance = getInstanceDataSource(instance, name);
    Map<String, Object> settings = settingsMap.get(dataSourceInstance);
    if (settings == null && dataSourceInstance == null) {
        settings = defaultSettingsMap.get(name);
        if (settings == null) {
            throw new UserException(-1, "Could not find settings for tenant-less datasource: " + name + " available (tenant-less) datasources: " + defaultSettingsMap.keySet());
        }
    }
    return settings;
}
Also used : UserException(com.dexels.navajo.script.api.UserException) DataSource(javax.sql.DataSource)

Aggregations

UserException (com.dexels.navajo.script.api.UserException)113 MappableException (com.dexels.navajo.script.api.MappableException)54 IOException (java.io.IOException)33 NavajoException (com.dexels.navajo.document.NavajoException)25 Message (com.dexels.navajo.document.Message)22 SQLException (java.sql.SQLException)19 SystemException (com.dexels.navajo.script.api.SystemException)18 Binary (com.dexels.navajo.document.types.Binary)14 ConditionErrorException (com.dexels.navajo.server.ConditionErrorException)13 Property (com.dexels.navajo.document.Property)12 ArrayList (java.util.ArrayList)12 Navajo (com.dexels.navajo.document.Navajo)11 AuthorizationException (com.dexels.navajo.script.api.AuthorizationException)11 ResultSet (java.sql.ResultSet)11 MappingException (com.dexels.navajo.script.api.MappingException)10 ResultSetMetaData (java.sql.ResultSetMetaData)9 Element (org.w3c.dom.Element)8 CompilationException (com.dexels.navajo.script.api.CompilationException)7 File (java.io.File)7 NodeList (org.w3c.dom.NodeList)7