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.TrashRenderer;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.model.ContainerModel;
023    import com.liferay.portal.model.LayoutConstants;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.PortletKeys;
028    import com.liferay.portlet.PortletURLFactoryUtil;
029    import com.liferay.portlet.messageboards.model.MBCategory;
030    import com.liferay.portlet.messageboards.model.MBThread;
031    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
032    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
033    import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
034    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
035    import com.liferay.portlet.messageboards.util.MBUtil;
036    
037    import java.util.ArrayList;
038    import java.util.List;
039    
040    import javax.portlet.PortletRequest;
041    import javax.portlet.PortletURL;
042    
043    /**
044     * Implements trash handling for message boards thread entity.
045     *
046     * @author Zsolt Berentey
047     */
048    public class MBThreadTrashHandler extends BaseTrashHandler {
049    
050            public static final String CLASS_NAME = MBThread.class.getName();
051    
052            public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
053                    throws PortalException, SystemException {
054    
055                    for (long classPK : classPKs) {
056                            if (checkPermission) {
057                                    MBThreadServiceUtil.deleteThread(classPK);
058                            }
059                            else {
060                                    MBThreadLocalServiceUtil.deleteThread(classPK);
061                            }
062                    }
063            }
064    
065            public String getClassName() {
066                    return CLASS_NAME;
067            }
068    
069            @Override
070            public List<ContainerModel> getContainerModels(
071                            long classPK, long parentContainerModelId, int start, int end)
072                    throws PortalException, SystemException {
073    
074                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>();
075    
076                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
077    
078                    List<MBCategory> categories = MBCategoryLocalServiceUtil.getCategories(
079                            thread.getGroupId(), parentContainerModelId,
080                            WorkflowConstants.STATUS_APPROVED, start, end);
081    
082                    for (MBCategory category : categories) {
083                            containerModels.add(category);
084                    }
085    
086                    return containerModels;
087            }
088    
089            @Override
090            public int getContainerModelsCount(
091                            long classPK, long parentContainerModelId)
092                    throws PortalException, SystemException {
093    
094                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
095    
096                    return MBCategoryLocalServiceUtil.getCategoriesCount(
097                            thread.getGroupId(), parentContainerModelId,
098                            WorkflowConstants.STATUS_APPROVED);
099            }
100    
101            @Override
102            public String getRestoreLink(PortletRequest portletRequest, long classPK)
103                    throws PortalException, SystemException {
104    
105                    String portletId = PortletKeys.MESSAGE_BOARDS;
106    
107                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
108    
109                    long plid = PortalUtil.getPlidFromPortletId(
110                            thread.getGroupId(), PortletKeys.MESSAGE_BOARDS);
111    
112                    if (plid == LayoutConstants.DEFAULT_PLID) {
113                            plid = PortalUtil.getControlPanelPlid(portletRequest);
114    
115                            portletId = PortletKeys.MESSAGE_BOARDS_ADMIN;
116                    }
117    
118                    PortletURL portletURL = PortletURLFactoryUtil.create(
119                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
120    
121                    portletURL.setParameter("struts_action", "/message_boards_admin/view");
122                    portletURL.setParameter(
123                            "mbCategoryId", String.valueOf(thread.getCategoryId()));
124    
125                    return portletURL.toString();
126            }
127    
128            @Override
129            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
130                    throws PortalException, SystemException {
131    
132                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
133    
134                    return MBUtil.getAbsolutePath(portletRequest, thread.getCategoryId());
135            }
136    
137            @Override
138            public TrashRenderer getTrashRenderer(long classPK)
139                    throws PortalException, SystemException {
140    
141                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
142    
143                    return new MBThreadTrashRenderer(thread);
144            }
145    
146            public boolean isInTrash(long classPK)
147                    throws PortalException, SystemException {
148    
149                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
150    
151                    if (thread.isInTrash() || thread.isInTrashContainer()) {
152                            return true;
153                    }
154    
155                    return false;
156            }
157    
158            @Override
159            public boolean isInTrashContainer(long classPK)
160                            throws PortalException, SystemException {
161    
162                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
163    
164                    return thread.isInTrashContainer();
165            }
166    
167            @Override
168            public boolean isMovable() {
169                    return true;
170            }
171    
172            @Override
173            public void moveEntry(
174                            long classPK, long containerModelId, ServiceContext serviceContext)
175                    throws PortalException, SystemException {
176    
177                    MBThreadServiceUtil.moveThread(containerModelId, classPK);
178            }
179    
180            @Override
181            public void moveTrashEntry(
182                            long classPK, long containerModelId, ServiceContext serviceContext)
183                    throws PortalException, SystemException {
184    
185                    MBThreadServiceUtil.moveThreadFromTrash(containerModelId, classPK);
186            }
187    
188            public void restoreTrashEntries(long[] classPKs)
189                    throws PortalException, SystemException {
190    
191                    for (long classPK : classPKs) {
192                            MBThreadServiceUtil.restoreThreadFromTrash(classPK);
193                    }
194            }
195    
196            @Override
197            protected boolean hasPermission(
198                            PermissionChecker permissionChecker, long classPK, String actionId)
199                    throws PortalException, SystemException {
200    
201                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
202    
203                    return MBMessagePermission.contains(
204                            permissionChecker, thread.getRootMessageId(), actionId);
205            }
206    
207    }