001    /**
002     * Copyright (c) 2000-2013 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.permission.MBMessagePermission;
034    import com.liferay.portlet.messageboards.util.MBUtil;
035    
036    import java.util.ArrayList;
037    import java.util.List;
038    
039    import javax.portlet.PortletRequest;
040    import javax.portlet.PortletURL;
041    
042    /**
043     * Implements trash handling for message boards thread entity.
044     *
045     * @author Zsolt Berentey
046     */
047    public class MBThreadTrashHandler extends BaseTrashHandler {
048    
049            public void deleteTrashEntry(long classPK)
050                    throws PortalException, SystemException {
051    
052                    MBThreadLocalServiceUtil.deleteThread(classPK);
053            }
054    
055            public String getClassName() {
056                    return MBThread.class.getName();
057            }
058    
059            @Override
060            public String getContainerModelClassName() {
061                    return MBCategory.class.getName();
062            }
063    
064            @Override
065            public String getContainerModelName() {
066                    return "category";
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                            portletId = PortletKeys.MESSAGE_BOARDS_ADMIN;
114    
115                            plid = PortalUtil.getControlPanelPlid(portletRequest);
116                    }
117    
118                    PortletURL portletURL = PortletURLFactoryUtil.create(
119                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
120    
121                    if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
122                            portletURL.setParameter("struts_action", "/message_boards/view");
123                    }
124                    else {
125                            portletURL.setParameter(
126                                    "struts_action", "/message_boards_admin/view");
127                    }
128    
129                    portletURL.setParameter(
130                            "mbCategoryId", String.valueOf(thread.getCategoryId()));
131    
132                    return portletURL.toString();
133            }
134    
135            @Override
136            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
137                    throws PortalException, SystemException {
138    
139                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
140    
141                    return MBUtil.getAbsolutePath(portletRequest, thread.getCategoryId());
142            }
143    
144            @Override
145            public ContainerModel getTrashContainer(long classPK)
146                    throws PortalException, SystemException {
147    
148                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
149    
150                    return thread.getTrashContainer();
151            }
152    
153            @Override
154            public TrashRenderer getTrashRenderer(long classPK)
155                    throws PortalException, SystemException {
156    
157                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
158    
159                    return new MBThreadTrashRenderer(thread);
160            }
161    
162            public boolean isInTrash(long classPK)
163                    throws PortalException, SystemException {
164    
165                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
166    
167                    return thread.isInTrash();
168            }
169    
170            @Override
171            public boolean isInTrashContainer(long classPK)
172                            throws PortalException, SystemException {
173    
174                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
175    
176                    return thread.isInTrashContainer();
177            }
178    
179            @Override
180            public boolean isMovable() {
181                    return true;
182            }
183    
184            @Override
185            public void moveEntry(
186                            long userId, long classPK, long containerModelId,
187                            ServiceContext serviceContext)
188                    throws PortalException, SystemException {
189    
190                    MBThreadLocalServiceUtil.moveThread(userId, containerModelId, classPK);
191            }
192    
193            @Override
194            public void moveTrashEntry(
195                            long userId, long classPK, long containerModelId,
196                            ServiceContext serviceContext)
197                    throws PortalException, SystemException {
198    
199                    MBThreadLocalServiceUtil.moveThreadFromTrash(
200                            userId, containerModelId, classPK);
201            }
202    
203            public void restoreTrashEntry(long userId, long classPK)
204                    throws PortalException, SystemException {
205    
206                    MBThreadLocalServiceUtil.restoreThreadFromTrash(userId, classPK);
207            }
208    
209            @Override
210            protected boolean hasPermission(
211                            PermissionChecker permissionChecker, long classPK, String actionId)
212                    throws PortalException, SystemException {
213    
214                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
215    
216                    return MBMessagePermission.contains(
217                            permissionChecker, thread.getRootMessageId(), actionId);
218            }
219    
220    }