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.service.GroupLocalServiceUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
30  import com.liferay.portlet.social.model.SocialActivity;
31  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
32  import com.liferay.portlet.tasks.model.TasksProposal;
33  import com.liferay.portlet.tasks.service.TasksProposalLocalServiceUtil;
34  
35  /**
36   * <a href="TasksActivityInterpreter.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Raymond Augé
39   *
40   */
41  public class TasksActivityInterpreter extends BaseSocialActivityInterpreter {
42  
43      public String[] getClassNames() {
44          return _CLASS_NAMES;
45      }
46  
47      protected SocialActivityFeedEntry doInterpret(
48              SocialActivity activity, ThemeDisplay themeDisplay)
49          throws Exception {
50  
51          String creatorUserName = getUserName(
52              activity.getUserId(), themeDisplay);
53          String receiverUserName = getUserName(
54              activity.getReceiverUserId(), themeDisplay);
55  
56          int activityType = activity.getType();
57  
58          JSONObject extraData = null;
59  
60          if (Validator.isNotNull(activity.getExtraData())) {
61              extraData = JSONFactoryUtil.createJSONObject(
62                  activity.getExtraData());
63          }
64  
65          // Title
66  
67          String groupName = StringPool.BLANK;
68  
69          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
70              Group group = GroupLocalServiceUtil.getGroup(activity.getGroupId());
71  
72              groupName = group.getDescriptiveName();
73          }
74  
75          String titlePattern = null;
76          Object[] titleArguments = null;
77  
78          if (activityType == TasksActivityKeys.ADD_PROPOSAL) {
79              titlePattern = "activity-tasks-add-proposal";
80  
81              if (Validator.isNotNull(groupName)) {
82                  titlePattern += "-in";
83              }
84  
85              titleArguments = new Object[] {creatorUserName, groupName};
86          }
87          else if (activityType == TasksActivityKeys.ASSIGN_PROPOSAL) {
88              titlePattern = "activity-tasks-assign-proposal";
89  
90              if (Validator.isNotNull(groupName)) {
91                  titlePattern += "-in";
92              }
93  
94              titleArguments = new Object[] {
95                  creatorUserName, receiverUserName, groupName
96              };
97          }
98          else if (activityType == TasksActivityKeys.REVIEW_PROPOSAL) {
99              titlePattern = "activity-tasks-review-proposal";
100 
101             if (Validator.isNotNull(groupName)) {
102                 titlePattern += "-in";
103             }
104 
105             titleArguments = new Object[] {
106                 creatorUserName, receiverUserName, groupName
107             };
108         }
109 
110         String title = themeDisplay.translate(titlePattern, titleArguments);
111 
112         // Body
113 
114         TasksProposal proposal = TasksProposalLocalServiceUtil.getProposal(
115             activity.getClassPK());
116 
117         StringBuilder sb = new StringBuilder();
118 
119         sb.append("<b>");
120         sb.append(proposal.getName());
121         sb.append("</b> (");
122         sb.append(
123             themeDisplay.translate(
124                 "model.resource." + proposal.getClassName()));
125         sb.append(")<br />");
126         sb.append(themeDisplay.translate("description"));
127         sb.append(": ");
128         sb.append(proposal.getDescription());
129 
130         if (activityType != TasksActivityKeys.ADD_PROPOSAL) {
131             int stage = extraData.getInt("stage");
132             boolean completed = extraData.getBoolean("completed");
133             boolean rejected = extraData.getBoolean("rejected");
134 
135             sb.append("<br />");
136             sb.append(themeDisplay.translate("stage"));
137             sb.append(": ");
138             sb.append(stage);
139             sb.append("<br />");
140             sb.append(themeDisplay.translate("status"));
141             sb.append(": ");
142 
143             if (completed && rejected) {
144                 sb.append(themeDisplay.translate("rejected"));
145             }
146             else if (completed && !rejected) {
147                 sb.append(themeDisplay.translate("approved"));
148             }
149             else {
150                 sb.append(themeDisplay.translate("awaiting-approval"));
151             }
152         }
153 
154         String body = sb.toString();
155 
156         return new SocialActivityFeedEntry(title, body);
157     }
158 
159     private static final String[] _CLASS_NAMES = new String[] {
160         TasksProposal.class.getName()
161     };
162 
163 }