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 String getContainerModelClassName() {
085                    return MBCategory.class.getName();
086            }
087    
088            @Override
089            public String getContainerModelName() {
090                    return "category";
091            }
092    
093            @Override
094            public List<ContainerModel> getContainerModels(
095                            long classPK, long parentContainerModelId, int start, int end)
096                    throws PortalException, SystemException {
097    
098                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
099    
100                    List<MBCategory> categories = MBCategoryLocalServiceUtil.getCategories(
101                            category.getGroupId(), parentContainerModelId,
102                            WorkflowConstants.STATUS_APPROVED, start, end);
103    
104                    List<ContainerModel> containerModels = new ArrayList<ContainerModel> ();
105    
106                    for (MBCategory curCategory : categories) {
107                            containerModels.add(curCategory);
108                    }
109    
110                    return containerModels;
111            }
112    
113            @Override
114            public int getContainerModelsCount(
115                            long classPK, long parentContainerModelId)
116                    throws PortalException, SystemException {
117    
118                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
119    
120                    return MBCategoryLocalServiceUtil.getCategoriesCount(
121                            category.getGroupId(), parentContainerModelId,
122                            WorkflowConstants.STATUS_APPROVED);
123            }
124    
125            @Override
126            public String getDeleteMessage() {
127                    return "found-in-deleted-category-x";
128            }
129    
130            @Override
131            public List<ContainerModel> getParentContainerModels(long containerModelId)
132                    throws PortalException, SystemException {
133    
134                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>();
135    
136                    ContainerModel containerModel = getContainerModel(containerModelId);
137    
138                    while (containerModel.getParentContainerModelId() > 0) {
139                            containerModel = getContainerModel(
140                                    containerModel.getParentContainerModelId());
141    
142                            if (containerModel == null) {
143                                    break;
144                            }
145    
146                            containerModels.add(containerModel);
147                    }
148    
149                    return containerModels;
150            }
151    
152            @Override
153            public String getRestoreLink(PortletRequest portletRequest, long classPK)
154                    throws PortalException, SystemException {
155    
156                    String portletId = PortletKeys.MESSAGE_BOARDS;
157    
158                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
159    
160                    long plid = PortalUtil.getPlidFromPortletId(
161                            category.getGroupId(), PortletKeys.MESSAGE_BOARDS);
162    
163                    if (plid == LayoutConstants.DEFAULT_PLID) {
164                            portletId = PortletKeys.MESSAGE_BOARDS_ADMIN;
165    
166                            plid = PortalUtil.getControlPanelPlid(portletRequest);
167                    }
168    
169                    PortletURL portletURL = PortletURLFactoryUtil.create(
170                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
171    
172                    if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
173                            portletURL.setParameter("struts_action", "/message_boards/view");
174                    }
175                    else {
176                            portletURL.setParameter(
177                                    "struts_action", "/message_boards_admin/view");
178                    }
179    
180                    portletURL.setParameter(
181                            "mbCategoryId", String.valueOf(category.getParentCategoryId()));
182    
183                    return portletURL.toString();
184            }
185    
186            @Override
187            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
188                    throws PortalException, SystemException {
189    
190                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
191    
192                    return MBUtil.getAbsolutePath(
193                            portletRequest, category.getParentCategoryId());
194            }
195    
196            @Override
197            public String getRootContainerModelName() {
198                    return "home";
199            }
200    
201            @Override
202            public String getTrashContainedModelName() {
203                    return "threads";
204            }
205    
206            @Override
207            public int getTrashContainedModelsCount(long classPK)
208                    throws PortalException, SystemException {
209    
210                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
211    
212                    return MBThreadLocalServiceUtil.getThreadsCount(
213                            category.getGroupId(), classPK, WorkflowConstants.STATUS_APPROVED);
214            }
215    
216            @Override
217            public List<TrashRenderer> getTrashContainedModelTrashRenderers(
218                            long classPK, int start, int end)
219                    throws PortalException, SystemException {
220    
221                    List<TrashRenderer> trashRenderers = new ArrayList<TrashRenderer>();
222    
223                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
224    
225                    List<MBThread> threads = MBThreadLocalServiceUtil.getThreads(
226                            category.getGroupId(), classPK, WorkflowConstants.STATUS_APPROVED,
227                            start, end);
228    
229                    for (MBThread thread : threads) {
230                            TrashHandler trashHandler =
231                                    TrashHandlerRegistryUtil.getTrashHandler(
232                                            MBThread.class.getName());
233    
234                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
235                                    thread.getPrimaryKey());
236    
237                            trashRenderers.add(trashRenderer);
238                    }
239    
240                    return trashRenderers;
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_FOLDER);
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            public boolean isInTrash(long classPK)
315                    throws PortalException, SystemException {
316    
317                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
318    
319                    if (category.isInTrash() || category.isInTrashContainer()) {
320                            return true;
321                    }
322    
323                    return false;
324            }
325    
326            @Override
327            public boolean isInTrashContainer(long classPK)
328                    throws PortalException, SystemException {
329    
330                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
331    
332                    return category.isInTrashContainer();
333            }
334    
335            @Override
336            public boolean isMovable() {
337                    return true;
338            }
339    
340            @Override
341            public boolean isRestorable(long classPK)
342                    throws PortalException, SystemException {
343    
344                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
345    
346                    return !category.isInTrashContainer();
347            }
348    
349            @Override
350            public void moveEntry(
351                            long classPK, long containerModelId, ServiceContext serviceContext)
352                    throws PortalException, SystemException {
353    
354                    MBCategoryServiceUtil.moveCategory(classPK, containerModelId, false);
355            }
356    
357            @Override
358            public void moveTrashEntry(
359                            long classPK, long containerModelId, ServiceContext serviceContext)
360                    throws PortalException, SystemException {
361    
362                    MBCategoryServiceUtil.moveCategoryFromTrash(classPK, containerModelId);
363            }
364    
365            public void restoreTrashEntries(long[] classPKs)
366                    throws PortalException, SystemException {
367    
368                    for (long classPK : classPKs) {
369                            MBCategoryServiceUtil.restoreCategoryFromTrash(classPK);
370                    }
371            }
372    
373            @Override
374            public void updateTitle(long classPK, String name)
375                    throws PortalException, SystemException {
376    
377                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
378    
379                    category.setName(name);
380    
381                    MBCategoryLocalServiceUtil.updateMBCategory(category);
382            }
383    
384            @Override
385            protected boolean hasPermission(
386                            PermissionChecker permissionChecker, long classPK, String actionId)
387                    throws PortalException, SystemException {
388    
389                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
390    
391                    return MBCategoryPermission.contains(
392                            permissionChecker, category, actionId);
393            }
394    
395    }