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