Example usage for org.springframework.jdbc.core JdbcTemplate queryForObject

List of usage examples for org.springframework.jdbc.core JdbcTemplate queryForObject

Introduction

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

Prototype

@Override
    @Nullable
    public <T> T queryForObject(String sql, Object[] args, int[] argTypes, Class<T> requiredType)
            throws DataAccessException 

Source Link

Usage

From source file:no.kantega.publishing.common.ao.ContentAOJdbcImpl.java

@Override
public Content getContent(ContentIdentifier cid, boolean isAdminMode) {
    contentIdHelper.assureContentIdAndAssociationIdSet(cid);
    int requestedVersion = cid.getVersion();
    int contentVersionId = -1;

    int contentId = cid.getContentId();
    JdbcTemplate jdbcTemplate = getJdbcTemplate();
    if (isAdminMode) {
        if (requestedVersion == -1) {
            // When in administration mode users should see last version
            List<Integer> contentVersionIds = jdbcTemplate.queryForList(
                    "select ContentVersionId from contentversion where ContentId = ? order by Version desc",
                    Integer.class, contentId);
            if (contentVersionIds.isEmpty()) {
                return null;
            } else {
                contentVersionId = contentVersionIds.get(0);
            }/* w w  w  .j  a  va2 s . c om*/
        } else {

            try {
                contentVersionId = jdbcTemplate.queryForObject(
                        "select ContentVersionId from contentversion where ContentId = ? and Version = ? order by Version desc",
                        Integer.class, contentId, requestedVersion);
            } catch (EmptyResultDataAccessException e) {
                return null;
            }
        }
    } else if (cid.getStatus() == ContentStatus.HEARING) {
        // Find version for hearing, if no hearing is found, active version is returned
        int activeversion = jdbcTemplate.queryForObject(
                "select ContentVersionId from contentversion where ContentId = ? and contentversion.IsActive = 1 order by Version desc",
                Integer.class, contentId);
        contentVersionId = jdbcTemplate.queryForObject(
                "select ContentVersionId from contentversion where ContentId = ? AND Status = ? AND ContentVersionId > ? order by Version desc",
                Integer.class, contentId, ContentStatus.HEARING.getTypeAsInt(), activeversion);
    } else {
        // Others should see active version
        contentVersionId = -1;
    }

    StringBuilder query = new StringBuilder(
            "select * from content, contentversion where content.ContentId = contentversion.ContentId");
    List<Integer> params = new ArrayList<>(2);
    if (contentVersionId != -1) {
        // Hent angitt versjon
        query.append(" and contentversion.ContentVersionId = ?");
        params.add(contentVersionId);
    } else {
        // Hent aktiv versjon
        query.append(" and contentversion.IsActive = 1");
    }
    query.append(" and content.ContentId = ? order by Version");
    params.add(contentId);

    Content content = null;
    try {
        content = jdbcTemplate.queryForObject(query.toString(), new ContentRowMapper(false), params.toArray());
    } catch (EmptyResultDataAccessException e) {
        return null;
    }

    List<Association> associations = jdbcTemplate.query(
            "SELECT * FROM associations WHERE ContentId = ? AND (IsDeleted IS NULL OR IsDeleted = 0)",
            new AssociationRowMapper(), contentId);

    // Get associations for this page
    boolean foundCurrentAssociation = false;
    for (Association a : associations) {
        if (!foundCurrentAssociation) {
            // Dersom knytningsid ikke er angitt bruker vi default for angitt site
            int associationId = cid.getAssociationId();
            if ((associationId == a.getId()) || (associationId == -1
                    && a.getAssociationtype() == AssociationType.DEFAULT_POSTING_FOR_SITE
                    && a.getSiteId() == cid.getSiteId())) {
                foundCurrentAssociation = true;
                a.setCurrent(true);
            }
        }
        content.addAssociation(a);
    }

    if (!foundCurrentAssociation) {
        // Knytningsid er ikke angitt, og heller ikke site, bruk den frste
        for (Association a : associations) {
            if (a.getAssociationtype() == AssociationType.DEFAULT_POSTING_FOR_SITE) {
                foundCurrentAssociation = true;
                a.setCurrent(true);
                break;
            }
        }

        if (!foundCurrentAssociation && associations.size() > 0) {
            Association a = associations.get(0);
            a.setCurrent(true);
            log.debug("Fant ingen defaultknytning: {}", contentId);
        }
    }

    if (content.getAssociation() == null) {
        // All associations to page are deleted, dont return page
        return null;
    }

    // Get content attributes
    jdbcTemplate.query("select * from contentattributes where ContentVersionId = ?",
            new ContentAttributeRowMapper(content), content.getVersionId());
    content.indexAttributes();
    List<Topic> topics = topicDao.getTopicsByContentId(contentId);
    content.setTopics(topics);

    return content;
}