Commits

Juan Carlos Picado Herrera committed 815e877

ENCUESTAME-48 Add twitter account, In progress

Comments (0)

Files changed (5)

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

      * @param account
      * @throws EnMeExpcetion
      */
-    public void addOAuthTokenSocialAccount(
+    public void addOrUpdateOAuthTokenSocialAccount(
             final Long socialAccountId,
             final String token,
             final String tokenSecret,
             final String username,
-            final UserAccount account) throws EnMeExpcetion{
-        final SocialAccount socialAccount = new SocialAccount();
+            final UserAccount account,
+            final SocialProvider socialProvider) throws EnMeExpcetion{
+            SocialAccount socialAccount = getAccountDao().getSocialAccount(socialProvider, socialAccountId);
+            if (socialAccount == null) {
+                log.info("adding new social account");
+                socialAccount = new SocialAccount();
+            }
             log.debug("Updating  Token to {"+token);
             log.debug("Updating Secret Token to {"+tokenSecret);
             socialAccount.setToken(token);
             socialAccount.setVerfied(Boolean.TRUE);
+            socialAccount.setAccounType(socialProvider);
             socialAccount.setSecUsers(account.getAccount());
             socialAccount.setSocialAccountName(username);
             socialAccount.setType(TypeAuth.OAUTH);

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

      * @param tokenSecret
      * @param username
      * @param account
+     * @param socialProvider
      * @throws EnMeExpcetion
      */
-    public void addOAuthTokenSocialAccount(
+    public void addOrUpdateOAuthTokenSocialAccount(
             final Long socialAccountId,
             final String token,
             final String tokenSecret,
             final String username,
-            final UserAccount account) throws EnMeExpcetion;
+            final UserAccount account,
+            final SocialProvider socialProvider) throws EnMeExpcetion;
 
     /**
      * Assign Permission,

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

          log.debug("pin "+pin);
          try {
             final HashMap<String, Object> jsonResponse = new HashMap<String, Object>();
-            final HashMap<String, Object> r = this.createOAuthSocialAccountWithPinNumber(pin, getUserAccount());
+            final HashMap<String, Object> r = this.createOAuthSocialAccountWithPinNumber(pin, getUserAccount(), SocialProvider.TWITTER);
             jsonResponse.put("confirm", r.get("confirm"));
-            jsonResponse.put("screenName", r.get("screenName"));
+            jsonResponse.put("message", r.get("message"));
             setItemResponse(jsonResponse);
         } catch (Exception e) {
-            setItemResponse("url", "");
+            final HashMap<String, Object> jsonResponse = new HashMap<String, Object>();
+            jsonResponse.put("confirm", false);
+            jsonResponse.put("message", e.getMessage());
+            setItemResponse(jsonResponse);
             setError(e.getMessage(), response);
         }
          return returnData();
     }
 
     /**
-     *
+     * Create Social Account with Pin Number.
      * @param pin
      * @param account
+     * @param socialProvider
      * @param userId
      * @return
      */
-    public HashMap<String, Object> createOAuthSocialAccountWithPinNumber(final String pin, final UserAccount account){
+    public HashMap<String, Object> createOAuthSocialAccountWithPinNumber(
+            final String pin,
+            final UserAccount account,
+            final SocialProvider socialProvider){
         log.debug("confirmOAuthPin");
         boolean confirmed = false;
         AccessToken accessToken = null;
                          log.debug("Access Token UserId {"+accessToken.getUserId());
                          log.debug("New Token {"+accessToken.getToken());
                          log.debug("New Secret Token {"+accessToken.getTokenSecret());
-                         getSecurityService().addOAuthTokenSocialAccount((long) accessToken.getUserId(),
+                         getSecurityService().addOrUpdateOAuthTokenSocialAccount((long) accessToken.getUserId(),
                                  accessToken.getToken(),
                                  accessToken.getTokenSecret(),
                                  accessToken.getScreenName(),
-                                 account);
+                                 account,
+                                 socialProvider);
                          confirmed = true;
+                         response.put("message", "ok");
                     }
                 }
             } catch (Exception e) {
                 log.error(e);
                 e.printStackTrace();
+                response.put("message", e.getMessage());
             }
         }
+        response.put("confirm", confirmed);
         return response;
     }
 

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

     SocialAccount getTwitterAccount(final Long twitterAccountId);
 
     /**
+     * Get Social Account.
+     * @param socialProvider
+     * @param socialAccountId
+     * @return
+     */
+    SocialAccount getSocialAccount(final SocialProvider socialProvider, final Long socialAccountId);
+
+    /**
      * 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 socialProvider
+     * @param socialAccountId
+     * @return
+     */
+    public SocialAccount getSocialAccount(final SocialProvider socialProvider, final Long socialAccountId){
+        final DetachedCriteria criteria = DetachedCriteria.forClass(SocialAccount.class);
+        log.debug("accounType "+socialProvider);
+        log.debug("socialAccountId "+socialAccountId);
+        criteria.add(Restrictions.eq("accounType", socialProvider));
+        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}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.