View Javadoc
1   /*
2    * Copyright (C) 2003-2015 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  package org.exoplatform.shareextension.service;
20  
21  import org.exoplatform.model.SocialPostInfo;
22  import org.exoplatform.singleton.SocialServiceHelper;
23  import org.exoplatform.social.client.api.SocialClientLibException;
24  import org.exoplatform.social.client.api.model.RestActivity;
25  import org.exoplatform.social.client.api.model.RestIdentity;
26  import org.exoplatform.social.client.api.service.QueryParams;
27  import org.exoplatform.social.client.api.service.QueryParams.QueryParamOption;
28  import org.exoplatform.social.client.core.service.QueryParamsImpl;
29  
30  import android.util.Log;
31  
32  /**
33   * Created by The eXo Platform SAS<br/>
34   * An Action for posting an activity on Platform. Supports DEFAULT_ACTIVITY,
35   * LINK_ACTIVITY and DOC_ACTIVITY types.
36   * 
37   * @author Philippe Aristote paristote@exoplatform.com
38   * @since Jun 17, 2015
39   */
40  public class PostAction extends Action {
41    /**
42     * create and execute post action, wait for return result
43     * 
44     * @param post
45     * @param listener
46     * @return just created activity or null if execution failed.
47     */
48    public static RestActivity execute(SocialPostInfo post, PostActionListener listener) {
49  
50      PostAction action = new PostAction();
51      action.postInfo = post;
52      action.listener = listener;
53      action.execute();
54      return listener.mRestActivity;
55  
56    }
57  
58    @Override
59    protected boolean doExecute() {
60  
61      boolean postResult = false;
62      if (SocialPostInfo.TYPE_DOC.equals(postInfo.activityType))
63        postResult = postDocActivity();
64      else if (SocialPostInfo.TYPE_LINK.equals(postInfo.activityType))
65        postResult = postLinkActivity();
66      else {
67        postResult = postTextActivity();
68      }
69      boolean ret = false;
70      if (postResult) {
71        ret = listener.onSuccess("Message posted successfully");
72      } else {
73        ret = listener.onError("Could not post the message");
74      }
75      return ret;
76    }
77  
78    private boolean postDocActivity() {
79      // Post the DOC activity
80      RestActivity activity = new RestActivity();
81      activity.setTitle(postInfo.postMessage);
82      // These types are actually the same (DOC_ACTIVITY).
83      // If they change later on PLF, we'll have to update in SCL only
84      if (postInfo.isPublic())
85        activity.setType(RestActivity.DOC_ACTIVITY_TYPE);
86      else
87        activity.setType(RestActivity.SPACE_DOC_ACTIVITY_TYPE);
88      postInfo.addTemplateParam("MESSAGE", postInfo.postMessage);
89      activity.setTemplateParams(postInfo.templateParams);
90  
91      return postActivity(activity);
92    }
93  
94    private boolean postLinkActivity() {
95      // Post the LINK activity
96      RestActivity activity = new RestActivity();
97      activity.setTitle(postInfo.postMessage);
98      activity.setTemplateParams(postInfo.templateParams);
99      // These types are actually the same (LINK_ACTIVITY).
100     // If they change later on PLF, we'll have to update in SCL only
101     if (postInfo.isPublic())
102       activity.setType(RestActivity.LINK_ACTIVITY_TYPE);
103     else
104       activity.setType(RestActivity.SPACE_LINK_ACTIVITY_TYPE);
105 
106     return postActivity(activity);
107   }
108 
109   private boolean postTextActivity() {
110     // Post the DEFAULT activity
111     RestActivity activity = new RestActivity();
112     activity.setTitle(postInfo.postMessage);
113     if (postInfo.isPublic())
114       activity.setType(RestActivity.DEFAULT_ACTIVITY_TYPE);
115     else
116       activity.setType(RestActivity.SPACE_DEFAULT_ACTIVITY_TYPE);
117 
118     return postActivity(activity);
119   }
120 
121   private boolean postActivity(RestActivity activity) {
122     // Perform the actual Post using the Social Activity service
123     try {
124       if (postInfo.isPublic()) {
125         RestActivity createdActivity = SocialServiceHelper.getInstance().activityService.create(activity);
126         if (listener instanceof PostActionListener) {
127           ((PostActionListener) listener).mRestActivity = createdActivity;
128         }
129         return (createdActivity != null);
130       } else {
131         String spaceId = retrieveSpaceId(postInfo.destinationSpace.name);
132         if (spaceId != null) {
133           QueryParamOption paramSpaceId = QueryParams.IDENTITY_ID_PARAM;
134           paramSpaceId.setValue(spaceId);
135           QueryParams params = new QueryParamsImpl();
136           params.append(paramSpaceId);
137           activity.setIdentityId(spaceId);
138           RestActivity createdActivity = SocialServiceHelper.getInstance().activityService.create(activity, params);
139           if (listener instanceof PostActionListener) {
140             ((PostActionListener) listener).mRestActivity = createdActivity;
141           }
142           return (createdActivity != null);
143         } else {
144           Log.e(LOG_TAG, "Post message failed: could not get space ID for space " + postInfo.destinationSpace);
145         }
146       }
147     } catch (Exception e) {
148      // XXX cannot replace because SocialClientLib can throw exceptions like ServerException, UnsupportMethod ,..
149       Log.e(LOG_TAG, "Post message failed", e);
150     }
151     return false;
152   }
153 
154   private String retrieveSpaceId(String spaceName) {
155     // Retrieve the space identity from the Social Identity service
156     try {
157       RestIdentity spaceIdentity = SocialServiceHelper.getInstance().identityService.getIdentity("space", spaceName);
158       return spaceIdentity.getId();
159     } catch (SocialClientLibException e) {
160       Log.e(LOG_TAG, "Could not retrieve the space ID of " + spaceName, e);
161     }
162     return null;
163   }
164 
165   public static class PostActionListener implements ActionListener {
166 
167     private RestActivity mRestActivity;
168 
169     @Override
170     public boolean onSuccess(String message) {
171       return true;
172     }
173 
174     @Override
175     public boolean onError(String error) {
176       return false;
177     }
178 
179     public RestActivity getRestActivity() {
180       return mRestActivity;
181     }
182 
183   }
184 
185 }