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.TrashActionKeys;
021    import com.liferay.portal.kernel.trash.TrashRenderer;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.model.ContainerModel;
024    import com.liferay.portal.model.LayoutConstants;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.security.permission.PermissionChecker;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.PortletKeys;
030    import com.liferay.portlet.PortletURLFactoryUtil;
031    import com.liferay.portlet.messageboards.model.MBCategory;
032    import com.liferay.portlet.messageboards.model.MBThread;
033    import com.liferay.portlet.messageboards.service.MBCategoryLocalServiceUtil;
034    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
035    import com.liferay.portlet.messageboards.service.permission.MBCategoryPermission;
036    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
037    import com.liferay.portlet.messageboards.util.MBUtil;
038    
039    import java.util.ArrayList;
040    import java.util.List;
041    
042    import javax.portlet.PortletRequest;
043    import javax.portlet.PortletURL;
044    
045    /**
046     * Implements trash handling for message boards thread entity.
047     *
048     * @author Zsolt Berentey
049     */
050    public class MBThreadTrashHandler extends BaseTrashHandler {
051    
052            @Override
053            public void deleteTrashEntry(long classPK)
054                    throws PortalException, SystemException {
055    
056                    MBThreadLocalServiceUtil.deleteThread(classPK);
057            }
058    
059            @Override
060            public String getClassName() {
061                    return MBThread.class.getName();
062            }
063    
064            @Override
065            public String getContainerModelClassName() {
066                    return MBCategory.class.getName();
067            }
068    
069            @Override
070            public String getContainerModelName() {
071                    return "category";
072            }
073    
074            @Override
075            public List<ContainerModel> getContainerModels(
076                            long classPK, long parentContainerModelId, int start, int end)
077                    throws PortalException, SystemException {
078    
079                    List<ContainerModel> containerModels = new ArrayList<ContainerModel>();
080    
081                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
082    
083                    List<MBCategory> categories = MBCategoryLocalServiceUtil.getCategories(
084                            thread.getGroupId(), parentContainerModelId,
085                            WorkflowConstants.STATUS_APPROVED, start, end);
086    
087                    for (MBCategory category : categories) {
088                            containerModels.add(category);
089                    }
090    
091                    return containerModels;
092            }
093    
094            @Override
095            public int getContainerModelsCount(
096                            long classPK, long parentContainerModelId)
097                    throws PortalException, SystemException {
098    
099                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
100    
101                    return MBCategoryLocalServiceUtil.getCategoriesCount(
102                            thread.getGroupId(), parentContainerModelId,
103                            WorkflowConstants.STATUS_APPROVED);
104            }
105    
106            @Override
107            public String getRestoreLink(PortletRequest portletRequest, long classPK)
108                    throws PortalException, SystemException {
109    
110                    String portletId = PortletKeys.MESSAGE_BOARDS;
111    
112                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
113    
114                    long plid = PortalUtil.getPlidFromPortletId(
115                            thread.getGroupId(), PortletKeys.MESSAGE_BOARDS);
116    
117                    if (plid == LayoutConstants.DEFAULT_PLID) {
118                            portletId = PortletKeys.MESSAGE_BOARDS_ADMIN;
119    
120                            plid = PortalUtil.getControlPanelPlid(portletRequest);
121                    }
122    
123                    PortletURL portletURL = PortletURLFactoryUtil.create(
124                            portletRequest, portletId, plid, PortletRequest.RENDER_PHASE);
125    
126                    if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
127                            portletURL.setParameter("struts_action", "/message_boards/view");
128                    }
129                    else {
130                            portletURL.setParameter(
131                                    "struts_action", "/message_boards_admin/view");
132                    }
133    
134                    portletURL.setParameter(
135                            "mbCategoryId", String.valueOf(thread.getCategoryId()));
136    
137                    return portletURL.toString();
138            }
139    
140            @Override
141            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
142                    throws PortalException, SystemException {
143    
144                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
145    
146                    return MBUtil.getAbsolutePath(portletRequest, thread.getCategoryId());
147            }
148    
149            @Override
150            public ContainerModel getTrashContainer(long classPK)
151                    throws PortalException, SystemException {
152    
153                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
154    
155                    return thread.getTrashContainer();
156            }
157    
158            @Override
159            public TrashRenderer getTrashRenderer(long classPK)
160                    throws PortalException, SystemException {
161    
162                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
163    
164                    return new MBThreadTrashRenderer(thread);
165            }
166    
167            @Override
168            public boolean hasTrashPermission(
169                            PermissionChecker permissionChecker, long groupId, long classPK,
170                            String trashActionId)
171                    throws PortalException, SystemException {
172    
173                    if (trashActionId.equals(TrashActionKeys.MOVE)) {
174                            return MBCategoryPermission.contains(
175                                    permissionChecker, groupId, classPK, ActionKeys.ADD_MESSAGE);
176                    }
177    
178                    return super.hasTrashPermission(
179                            permissionChecker, groupId, classPK, trashActionId);
180            }
181    
182            @Override
183            public boolean isInTrash(long classPK)
184                    throws PortalException, SystemException {
185    
186                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
187    
188                    return thread.isInTrash();
189            }
190    
191            @Override
192            public boolean isInTrashContainer(long classPK)
193                    throws PortalException, SystemException {
194    
195                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
196    
197                    return thread.isInTrashContainer();
198            }
199    
200            @Override
201            public boolean isMovable() {
202                    return true;
203            }
204    
205            @Override
206            public boolean isRestorable(long classPK)
207                    throws PortalException, SystemException {
208    
209                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
210    
211                    if ((thread.getCategoryId() > 0) &&
212                            (MBCategoryLocalServiceUtil.fetchMBCategory(
213                                    thread.getCategoryId()) == null)) {
214    
215                            return false;
216                    }
217    
218                    return !thread.isInTrashContainer();
219            }
220    
221            @Override
222            public void moveEntry(
223                            long userId, long classPK, long containerModelId,
224                            ServiceContext serviceContext)
225                    throws PortalException, SystemException {
226    
227                    MBThreadLocalServiceUtil.moveThread(userId, containerModelId, classPK);
228            }
229    
230            @Override
231            public void moveTrashEntry(
232                            long userId, long classPK, long containerModelId,
233                            ServiceContext serviceContext)
234                    throws PortalException, SystemException {
235    
236                    MBThreadLocalServiceUtil.moveThreadFromTrash(
237                            userId, containerModelId, classPK);
238            }
239    
240            @Override
241            public void restoreTrashEntry(long userId, long classPK)
242                    throws PortalException, SystemException {
243    
244                    MBThreadLocalServiceUtil.restoreThreadFromTrash(userId, classPK);
245            }
246    
247            @Override
248            protected boolean hasPermission(
249                            PermissionChecker permissionChecker, long classPK, String actionId)
250                    throws PortalException, SystemException {
251    
252                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
253    
254                    return MBMessagePermission.contains(
255                            permissionChecker, thread.getRootMessageId(), actionId);
256            }
257    
258    }