001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.tasks.social;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.security.permission.ActionKeys;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.service.GroupLocalServiceUtil;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
029    import com.liferay.portlet.social.model.SocialActivity;
030    import com.liferay.portlet.social.model.SocialActivityFeedEntry;
031    import com.liferay.portlet.tasks.model.TasksProposal;
032    import com.liferay.portlet.tasks.service.TasksProposalLocalServiceUtil;
033    import com.liferay.portlet.tasks.service.permission.TasksProposalPermission;
034    
035    /**
036     * @author Raymond Augé
037     */
038    public class TasksActivityInterpreter extends BaseSocialActivityInterpreter {
039    
040            public String[] getClassNames() {
041                    return _CLASS_NAMES;
042            }
043    
044            protected SocialActivityFeedEntry doInterpret(
045                            SocialActivity activity, ThemeDisplay themeDisplay)
046                    throws Exception {
047    
048                    PermissionChecker permissionChecker =
049                            themeDisplay.getPermissionChecker();
050    
051                    if (!TasksProposalPermission.contains(
052                                    permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
053    
054                            return null;
055                    }
056    
057                    String creatorUserName = getUserName(
058                            activity.getUserId(), themeDisplay);
059                    String receiverUserName = getUserName(
060                            activity.getReceiverUserId(), themeDisplay);
061    
062                    int activityType = activity.getType();
063    
064                    JSONObject extraData = null;
065    
066                    if (Validator.isNotNull(activity.getExtraData())) {
067                            extraData = JSONFactoryUtil.createJSONObject(
068                                    activity.getExtraData());
069                    }
070    
071                    // Title
072    
073                    String groupName = StringPool.BLANK;
074    
075                    if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
076                            Group group = GroupLocalServiceUtil.getGroup(activity.getGroupId());
077    
078                            groupName = group.getDescriptiveName();
079                    }
080    
081                    String titlePattern = null;
082                    Object[] titleArguments = null;
083    
084                    if (activityType == TasksActivityKeys.ADD_PROPOSAL) {
085                            titlePattern = "activity-tasks-add-proposal";
086    
087                            if (Validator.isNotNull(groupName)) {
088                                    titlePattern += "-in";
089                            }
090    
091                            titleArguments = new Object[] {
092                                    creatorUserName, HtmlUtil.escape(groupName)
093                            };
094                    }
095                    else if (activityType == TasksActivityKeys.ASSIGN_PROPOSAL) {
096                            titlePattern = "activity-tasks-assign-proposal";
097    
098                            if (Validator.isNotNull(groupName)) {
099                                    titlePattern += "-in";
100                            }
101    
102                            titleArguments = new Object[] {
103                                    creatorUserName, receiverUserName, HtmlUtil.escape(groupName)
104                            };
105                    }
106                    else if (activityType == TasksActivityKeys.REVIEW_PROPOSAL) {
107                            titlePattern = "activity-tasks-review-proposal";
108    
109                            if (Validator.isNotNull(groupName)) {
110                                    titlePattern += "-in";
111                            }
112    
113                            titleArguments = new Object[] {
114                                    creatorUserName, receiverUserName, HtmlUtil.escape(groupName)
115                            };
116                    }
117    
118                    String title = themeDisplay.translate(titlePattern, titleArguments);
119    
120                    // Body
121    
122                    TasksProposal proposal = TasksProposalLocalServiceUtil.getProposal(
123                            activity.getClassPK());
124    
125                    StringBundler sb = new StringBundler();
126    
127                    sb.append("<b>");
128                    sb.append(HtmlUtil.escape(proposal.getName()));
129                    sb.append("</b> (");
130                    sb.append(
131                            themeDisplay.translate(
132                                    "model.resource." + proposal.getClassName()));
133                    sb.append(")<br />");
134                    sb.append(themeDisplay.translate("description"));
135                    sb.append(": ");
136                    sb.append(HtmlUtil.escape(proposal.getDescription()));
137    
138                    if (activityType != TasksActivityKeys.ADD_PROPOSAL) {
139                            int stage = extraData.getInt("stage");
140                            boolean completed = extraData.getBoolean("completed");
141                            boolean rejected = extraData.getBoolean("rejected");
142    
143                            sb.append("<br />");
144                            sb.append(themeDisplay.translate("stage"));
145                            sb.append(": ");
146                            sb.append(stage);
147                            sb.append("<br />");
148                            sb.append(themeDisplay.translate("status"));
149                            sb.append(": ");
150    
151                            if (completed && rejected) {
152                                    sb.append(themeDisplay.translate("rejected"));
153                            }
154                            else if (completed && !rejected) {
155                                    sb.append(themeDisplay.translate("approved"));
156                            }
157                            else {
158                                    sb.append(themeDisplay.translate("awaiting-approval"));
159                            }
160                    }
161    
162                    String body = sb.toString();
163    
164                    return new SocialActivityFeedEntry(title, body);
165            }
166    
167            private static final String[] _CLASS_NAMES = new String[] {
168                    TasksProposal.class.getName()
169            };
170    
171    }