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.TrashHandler;
022    import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
023    import com.liferay.portal.kernel.trash.TrashRenderer;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.model.ContainerModel;
026    import com.liferay.portal.model.LayoutConstants;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.security.permission.PermissionChecker;
029    import com.liferay.portal.service.ServiceContext;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portlet.PortletURLFactoryUtil;
033    import com.liferay.portlet.messageboards.model.MBCategory;
034    import com.liferay.portlet.messageboards.model.MBThread;
035    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
036    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
037    import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
038    import com.liferay.portlet.messageboards.util.MBUtil;
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 the message boards category entity.
048     *
049     * @author Eduardo Garcia
050     */
051    public class MBCategoryTrashHandler extends BaseTrashHandler {
052    
053            public void deleteTrashEntry(long classPK)
054                    throws PortalException, SystemException {
055    
056                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
057    
058                    MBCategoryLocalServiceUtil.deleteCategory(category, false);
059            }
060    
061            public String getClassName() {
062                    return MBCategory.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                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
088    
089                    List<MBCategory> categories = MBCategoryLocalServiceUtil.getCategories(
090                            category.getGroupId(), parentContainerModelId,
091                            WorkflowConstants.STATUS_APPROVED, start, end);
092    
093                    List<ContainerModel> containerModels = new ArrayList<ContainerModel> ();
094    
095                    for (MBCategory curCategory : categories) {
096                            containerModels.add(curCategory);
097                    }
098    
099                    return containerModels;
100            }
101    
102            @Override
103            public int getContainerModelsCount(
104                            long classPK, long parentContainerModelId)
105                    throws PortalException, SystemException {
106    
107                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
108    
109                    return MBCategoryLocalServiceUtil.getCategoriesCount(
110                            category.getGroupId(), parentContainerModelId,
111                            WorkflowConstants.STATUS_APPROVED);
112            }
113    
114            @Override
115            public String getDeleteMessage() {
116                    return "found-in-deleted-category-x";
117            }
118    
119            @Override
120            public List<ContainerModel> getParentContainerModels(long containerModelId)
121                    throws PortalException, SystemException {
122    
123                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>();
124    
125                    ContainerModel containerModel = getContainerModel(containerModelId);
126    
127                    while (containerModel.getParentContainerModelId() > 0) {
128                            containerModel = getContainerModel(
129                                    containerModel.getParentContainerModelId());
130    
131                            if (containerModel == null) {
132                                    break;
133                            }
134    
135                            containerModels.add(containerModel);
136                    }
137    
138                    return containerModels;
139            }
140    
141            @Override
142            public String getRestoreLink(PortletRequest portletRequest, long classPK)
143                    throws PortalException, SystemException {
144    
145                    String portletId = PortletKeys.MESSAGE_BOARDS;
146    
147                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
148    
149                    long plid = PortalUtil.getPlidFromPortletId(
150                            category.getGroupId(), PortletKeys.MESSAGE_BOARDS);
151    
152                    if (plid == LayoutConstants.DEFAULT_PLID) {
153                            portletId = PortletKeys.MESSAGE_BOARDS_ADMIN;
154    
155                            plid = PortalUtil.getControlPanelPlid(portletRequest);
156                    }
157    
158                    PortletURL portletURL = PortletURLFactoryUtil.create(
159                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
160    
161                    if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
162                            portletURL.setParameter("struts_action", "/message_boards/view");
163                    }
164                    else {
165                            portletURL.setParameter(
166                                    "struts_action", "/message_boards_admin/view");
167                    }
168    
169                    portletURL.setParameter(
170                            "mbCategoryId", String.valueOf(category.getParentCategoryId()));
171    
172                    return portletURL.toString();
173            }
174    
175            @Override
176            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
177                    throws PortalException, SystemException {
178    
179                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
180    
181                    return MBUtil.getAbsolutePath(
182                            portletRequest, category.getParentCategoryId());
183            }
184    
185            @Override
186            public String getRootContainerModelName() {
187                    return "home";
188            }
189    
190            @Override
191            public String getTrashContainedModelName() {
192                    return "threads";
193            }
194    
195            @Override
196            public int getTrashContainedModelsCount(long classPK)
197                    throws PortalException, SystemException {
198    
199                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
200    
201                    return MBThreadLocalServiceUtil.getThreadsCount(
202                            category.getGroupId(), classPK, WorkflowConstants.STATUS_APPROVED);
203            }
204    
205            @Override
206            public List<TrashRenderer> getTrashContainedModelTrashRenderers(
207                            long classPK, int start, int end)
208                    throws PortalException, SystemException {
209    
210                    List<TrashRenderer> trashRenderers = new ArrayList<TrashRenderer>();
211    
212                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
213    
214                    List<MBThread> threads = MBThreadLocalServiceUtil.getThreads(
215                            category.getGroupId(), classPK, WorkflowConstants.STATUS_APPROVED,
216                            start, end);
217    
218                    for (MBThread thread : threads) {
219                            TrashHandler trashHandler =
220                                    TrashHandlerRegistryUtil.getTrashHandler(
221                                            MBThread.class.getName());
222    
223                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
224                                    thread.getPrimaryKey());
225    
226                            trashRenderers.add(trashRenderer);
227                    }
228    
229                    return trashRenderers;
230            }
231    
232            @Override
233            public ContainerModel getTrashContainer(long classPK)
234                    throws PortalException, SystemException {
235    
236                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
237    
238                    return category.getTrashContainer();
239            }
240    
241            @Override
242            public String getTrashContainerModelName() {
243                    return "categories";
244            }
245    
246            @Override
247            public int getTrashContainerModelsCount(long classPK)
248                    throws PortalException, SystemException {
249    
250                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
251    
252                    return MBCategoryLocalServiceUtil.getCategoriesCount(
253                            category.getGroupId(), classPK, WorkflowConstants.STATUS_APPROVED);
254            }
255    
256            @Override
257            public List<TrashRenderer> getTrashContainerModelTrashRenderers(
258                            long classPK, int start, int end)
259                    throws PortalException, SystemException {
260    
261                    List<TrashRenderer> trashRenderers = new ArrayList<TrashRenderer>();
262    
263                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
264    
265                    List<MBCategory> categories = MBCategoryLocalServiceUtil.getCategories(
266                            category.getGroupId(), classPK, WorkflowConstants.STATUS_APPROVED,
267                            start, end);
268    
269                    for (MBCategory curCategory : categories) {
270                            TrashHandler trashHandler =
271                                    TrashHandlerRegistryUtil.getTrashHandler(
272                                            MBCategory.class.getName());
273    
274                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
275                                    curCategory.getPrimaryKey());
276    
277                            trashRenderers.add(trashRenderer);
278                    }
279    
280                    return trashRenderers;
281            }
282    
283            @Override
284            public TrashRenderer getTrashRenderer(long classPK)
285                    throws PortalException, SystemException {
286    
287                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
288    
289                    return new MBCategoryTrashRenderer(category);
290            }
291    
292            @Override
293            public boolean hasTrashPermission(
294                            PermissionChecker permissionChecker, long groupId, long classPK,
295                            String trashActionId)
296                    throws PortalException, SystemException {
297    
298                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
299                            return MBCategoryPermission.contains(
300                                    permissionChecker, groupId, classPK, ActionKeys.ADD_FOLDER);
301                    }
302    
303                    return super.hasTrashPermission(
304                            permissionChecker, groupId, classPK, trashActionId);
305            }
306    
307            @Override
308            public boolean isContainerModel() {
309                    return true;
310            }
311    
312            public boolean isInTrash(long classPK)
313                    throws PortalException, SystemException {
314    
315                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
316    
317                    return category.isInTrash();
318            }
319    
320            @Override
321            public boolean isInTrashContainer(long classPK)
322                    throws PortalException, SystemException {
323    
324                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
325    
326                    return category.isInTrashContainer();
327            }
328    
329            @Override
330            public boolean isMovable() {
331                    return true;
332            }
333    
334            @Override
335            public boolean isRestorable(long classPK)
336                    throws PortalException, SystemException {
337    
338                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
339    
340                    return !category.isInTrashContainer();
341            }
342    
343            @Override
344            public void moveEntry(
345                            long userId, long classPK, long containerModelId,
346                            ServiceContext serviceContext)
347                    throws PortalException, SystemException {
348    
349                    MBCategoryLocalServiceUtil.moveCategory(
350                            classPK, containerModelId, false);
351            }
352    
353            @Override
354            public void moveTrashEntry(
355                            long userId, long classPK, long containerModelId,
356                            ServiceContext serviceContext)
357                    throws PortalException, SystemException {
358    
359                    MBCategoryLocalServiceUtil.moveCategoryFromTrash(
360                            userId, classPK, containerModelId);
361            }
362    
363            public void restoreTrashEntry(long userId, long classPK)
364                    throws PortalException, SystemException {
365    
366                    MBCategoryLocalServiceUtil.restoreCategoryFromTrash(userId, classPK);
367            }
368    
369            @Override
370            public void updateTitle(long classPK, String name)
371                    throws PortalException, SystemException {
372    
373                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
374    
375                    category.setName(name);
376    
377                    MBCategoryLocalServiceUtil.updateMBCategory(category);
378            }
379    
380            @Override
381            protected boolean hasPermission(
382                            PermissionChecker permissionChecker, long classPK, String actionId)
383                    throws PortalException, SystemException {
384    
385                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
386    
387                    return MBCategoryPermission.contains(
388                            permissionChecker, category, actionId);
389            }
390    
391    }