Example usage for org.springframework.jdbc.core.namedparam NamedParameterJdbcTemplate NamedParameterJdbcTemplate

List of usage examples for org.springframework.jdbc.core.namedparam NamedParameterJdbcTemplate NamedParameterJdbcTemplate

Introduction

In this page you can find the example usage for org.springframework.jdbc.core.namedparam NamedParameterJdbcTemplate NamedParameterJdbcTemplate.

Prototype

public NamedParameterJdbcTemplate(JdbcOperations classicJdbcTemplate) 

Source Link

Document

Create a new NamedParameterJdbcTemplate for the given classic Spring org.springframework.jdbc.core.JdbcTemplate .

Usage

From source file:com.joliciel.talismane.terminology.postgres.PostGresTerminologyBase.java

void saveExpansions(Term term) {
    PostGresTerm iTerm = (PostGresTerm) term;
    for (Term expansion : iTerm.getExpansionSet().getItemsAdded()) {
        NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
        String sql = "INSERT INTO term_expansions (termexp_term_id, termexp_expansion_id)"
                + " VALUES (:termexp_term_id, :termexp_expansion_id)";
        MapSqlParameterSource paramSource = new MapSqlParameterSource();
        paramSource.addValue("termexp_term_id", iTerm.getId());
        paramSource.addValue("termexp_expansion_id", ((PostGresTerm) expansion).getId());

        LOG.trace(sql);/*  w  w w.ja v a2  s . c o m*/
        LogParameters(paramSource);
        jt.update(sql, paramSource);
    }
    iTerm.getExpansionSet().cleanSlate();
}

From source file:com.joliciel.jochre.graphics.GraphicsDaoJdbc.java

@Override
public void saveRowOfShapes(RowOfShapes row) {
    try {//from w  w w.  ja v a 2 s.  c o  m
        NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
        MapSqlParameterSource paramSource = new MapSqlParameterSource();
        RowOfShapesInternal iRow = (RowOfShapesInternal) row;

        paramSource.addValue("row_paragraph_id", row.getParagraphId());
        paramSource.addValue("row_index", row.getIndex());
        paramSource.addValue("row_height", row.getXHeight());
        String sql = null;

        if (row.isNew()) {
            sql = "SELECT nextval('ocr_row_id_seq')";
            LOG.debug(sql);
            int rowId = jt.queryForInt(sql, paramSource);
            paramSource.addValue("row_id", rowId);

            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(row.getImage(), "png", os);
            os.flush();
            paramSource.addValue("row_image", os.toByteArray());
            os.close();

            sql = "INSERT INTO ocr_row (row_id, row_paragraph_id, row_index, row_image, row_height) "
                    + "VALUES (:row_id, :row_paragraph_id, :row_index, :row_image, :row_height)";

            LOG.debug(sql);
            logParameters(paramSource);
            jt.update(sql, paramSource);

            iRow.clearMemory();
            iRow.setId(rowId);
        } else {
            paramSource.addValue("row_id", row.getId());

            sql = "UPDATE ocr_row" + " SET row_paragraph_id = :row_paragraph_id" + ", row_index = :row_index"
                    + ", row_height = :row_height" + " WHERE row_id = :row_id";

            LOG.debug(sql);
            logParameters(paramSource);
            jt.update(sql, paramSource);
        }
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:com.joliciel.frenchTreebank.TreebankDaoImpl.java

public List<PhraseUnitInternal> findPhraseUnits(Phrase phrase) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PHRASE_UNIT + "," + SELECT_PHRASE_SUBUNIT + " FROM ftb_phrase_unit pu"
            + " LEFT JOIN ftb_phrase_subunit psu ON pu.punit_id = psu.psubunit_punit_id"
            + " WHERE punit_phrase_id = :phrase_id" + " ORDER BY punit_position, psubunit_position";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("phrase_id", phrase.getId());

    LOG.info(sql);//from   ww  w. java2 s . co m
    TreebankDaoImpl.LogParameters(paramSource);
    SqlRowSet rowSet = jt.queryForRowSet(sql, paramSource);
    PhraseUnitMapper phraseUnitMapper = new PhraseUnitMapper(this.treebankServiceInternal);
    PhraseSubUnitMapper phraseSubUnitMapper = new PhraseSubUnitMapper(this.treebankServiceInternal);

    List<PhraseUnitInternal> phraseUnits = new ArrayList<PhraseUnitInternal>();
    int currentPunitId = 0;
    PhraseUnitInternal currentPhraseUnit = null;
    while (rowSet.next()) {
        int phraseUnitId = rowSet.getInt("punit_id");
        if (phraseUnitId != currentPunitId) {
            currentPhraseUnit = phraseUnitMapper.mapRow(rowSet);
            currentPhraseUnit.setSubunitsInternal(new ArrayList<PhraseSubunit>());
            phraseUnits.add(currentPhraseUnit);
            currentPunitId = phraseUnitId;
        }

        int phraseSubunitId = rowSet.getInt("psubunit_id");
        if (phraseSubunitId != 0) {
            PhraseSubunit psubunit = phraseSubUnitMapper.mapRow(rowSet);
            currentPhraseUnit.getSubunitsInternal().add(psubunit);
        }
    }
    return phraseUnits;
}

From source file:com.joliciel.talismane.terminology.postgres.PostGresTerminologyBase.java

void saveHeads(Term term) {
    PostGresTerm iTerm = (PostGresTerm) term;
    for (Term head : iTerm.getHeadSet().getItemsAdded()) {
        NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
        String sql = "INSERT INTO term_heads (termhead_term_id, termhead_head_id)"
                + " VALUES (:termhead_term_id, :termhead_head_id)";
        MapSqlParameterSource paramSource = new MapSqlParameterSource();
        paramSource.addValue("termhead_head_id", ((PostGresTerm) head).getId());
        paramSource.addValue("termhead_term_id", iTerm.getId());

        LOG.trace(sql);/*  w  w w. j  a v  a 2 s .co  m*/
        LogParameters(paramSource);
        jt.update(sql, paramSource);
    }
    iTerm.getHeadSet().cleanSlate();
}

From source file:com.joliciel.jochre.graphics.GraphicsDaoJdbc.java

@Override
public void saveGroupOfShapes(GroupOfShapes group) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    GroupOfShapesInternal iGroup = (GroupOfShapesInternal) group;

    paramSource.addValue("group_row_id", group.getRowId());
    paramSource.addValue("group_index", group.getIndex());
    paramSource.addValue("group_hard_hyphen", group.isHardHyphen());
    paramSource.addValue("group_broken_word", group.isBrokenWord());
    paramSource.addValue("group_segment_problem", group.isSegmentationProblem());
    paramSource.addValue("group_skip", group.isSkip());
    String sql = null;//ww  w.j  av  a  2  s  . c  o  m

    if (group.isNew()) {
        sql = "SELECT nextval('ocr_group_id_seq')";
        LOG.debug(sql);
        int groupId = jt.queryForInt(sql, paramSource);
        paramSource.addValue("group_id", groupId);

        sql = "INSERT INTO ocr_group (group_id, group_row_id, group_index, group_hard_hyphen, group_broken_word, group_segment_problem, group_skip) "
                + "VALUES (:group_id, :group_row_id, :group_index, :group_hard_hyphen, :group_broken_word, :group_segment_problem, :group_skip)";

        LOG.debug(sql);
        logParameters(paramSource);
        jt.update(sql, paramSource);

        iGroup.setId(groupId);
    } else {
        paramSource.addValue("group_id", group.getId());

        sql = "UPDATE ocr_group" + " SET group_row_id = :group_row_id" + ", group_index = :group_index"
                + ", group_hard_hyphen = :group_hard_hyphen" + ", group_broken_word = :group_broken_word"
                + ", group_segment_problem = :group_segment_problem" + ", group_skip = :group_skip"
                + " WHERE group_id = :group_id";

        LOG.debug(sql);
        logParameters(paramSource);
        jt.update(sql, paramSource);
    }
}

From source file:com.joliciel.frenchTreebank.TreebankDaoImpl.java

@Override
public void findAllWordsAndLemmas(Phrase phrase, List<? extends PhraseUnit> phraseUnits) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT punit_id, w.word_id as w_word_id, w.word_text as w_word_text, w.word_original_text as w_word_original_text"
            + ", l.word_id as l_word_id, l.word_text as l_word_text, l.word_original_text as l_word_original_text"
            + " FROM ftb_phrase_unit pu"
            + " INNER JOIN ftb_phrase_child pc ON punit_phrase_id = pchild_child_id AND pchild_phrase_id = :pchild_phrase_id"
            + " INNER JOIN ftb_word w ON punit_word_id = w.word_id"
            + " INNER JOIN ftb_word l ON punit_lemma_id = l.word_id" + " ORDER BY punit_position";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("pchild_phrase_id", phrase.getId());

    LOG.info(sql);// w  w w . ja  va2 s . co  m
    TreebankDaoImpl.LogParameters(paramSource);
    SqlRowSet rowSet = jt.queryForRowSet(sql, paramSource);
    WordMapper wordMapper = new WordMapper("w", this.treebankServiceInternal);
    WordMapper lemmaMapper = new WordMapper("l", this.treebankServiceInternal);
    while (rowSet.next()) {
        int phraseUnitId = rowSet.getInt("punit_id");
        Word word = wordMapper.mapRow(rowSet);
        Word lemma = lemmaMapper.mapRow(rowSet);
        for (PhraseUnit phraseUnit : phraseUnits) {
            if (phraseUnit.getId() == phraseUnitId) {
                PhraseUnitInternal iPhraseUnit = (PhraseUnitInternal) phraseUnit;
                iPhraseUnit.setWord(word);
                iPhraseUnit.setLemma(lemma);
                break;
            }
        }
    }
}

From source file:com.joliciel.jochre.graphics.GraphicsDaoJdbc.java

@Override
public Paragraph loadParagraph(int paragraphId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PARAGRAPH + " FROM ocr_paragraph WHERE paragraph_id=:paragraph_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("paragraph_id", paragraphId);

    LOG.debug(sql);// w  w w. j  a v  a  2 s .c om
    logParameters(paramSource);
    Paragraph paragraph = null;
    try {
        paragraph = (Paragraph) jt.queryForObject(sql, paramSource,
                new ParagraphMapper(this.getGraphicsServiceInternal()));
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return paragraph;
}

From source file:com.joliciel.frenchTreebank.TreebankDaoImpl.java

@SuppressWarnings("unchecked")
@Override/*w w  w .ja v  a 2 s  .c  om*/
public List<PhraseSubunit> findPhraseSubunits(PhraseUnit phraseUnit) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PHRASE_SUBUNIT + " FROM ftb_phrase_subunit"
            + " WHERE psubunit_punit_id = :psubunit_punit_id" + " ORDER BY psubunit_position";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("psubunit_punit_id", phraseUnit.getId());

    LOG.info(sql);
    TreebankDaoImpl.LogParameters(paramSource);
    List<PhraseSubunit> phraseSubunits = jt.query(sql, paramSource,
            new PhraseSubUnitMapper(this.treebankServiceInternal));

    return phraseSubunits;
}

From source file:com.joliciel.jochre.graphics.GraphicsDaoJdbc.java

@Override
public List<Paragraph> findParagraphs(JochreImage jochreImage) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PARAGRAPH
            + " FROM ocr_paragraph WHERE paragraph_image_id=:paragraph_image_id" + " ORDER BY paragraph_index";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("paragraph_image_id", jochreImage.getId());

    LOG.debug(sql);//from  w w w.j  a va 2 s .  c  o m
    logParameters(paramSource);
    @SuppressWarnings("unchecked")
    List<Paragraph> paragraphs = jt.query(sql, paramSource,
            new ParagraphMapper(this.getGraphicsServiceInternal()));

    return paragraphs;
}

From source file:com.joliciel.frenchTreebank.TreebankDaoImpl.java

public PhraseUnit loadPhraseUnit(int phraseUnitId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PHRASE_UNIT + " FROM ftb_phrase_unit WHERE punit_id=:punit_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("punit_id", phraseUnitId);

    LOG.info(sql);//from   w  ww. j av a  2  s.  c om
    TreebankDaoImpl.LogParameters(paramSource);
    PhraseUnit phraseUnit = null;
    try {
        phraseUnit = (PhraseUnit) jt.queryForObject(sql, paramSource,
                new PhraseUnitMapper(this.treebankServiceInternal));
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return phraseUnit;
}