Commits

Juan Carlos Picado Herrera committed 0178606

ENCUESTAME-48 Add twitter account, In progress

  • Participants
  • Parent commits 172d67c

Comments (0)

Files changed (9)

encuestame-business/src/main/java/org/encuestame/business/service/AbstractSocialService.java

 import org.encuestame.persistence.domain.social.SocialProvider;
 import org.encuestame.persistence.exception.EnMeExpcetion;
 import org.encuestame.persistence.exception.EnMeNoResultsFoundException;
+import org.encuestame.persistence.exception.IllegalSocialActionException;
 import org.encuestame.utils.security.SocialAccountBean;
 
 import twitter4j.Twitter;
 import twitter4j.http.AccessToken;
 
 /**
- *
+ * Social service layer.
  * @author Picado, Juan juanATencuestame.org
  * @since Mar 7, 2011
  */
 public class AbstractSocialService extends AbstractConfigurationService {
 
+    /**
+     * Log.
+     */
     private Logger log = Logger.getLogger(this.getClass());
 
     /** Twitter Service. **/
     private ITwitterService twitterService;
 
    /**
-    *
+    * Twitter error code.
     */
    private static int TWITTER_AUTH_ERROR = 401;
 
     }
 
     /**
+     * Change state social account.
+     * @param accountId
+     * @param username
+     * @param action
+     * @throws EnMeNoResultsFoundException
+     * @throws IllegalSocialActionException
+     */
+    public void changeStateSocialAccount(
+            final Long accountId,
+            final String username,
+            final String action) throws EnMeNoResultsFoundException, IllegalSocialActionException{
+        final UserAccount userAccount = getUserAccount(username);
+        final SocialAccount social = getAccountDao().getSocialAccount(accountId, userAccount.getAccount());
+        if(social == null){
+            throw new EnMeNoResultsFoundException("social accout not found");
+        }
+        if("default".equals(action)){
+           log.info("update social twitter account");
+           social.setDefaultSelected(!social.getDefaultSelected());
+           getAccountDao().saveOrUpdate(social);
+        } else if("remove".equals(action)){
+            getAccountDao().delete(social);
+        } else if("valid".equals(action)){
+            social.setVerfied(!social.getVerfied());
+            getAccountDao().saveOrUpdate(social);
+        } else {
+            throw new IllegalSocialActionException();
+        }
+    }
+
+    /**
      * Verify OAuth Credentials
      * @param token token stored
      * @param secretToken secret Token
      * @param tokenSecret
      * @param username
      * @param account
+     * @param socialProvider
      * @throws EnMeExpcetion
      */
     public void addOrUpdateOAuthTokenSocialAccount(
         return comfirmedSocialAccounts;
    }
 
-
-    /**
-    *
+   /**
+    * Get social account by id.
     * @param accountId
     * @return
     */
    protected SocialAccount getSocialAccount(final Long accountId){
-        return  getAccountDao().getTwitterAccount(accountId); //TODO: filter by Username Too
+        return  getAccountDao().getTwitterAccount(accountId); //TODO: ENCUESTAME-113
    }
 
    /**
        log.info("update Twitter Account");
    }
 
-
    /**
     * @return the twitterService
     */

encuestame-business/src/main/java/org/encuestame/business/service/imp/ISecurityService.java

      */
     void setSuspendedNotification(final Boolean suspendedNotification);
 
-    /**
-     * Update Twitter Account.
-     * @param accountBean accountBean
-     * @param password password
-     * @param verify verify
-     */
-     void updateTwitterAccount(final SocialAccountBean accountBean, final String password,
-                               final Boolean verify);
-
-     /**
-      * Update OAuth Secret Twitter Credentials.
-      * @param accountBean
-      * @param username
-      */
-     void updateSecretTwitterCredentials(final SocialAccountBean accountBean,
-             final String username) throws EnMeExpcetion;
-
-     /**
-      * Get Twitter Account.
-      * @param twitterAccountId
-      * @return
-      */
-     SocialAccountBean getTwitterAccount(final Long twitterAccountId);
-
 
     /**
      * Load Groups by Client.
     UserAccountBean findUserByEmail(final String email);
 
     /**
-     * Get User Logged Twitter Accounts.
-     * @param username
-     * @param provider
-     * @return
-     * @throws EnMeNoResultsFoundException
-     */
-    List<SocialAccountBean> getUserLoggedSocialAccount(
-            final String username
-            , final SocialProvider provider) throws EnMeNoResultsFoundException;
-
-    /**
-     * Get User Logged Verified Twitter Accounts.
-     * @param username username
-     * @param socialProvider
-     * @return
-     * @throws EnMeNoResultsFoundException
-     */
-    List<SocialAccountBean> getUserLoggedVerifiedTwitterAccount(
-            final String username,
-            final SocialProvider socialProvider) throws EnMeNoResultsFoundException;
-
-    /**
      * Get Email List by Username.
      * @param username
      * @return
     List<UnitLists> getListbyUsername(final String username) throws EnMeNoResultsFoundException;
 
     /**
-     * Update OAuth Token/Secret Social Account.
-     * @param socialAccountId
-     * @param token
-     * @param tokenSecret
-     * @param username
-     * @param account
-     * @param socialProvider
-     * @throws EnMeExpcetion
-     */
-    public void addOrUpdateOAuthTokenSocialAccount(
-            final Long socialAccountId,
-            final String token,
-            final String tokenSecret,
-            final String username,
-            final UserAccount account,
-            final SocialProvider socialProvider) throws EnMeExpcetion;
-
-    /**
      * Assign Permission,
      * @param userId user id
      * @param permission {@link EnMePermission}.

encuestame-core/src/main/java/org/encuestame/core/service/IService.java

 import org.encuestame.core.util.MessageSourceFactoryBean;
 
 /**
- * Class Description.
+ * Service Interface.
  * @author Picado, Juan juan@encuestame.org
  * @since 29/11/2009 21:47:14
  * @version $Id$
  */
-public interface IService{
+public interface IService extends SocialOperations{
 
     /**
      * Getter.

encuestame-core/src/main/java/org/encuestame/core/service/SocialOperations.java

+
+package org.encuestame.core.service;
+
+import java.util.List;
+
+import org.encuestame.persistence.domain.security.UserAccount;
+import org.encuestame.persistence.domain.social.SocialProvider;
+import org.encuestame.persistence.exception.EnMeExpcetion;
+import org.encuestame.persistence.exception.EnMeNoResultsFoundException;
+import org.encuestame.persistence.exception.IllegalSocialActionException;
+import org.encuestame.utils.security.SocialAccountBean;
+
+/**
+ * Social Factory.
+ * @author Picado, Juan juanATencuestame.org
+ * @since Mar 8, 2011
+ */
+public interface SocialOperations {
+
+    /**
+     * Change state social account.
+     * @param accountId
+     * @param username
+     * @param action
+     * @throws EnMeNoResultsFoundException
+     * @throws IllegalSocialActionException
+     */
+    void changeStateSocialAccount(
+            final Long accountId,
+            final String username,
+            final String action) throws EnMeNoResultsFoundException, IllegalSocialActionException;
+
+    /**
+     * Update Twitter Account.
+     * @param accountBean accountBean
+     * @param password password
+     * @param verify verify
+     */
+     void updateTwitterAccount(final SocialAccountBean accountBean, final String password,
+                               final Boolean verify);
+
+     /**
+      * Update OAuth Secret Twitter Credentials.
+      * @param accountBean
+      * @param username
+      */
+     void updateSecretTwitterCredentials(final SocialAccountBean accountBean,
+             final String username) throws EnMeExpcetion;
+
+     /**
+      * Get Twitter Account.
+      * @param twitterAccountId
+      * @return
+      */
+     SocialAccountBean getTwitterAccount(final Long twitterAccountId);
+
+     /**
+      * Get User Logged Twitter Accounts.
+      * @param username
+      * @param provider
+      * @return
+      * @throws EnMeNoResultsFoundException
+      */
+     List<SocialAccountBean> getUserLoggedSocialAccount(
+             final String username
+             , final SocialProvider provider) throws EnMeNoResultsFoundException;
+
+     /**
+      * Get User Logged Verified Twitter Accounts.
+      * @param username username
+      * @param socialProvider
+      * @return
+      * @throws EnMeNoResultsFoundException
+      */
+     List<SocialAccountBean> getUserLoggedVerifiedTwitterAccount(
+             final String username,
+             final SocialProvider socialProvider) throws EnMeNoResultsFoundException;
+
+     /**
+      * Update OAuth Token/Secret Social Account.
+      * @param socialAccountId
+      * @param token
+      * @param tokenSecret
+      * @param username
+      * @param account
+      * @param socialProvider
+      * @throws EnMeExpcetion
+      */
+     public void addOrUpdateOAuthTokenSocialAccount(
+             final Long socialAccountId,
+             final String token,
+             final String tokenSecret,
+             final String username,
+             final UserAccount account,
+             final SocialProvider socialProvider) throws EnMeExpcetion;
+
+}

encuestame-mvc/src/main/java/org/encuestame/mvc/controller/social/json/SocialAccountsJsonController.java

 import org.encuestame.persistence.domain.security.UserAccount;
 import org.encuestame.persistence.domain.social.SocialProvider;
 import org.encuestame.persistence.exception.EnMeExpcetion;
+import org.encuestame.persistence.exception.EnMeNoResultsFoundException;
+import org.encuestame.persistence.exception.IllegalSocialActionException;
 import org.encuestame.utils.security.SocialAccountBean;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.security.access.prepost.PreAuthorize;
     }
 
     /**
-     * Create or Update Twitter Social Account.
+     * Change state of social account.
      * @param type
-     * @param consumerKey
-     * @param consumerSecret
-     * @param usernameAccount
      * @param socialAccountId
      * @param request
      * @param response
     @RequestMapping(value = "/api/social/twitter/account/{type}.json", method = RequestMethod.GET)
     public ModelMap actionTwitterAccount(
             @PathVariable String type,
-            @RequestParam(value = "socialAccountId", required = true) String socialAccountId,
+            @RequestParam(value = "socialAccountId", required = true) Long socialAccountId,
             HttpServletRequest request,
             HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
          try {
-           final UserAccount userAccount = getUserAccount();
-           if("default".equals(type)){
-              log.info("update social twitter account");
-           } else if("remove".equals(type)){
-
-           } else if("valid".equals(type)){
-
-           } else {
-               setError("type not valid", response);
-           }
-        } catch (EnMeExpcetion e) {
-            setItemResponse("url", "");
+           getSecurityService().changeStateSocialAccount(socialAccountId, getUserPrincipalUsername(), type);
+        } catch (IllegalSocialActionException e) {
+            setError(e.getMessage(), response);
+        } catch (EnMeNoResultsFoundException e) {
             setError(e.getMessage(), response);
         }
         return returnData();

encuestame-persistence/src/main/java/org/encuestame/persistence/dao/IAccountDao.java

     SocialAccount getSocialAccount(final SocialProvider socialProvider, final Long socialAccountId);
 
     /**
+     * Get Social Account.
+     * @param socialAccountId
+     * @param account
+     * @return
+     */
+    SocialAccount getSocialAccount(final Long socialAccountId, final Account account);
+
+    /**
      * Get Twitter Verified Accounts.
      * @param secUsers {@link AccountDaoImp}
      * @param provider {@link SocialProvider}

encuestame-persistence/src/main/java/org/encuestame/persistence/dao/imp/AccountDaoImp.java

     }
 
     /**
+     * Get Social Account.
+     * @param socialAccountId
+     * @param account
+     * @return
+     */
+    public SocialAccount getSocialAccount(final Long socialAccountId, final Account account){
+        final DetachedCriteria criteria = DetachedCriteria.forClass(SocialAccount.class);
+        criteria.add(Restrictions.eq("secUsers", account));
+        criteria.add(Restrictions.eq("socialUserId", socialAccountId));
+        return (SocialAccount) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
+    }
+
+    /**
      * Get Primary User By Id.
      * @param userId user id
      * @return {@link Account}

encuestame-persistence/src/main/java/org/encuestame/persistence/domain/security/SocialAccount.java

     private String secretToken;
 
     /**
+     * Default Selected.
+     */
+    private Boolean defaultSelected = false;
+
+    /**
      * Type.
      */
     private TypeAuth type = TypeAuth.OAUTH; //Twitter only accept OAuth.
     public void setSecretToken(String secretToken) {
         this.secretToken = secretToken;
     }
+
+    /**
+     * @return the defaultSelected
+     */
+    @Column(name = "default_selected", nullable = true)
+    public Boolean getDefaultSelected() {
+        return defaultSelected;
+    }
+
+    /**
+     * @param defaultSelected the defaultSelected to set
+     */
+    public void setDefaultSelected(Boolean defaultSelected) {
+        this.defaultSelected = defaultSelected;
+    }
 }

encuestame-persistence/src/main/java/org/encuestame/persistence/exception/IllegalSocialActionException.java

+
+package org.encuestame.persistence.exception;
+
+/**
+ * Illegal social action exception.
+ * @author Picado, Juan juanATencuestame.org
+ * @since Mar 7, 2011
+ */
+public class IllegalSocialActionException  extends EnMeExpcetion{
+
+
+    /**
+     * Serial.
+     */
+    private static final long serialVersionUID = -5547611858006644546L;
+
+    /**
+     * Constructor.
+     */
+    public IllegalSocialActionException() {
+        super("social action not defined");
+    }
+}