Example usage for org.springframework.jdbc.support.rowset SqlRowSet wasNull

List of usage examples for org.springframework.jdbc.support.rowset SqlRowSet wasNull

Introduction

In this page you can find the example usage for org.springframework.jdbc.support.rowset SqlRowSet wasNull.

Prototype

boolean wasNull() throws InvalidResultSetAccessException;

Source Link

Document

Report whether the last column read had a value of SQL NULL .

Usage

From source file:org.restsql.core.impl.SqlUtils.java

public static Object getObjectByColumnLabel(final ColumnMetaData column, final SqlRowSet resultSet) {
    Object value = null;/*from w  ww .  j a  v a  2  s .c  o  m*/
    if (column.getColumnType() == Types.DATE && column.getColumnTypeName().equals("YEAR")) {
        value = new Integer(resultSet.getInt(column.getColumnLabel()));
        if (resultSet.wasNull()) {
            value = null;
        }
    } else {
        value = resultSet.getObject(column.getColumnLabel());
    }

    return value;
}

From source file:org.restsql.core.impl.SqlUtils.java

public static Object getObjectByColumnNumber(final ColumnMetaData column, final SqlRowSet resultSet) {
    Object value = null;/*from w  ww .  j a v a 2  s  .  c om*/
    if (column.getColumnType() == Types.DATE && column.getColumnTypeName().equals("YEAR")) {
        value = new Integer(resultSet.getInt(column.getColumnNumber()));
        if (resultSet.wasNull()) {
            value = null;
        }
    } else {
        value = resultSet.getObject(column.getColumnNumber());
    }
    return value;
}

From source file:org.apereo.lap.services.ThresholdTriggerService.java

public boolean triggerSSP(List<Output> outputs) {

    List<Configuration> configurations = configurationRepository.findAll();

    // Check If Configuration Is Available And Active For SSP
    if (configurations.size() == 0 || !configurations.get(0).isSSPActive())
        return false;

    for (Output output : outputs) {
        Map<String, String> sourceToHeaderMap = output.makeSourceTargetMap();
        String selectSQL = output.makeTempDBSelectSQL();

        SqlRowSet rowSet;
        try {/*w  w  w . j  ava 2s.  co  m*/
            rowSet = storage.getTempJdbcTemplate().queryForRowSet(selectSQL);
        } catch (Exception e) {
            throw new RuntimeException("Failure while trying to retrieve the output data set: " + selectSQL);
        }

        while (rowSet.next()) {

            if (!rowSet.wasNull()) {
                String[] rowVals = new String[sourceToHeaderMap.size()];

                if (rowVals.length > 0) {
                    String userId = rowSet.getString(1);

                    if (rowVals.length > 2) {
                        try {
                            if (rowSet.getString(3) == "HIGH RISK") {
                                try {
                                    sspService.createEarlyAlert(configurations.get(0).getSspBaseUrl(), userId);
                                } catch (IOException ex) {
                                    logger.error(ex.getMessage());
                                }
                            }
                        } catch (NumberFormatException ex) {
                            logger.error(ex.getMessage());
                        }
                    }
                }
            }
        }

        return true;
    }

    return false;
}

From source file:org.apereo.lap.services.output.StorageOutputHandler.java

@Override
public OutputResult writeOutput(Output output) {
    OutputResult result = new OutputResult(output);

    Map<String, String> sourceToHeaderMap = output.makeSourceTargetMap();
    String selectSQL = output.makeTempDBSelectSQL();

    SqlRowSet rowSet;
    try {// w  ww .  j av a 2s  .  c  o  m
        rowSet = storage.getTempJdbcTemplate().queryForRowSet(selectSQL);
    } catch (Exception e) {
        throw new RuntimeException("Failure while trying to retrieve the output data set: " + selectSQL);
    }

    String groupId = UUID.randomUUID().toString();

    while (rowSet.next()) {

        RiskConfidence riskConfidence = new RiskConfidence();

        if (!rowSet.wasNull()) {
            riskConfidence.setGroupId(groupId);

            String[] rowVals = new String[sourceToHeaderMap.size()];

            if (rowVals.length > 0)
                riskConfidence.setAlternativeId(rowSet.getString(1));

            if (rowVals.length > 1)
                riskConfidence.setCourseId(rowSet.getString(2));

            if (rowVals.length > 2)
                riskConfidence.setModelRiskConfidence(rowSet.getString(3));

            riskConfidenceRepository.save(riskConfidence);
        }
    }

    return result;
}

From source file:org.apereo.lap.services.output.CSVOutputHandler.java

@Override
public OutputResult writeOutput(Output output) {
    OutputResult result = new OutputResult(output);
    // make sure we can write the CSV
    File csv = new File(configuration.getOutputDirectory(), output.filename);
    boolean created;
    try {//from  w ww  .j  ava2 s. co m
        created = csv.createNewFile();
        if (logger.isDebugEnabled())
            logger.debug("CSV file created (" + created + "): " + csv.getAbsolutePath());
    } catch (IOException e) {
        throw new IllegalStateException("Exception creating CSV file: " + csv.getAbsolutePath() + ": " + e, e);
    }
    if (!created) { // created file is going to be a writeable file so no check needed
        if (csv.isFile() && csv.canRead() && csv.canWrite()) {
            // file exists and we can write to it
            if (logger.isDebugEnabled())
                logger.debug("CSV file is writeable: " + csv.getAbsolutePath());
        } else {
            throw new IllegalStateException("Cannot write to the CSV file: " + csv.getAbsolutePath());
        }
    }

    // make sure we can read from the temp data source
    try {
        int rows = storage.getTempJdbcTemplate().queryForObject(output.makeTempDBCheckSQL(), Integer.class);
        logger.info(
                "Preparing to output " + rows + " from temp table " + output.from + " to " + output.filename);
    } catch (Exception e) {
        throw new RuntimeException(
                "Failure while trying to count the output data rows: " + output.makeTempDBCheckSQL());
    }

    Map<String, String> sourceToHeaderMap = output.makeSourceTargetMap();
    String selectSQL = output.makeTempDBSelectSQL();

    // fetch the data to write to CSV
    SqlRowSet rowSet;
    try {
        // for really large data we probably need to use http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/jdbc/core/RowCallbackHandler.html
        rowSet = storage.getTempJdbcTemplate().queryForRowSet(selectSQL);
    } catch (Exception e) {
        throw new RuntimeException("Failure while trying to retrieve the output data set: " + selectSQL);
    }

    // write data to the CSV file
    int lines = 0;
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(new BufferedWriter(new FileWriter(csv, true)));
        CSVWriter writer = new CSVWriter(pw);

        // write out the header
        writer.writeNext(sourceToHeaderMap.values().toArray(new String[sourceToHeaderMap.size()]));

        // write out the rows
        while (rowSet.next()) {
            String[] rowVals = new String[sourceToHeaderMap.size()];
            for (int i = 0; i < sourceToHeaderMap.size(); i++) {
                rowVals[i] = (rowSet.wasNull() ? null : rowSet.getString(i + 1));
            }
            writer.writeNext(rowVals);
        }

        IOUtils.closeQuietly(writer);
    } catch (Exception e) {
        throw new RuntimeException("Failure writing output to CSV (" + csv.getAbsolutePath() + "): " + e, e);
    } finally {
        IOUtils.closeQuietly(pw);
    }

    result.done(lines, null);
    return result;
}

From source file:org.apereo.lap.services.output.handlers.CSVOutputHandler.java

@Override
public OutputResult writeOutput(Output output) {
    OutputResult result = new OutputResult(output);
    // make sure we can write the CSV
    File csv = configuration.getOutputDirectory().resolve(output.filename).toFile();
    boolean created;
    try {//from  ww  w.  ja v  a2s  .c  om
        created = csv.createNewFile();
        if (logger.isDebugEnabled())
            logger.debug("CSV file created (" + created + "): " + csv.getAbsolutePath());
    } catch (IOException e) {
        throw new IllegalStateException("Exception creating CSV file: " + csv.getAbsolutePath() + ": " + e, e);
    }
    if (!created) { // created file is going to be a writeable file so no check needed
        if (csv.isFile() && csv.canRead() && csv.canWrite()) {
            // file exists and we can write to it
            if (logger.isDebugEnabled())
                logger.debug("CSV file is writeable: " + csv.getAbsolutePath());
        } else {
            throw new IllegalStateException("Cannot write to the CSV file: " + csv.getAbsolutePath());
        }
    }

    // make sure we can read from the temp data source
    try {
        int rows = storage.getTempJdbcTemplate().queryForObject(output.makeTempDBCheckSQL(), Integer.class);
        logger.info(
                "Preparing to output " + rows + " from temp table " + output.from + " to " + output.filename);
    } catch (Exception e) {
        throw new RuntimeException(
                "Failure while trying to count the output data rows: " + output.makeTempDBCheckSQL());
    }

    Map<String, String> sourceToHeaderMap = output.makeSourceTargetMap();
    String selectSQL = output.makeTempDBSelectSQL();

    // fetch the data to write to CSV
    SqlRowSet rowSet;
    try {
        // for really large data we probably need to use http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/jdbc/core/RowCallbackHandler.html
        rowSet = storage.getTempJdbcTemplate().queryForRowSet(selectSQL);
    } catch (Exception e) {
        throw new RuntimeException("Failure while trying to retrieve the output data set: " + selectSQL);
    }

    // write data to the CSV file
    int lines = 0;
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(new BufferedWriter(new FileWriter(csv, true)));
        CSVWriter writer = new CSVWriter(pw);

        // write out the header
        writer.writeNext(sourceToHeaderMap.values().toArray(new String[sourceToHeaderMap.size()]));

        // write out the rows
        while (rowSet.next()) {
            String[] rowVals = new String[sourceToHeaderMap.size()];
            for (int i = 0; i < sourceToHeaderMap.size(); i++) {
                rowVals[i] = (rowSet.wasNull() ? null : rowSet.getString(i + 1));
            }
            writer.writeNext(rowVals);
        }

        IOUtils.closeQuietly(writer);
    } catch (Exception e) {
        throw new RuntimeException("Failure writing output to CSV (" + csv.getAbsolutePath() + "): " + e, e);
    } finally {
        IOUtils.closeQuietly(pw);
    }

    result.done(lines, null);
    return result;
}

From source file:org.apereo.lap.services.output.handlers.SSPEarlyAlertOutputHandler.java

@Override
public OutputResult writeOutput(Output output) {
    logger.debug(output.toString());//w  w  w . ja v a2 s.  com

    SSPConfigPersistentStorage sspConfigPersistentStorage = storageFactory.getSSPConfigPersistentStorage();
    SSPConfig sspConfig = sspConfigPersistentStorage.get();

    if (sspConfig == null) {
        throw new RuntimeException("No SSP Configuration");
    }

    ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
    resourceDetails.setClientId(sspConfig.getKey());
    resourceDetails.setClientSecret(sspConfig.getSecret());

    String baseUrl = sspConfig.getUrl();
    if (!baseUrl.endsWith("/")) {
        baseUrl = baseUrl.concat("/");
    }

    resourceDetails.setAccessTokenUri(baseUrl + "ssp/api/1/oauth2/token");
    DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();

    OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resourceDetails, clientContext);
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setSupportedMediaTypes(
            Arrays.asList(MediaType.APPLICATION_JSON, MediaType.valueOf("text/javascript")));
    restTemplate.setMessageConverters(Arrays.<HttpMessageConverter<?>>asList(converter));

    OutputResult result = new OutputResult(output);

    String selectSQL = output.makeTempDBSelectSQL();

    SqlRowSet rowSet;
    try {
        rowSet = storage.getTempJdbcTemplate().queryForRowSet(selectSQL);

    } catch (Exception e) {
        throw new RuntimeException("Failure while trying to retrieve the output data set: " + selectSQL);
    }

    Map<String, Integer> riskMap = new HashMap<String, Integer>();
    riskMap.put("NO RISK", 0);
    riskMap.put("LOW RISK", 1);
    riskMap.put("MEDIUM RISK", 2);
    riskMap.put("HIGH RISK", 3);

    Integer riskThreshold = riskMap.get(sspConfig.getRiskRule());

    List<EarlyAlert> earlyAlertList = new ArrayList<SSPEarlyAlertOutputHandler.EarlyAlert>();
    while (rowSet.next()) {
        if (!rowSet.wasNull()) {

            String student = rowSet.getString(1);
            String course = rowSet.getString(2);
            String risk = rowSet.getString(3);

            Integer riskScore = riskMap.get(risk);

            if (riskScore >= riskThreshold) {
                EarlyAlert earlyAlert = new EarlyAlert(course, student,
                        "Automated early alert due to risk score above acceptable limit", risk, null);
                earlyAlertList.add(earlyAlert);
            }

            logger.debug(String.format("student: %s, course: %s, risk:%s", student, course, risk));
        }
    }

    if (earlyAlertList.size() > 0) {
        EarlyAlertMessage message = new EarlyAlertMessage("test.com", "test", earlyAlertList);
        restTemplate.postForLocation(baseUrl + "ssp/api/1/bulkEarlyAlerts", message);
    }

    return result;
}