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