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