Example usage for org.springframework.jdbc.core RowMapper RowMapper

List of usage examples for org.springframework.jdbc.core RowMapper RowMapper

Introduction

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

Prototype

RowMapper

Source Link

Usage

From source file:com.ewcms.component.online.dao.WorkingDAO.java

public List<Article> findArticle(int id) {

    //        String sql = "Select url,title,published From plugin_workingbody_articlemain t1,content_article_main t2,content_article t3 "
    //                + "Where t1.articlemain_id = t2.id And t2.article_id = t3.id And t3.status ='RELEASE' And t1.workingbody_id = ? "
    //                + "Order By t3.published";
    String sql = "Select url,title,published From "
            + "plugin_workingbody_articlemain t1 left join content_article_main t2 on t1.articlemain_id=t2.id "
            + "left join content_article t3 on t2.article_id=t3.id "
            + "Where t3.status ='RELEASE' And t1.workingbody_id = ?  Order By t3.published ";

    List<Article> articles = jdbcTemplate.query(sql, new Object[] { id }, new RowMapper<Article>() {
        @Override/* w w  w .j  a va2s  .com*/
        public Article mapRow(ResultSet rs, int rowNum) throws SQLException {
            Article article = new Article();
            article.setUrl(rs.getString("url"));
            article.setTitle(rs.getString("title"));
            article.setPublished(rs.getDate("published"));
            return article;
        }
    });

    return articles;

}

From source file:eu.trentorise.smartcampus.permissionprovider.oauth.AutoJdbcTokenStore.java

public OAuth2AccessToken readAccessTokenForRefreshToken(String tokenValue) {
    OAuth2AccessToken accessToken = null;

    String key = extractTokenKey(tokenValue);

    try {//w w w  . ja va 2 s.c  om
        accessToken = jdbcTemplate.queryForObject(selectAccessTokenFromRefreshTokenSql,
                new RowMapper<OAuth2AccessToken>() {
                    public OAuth2AccessToken mapRow(ResultSet rs, int rowNum) throws SQLException {
                        return deserializeAccessToken(rs.getBytes(2));
                    }
                }, key);
    } catch (EmptyResultDataAccessException e) {
        if (logger.isInfoEnabled()) {
            logger.debug("Failed to find access token for refresh " + tokenValue);
        }
    } catch (IllegalArgumentException e) {
        logger.error("Could not extract access token for refresh " + tokenValue);
    }

    return accessToken;
}

From source file:repository.EventLogDAO.java

/**
 * Gets a list of events in the Event Log
 * @return/*  www.ja  v  a2  s.c  om*/
 */
public List<EventLog> getEventsList() {
    return template.query(
            "SELECT EventID,ClientID,First_Name,Last_Name,UserID,Username,Interaction_Type,Interaction_Date FROM interactions",
            new RowMapper<EventLog>() {
                public EventLog mapRow(ResultSet rs, int row) throws SQLException {
                    EventLog a = new EventLog();
                    a.setEventid(rs.getInt("EventID"));
                    a.setClientid(rs.getInt("ClientID"));
                    a.setClientFirstName(rs.getString("First_Name"));
                    a.setClientLastName(rs.getString("Last_Name"));
                    a.setUserid(rs.getInt("UserID"));
                    a.setUsername(rs.getString("Username"));
                    a.setInteraction(rs.getString("Interaction_Type"));
                    a.setDate(rs.getString("Interaction_Date"));
                    return a;
                }
            });
}

From source file:gov.nih.nci.cabig.caaers.dao.index.AbstractIndexDao.java

public int findAssociatedRole(String loginId, Integer entityId) {
    final int[] roleValue = new int[] { 0 };
    StringBuffer sb = new StringBuffer("select role from ").append(indexTableName())
            .append(" where login_id = '").append(loginId).append("' and (").append(entityIdColumnName())
            .append(" = ").append(String.valueOf(entityId));
    if (entityId > Integer.MIN_VALUE) {
        sb.append(" or ").append(entityIdColumnName()).append(" = ").append(Integer.MIN_VALUE);
    }/*from   w  ww .j a  v  a  2  s.c  o m*/
    sb.append(")");

    getJdbcTemplate().query(sb.toString(), new RowMapper() {
        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
            roleValue[0] |= rs.getInt(1);
            return null;
        }
    });
    return roleValue[0];
}

From source file:com.ewcms.component.online.dao.AdvisorDAO.java

public List<Advisor> findAdvisorByUsernam(String username) {
    String sql = "Select t1.id As t1_id,t1.title As t1_title,t1.content As t1_content,"
            + "t1.replay As t1_replay,t1.state As t1_state,t1.date As t1_date,"
            + "t2.id as t2_id,t2.matter_name As t2_name,t3.id As t3_id,t3.name As t3_name "
            + "From plugin_online_advisory t1 " + "LEFT JOIN plugin_matter t2 ON t1.matter_id =t2.id "
            + "LEFT JOIN site_organ t3 ON t1.organ_id=t3.id " + "Where t1.username = ? "
            + "Order By t1.date Desc";

    return jdbcTemplate.query(sql, new Object[] { username }, new RowMapper<Advisor>() {

        @Override/*from   ww w .ja  va  2s.  c  om*/
        public Advisor mapRow(ResultSet rs, int rowNum) throws SQLException {
            Advisor advisor = new Advisor();
            advisor.setId(rs.getInt("t1_id"));
            advisor.setTitle(rs.getString("t1_title"));
            advisor.setState(rs.getInt("t1_state"));
            advisor.setDate(rs.getDate("t1_date"));
            advisor.setContent(rs.getString("t1_content"));
            advisor.setReplay(rs.getString("t1_replay"));
            advisor.setMatterId(rs.getInt("t2_id"));
            advisor.setMatterName(rs.getString("t2_name"));
            advisor.setOrganId(rs.getInt("t3_id"));
            advisor.setOrganName(rs.getString("t3_name"));

            return advisor;
        }
    });
}

From source file:com.sfs.whichdoctor.search.TagSearchDAOImpl.java

/**
 * Perform a search for tags.//  www  . j  a  v  a2 s.  co m
 *
 * @param objecttype the objecttype
 * @param order Order options are: alpha (alphabetical), count (tag count)
 * @param user the user
 *
 * @return the map< string, collection< tag bean>>
 *
 * @throws WhichDoctorSearchDaoException the which doctor search dao
 *             exception
 */
@SuppressWarnings("unchecked")
public final Map<String, Collection<TagBean>> search(final String objecttype, final String order,
        final UserBean user) throws WhichDoctorSearchDaoException {

    if (objecttype == null) {
        throw new WhichDoctorSearchDaoException("An object type must be " + "specified for a tag search");
    }

    Map<String, Collection<TagBean>> results = new HashMap<String, Collection<TagBean>>();

    // Holds a running total of the maximum tag count for each tag type
    HashMap<String, Integer> maxTagCount = new HashMap<String, Integer>();

    String searchSQL = this.getSQL().getValue("tag/search");

    /* Default order is alphabetical */
    if (StringUtils.equalsIgnoreCase(order, "count")) {
        searchSQL += " ORDER BY TagCount DESC";
    } else {
        searchSQL += " ORDER BY TagName ASC";
    }

    dataLogger.info("Performing tag search");

    Collection<TagBean> tagResults = new ArrayList<TagBean>();

    try {
        tagResults = this.getJdbcTemplateReader().query(searchSQL,
                new Object[] { objecttype, "Private", "Private", user.getDN() }, new RowMapper() {
                    public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                        TagBean tag = new TagBean();
                        tag.setTagName(rs.getString("TagName"));
                        tag.setTagType(rs.getString("TagType"));
                        tag.setTagCount(rs.getInt("TagCount"));

                        return tag;
                    }
                });
    } catch (IncorrectResultSizeDataAccessException ie) {
        dataLogger.debug("No results found for search: " + ie.getMessage());
    }

    for (TagBean tag : tagResults) {
        Collection<TagBean> tags = new ArrayList<TagBean>();
        if (results.containsKey(tag.getTagType())) {
            // Get existing array from
            tags = results.get(tag.getTagType());
        }

        if (maxTagCount.containsKey(tag.getTagType())) {
            Integer maxCount = maxTagCount.get(tag.getTagType());
            if (tag.getTagCount() > maxCount) {
                maxTagCount.put(tag.getTagType(), tag.getTagCount());
            }
        } else {
            maxTagCount.put(tag.getTagType(), new Integer(tag.getTagCount()));
        }
        tags.add(tag);

        results.put(tag.getTagType(), tags);
    }

    /* Iterate through results setting max tag count */
    for (String key : results.keySet()) {
        Integer maxCount = maxTagCount.get(key);

        Collection<TagBean> tempTags = results.get(key);
        Collection<TagBean> tags = new ArrayList<TagBean>();

        for (TagBean tag : tempTags) {
            tag.setMaxCount(maxCount);
            tags.add(tag);
        }
        results.put(key, tags);
    }

    /* Load all tags */
    String searchAllSQL = getSQL().getValue("tag/searchAll");

    if (order.compareToIgnoreCase("count") == 0) {
        searchAllSQL += " ORDER BY TagCount DESC";
    } else {
        searchAllSQL += " ORDER BY TagName ASC";
    }

    Collection<TagBean> allTagResults = new ArrayList<TagBean>();
    try {
        allTagResults = this.getJdbcTemplateReader().query(searchAllSQL,
                new Object[] { objecttype, "Private", "Private", user.getDN() }, new RowMapper() {
                    public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                        TagBean tag = new TagBean();
                        tag.setTagName(rs.getString("TagName"));
                        tag.setTagType("All");
                        tag.setTagCount(rs.getInt("TagCount"));

                        return tag;
                    }
                });
    } catch (IncorrectResultSizeDataAccessException ie) {
        dataLogger.debug("No results found for search: " + ie.getMessage());
    }

    int maxAllTagCount = 0;
    for (TagBean tag : allTagResults) {
        if (tag.getTagCount() > maxAllTagCount) {
            maxAllTagCount = tag.getTagCount();
        }
    }

    Collection<TagBean> allTags = new ArrayList<TagBean>();

    for (TagBean tag : allTagResults) {
        tag.setMaxCount(maxAllTagCount);
        allTags.add(tag);
    }

    if (allTags.size() > 0) {
        // At least one tag has been found, add it to results
        results.put("All", allTags);
    }
    return results;
}

From source file:com.seajas.search.utilities.spring.security.dao.UserDAO.java

/**
 * Find all users.//from   w w  w.  j a v a  2 s  .  c o  m
 * 
 * @return List<User>
 */
public List<User> findAllUsers() {
    String sql = "SELECT id, username, password, fullname, is_enabled FROM user";

    RowMapper<User> mapper = new RowMapper<User>() {
        @Override
        public User mapRow(final ResultSet resultSet, final int rowNum) throws SQLException {
            Integer id = resultSet.getInt("id");
            String username = resultSet.getString("username");
            String password = resultSet.getString("password");
            String fullname = resultSet.getString("fullname");
            Boolean isEnabled = resultSet.getInt("is_enabled") == 1 ? true : false;

            return new User(id, username, password, fullname, isEnabled);
        }
    };

    return jdbc.query(sql, mapper);
}

From source file:com.iih5.smartorm.model.DbExecutor.java

/**
 * Model/* w w  w .  j  a  va 2  s  .co m*/
 * @param sql
 * @param paras
 * @param model
 * @param <T>
 * @return
 * @
 */
<T> List<T> queryList(String sql, Object[] paras, final Class<T> model) {
    final Set<String> columnMeta = new HashSet<String>();
    return jdbc.query(sql, paras, new RowMapper<T>() {
        public T mapRow(ResultSet rs, int rowNum) throws SQLException {
            try {
                if (columnMeta.size() == 0) {
                    for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                        String column = rs.getMetaData().getColumnLabel(i + 1);
                        columnMeta.add(column);
                    }
                }
                Model mModel = (Model) model.newInstance();
                Field[] fields = mModel.getClass().getFields();
                if (0 < fields.length) {
                    for (Field f : fields) {
                        if (columnMeta.contains(f.getName())) {
                            f.set(mModel, rs.getObject(f.getName()));
                        }
                    }
                } else {
                    ResultSetMetaData rad = rs.getMetaData();
                    int columnCount = rad.getColumnCount();
                    Map<String, Object> attrs = mModel.getAttrs();
                    for (int i = 1; i <= columnCount; i++) {
                        Object value = rs.getObject(i);
                        attrs.put(rad.getColumnName(i), value);
                    }
                }
                return (T) mModel;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    });
}

From source file:com.krawler.spring.calendar.calendarmodule.JdbcCalendarUserDao.java

@Override
public List<String> getSelectedCalendarIds(String referenceId) {
    String query = "select distinct cid from calendarusermap where userid=?";
    return queryJDBC(query, new Object[] { referenceId }, new RowMapper<String>() {

        @Override/*from  w ww  . j  av  a  2 s.c om*/
        public String mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getString("cid");
        }
    });
}

From source file:ru.org.linux.topic.TopicListDaoImpl.java

/**
 *
 * @return//from w ww  .java2s.c o m
 */
private static RowMapper<TopicListDto.DeletedTopic> getRowMapperForDeletedTopics() {
    return new RowMapper<TopicListDto.DeletedTopic>() {
        @Override
        public TopicListDto.DeletedTopic mapRow(ResultSet rs, int rowNum) throws SQLException {
            return new TopicListDto.DeletedTopic(rs);
        }
    };
}