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 String getContainerModelClassName() {
071                    return MBCategory.class.getName();
072            }
073    
074            @Override
075            public String getContainerModelName() {
076                    return "category";
077            }
078    
079            @Override
080            public List<ContainerModel> getContainerModels(
081                            long classPK, long parentContainerModelId, int start, int end)
082                    throws PortalException, SystemException {
083    
084                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>();
085    
086                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
087    
088                    List<MBCategory> categories = MBCategoryLocalServiceUtil.getCategories(
089                            thread.getGroupId(), parentContainerModelId,
090                            WorkflowConstants.STATUS_APPROVED, start, end);
091    
092                    for (MBCategory category : categories) {
093                            containerModels.add(category);
094                    }
095    
096                    return containerModels;
097            }
098    
099            @Override
100            public int getContainerModelsCount(
101                            long classPK, long parentContainerModelId)
102                    throws PortalException, SystemException {
103    
104                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
105    
106                    return MBCategoryLocalServiceUtil.getCategoriesCount(
107                            thread.getGroupId(), parentContainerModelId,
108                            WorkflowConstants.STATUS_APPROVED);
109            }
110    
111            @Override
112            public String getRestoreLink(PortletRequest portletRequest, long classPK)
113                    throws PortalException, SystemException {
114    
115                    String portletId = PortletKeys.MESSAGE_BOARDS;
116    
117                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
118    
119                    long plid = PortalUtil.getPlidFromPortletId(
120                            thread.getGroupId(), PortletKeys.MESSAGE_BOARDS);
121    
122                    if (plid == LayoutConstants.DEFAULT_PLID) {
123                            portletId = PortletKeys.MESSAGE_BOARDS_ADMIN;
124    
125                            plid = PortalUtil.getControlPanelPlid(portletRequest);
126                    }
127    
128                    PortletURL portletURL = PortletURLFactoryUtil.create(
129                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
130    
131                    if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
132                            portletURL.setParameter("struts_action", "/message_boards/view");
133                    }
134                    else {
135                            portletURL.setParameter(
136                                    "struts_action", "/message_boards_admin/view");
137                    }
138    
139                    portletURL.setParameter(
140                            "mbCategoryId", String.valueOf(thread.getCategoryId()));
141    
142                    return portletURL.toString();
143            }
144    
145            @Override
146            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
147                    throws PortalException, SystemException {
148    
149                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
150    
151                    return MBUtil.getAbsolutePath(portletRequest, thread.getCategoryId());
152            }
153    
154            @Override
155            public TrashRenderer getTrashRenderer(long classPK)
156                    throws PortalException, SystemException {
157    
158                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
159    
160                    return new MBThreadTrashRenderer(thread);
161            }
162    
163            public boolean isInTrash(long classPK)
164                    throws PortalException, SystemException {
165    
166                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
167    
168                    if (thread.isInTrash() || thread.isInTrashContainer()) {
169                            return true;
170                    }
171    
172                    return false;
173            }
174    
175            @Override
176            public boolean isInTrashContainer(long classPK)
177                            throws PortalException, SystemException {
178    
179                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
180    
181                    return thread.isInTrashContainer();
182            }
183    
184            @Override
185            public boolean isMovable() {
186                    return true;
187            }
188    
189            @Override
190            public void moveEntry(
191                            long classPK, long containerModelId, ServiceContext serviceContext)
192                    throws PortalException, SystemException {
193    
194                    MBThreadServiceUtil.moveThread(containerModelId, classPK);
195            }
196    
197            @Override
198            public void moveTrashEntry(
199                            long classPK, long containerModelId, ServiceContext serviceContext)
200                    throws PortalException, SystemException {
201    
202                    MBThreadServiceUtil.moveThreadFromTrash(containerModelId, classPK);
203            }
204    
205            public void restoreTrashEntries(long[] classPKs)
206                    throws PortalException, SystemException {
207    
208                    for (long classPK : classPKs) {
209                            MBThreadServiceUtil.restoreThreadFromTrash(classPK);
210                    }
211            }
212    
213            @Override
214            protected boolean hasPermission(
215                            PermissionChecker permissionChecker, long classPK, String actionId)
216                    throws PortalException, SystemException {
217    
218                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
219    
220                    return MBMessagePermission.contains(
221                            permissionChecker, thread.getRootMessageId(), actionId);
222            }
223    
224    }