1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.tasks.social;
24  
25  import com.liferay.portal.kernel.json.JSONFactoryUtil;
26  import com.liferay.portal.kernel.json.JSONObject;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Group;
30  import com.liferay.portal.service.GroupLocalServiceUtil;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
33  import com.liferay.portlet.social.model.SocialActivity;
34  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
35  import com.liferay.portlet.tasks.model.TasksProposal;
36  import com.liferay.portlet.tasks.service.TasksProposalLocalServiceUtil;
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          String creatorUserName = getUserName(
55              activity.getUserId(), themeDisplay);
56          String receiverUserName = getUserName(
57              activity.getReceiverUserId(), themeDisplay);
58  
59          int activityType = activity.getType();
60  
61          JSONObject extraData = null;
62  
63          if (Validator.isNotNull(activity.getExtraData())) {
64              extraData = JSONFactoryUtil.createJSONObject(
65                  activity.getExtraData());
66          }
67  
68          // Title
69  
70          String groupName = StringPool.BLANK;
71  
72          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
73              Group group = GroupLocalServiceUtil.getGroup(activity.getGroupId());
74  
75              groupName = group.getDescriptiveName();
76          }
77  
78          String titlePattern = null;
79          Object[] titleArguments = null;
80  
81          if (activityType == TasksActivityKeys.ADD_PROPOSAL) {
82              titlePattern = "activity-tasks-add-proposal";
83  
84              if (Validator.isNotNull(groupName)) {
85                  titlePattern += "-in";
86              }
87  
88              titleArguments = new Object[] {creatorUserName, groupName};
89          }
90          else if (activityType == TasksActivityKeys.ASSIGN_PROPOSAL) {
91              titlePattern = "activity-tasks-assign-proposal";
92  
93              if (Validator.isNotNull(groupName)) {
94                  titlePattern += "-in";
95              }
96  
97              titleArguments = new Object[] {
98                  creatorUserName, receiverUserName, groupName
99              };
100         }
101         else if (activityType == TasksActivityKeys.REVIEW_PROPOSAL) {
102             titlePattern = "activity-tasks-review-proposal";
103 
104             if (Validator.isNotNull(groupName)) {
105                 titlePattern += "-in";
106             }
107 
108             titleArguments = new Object[] {
109                 creatorUserName, receiverUserName, groupName
110             };
111         }
112 
113         String title = themeDisplay.translate(titlePattern, titleArguments);
114 
115         // Body
116 
117         TasksProposal proposal = TasksProposalLocalServiceUtil.getProposal(
118             activity.getClassPK());
119 
120         StringBuilder sb = new StringBuilder();
121 
122         sb.append("<b>");
123         sb.append(proposal.getName());
124         sb.append("</b> (");
125         sb.append(
126             themeDisplay.translate(
127                 "model.resource." + proposal.getClassName()));
128         sb.append(")<br />");
129         sb.append(themeDisplay.translate("description"));
130         sb.append(": ");
131         sb.append(proposal.getDescription());
132 
133         if (activityType != TasksActivityKeys.ADD_PROPOSAL) {
134             int stage = extraData.getInt("stage");
135             boolean completed = extraData.getBoolean("completed");
136             boolean rejected = extraData.getBoolean("rejected");
137 
138             sb.append("<br />");
139             sb.append(themeDisplay.translate("stage"));
140             sb.append(": ");
141             sb.append(stage);
142             sb.append("<br />");
143             sb.append(themeDisplay.translate("status"));
144             sb.append(": ");
145 
146             if (completed && rejected) {
147                 sb.append(themeDisplay.translate("rejected"));
148             }
149             else if (completed && !rejected) {
150                 sb.append(themeDisplay.translate("approved"));
151             }
152             else {
153                 sb.append(themeDisplay.translate("awaiting-approval"));
154             }
155         }
156 
157         String body = sb.toString();
158 
159         return new SocialActivityFeedEntry(title, body);
160     }
161 
162     private static final String[] _CLASS_NAMES = new String[] {
163         TasksProposal.class.getName()
164     };
165 
166 }