001    /**
002     * Copyright (c) 2000-2013 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.messageboards.trash;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.trash.BaseTrashHandler;
020    import com.liferay.portal.kernel.trash.TrashActionKeys;
021    import com.liferay.portal.kernel.trash.TrashRenderer;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.model.ContainerModel;
024    import com.liferay.portal.model.LayoutConstants;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.security.permission.PermissionChecker;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.PortletKeys;
030    import com.liferay.portlet.PortletURLFactoryUtil;
031    import com.liferay.portlet.messageboards.model.MBCategory;
032    import com.liferay.portlet.messageboards.model.MBThread;
033    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
034    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
035    import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
036    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
037    import com.liferay.portlet.messageboards.util.MBUtil;
038    import com.liferay.portlet.trash.model.TrashEntry;
039    
040    import java.util.ArrayList;
041    import java.util.List;
042    
043    import javax.portlet.PortletRequest;
044    import javax.portlet.PortletURL;
045    
046    /**
047     * Implements trash handling for message boards thread entity.
048     *
049     * @author Zsolt Berentey
050     */
051    public class MBThreadTrashHandler extends BaseTrashHandler {
052    
053            @Override
054            public void deleteTrashEntry(long classPK)
055                    throws PortalException, SystemException {
056    
057                    MBThreadLocalServiceUtil.deleteThread(classPK);
058            }
059    
060            @Override
061            public String getClassName() {
062                    return MBThread.class.getName();
063            }
064    
065            @Override
066            public ContainerModel getContainerModel(long containerModelId)
067                    throws PortalException, SystemException {
068    
069                    return MBCategoryLocalServiceUtil.getCategory(containerModelId);
070            }
071    
072            @Override
073            public String getContainerModelClassName() {
074                    return MBCategory.class.getName();
075            }
076    
077            @Override
078            public String getContainerModelName() {
079                    return "category";
080            }
081    
082            @Override
083            public List<ContainerModel> getContainerModels(
084                            long classPK, long parentContainerModelId, int start, int end)
085                    throws PortalException, SystemException {
086    
087                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>();
088    
089                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
090    
091                    List<MBCategory> categories = MBCategoryLocalServiceUtil.getCategories(
092                            thread.getGroupId(), parentContainerModelId,
093                            WorkflowConstants.STATUS_APPROVED, start, end);
094    
095                    for (MBCategory category : categories) {
096                            containerModels.add(category);
097                    }
098    
099                    return containerModels;
100            }
101    
102            @Override
103            public int getContainerModelsCount(
104                            long classPK, long parentContainerModelId)
105                    throws PortalException, SystemException {
106    
107                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
108    
109                    return MBCategoryLocalServiceUtil.getCategoriesCount(
110                            thread.getGroupId(), parentContainerModelId,
111                            WorkflowConstants.STATUS_APPROVED);
112            }
113    
114            @Override
115            public String getRestoreContainedModelLink(
116                            PortletRequest portletRequest, long classPK)
117                    throws PortalException, SystemException {
118    
119                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
120    
121                    PortletURL portletURL = getRestoreURL(portletRequest, classPK, false);
122    
123                    portletURL.setParameter(
124                            "mbCategoryId", String.valueOf(thread.getCategoryId()));
125                    portletURL.setParameter(
126                            "messageId", String.valueOf(thread.getRootMessageId()));
127    
128                    return portletURL.toString();
129            }
130    
131            @Override
132            public String getRestoreContainerModelLink(
133                            PortletRequest portletRequest, long classPK)
134                    throws PortalException, SystemException {
135    
136                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
137    
138                    PortletURL portletURL = getRestoreURL(portletRequest, classPK, true);
139    
140                    portletURL.setParameter(
141                            "mbCategoryId", String.valueOf(thread.getCategoryId()));
142    
143                    return portletURL.toString();
144            }
145    
146            @Override
147            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
148                    throws PortalException, SystemException {
149    
150                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
151    
152                    return MBUtil.getAbsolutePath(portletRequest, thread.getCategoryId());
153            }
154    
155            @Override
156            public TrashEntry getTrashEntry(long classPK)
157                    throws PortalException, SystemException {
158    
159                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
160    
161                    return thread.getTrashEntry();
162            }
163    
164            @Override
165            public TrashRenderer getTrashRenderer(long classPK)
166                    throws PortalException, SystemException {
167    
168                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
169    
170                    return new MBThreadTrashRenderer(thread);
171            }
172    
173            @Override
174            public boolean hasTrashPermission(
175                            PermissionChecker permissionChecker, long groupId, long classPK,
176                            String trashActionId)
177                    throws PortalException, SystemException {
178    
179                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
180                            return MBCategoryPermission.contains(
181                                    permissionChecker, groupId, classPK, ActionKeys.ADD_MESSAGE);
182                    }
183    
184                    return super.hasTrashPermission(
185                            permissionChecker, groupId, classPK, trashActionId);
186            }
187    
188            @Override
189            public boolean isInTrash(long classPK)
190                    throws PortalException, SystemException {
191    
192                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
193    
194                    return thread.isInTrash();
195            }
196    
197            @Override
198            public boolean isInTrashContainer(long classPK)
199                    throws PortalException, SystemException {
200    
201                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
202    
203                    return thread.isInTrashContainer();
204            }
205    
206            @Override
207            public boolean isMovable() {
208                    return true;
209            }
210    
211            @Override
212            public boolean isRestorable(long classPK)
213                    throws PortalException, SystemException {
214    
215                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
216    
217                    if ((thread.getCategoryId() > 0) &&
218                            (MBCategoryLocalServiceUtil.fetchMBCategory(
219                                    thread.getCategoryId()) == null)) {
220    
221                            return false;
222                    }
223    
224                    return !thread.isInTrashContainer();
225            }
226    
227            @Override
228            public void moveEntry(
229                            long userId, long classPK, long containerModelId,
230                            ServiceContext serviceContext)
231                    throws PortalException, SystemException {
232    
233                    MBThreadLocalServiceUtil.moveThread(userId, containerModelId, classPK);
234            }
235    
236            @Override
237            public void moveTrashEntry(
238                            long userId, long classPK, long containerModelId,
239                            ServiceContext serviceContext)
240                    throws PortalException, SystemException {
241    
242                    MBThreadLocalServiceUtil.moveThreadFromTrash(
243                            userId, containerModelId, classPK);
244            }
245    
246            @Override
247            public void restoreTrashEntry(long userId, long classPK)
248                    throws PortalException, SystemException {
249    
250                    MBThreadLocalServiceUtil.restoreThreadFromTrash(userId, classPK);
251            }
252    
253            protected PortletURL getRestoreURL(
254                            PortletRequest portletRequest, long classPK,
255                            boolean isContainerModel)
256                    throws PortalException, SystemException {
257    
258                    String portletId = PortletKeys.MESSAGE_BOARDS;
259    
260                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
261    
262                    long plid = PortalUtil.getPlidFromPortletId(
263                            thread.getGroupId(), PortletKeys.MESSAGE_BOARDS);
264    
265                    if (plid == LayoutConstants.DEFAULT_PLID) {
266                            portletId = PortletKeys.MESSAGE_BOARDS_ADMIN;
267    
268                            plid = PortalUtil.getControlPanelPlid(portletRequest);
269                    }
270    
271                    PortletURL portletURL = PortletURLFactoryUtil.create(
272                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
273    
274                    if (isContainerModel) {
275                            if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
276                                    portletURL.setParameter(
277                                            "struts_action", "/message_boards/view");
278                            }
279                            else {
280                                    portletURL.setParameter(
281                                            "struts_action", "/message_boards_admin/view");
282                            }
283                    }
284                    else {
285                            if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
286                                    portletURL.setParameter(
287                                            "struts_action", "/message_boards/view_message");
288                            }
289                            else {
290                                    portletURL.setParameter(
291                                            "struts_action", "/message_boards_admin/view_message");
292                            }
293                    }
294    
295                    return portletURL;
296            }
297    
298            @Override
299            protected boolean hasPermission(
300                            PermissionChecker permissionChecker, long classPK, String actionId)
301                    throws PortalException, SystemException {
302    
303                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
304    
305                    return MBMessagePermission.contains(
306                            permissionChecker, thread.getRootMessageId(), actionId);
307            }
308    
309    }