Search in sources :

Example 6 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project perun by CESNET.

the class MailManagerImpl method getApplicationMails.

@Override
public List<ApplicationMail> getApplicationMails(PerunSession sess, ApplicationForm form) throws PerunException {
    List<ApplicationMail> mails = new ArrayList<ApplicationMail>();
    mails = jdbc.query(MAILS_SELECT_BY_FORM_ID, new RowMapper<ApplicationMail>() {

        @Override
        public ApplicationMail mapRow(ResultSet rs, int arg1) throws SQLException {
            return new ApplicationMail(rs.getInt("id"), AppType.valueOf(rs.getString("app_type")), rs.getInt("form_id"), MailType.valueOf(rs.getString("mail_type")), rs.getBoolean("send"));
        }
    }, form.getId());
    for (ApplicationMail mail : mails) {
        List<MailText> texts = new ArrayList<MailText>();
        texts = jdbc.query(MAIL_TEXTS_SELECT_BY_MAIL_ID, new RowMapper<MailText>() {

            @Override
            public MailText mapRow(ResultSet rs, int arg1) throws SQLException {
                return new MailText(new Locale(rs.getString("locale")), rs.getString("subject"), rs.getString("text"));
            }
        }, mail.getId());
        for (MailText text : texts) {
            // fil localized messages
            mail.getMessage().put(text.getLocale(), text);
        }
    }
    return mails;
}
Also used : ResultSet(java.sql.ResultSet) MailText(cz.metacentrum.perun.registrar.model.ApplicationMail.MailText) ApplicationMail(cz.metacentrum.perun.registrar.model.ApplicationMail) RowMapper(org.springframework.jdbc.core.RowMapper)

Example 7 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project perun by CESNET.

the class MailManagerImpl method getMailById.

@Override
public ApplicationMail getMailById(PerunSession sess, Integer id) throws InternalErrorException, PrivilegeException {
    // TODO authz
    ApplicationMail mail = null;
    // get mail def
    try {
        List<ApplicationMail> mails = jdbc.query("select id,app_type,form_id,mail_type,send from application_mails where id=?", new RowMapper<ApplicationMail>() {

            @Override
            public ApplicationMail mapRow(ResultSet rs, int arg1) throws SQLException {
                return new ApplicationMail(rs.getInt("id"), AppType.valueOf(rs.getString("app_type")), rs.getInt("form_id"), MailType.valueOf(rs.getString("mail_type")), rs.getBoolean("send"));
            }
        }, id);
        // set
        if (mails.size() != 1) {
            log.error("[MAIL MANAGER] Wrong number of mail definitions returned by unique params, expected 1 but was: " + mails.size());
            throw new InternalErrorException("Wrong number of mail definitions returned by unique params, expected 1 but was: " + mails.size());
        }
        mail = mails.get(0);
    } catch (EmptyResultDataAccessException ex) {
        throw new InternalErrorException("Mail definition with ID=" + id + " doesn't exists.");
    }
    List<MailText> texts = new ArrayList<MailText>();
    try {
        texts = jdbc.query(MAIL_TEXTS_SELECT_BY_MAIL_ID, new RowMapper<MailText>() {

            @Override
            public MailText mapRow(ResultSet rs, int arg1) throws SQLException {
                return new MailText(new Locale(rs.getString("locale")), rs.getString("subject"), rs.getString("text"));
            }
        }, mail.getId());
    } catch (EmptyResultDataAccessException ex) {
        // if no texts it's error
        log.error("[MAIL MANAGER] Mail do not contains any text message: {}", ex);
        return mail;
    }
    for (MailText text : texts) {
        // fill localized messages
        mail.getMessage().put(text.getLocale(), text);
    }
    return mail;
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) MailText(cz.metacentrum.perun.registrar.model.ApplicationMail.MailText) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) ApplicationMail(cz.metacentrum.perun.registrar.model.ApplicationMail) RowMapper(org.springframework.jdbc.core.RowMapper)

Example 8 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project Gargoyle by callakrsos.

the class SqlPane method applySelectScript.

/**
	 * 테이블의 SELECT문을 리턴.
	 *
	 * @param e
	 */
public void applySelectScript(ActionEvent e) {
    TreeItem<K> selectedItem = schemaTree.getSelectionModel().getSelectedItem();
    String tableName = this.getSelectedTreeByTableName(selectedItem);
    SqlTab selectedTab = getSelectedSqlTab();
    TreeItem<K> schemaTreeItem = selectedItem.getParent();
    String schema = schemaTreeItem.getValue().toString();
    try (Connection connection = connectionSupplier.get()) {
        List<String> columns = DbUtil.columns(connection, tableName);
        if (columns == null || columns.isEmpty()) {
            String driver = DbUtil.getDriverNameByConnection(connection);
            String sql = ConfigResourceLoader.getInstance().get(ConfigResourceLoader.SQL_TABLE_COLUMNS_WRAPPER, driver);
            Map<String, Object> map = new HashMap<>(2);
            map.put("databaseName", schema);
            map.put("tableName", tableName);
            sql = ValueUtil.getVelocityToText(sql, map, true);
            columns = DbUtil.select(connection, sql, 10, (RowMapper<String>) (rs, rowNum) -> rs.getString(1));
        }
        redueceAction(columns, ",\n", v -> selectedTab.appendTextSql(String.format("select\n%s \nfrom %s ", v.substring(0, v.length()), tableName)));
    } catch (Exception e1) {
        LOGGER.error(ValueUtil.toString(e1));
    }
}
Also used : HashMap(java.util.HashMap) Connection(java.sql.Connection) SqlTab(com.kyj.fx.voeditor.visual.component.sql.tab.SqlTab) RowMapper(org.springframework.jdbc.core.RowMapper)

Example 9 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project camel by apache.

the class DefaultSqlEndpoint method queryForObject.

@SuppressWarnings("unchecked")
public Object queryForObject(ResultSet rs) throws SQLException {
    Object result = null;
    if (outputClass == null) {
        RowMapper rowMapper = new ColumnMapRowMapper();
        RowMapperResultSetExtractor<Map<String, Object>> mapper = new RowMapperResultSetExtractor<Map<String, Object>>(rowMapper);
        List<Map<String, Object>> data = mapper.extractData(rs);
        if (data.size() > 1) {
            throw new SQLDataException("Query result not unique for outputType=SelectOne. Got " + data.size() + " count instead.");
        } else if (data.size() == 1) {
            // Set content depend on number of column from query result
            Map<String, Object> row = data.get(0);
            if (row.size() == 1) {
                result = row.values().iterator().next();
            } else {
                result = row;
            }
        }
    } else {
        Class<?> outputClzz = getCamelContext().getClassResolver().resolveClass(outputClass);
        RowMapper rowMapper = new BeanPropertyRowMapper(outputClzz);
        RowMapperResultSetExtractor<?> mapper = new RowMapperResultSetExtractor(rowMapper);
        List<?> data = mapper.extractData(rs);
        if (data.size() > 1) {
            throw new SQLDataException("Query result not unique for outputType=SelectOne. Got " + data.size() + " count instead.");
        } else if (data.size() == 1) {
            result = data.get(0);
        }
    }
    // If data.size is zero, let result be null.
    return result;
}
Also used : ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) SQLDataException(java.sql.SQLDataException) RowMapperResultSetExtractor(org.springframework.jdbc.core.RowMapperResultSetExtractor) Map(java.util.Map) BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) RowMapper(org.springframework.jdbc.core.RowMapper) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper)

Example 10 with RowMapper

use of org.springframework.jdbc.core.RowMapper in project camel by apache.

the class DefaultSqlEndpoint method queryForList.

@SuppressWarnings("unchecked")
public List<?> queryForList(ResultSet rs, boolean allowMapToClass) throws SQLException {
    if (allowMapToClass && outputClass != null) {
        Class<?> outputClazz = getCamelContext().getClassResolver().resolveClass(outputClass);
        RowMapper rowMapper = new BeanPropertyRowMapper(outputClazz);
        RowMapperResultSetExtractor<?> mapper = new RowMapperResultSetExtractor(rowMapper);
        List<?> data = mapper.extractData(rs);
        return data;
    } else {
        ColumnMapRowMapper rowMapper = new ColumnMapRowMapper();
        RowMapperResultSetExtractor<Map<String, Object>> mapper = new RowMapperResultSetExtractor<Map<String, Object>>(rowMapper);
        List<Map<String, Object>> data = mapper.extractData(rs);
        return data;
    }
}
Also used : BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper) RowMapperResultSetExtractor(org.springframework.jdbc.core.RowMapperResultSetExtractor) Map(java.util.Map) BeanPropertyRowMapper(org.springframework.jdbc.core.BeanPropertyRowMapper) RowMapper(org.springframework.jdbc.core.RowMapper) ColumnMapRowMapper(org.springframework.jdbc.core.ColumnMapRowMapper)

Aggregations

RowMapper (org.springframework.jdbc.core.RowMapper)13 ResultSet (java.sql.ResultSet)7 Map (java.util.Map)4 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)4 ApplicationMail (cz.metacentrum.perun.registrar.model.ApplicationMail)3 MailText (cz.metacentrum.perun.registrar.model.ApplicationMail.MailText)3 SQLException (java.sql.SQLException)3 BeanPropertyRowMapper (org.springframework.jdbc.core.BeanPropertyRowMapper)3 ColumnMapRowMapper (org.springframework.jdbc.core.ColumnMapRowMapper)3 SqlTab (com.kyj.fx.voeditor.visual.component.sql.tab.SqlTab)2 Connection (java.sql.Connection)2 RowMapperResultSetExtractor (org.springframework.jdbc.core.RowMapperResultSetExtractor)2 AlbumInfo (com.github.hakko.musiccabinet.domain.model.music.AlbumInfo)1 DockNode (com.kyj.fx.voeditor.visual.component.dock.pane.DockNode)1 DockPane (com.kyj.fx.voeditor.visual.component.dock.pane.DockPane)1 DockPos (com.kyj.fx.voeditor.visual.component.dock.pane.DockPos)1 VariableMappingView (com.kyj.fx.voeditor.visual.component.popup.VariableMappingView)1 ISchemaTreeItem (com.kyj.fx.voeditor.visual.component.sql.functions.ISchemaTreeItem)1 SQLPaneMotionable (com.kyj.fx.voeditor.visual.component.sql.functions.SQLPaneMotionable)1 SqlTabPane (com.kyj.fx.voeditor.visual.component.sql.tab.SqlTabPane)1