1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.tasks.social;
21  
22  import com.liferay.portal.kernel.json.JSONFactoryUtil;
23  import com.liferay.portal.kernel.json.JSONObject;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.Group;
27  import com.liferay.portal.security.permission.ActionKeys;
28  import com.liferay.portal.security.permission.PermissionChecker;
29  import com.liferay.portal.service.GroupLocalServiceUtil;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
32  import com.liferay.portlet.social.model.SocialActivity;
33  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
34  import com.liferay.portlet.tasks.model.TasksProposal;
35  import com.liferay.portlet.tasks.service.TasksProposalLocalServiceUtil;
36  import com.liferay.portlet.tasks.service.permission.TasksProposalPermission;
37  
38  /**
39   * <a href="TasksActivityInterpreter.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Raymond Augé
42   *
43   */
44  public class TasksActivityInterpreter extends BaseSocialActivityInterpreter {
45  
46      public String[] getClassNames() {
47          return _CLASS_NAMES;
48      }
49  
50      protected SocialActivityFeedEntry doInterpret(
51              SocialActivity activity, ThemeDisplay themeDisplay)
52          throws Exception {
53  
54          PermissionChecker permissionChecker =
55              themeDisplay.getPermissionChecker();
56  
57          if (!TasksProposalPermission.contains(
58                  permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
59  
60              return null;
61          }
62  
63          String creatorUserName = getUserName(
64              activity.getUserId(), themeDisplay);
65          String receiverUserName = getUserName(
66              activity.getReceiverUserId(), themeDisplay);
67  
68          int activityType = activity.getType();
69  
70          JSONObject extraData = null;
71  
72          if (Validator.isNotNull(activity.getExtraData())) {
73              extraData = JSONFactoryUtil.createJSONObject(
74                  activity.getExtraData());
75          }
76  
77          // Title
78  
79          String groupName = StringPool.BLANK;
80  
81          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
82              Group group = GroupLocalServiceUtil.getGroup(activity.getGroupId());
83  
84              groupName = group.getDescriptiveName();
85          }
86  
87          String titlePattern = null;
88          Object[] titleArguments = null;
89  
90          if (activityType == TasksActivityKeys.ADD_PROPOSAL) {
91              titlePattern = "activity-tasks-add-proposal";
92  
93              if (Validator.isNotNull(groupName)) {
94                  titlePattern += "-in";
95              }
96  
97              titleArguments = new Object[] {creatorUserName, groupName};
98          }
99          else if (activityType == TasksActivityKeys.ASSIGN_PROPOSAL) {
100             titlePattern = "activity-tasks-assign-proposal";
101 
102             if (Validator.isNotNull(groupName)) {
103                 titlePattern += "-in";
104             }
105 
106             titleArguments = new Object[] {
107                 creatorUserName, receiverUserName, groupName
108             };
109         }
110         else if (activityType == TasksActivityKeys.REVIEW_PROPOSAL) {
111             titlePattern = "activity-tasks-review-proposal";
112 
113             if (Validator.isNotNull(groupName)) {
114                 titlePattern += "-in";
115             }
116 
117             titleArguments = new Object[] {
118                 creatorUserName, receiverUserName, groupName
119             };
120         }
121 
122         String title = themeDisplay.translate(titlePattern, titleArguments);
123 
124         // Body
125 
126         TasksProposal proposal = TasksProposalLocalServiceUtil.getProposal(
127             activity.getClassPK());
128 
129         StringBuilder sb = new StringBuilder();
130 
131         sb.append("<b>");
132         sb.append(proposal.getName());
133         sb.append("</b> (");
134         sb.append(
135             themeDisplay.translate(
136                 "model.resource." + proposal.getClassName()));
137         sb.append(")<br />");
138         sb.append(themeDisplay.translate("description"));
139         sb.append(": ");
140         sb.append(proposal.getDescription());
141 
142         if (activityType != TasksActivityKeys.ADD_PROPOSAL) {
143             int stage = extraData.getInt("stage");
144             boolean completed = extraData.getBoolean("completed");
145             boolean rejected = extraData.getBoolean("rejected");
146 
147             sb.append("<br />");
148             sb.append(themeDisplay.translate("stage"));
149             sb.append(": ");
150             sb.append(stage);
151             sb.append("<br />");
152             sb.append(themeDisplay.translate("status"));
153             sb.append(": ");
154 
155             if (completed && rejected) {
156                 sb.append(themeDisplay.translate("rejected"));
157             }
158             else if (completed && !rejected) {
159                 sb.append(themeDisplay.translate("approved"));
160             }
161             else {
162                 sb.append(themeDisplay.translate("awaiting-approval"));
163             }
164         }
165 
166         String body = sb.toString();
167 
168         return new SocialActivityFeedEntry(title, body);
169     }
170 
171     private static final String[] _CLASS_NAMES = new String[] {
172         TasksProposal.class.getName()
173     };
174 
175 }