org.encuestame.persistence.dao.imp.AbstractSocialAccount.java Source code

Java tutorial

Introduction

Here is the source code for org.encuestame.persistence.dao.imp.AbstractSocialAccount.java

Source

/*
 ************************************************************************************
 * Copyright (C) 2001-2011 encuestame: system online surveys Copyright (C) 2011
 * encuestame Development Team.
 * Licensed under the Apache Software License version 2.0
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to  in writing,  software  distributed
 * under the License is distributed  on  an  "AS IS"  BASIS,  WITHOUT  WARRANTIES  OR
 * CONDITIONS OF ANY KIND, either  express  or  implied.  See  the  License  for  the
 * specific language governing permissions and limitations under the License.
 ************************************************************************************
 */
package org.encuestame.persistence.dao.imp;

import java.util.HashMap;
import java.util.List;

import org.encuestame.persistence.domain.security.Account;
import org.encuestame.persistence.domain.security.SocialAccount;
import org.encuestame.persistence.domain.security.UserAccount;
import org.encuestame.persistence.domain.tweetpoll.TweetPollSavedPublishedStatus;
import org.encuestame.util.exception.EnMeNoResultsFoundException;
import org.encuestame.utils.oauth.AccessGrant;
import org.encuestame.utils.oauth.OAuth1Token;
import org.encuestame.utils.social.SocialProvider;
import org.encuestame.utils.social.TypeAuth;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.support.DataAccessUtils;

import junit.framework.Assert;

/**
 * SocialAccount dao support.
 * @author Picado, Juan juanATencuestame.org
 * @since Jun 30, 2011
 */
@SuppressWarnings("deprecation")
public abstract class AbstractSocialAccount extends AbstractHibernateDaoSupport {

    /**
     * Return a {@link SocialAccount} by account Id.
     * @param socialAccountId
     * @return
     */
    public final SocialAccount getSocialAccountById(final Long socialAccountId) {
        return (getHibernateTemplate().get(SocialAccount.class, socialAccountId));
    }

    /**
     * Return a {@link SocialAccount} by {@link SocialProvider} and social profile id (unique).
     * @param socialProvider {@link SocialProvider}
     * @param socialProfileId social profile id.
     * @return {@link SocialAccount}.
     */
    public final SocialAccount getSocialAccount(final SocialProvider socialProvider, final String socialProfileId) {
        final DetachedCriteria criteria = DetachedCriteria.forClass(SocialAccount.class);
        criteria.add(Restrictions.eq("accounType", socialProvider));
        criteria.add(Restrictions.eq("socialProfileId", socialProfileId));
        return (SocialAccount) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
    }

    /**
     * Return a {@link SocialAccount} by {@link SocialProvider} and social profile id (unique) and social user name.
     * @param socialProvider
     * @param socialProfileId
     * @param socialUsername
     * @return
     */
    public final SocialAccount getSocialAccount(final SocialProvider socialProvider, final String socialProfileId,
            final String socialUsername) {
        final DetachedCriteria criteria = DetachedCriteria.forClass(SocialAccount.class);
        criteria.add(Restrictions.eq("accounType", socialProvider));
        criteria.add(Restrictions.eq("socialProfileId", socialProfileId));
        criteria.add(Restrictions.eq("socialAccountName", socialUsername));
        return (SocialAccount) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
    }

    /**
     * Return a {@link SocialAccount} by id and {@link Account}.
     * @param socialAccountId
     * @param account
     * @return
     */
    public final SocialAccount getSocialAccount(final Long socialAccountId, final Account account) {
        final DetachedCriteria criteria = DetachedCriteria.forClass(SocialAccount.class);
        criteria.add(Restrictions.eq("account", account));
        criteria.add(Restrictions.eq("id", socialAccountId));
        return (SocialAccount) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
    }

    /**
     *
     * @param socialAccountId
     * @param account
     * @param userAccount
     * @return
     */
    public final SocialAccount getSocialAccount(final Long socialAccountId, final Account account,
            final UserAccount userAccount) {
        final DetachedCriteria criteria = DetachedCriteria.forClass(SocialAccount.class);
        criteria.add(Restrictions.eq("account", account));
        criteria.add(Restrictions.eq("userOwner", userAccount));
        criteria.add(Restrictions.eq("id", socialAccountId));
        return (SocialAccount) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
    }

    /**
     * Return {@link SocialAccount} by Account Owner and Social Profile Id.
     * @param socialProfileId
     * @param userAccount
     * @return
     */
    public final SocialAccount getSocialAccount(final Long socialProfileId, final UserAccount userAccount) {
        final DetachedCriteria criteria = DetachedCriteria.forClass(SocialAccount.class);
        criteria.add(Restrictions.eq("userOwner", userAccount));
        criteria.add(Restrictions.eq("socialProfileId", socialProfileId));
        return (SocialAccount) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
    }

    /**
     * Return a List of {@link SocialAccount} by {@link Account}.
     * This method is used to share all social accounts connections for all members.
     * @param account {@link Account}
     * @param provider {@link SocialProvider}
     * @return list of {@link SocialAccount}
     */
    @SuppressWarnings("unchecked")
    public final List<SocialAccount> getSocialAccountByAccount(final Account account,
            final SocialProvider provider) {
        final DetachedCriteria criteria = DetachedCriteria.forClass(SocialAccount.class);
        criteria.add(Restrictions.eq("account", account));
        //if provider is null, we fetch everything
        if (provider != null) {
            criteria.add(Restrictions.eq("accounType", provider));
        }
        return (List<SocialAccount>) getHibernateTemplate().findByCriteria(criteria);
    }

    /**
     *
     * @param account
     * @param provider
     * @return
     */
    @SuppressWarnings("unchecked")
    public final List<SocialAccount> getSocialVerifiedAccountByUserAccount(final Account account,
            final SocialProvider provider) {
        final DetachedCriteria criteria = DetachedCriteria.forClass(SocialAccount.class);
        criteria.add(Restrictions.eq("account", account));
        criteria.add(Restrictions.eq("verfied", Boolean.TRUE));
        if (!provider.equals(SocialProvider.ALL)) { // if provider is ALL, we fetch everything
            criteria.add(Restrictions.eq("accounType", provider));
        }
        return (List<SocialAccount>) getHibernateTemplate().findByCriteria(criteria);
    }

    /**
     * Create new social connection
     ider}
     * @param accessGrant {@link AccessGrant}
     * @param socialAccountId social account id.
     * @return
     */
    public SocialAccount updateSocialAccountConnection(final AccessGrant accessGrant, //OAuth2
            final String socialAccountId, final SocialAccount currentSocialAccount) {
        log.debug("Add Connection " + accessGrant);
        log.debug("Add Connection " + socialAccountId);
        //reference to social account.
        //connection.setSocialAccount(socialAccount);
        //store oauth provider permissions.
        if (SocialProvider.getTypeAuth(currentSocialAccount.getAccounType()).equals(TypeAuth.OAUTH1)) {
            //OAuth access token.
            //connection.setAccessToken(token.getValue());
            //OAuth1
            //connection.setSecretToken(token.getSecret());
            //TODO: pending OAUTH1.
            log.debug("pending OAUTH1 - OAuth 1 social connection is not available right now.");
        } else if (SocialProvider.getTypeAuth(currentSocialAccount.getAccounType()).equals(TypeAuth.OAUTH2)) {
            //OAuth2
            currentSocialAccount.setAccessToken(accessGrant.getAccessToken());
            currentSocialAccount.setRefreshToken(accessGrant.getRefreshToken());
            currentSocialAccount.setExpires(accessGrant.getExpires());
        }
        Assert.assertNotNull(socialAccountId);
        currentSocialAccount.setSocialProfileId(socialAccountId);
        this.saveOrUpdate(currentSocialAccount); //TODO: this seems not save or update properly.
        log.debug("Added Connection:{" + currentSocialAccount.toString());
        return currentSocialAccount;
    }

    /**
     * {@link SocialAccount} Is Connected.
     * @param accountId
     * @param provider
     * @return
     */
    public boolean isConnected(String accountId, SocialProvider provider) {
        boolean account = false;
        final SocialAccount connection = this.getAccountConnection(accountId, provider);
        log.debug("Is connected " + account);
        if (connection != null) {
            account = true;
        }
        return account;
    }

    /**
     * Get Account Connection.
     * @param accountId
     * @return
     */
    public SocialAccount getAccountConnection(final String accountId, final SocialProvider provider) {
        final DetachedCriteria criteria = DetachedCriteria.forClass(SocialAccount.class);
        criteria.createAlias("userAccout", "userAccout");
        criteria.add(Restrictions.eq("userAccout.uid", accountId));
        criteria.add(Restrictions.eq("accounType", provider));
        return (SocialAccount) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
    }

    /**
     * Get Access Token.
     * @param accountId
     * @param provider
     * @return
     * @throws EnMeNoResultsFoundException
     */
    public OAuth1Token getAccessToken(String accountId, SocialProvider provider)
            throws EnMeNoResultsFoundException {
        final SocialAccount ac = this.getAccountConnection(accountId, provider);
        if (ac != null) {
            final OAuth1Token oAuthToken = new OAuth1Token(ac.getAccessToken(), ac.getSecretToken());
            return oAuthToken;
        } else {
            throw new EnMeNoResultsFoundException("connection not found");
        }
    }

    /**
     * Get Provider Account Id.
     * @param accountId
     * @param provider
     * @return
     * @throws EnMeNoResultsFoundException
     */
    public Long getProviderAccountId(String accountId, SocialProvider provider) throws EnMeNoResultsFoundException {
        final SocialAccount ac = this.getAccountConnection(accountId, provider);
        if (ac != null) {
            return ac.getId();
        } else {
            throw new EnMeNoResultsFoundException("connection not found");
        }
    }

    /*
     * (non-Javadoc)
     * @see org.encuestame.persistence.dao.IAccountDao#findAccountConnectionBySocialProfileId(org.encuestame.persistence.domain.social.SocialProvider, java.lang.String)
     */
    public SocialAccount findAccountConnectionBySocialProfileId(final SocialProvider provider,
            final String socialProfileId) {
        final DetachedCriteria criteria = DetachedCriteria.forClass(SocialAccount.class);
        criteria.add(Restrictions.eq("socialProfileId", socialProfileId));
        criteria.add(Restrictions.eq("accounType", provider));
        return (SocialAccount) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
    }

    /**
     * Get social accounts stats.
     * @param socialAccount {@link SocialAccount}.
     * @return
     */
    public HashMap<String, Long> getSocialAccountStats(final SocialAccount socialAccount) {
        final HashMap<String, Long> stats = new HashMap<String, Long>();
        log.debug("getSocialAccountStats " + socialAccount.getId());
        final DetachedCriteria criteria = DetachedCriteria.forClass(TweetPollSavedPublishedStatus.class);
        criteria.add(Restrictions.eq("socialAccount", socialAccount));
        criteria.setProjection(Projections.rowCount());
        @SuppressWarnings("unchecked")
        final List tweetPollstats = getHibernateTemplate().findByCriteria(criteria);
        log.debug("getSocialAccountStats " + tweetPollstats.size());
        log.debug("getSocialAccountStats " + tweetPollstats);
        if (tweetPollstats.size() > 0) {
            stats.put("tweetpoll", Long.valueOf(tweetPollstats.get(0).toString()));
        } else {
            stats.put("tweetpoll", 0L);
        }
        //TODO: in the future we can add another stats.
        stats.put("poll", 0L);
        stats.put("survey", 0L);
        log.debug("getSocialAccountStats stats" + stats);
        return stats;
    }
}