1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
35
36
37
38
39
40 public class PostAction extends Action {
41
42
43
44
45
46
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
80 RestActivity activity = new RestActivity();
81 activity.setTitle(postInfo.postMessage);
82
83
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
96 RestActivity activity = new RestActivity();
97 activity.setTitle(postInfo.postMessage);
98 activity.setTemplateParams(postInfo.templateParams);
99
100
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
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
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
149 Log.e(LOG_TAG, "Post message failed", e);
150 }
151 return false;
152 }
153
154 private String retrieveSpaceId(String spaceName) {
155
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 }