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:info.raack.appliancelabeler.data.JDBCDatabase.java

@Override
public Map<UserAppliance, ApplianceStateTransition> getLatestApplianceStatesForUserAppliances(
        List<UserAppliance> apps, final ApplianceEnergyConsumptionDetectionAlgorithm algorithm) {

    List<ApplianceStateTransition> latestTransitions = new ArrayList<ApplianceStateTransition>();

    // query will not execute properly if there are no appliance ids in the "in" list
    if (apps.size() > 0) {
        List<Integer> applianceIds = new ArrayList<Integer>();
        for (UserAppliance userAppliance : apps) {
            applianceIds.add(userAppliance.getId());
        }/*  w  w w .  j a v  a 2  s  .c  o  m*/

        MapSqlParameterSource parameters = new MapSqlParameterSource();
        parameters.addValue("user_appliance_ids", applianceIds);
        parameters.addValue("detection_algorithm", algorithm.getId());

        NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(jdbcTemplate);

        ApplianceStateTransitionMapper applianceStateTransitionMapper = new ApplianceStateTransitionMapper(
                algorithm);
        template.query(
                "select ast.id as ast_id, ast.start_on as ast_start_on, ast.time as ast_time, ua.id as ua_id, ua.name as ua_name, ua.algorithm_generated as ua_algorithm_generated from appliance_state_transitions ast, (select user_appliance_id, max(time) as maxdatetime from appliance_state_transitions where user_appliance_id in (:user_appliance_ids) and detection_algorithm = :detection_algorithm group by user_appliance_id) groupedast, user_appliances ua where ua.id = ast.user_appliance_id and ast.user_appliance_id = groupedast.user_appliance_id and ast.time = groupedast.maxdatetime order by ast_time",
                parameters, applianceStateTransitionMapper);
    }

    Map<UserAppliance, ApplianceStateTransition> latestApplianceStateTransitions = new HashMap<UserAppliance, ApplianceStateTransition>();

    for (ApplianceStateTransition transition : latestTransitions) {
        latestApplianceStateTransitions.put(transition.getUserAppliance(), transition);
    }

    return latestApplianceStateTransitions;
}

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

public void saveWord(WordInternal word) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("word_text", word.getText());
    paramSource.addValue("word_original_text", word.getOriginalText());
    if (word.isNew()) {
        String sql = "SELECT nextval('ftb_word_word_id_seq')";
        LOG.info(sql);/*from ww  w . j a v a2 s .c  o m*/
        int wordId = jt.queryForInt(sql, paramSource);
        paramSource.addValue("word_id", wordId);

        sql = "INSERT INTO ftb_word (word_id, word_text, word_original_text) VALUES (:word_id, :word_text, :word_original_text)";

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

        word.setId(wordId);
    } else {
        String sql = "UPDATE ftb_word" + " SET word_text = :word_text"
                + ", word_original_text = :word_original_text" + " WHERE word_id = :word_id";

        paramSource.addValue("word_id", word.getId());
        LOG.info(sql);
        TreebankDaoImpl.LogParameters(paramSource);
        jt.update(sql, paramSource);
    }
}

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

public void saveTreebankFile(TreebankFileInternal treebankFile) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("file_name", treebankFile.getFileName());

    if (treebankFile.isNew()) {
        String sql = "SELECT nextval('ftb_file_file_id_seq')";
        LOG.info(sql);//from   www .j a  v a2  s.  co  m
        int fileId = jt.queryForInt(sql, paramSource);
        paramSource.addValue("file_id", fileId);

        sql = "INSERT INTO ftb_file (file_id, file_name) " + "VALUES (:file_id, :file_name)";

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

        treebankFile.setId(fileId);
    }
}

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

public List<List<Entity>> findStuff(List<String> tablesToReturn, List<String> tables, List<String> conditions,
        List<String> orderBy) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    MapSqlParameterSource paramSource = new MapSqlParameterSource();

    List<String> tableNames = new ArrayList<String>();
    List<String> aliases = new ArrayList<String>();

    for (String tableToReturn : tablesToReturn) {
        StringTokenizer st = new StringTokenizer(tableToReturn, " ", false);
        String tableName = st.nextToken().trim();
        st.nextElement(); // skip the word "as"
        String alias = st.nextToken().trim();
        tableNames.add(tableName);//from w  w  w .  j av a2s  .  c om
        aliases.add(alias);
    }

    String sql = "SELECT DISTINCT ";
    boolean firstOne = true;
    int i = 0;
    for (String tableName : tableNames) {
        String alias = aliases.get(i++);
        List<String> columns = null;
        if (tableName.equals("ftb_phrase"))
            columns = DaoUtils.getSelectArray(SELECT_PHRASE, alias);
        else if (tableName.equals("ftb_phrase_unit"))
            columns = DaoUtils.getSelectArray(SELECT_PHRASE_UNIT, alias);
        else if (tableName.equals("ftb_word"))
            columns = DaoUtils.getSelectArray(SELECT_WORD, alias);
        else if (tableName.equals("ftb_sentence"))
            columns = DaoUtils.getSelectArray(SELECT_SENTENCE, alias);
        else
            throw new TreebankException("Unsupported  table for findStuff: " + tableName);

        for (String column : columns) {
            if (firstOne) {
                sql += column;
                firstOne = false;
            } else
                sql += ", " + column;
        }
    }

    firstOne = true;
    for (String table : tables) {
        if (firstOne) {
            sql += " FROM " + table;
            firstOne = false;
        } else
            sql += ", " + table;
    }
    firstOne = true;
    for (String condition : conditions) {
        if (firstOne) {
            sql += " WHERE " + condition;
            firstOne = false;
        } else {
            sql += " AND " + condition;
        }
    }

    if (orderBy.size() > 0) {
        firstOne = true;
        for (String column : orderBy) {
            if (firstOne) {
                sql += " ORDER BY " + column;
                firstOne = false;
            } else {
                sql += ", " + column;
            }
        }
    }
    LOG.info(sql);

    SqlRowSet rowSet = jt.queryForRowSet(sql, paramSource);
    List<List<Entity>> stuff = new ArrayList<List<Entity>>();
    while (rowSet.next()) {
        List<Entity> oneRow = new ArrayList<Entity>();
        i = 0;
        for (String tableName : tableNames) {
            String alias = aliases.get(i++);
            Entity entity = null;
            if (tableName.equals("ftb_phrase")) {
                PhraseMapper phraseMapper = new PhraseMapper(alias, this.treebankServiceInternal);
                Phrase phrase = phraseMapper.mapRow(rowSet);
                entity = phrase;
            } else if (tableName.equals("ftb_phrase_unit")) {
                PhraseUnitMapper phraseUnitMapper = new PhraseUnitMapper(alias, this.treebankServiceInternal);
                PhraseUnit phraseUnit = phraseUnitMapper.mapRow(rowSet);
                entity = phraseUnit;
            } else if (tableName.equals("ftb_word")) {
                WordMapper wordMapper = new WordMapper(alias, this.treebankServiceInternal);
                Word word = wordMapper.mapRow(rowSet);
                entity = word;
            }
            oneRow.add(entity);
        }
        i = 0;
        for (String tableName : tableNames) {
            String alias = aliases.get(i++);
            if (tableName.equals("ftb_sentence")) {
                // need to replace the phrase already created with a sentence
                int sentenceId = rowSet.getInt(alias + "_sentence_id");
                PhraseInternal sentencePhrase = null;
                for (Entity entity : oneRow) {
                    if (entity instanceof PhraseInternal) {
                        if (entity.getId() == sentenceId) {
                            sentencePhrase = (PhraseInternal) entity;
                            break;
                        }
                    }
                }
                if (sentencePhrase == null)
                    throw new TreebankException("Cannot return ftb_sentence without associated ftb_phrase");
                SentenceMapper sentenceMapper = new SentenceMapper(alias, this.treebankServiceInternal,
                        sentencePhrase);
                Sentence sentence = sentenceMapper.mapRow(rowSet);
                oneRow.remove(sentencePhrase);
                oneRow.add(sentence);
            }
        }
        stuff.add(oneRow);
    }
    return stuff;
}

From source file:dao.LineageDAO.java

public static void getImpactDatasets(List<String> searchUrnList, int level,
        List<ImpactDataset> impactDatasets) {
    if (searchUrnList != null && searchUrnList.size() > 0) {

        if (impactDatasets == null) {
            impactDatasets = new ArrayList<ImpactDataset>();
        }//from   ww  w .ja  v a 2s . c  o m

        List<String> pathList = new ArrayList<String>();
        List<String> nextSearchList = new ArrayList<String>();

        for (String urn : searchUrnList) {
            LineagePathInfo pathInfo = Lineage.convertFromURN(urn);
            if (pathInfo != null && StringUtils.isNotBlank(pathInfo.filePath)) {
                if (!pathList.contains(pathInfo.filePath)) {
                    pathList.add(pathInfo.filePath);
                }
            }
        }

        if (pathList != null && pathList.size() > 0) {
            Map<String, List> param = Collections.singletonMap("pathlist", pathList);
            NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(
                    getJdbcTemplate().getDataSource());
            List<ImpactDataset> impactDatasetList = namedParameterJdbcTemplate
                    .query(GET_ONE_LEVEL_IMPACT_DATABASES, param, new ImpactDatasetRowMapper());

            if (impactDatasetList != null) {
                for (ImpactDataset dataset : impactDatasetList) {
                    dataset.level = level;
                    if (impactDatasets.stream().filter(o -> o.urn.equals(dataset.urn)).findFirst()
                            .isPresent()) {
                        continue;
                    }
                    impactDatasets.add(dataset);
                    nextSearchList.add(dataset.urn);
                }
            }
        }

        if (nextSearchList.size() > 0) {
            getImpactDatasets(nextSearchList, level + 1, impactDatasets);
        }
    }
}

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

@Override
public List<PhraseInternal> findChildren(Phrase phrase) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PHRASE + " FROM ftb_phrase" + " WHERE phrase_parent_id = :phrase_id"
            + " ORDER BY phrase_position";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("phrase_id", phrase.getId());

    LOG.info(sql);/*from   w ww  .ja  v a 2 s  . c om*/
    TreebankDaoImpl.LogParameters(paramSource);
    @SuppressWarnings("unchecked")
    List<PhraseInternal> children = jt.query(sql, paramSource, new PhraseMapper(this.treebankServiceInternal));

    return children;
}

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

@Override
public Phrase loadPhrase(int phraseId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_PHRASE + " FROM ftb_phrase WHERE phrase_id=:phrase_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("phrase_id", phraseId);

    LOG.info(sql);/*  w  w  w . j  a  v a2 s  .  c  o  m*/
    TreebankDaoImpl.LogParameters(paramSource);
    Phrase phrase = null;
    try {
        phrase = (Phrase) jt.queryForObject(sql, paramSource, new PhraseMapper(this.treebankServiceInternal));
    } catch (EmptyResultDataAccessException ex) {
        ex.hashCode();
    }
    return phrase;
}

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

public Sentence loadFullSentence(int sentenceId) {
    PhraseMapper phraseMapper = new PhraseMapper(this.treebankServiceInternal);

    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_SENTENCE + ", " + SELECT_PHRASE + " FROM ftb_phrase p, ftb_sentence s"
            + " WHERE sentence_id=:sentence_id AND phrase_id=sentence_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("sentence_id", sentenceId);

    LOG.info(sql);// w  w  w .  ja v a 2 s .  c  o  m
    TreebankDaoImpl.LogParameters(paramSource);
    Sentence sentence = null;
    SqlRowSet rowSet = jt.queryForRowSet(sql, paramSource);

    if (rowSet.next()) {
        PhraseInternal phrase = (PhraseInternal) phraseMapper.mapRow(rowSet);
        SentenceMapper sentenceMapper = new SentenceMapper(this.treebankServiceInternal, phrase);
        sentence = sentenceMapper.mapRow(rowSet);
        this.treebankServiceInternal.getObjectCache().putEntity(Phrase.class, sentence.getId(), sentence);

    }

    sql = "SELECT " + SELECT_PHRASE + " FROM ftb_phrase "
            + " INNER JOIN ftb_phrase_child ON pchild_child_id = phrase_id"
            + " WHERE pchild_phrase_id = :sentence_id" + " ORDER BY phrase_depth, phrase_position";

    paramSource = new MapSqlParameterSource();
    paramSource.addValue("sentence_id", sentenceId);

    LOG.info(sql);
    TreebankDaoImpl.LogParameters(paramSource);

    rowSet = jt.queryForRowSet(sql, paramSource);

    List<PhraseInternal> phrases = new ArrayList<PhraseInternal>();

    while (rowSet.next()) {
        PhraseInternal phrase = (PhraseInternal) phraseMapper.mapRow(rowSet);
        phrase = (PhraseInternal) this.treebankServiceInternal.getObjectCache().getOrPutEntity(Phrase.class,
                phrase.getId(), phrase);
        phrases.add(phrase);
    }

    for (PhraseInternal phrase : phrases) {
        PhraseInternal parent = (PhraseInternal) phrase.getParent();
        if (parent != null) {
            parent.getChildrenDB().add(phrase);
        }
    }

    List<PhraseUnit> phraseUnits = this.findAllPhraseUnits(sentence);

    PhraseInternal sentenceInternal = (PhraseInternal) sentence;

    for (PhraseUnit phraseUnit : phraseUnits) {
        PhraseInternal phrase = (PhraseInternal) phraseUnit.getPhrase();
        PhraseUnitInternal punit = (PhraseUnitInternal) phraseUnit;
        phrase.getPhraseUnitsDB().add(punit);
        sentenceInternal.getAllPhraseUnitsDB().add(phraseUnit);
    }

    this.findAllWordsAndLemmas(sentence, phraseUnits);

    this.treebankServiceInternal.getObjectCache().clearCache(Phrase.class);

    return sentence;
}

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

public Sentence loadSentence(int sentenceId) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_SENTENCE + ", " + SELECT_PHRASE + " FROM ftb_phrase p, ftb_sentence s"
            + " WHERE sentence_id=:sentence_id AND phrase_id=sentence_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("sentence_id", sentenceId);

    LOG.info(sql);/* w  w w.  ja v  a 2s. c om*/
    TreebankDaoImpl.LogParameters(paramSource);
    Sentence sentence = null;
    SqlRowSet rowSet = jt.queryForRowSet(sql, paramSource);

    if (rowSet.next()) {
        PhraseMapper phraseMapper = new PhraseMapper(this.treebankServiceInternal);
        PhraseInternal phrase = (PhraseInternal) phraseMapper.mapRow(rowSet);
        SentenceMapper sentenceMapper = new SentenceMapper(this.treebankServiceInternal, phrase);
        sentence = sentenceMapper.mapRow(rowSet);
    }

    return sentence;
}

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

public List<Sentence> findSentences(TreebankFile treebankFile) {
    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(this.getDataSource());
    String sql = "SELECT " + SELECT_SENTENCE + ", " + SELECT_PHRASE + " FROM ftb_sentence"
            + " INNER JOIN ftb_phrase ON phrase_id=sentence_id" + " WHERE sentence_file_id=:sentence_file_id"
            + " ORDER BY sentence_id";
    MapSqlParameterSource paramSource = new MapSqlParameterSource();
    paramSource.addValue("sentence_file_id", treebankFile.getId());

    LOG.info(sql);//from   w ww  .  ja  v  a2  s.c om
    TreebankDaoImpl.LogParameters(paramSource);

    List<Sentence> sentences = new ArrayList<Sentence>();
    SqlRowSet rowSet = jt.queryForRowSet(sql, paramSource);

    while (rowSet.next()) {
        PhraseMapper phraseMapper = new PhraseMapper(this.treebankServiceInternal);
        PhraseInternal phrase = (PhraseInternal) phraseMapper.mapRow(rowSet);
        SentenceMapper sentenceMapper = new SentenceMapper(this.treebankServiceInternal, phrase);
        Sentence sentence = sentenceMapper.mapRow(rowSet);
        sentences.add(sentence);
    }
    return sentences;
}