001    /**
002     * Copyright (c) 2000-present 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.language.LanguageUtil;
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    import java.util.Locale;
044    
045    import javax.portlet.PortletRequest;
046    import javax.portlet.PortletURL;
047    
048    /**
049     * Implements trash handling for the message boards category entity.
050     *
051     * @author Eduardo Garcia
052     */
053    public class MBCategoryTrashHandler extends BaseTrashHandler {
054    
055            @Override
056            public void deleteTrashEntry(long classPK) throws PortalException {
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 {
070    
071                    return MBCategoryLocalServiceUtil.getCategory(containerModelId);
072            }
073    
074            @Override
075            public String getContainerModelClassName(long classPK) {
076                    return MBCategory.class.getName();
077            }
078    
079            @Override
080            public String getContainerModelName(long classPK) {
081                    return "category";
082            }
083    
084            @Override
085            public List<ContainerModel> getContainerModels(
086                            long classPK, long parentContainerModelId, int start, int end)
087                    throws PortalException {
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 {
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 {
124    
125                    List<ContainerModel> containerModels = new ArrayList<>();
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 getRestoreContainedModelLink(
145                            PortletRequest portletRequest, long classPK)
146                    throws PortalException {
147    
148                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
149    
150                    PortletURL portletURL = getRestoreURL(portletRequest, classPK);
151    
152                    portletURL.setParameter(
153                            "mbCategoryId", String.valueOf(category.getCategoryId()));
154    
155                    return portletURL.toString();
156            }
157    
158            @Override
159            public String getRestoreContainerModelLink(
160                            PortletRequest portletRequest, long classPK)
161                    throws PortalException {
162    
163                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
164    
165                    PortletURL portletURL = getRestoreURL(portletRequest, classPK);
166    
167                    portletURL.setParameter(
168                            "mbCategoryId", String.valueOf(category.getParentCategoryId()));
169    
170                    return portletURL.toString();
171            }
172    
173            @Override
174            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
175                    throws PortalException {
176    
177                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
178    
179                    return MBUtil.getAbsolutePath(
180                            portletRequest, category.getParentCategoryId());
181            }
182    
183            @Override
184            public String getRootContainerModelName() {
185                    return "category";
186            }
187    
188            @Override
189            public String getRootContainerModelTitle(
190                    long containerModelId, Locale locale) {
191    
192                    return LanguageUtil.get(locale, "home");
193            }
194    
195            @Override
196            public String getSubcontainerModelName() {
197                    return "category";
198            }
199    
200            @Override
201            public String getTrashContainedModelName() {
202                    return "threads";
203            }
204    
205            @Override
206            public int getTrashContainedModelsCount(long classPK)
207                    throws PortalException {
208    
209                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
210    
211                    return MBThreadLocalServiceUtil.getThreadsCount(
212                            category.getGroupId(), classPK, WorkflowConstants.STATUS_IN_TRASH);
213            }
214    
215            @Override
216            public List<TrashRenderer> getTrashContainedModelTrashRenderers(
217                            long classPK, int start, int end)
218                    throws PortalException {
219    
220                    List<TrashRenderer> trashRenderers = new ArrayList<>();
221    
222                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
223    
224                    List<MBThread> threads = MBThreadLocalServiceUtil.getThreads(
225                            category.getGroupId(), classPK, WorkflowConstants.STATUS_IN_TRASH,
226                            start, end);
227    
228                    for (MBThread thread : threads) {
229                            TrashHandler trashHandler =
230                                    TrashHandlerRegistryUtil.getTrashHandler(
231                                            MBThread.class.getName());
232    
233                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
234                                    thread.getPrimaryKey());
235    
236                            trashRenderers.add(trashRenderer);
237                    }
238    
239                    return trashRenderers;
240            }
241    
242            @Override
243            public String getTrashContainerModelName() {
244                    return "categories";
245            }
246    
247            @Override
248            public int getTrashContainerModelsCount(long classPK)
249                    throws PortalException {
250    
251                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
252    
253                    return MBCategoryLocalServiceUtil.getCategoriesCount(
254                            category.getGroupId(), classPK, WorkflowConstants.STATUS_IN_TRASH);
255            }
256    
257            @Override
258            public List<TrashRenderer> getTrashContainerModelTrashRenderers(
259                            long classPK, int start, int end)
260                    throws PortalException {
261    
262                    List<TrashRenderer> trashRenderers = new ArrayList<>();
263    
264                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
265    
266                    List<MBCategory> categories = MBCategoryLocalServiceUtil.getCategories(
267                            category.getGroupId(), classPK, WorkflowConstants.STATUS_IN_TRASH,
268                            start, end);
269    
270                    for (MBCategory curCategory : categories) {
271                            TrashHandler trashHandler =
272                                    TrashHandlerRegistryUtil.getTrashHandler(
273                                            MBCategory.class.getName());
274    
275                            TrashRenderer trashRenderer = trashHandler.getTrashRenderer(
276                                    curCategory.getPrimaryKey());
277    
278                            trashRenderers.add(trashRenderer);
279                    }
280    
281                    return trashRenderers;
282            }
283    
284            @Override
285            public TrashEntry getTrashEntry(long classPK) throws PortalException {
286                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
287    
288                    return category.getTrashEntry();
289            }
290    
291            @Override
292            public TrashRenderer getTrashRenderer(long classPK) throws PortalException {
293                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
294    
295                    return new MBCategoryTrashRenderer(category);
296            }
297    
298            @Override
299            public boolean hasTrashPermission(
300                            PermissionChecker permissionChecker, long groupId, long classPK,
301                            String trashActionId)
302                    throws PortalException {
303    
304                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
305                            return MBCategoryPermission.contains(
306                                    permissionChecker, groupId, classPK, ActionKeys.ADD_CATEGORY);
307                    }
308    
309                    return super.hasTrashPermission(
310                            permissionChecker, groupId, classPK, trashActionId);
311            }
312    
313            @Override
314            public boolean isContainerModel() {
315                    return true;
316            }
317    
318            @Override
319            public boolean isInTrash(long classPK) throws PortalException {
320                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
321    
322                    return category.isInTrash();
323            }
324    
325            @Override
326            public boolean isInTrashContainer(long classPK) throws PortalException {
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) throws PortalException {
339                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
340    
341                    if ((category.getParentCategoryId() > 0) &&
342                            (MBCategoryLocalServiceUtil.fetchMBCategory(
343                                    category.getParentCategoryId()) == null)) {
344    
345                            return false;
346                    }
347    
348                    return !category.isInTrashContainer();
349            }
350    
351            @Override
352            public void moveEntry(
353                            long userId, long classPK, long containerModelId,
354                            ServiceContext serviceContext)
355                    throws PortalException {
356    
357                    MBCategoryLocalServiceUtil.moveCategory(
358                            classPK, containerModelId, false);
359            }
360    
361            @Override
362            public void moveTrashEntry(
363                            long userId, long classPK, long containerModelId,
364                            ServiceContext serviceContext)
365                    throws PortalException {
366    
367                    MBCategoryLocalServiceUtil.moveCategoryFromTrash(
368                            userId, classPK, containerModelId);
369            }
370    
371            @Override
372            public void restoreTrashEntry(long userId, long classPK)
373                    throws PortalException {
374    
375                    MBCategoryLocalServiceUtil.restoreCategoryFromTrash(userId, classPK);
376            }
377    
378            @Override
379            public void updateTitle(long classPK, String name) throws PortalException {
380                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
381    
382                    category.setName(name);
383    
384                    MBCategoryLocalServiceUtil.updateMBCategory(category);
385            }
386    
387            protected PortletURL getRestoreURL(
388                            PortletRequest portletRequest, long classPK)
389                    throws PortalException {
390    
391                    String portletId = PortletKeys.MESSAGE_BOARDS;
392    
393                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
394    
395                    long plid = PortalUtil.getPlidFromPortletId(
396                            category.getGroupId(), PortletKeys.MESSAGE_BOARDS);
397    
398                    if (plid == LayoutConstants.DEFAULT_PLID) {
399                            portletId = PortletKeys.MESSAGE_BOARDS_ADMIN;
400    
401                            plid = PortalUtil.getControlPanelPlid(portletRequest);
402                    }
403    
404                    PortletURL portletURL = PortletURLFactoryUtil.create(
405                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
406    
407                    if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
408                            portletURL.setParameter("struts_action", "/message_boards/view");
409                    }
410                    else {
411                            portletURL.setParameter(
412                                    "struts_action", "/message_boards_admin/view");
413                    }
414    
415                    return portletURL;
416            }
417    
418            @Override
419            protected boolean hasPermission(
420                            PermissionChecker permissionChecker, long classPK, String actionId)
421                    throws PortalException {
422    
423                    MBCategory category = MBCategoryLocalServiceUtil.getCategory(classPK);
424    
425                    return MBCategoryPermission.contains(
426                            permissionChecker, category, actionId);
427            }
428    
429    }