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