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