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

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

Introduction

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

Prototype

@Override
    public int update(String sql, Map<String, ?> paramMap) throws DataAccessException 

Source Link

Usage

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

int getCurrentProjectId() {
    if (projectId == 0) {
        NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
        String sql = "SELECT project_id FROM project WHERE project_code=:project_code";
        MapSqlParameterSource paramSource = new MapSqlParameterSource();
        paramSource.addValue("project_code", this.projectCode);

        LOG.trace(sql);// w w  w  . j  a  v a2  s. co  m
        LogParameters(paramSource);
        try {
            projectId = jt.queryForInt(sql, paramSource);
        } catch (EmptyResultDataAccessException ex) {
            // do nothing
        }

        if (projectId == 0) {
            sql = "SELECT nextval('seq_project_id')";
            LOG.trace(sql);
            projectId = jt.queryForInt(sql, paramSource);
            paramSource.addValue("project_id", projectId);

            sql = "INSERT INTO project (project_id, project_code)" + " VALUES (:project_id, :project_code)";

            LOG.trace(sql);
            LogParameters(paramSource);
            jt.update(sql, paramSource);
        }
    }
    return projectId;
}

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

@Override
public void saveParagraph(Paragraph paragraph) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    ParagraphInternal iParagraph = (ParagraphInternal) paragraph;

    paramSource.addValue("paragraph_image_id", paragraph.getImageId());
    paramSource.addValue("paragraph_index", paragraph.getIndex());
    String sql = null;/*w w  w  .  j av a  2 s.  com*/

    if (paragraph.isNew()) {
        sql = "SELECT nextval('ocr_paragraph_id_seq')";
        LOG.debug(sql);
        int paragraphId = jt.queryForInt(sql, paramSource);
        paramSource.addValue("paragraph_id", paragraphId);

        sql = "INSERT INTO ocr_paragraph (paragraph_id, paragraph_image_id, paragraph_index) "
                + "VALUES (:paragraph_id, :paragraph_image_id, :paragraph_index)";

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

        iParagraph.setId(paragraphId);
    } else {
        paramSource.addValue("paragraph_id", paragraph.getId());

        sql = "UPDATE ocr_paragraph" + " SET paragraph_image_id = :paragraph_image_id"
                + ", paragraph_index = :paragraph_index" + " WHERE paragraph_id = :paragraph_id";

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

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

public void saveTerm(PostGresTerm term) {
    if (term.isDirty()) {
        int textId = this.getTextId(term.getText());
        term.setTextId(textId);//from   w  w  w.  ja v a 2s.co m

        NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
        MapSqlParameterSource paramSource = new MapSqlParameterSource();
        paramSource.addValue("term_text_id", term.getTextId());
        paramSource.addValue("term_frequency", term.getFrequency());
        paramSource.addValue("term_project_id", this.getCurrentProjectId());
        paramSource.addValue("term_marked", term.isMarked());
        paramSource.addValue("term_expansion_count", term.getExpansionCount());
        paramSource.addValue("term_head_count", term.getHeadCount());

        if (term.isNew()) {
            String sql = "SELECT nextval('seq_term_id')";
            LOG.trace(sql);
            int termId = jt.queryForInt(sql, paramSource);
            paramSource.addValue("term_id", termId);

            sql = "INSERT INTO term (term_id, term_text_id, term_project_id, term_frequency, term_marked, term_expansion_count, term_head_count)"
                    + " VALUES (:term_id, :term_text_id, :term_project_id, :term_frequency, :term_marked, :term_expansion_count, :term_head_count)";

            LOG.trace(sql);
            LogParameters(paramSource);
            jt.update(sql, paramSource);
            term.setId(termId);
        } else {
            String sql = "UPDATE term" + " SET term_text_id = :term_text_id"
                    + ", term_frequency = :term_frequency" + ", term_project_id = :term_project_id"
                    + ", term_marked = :term_marked" + ", term_expansion_count = :term_expansion_count"
                    + ", term_head_count = :term_head_count" + " WHERE term_id = :term_id";

            paramSource.addValue("term_id", term.getId());
            LOG.trace(sql);
            LogParameters(paramSource);
            jt.update(sql, paramSource);
        }
        term.setDirty(false);
    }
}

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

public void deleteCategory(int categoryId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    String sql = "DELETE FROM ftb_category WHERE cat_id = :cat_id";
    paramSource.addValue("cat_id", categoryId);
    LOG.info(sql);/*from w w  w. j  a  va2  s  .  co  m*/
    TreebankDaoImpl.LogParameters(paramSource);
    jt.update(sql, paramSource);
}

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

@Override
public void deleteTextItem(int textItemId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    String sql = "DELETE FROM ftb_text WHERE text_id = :text_id";
    paramSource.addValue("text_id", textItemId);
    LOG.info(sql);/*from w w w  .ja v  a  2s .co m*/
    TreebankDaoImpl.LogParameters(paramSource);
    jt.update(sql, paramSource);
}

From source file:com.joliciel.lefff.LefffDaoImpl.java

@Override
public void saveEntry(LefffEntryInternal entry) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("entry_word_id", entry.getWordId());
    paramSource.addValue("entry_lemma_id", entry.getLemmaId());
    paramSource.addValue("entry_predicate_id", entry.getPredicateId() == 0 ? null : entry.getPredicateId());
    paramSource.addValue("entry_morph_id", entry.getMorphologyId() == 0 ? null : entry.getMorphologyId());
    paramSource.addValue("entry_lexical_weight", entry.getLexicalWeight());
    paramSource.addValue("entry_category_id", entry.getCategoryId());
    if (entry.isNew()) {
        String sql = "SELECT nextval('seq_entry_id')";
        LOG.info(sql);// w  w w  .j  av a 2s  . co m
        int entryId = jt.queryForInt(sql, paramSource);
        paramSource.addValue("entry_id", entryId);

        sql = "INSERT INTO lef_entry (entry_id, entry_word_id, entry_lemma_id, entry_predicate_id, entry_morph_id, entry_lexical_weight, entry_category_id)"
                + " VALUES (:entry_id, :entry_word_id, :entry_lemma_id, :entry_predicate_id, :entry_morph_id, :entry_lexical_weight, :entry_category_id)";

        LOG.info(sql);
        LefffDaoImpl.LogParameters(paramSource);
        jt.update(sql, paramSource);

        entry.setId(entryId);
    } else {
        String sql = "UPDATE lef_entry" + " SET entry_word_id = :entry_word_id"
                + ", entry_lemma_id = :entry_lemma_id" + ", entry_predicate_id = :entry_predicate_id"
                + ", entry_morph_id = :entry_morph_id" + ", entry_lexical_weight = :entry_lexical_weight"
                + ", entry_category_id = :entry_category_id" + " WHERE entry_id = :entry_id";

        paramSource.addValue("entry_id", entry.getId());
        LOG.info(sql);
        LefffDaoImpl.LogParameters(paramSource);
        jt.update(sql, paramSource);
    }
}

From source file:com.joliciel.jochre.doc.DocumentDaoJdbc.java

@Override
public void saveJochreDocument(JochreDocument jochreDocument) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    JochreDocumentInternal iJochreDocument = (JochreDocumentInternal) jochreDocument;

    paramSource.addValue("doc_filename", jochreDocument.getFileName());
    paramSource.addValue("doc_name", jochreDocument.getName());
    paramSource.addValue("doc_owner_id", jochreDocument.getOwnerId());
    paramSource.addValue("doc_locale", jochreDocument.getLocale().getLanguage());
    paramSource.addValue("doc_name_local", jochreDocument.getNameLocal());
    paramSource.addValue("doc_publisher", jochreDocument.getPublisher());
    paramSource.addValue("doc_city", jochreDocument.getCity());
    paramSource.addValue("doc_year", jochreDocument.getYear());
    paramSource.addValue("doc_reference", jochreDocument.getReference());

    String sql = null;//from   w ww. ja v  a  2 s  .  c om

    if (jochreDocument.isNew()) {
        sql = "SELECT nextval('ocr_doc_id_seq')";
        LOG.info(sql);
        int jochreDocumentId = jt.queryForInt(sql, paramSource);
        paramSource.addValue("doc_id", jochreDocumentId);

        sql = "INSERT INTO ocr_document (doc_id, doc_filename, doc_name, doc_locale, doc_owner_id"
                + ", doc_name_local, doc_publisher, doc_city, doc_year, doc_reference) "
                + "VALUES (:doc_id, :doc_filename, :doc_name, :doc_locale, :doc_owner_id"
                + ", :doc_name_local, :doc_publisher, :doc_city, :doc_year, :doc_reference)";

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

        iJochreDocument.setId(jochreDocumentId);
    } else {
        paramSource.addValue("doc_id", jochreDocument.getId());

        sql = "UPDATE ocr_document" + " SET doc_filename = :doc_filename" + ", doc_name = :doc_name"
                + ", doc_locale = :doc_locale" + ", doc_owner_id = :doc_owner_id"
                + ", doc_name_local = :doc_name_local" + ", doc_publisher = :doc_publisher"
                + ", doc_city = :doc_city" + ", doc_year = :doc_year" + ", doc_reference = :doc_reference"
                + " WHERE doc_id = :doc_id";

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

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

public void saveContext(PostGresContext context) {
    if (context.isDirty()) {
        NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
        MapSqlParameterSource paramSource = new MapSqlParameterSource();
        paramSource.addValue("context_start_row", context.getLineNumber());
        paramSource.addValue("context_start_column", context.getColumnNumber());
        paramSource.addValue("context_text", context.getTextSegment());
        paramSource.addValue("context_term_id", ((PostGresTerm) context.getTerm()).getId());
        paramSource.addValue("context_file_id", this.getFileId(context.getFileName()));

        // context_id, context_start_row, context_start_column, context_text, context_file_id, context_term_id
        if (context.isNew()) {
            String sql = "SELECT nextval('seq_context_id')";
            LOG.trace(sql);//from  ww w .  j a  v  a  2s  . c o m
            int contextId = jt.queryForInt(sql, paramSource);
            paramSource.addValue("context_id", contextId);

            sql = "INSERT INTO context (context_id, context_start_row, context_start_column, context_text, context_file_id, context_term_id)"
                    + " VALUES (:context_id, :context_start_row, :context_start_column, :context_text, :context_file_id, :context_term_id)";

            LOG.trace(sql);
            LogParameters(paramSource);
            jt.update(sql, paramSource);
            context.setId(contextId);
        } else {
            String sql = "UPDATE context" + " SET context_start_row = :context_start_row"
                    + ", context_start_column = :context_start_column" + ", context_text = :context_text"
                    + ", context_file_id = :context_file_id" + ", context_term_id = :context_term_id"
                    + " WHERE context_id = :context_id";

            paramSource.addValue("context_id", context.getId());
            LOG.trace(sql);
            LogParameters(paramSource);
            jt.update(sql, paramSource);
        }
        context.setDirty(false);
    }
}

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

@Override
public void saveRowOfShapes(RowOfShapes row) {
    try {/*from  w  w w .  j ava2  s  . com*/
        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.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;/* w w w  . j  a  v a2  s.  c  om*/

    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);
    }
}