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.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.model.CompanyConstants;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
027    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
028    import com.liferay.portlet.messageboards.model.MBThread;
029    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
030    import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
031    import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
032    import com.liferay.portlet.messageboards.util.MBUtil;
033    import com.liferay.portlet.trash.util.TrashUtil;
034    
035    import java.util.Date;
036    
037    import javax.portlet.PortletRequest;
038    
039    /**
040     * Implements trash handling for message boards thread entity.
041     *
042     * @author Zsolt Berentey
043     */
044    public class MBThreadTrashHandler extends BaseTrashHandler {
045    
046            public static final String CLASS_NAME = MBThread.class.getName();
047    
048            @Override
049            public void deleteTrashAttachments(Group group, Date date)
050                    throws PortalException, SystemException {
051    
052                    long repositoryId = CompanyConstants.SYSTEM;
053    
054                    String[] threadFileNames = null;
055    
056                    try {
057                            threadFileNames = DLStoreUtil.getFileNames(
058                                    group.getCompanyId(), repositoryId, "messageboards");
059                    }
060                    catch (NoSuchDirectoryException nsde) {
061                            return;
062                    }
063    
064                    for (String threadFileName : threadFileNames) {
065                            String[] messageFileNames = null;
066    
067                            try {
068                                    messageFileNames = DLStoreUtil.getFileNames(
069                                            group.getCompanyId(), repositoryId, threadFileName);
070                            }
071                            catch (NoSuchDirectoryException nsde) {
072                                    continue;
073                            }
074    
075                            for (String messageFileName : messageFileNames) {
076                                    String fileTitle = StringUtil.extractLast(
077                                            messageFileName, StringPool.FORWARD_SLASH);
078    
079                                    if (fileTitle.startsWith(TrashUtil.TRASH_ATTACHMENTS_DIR)) {
080                                            String[] attachmentFileNames = DLStoreUtil.getFileNames(
081                                                    group.getCompanyId(), repositoryId,
082                                                    threadFileName + StringPool.FORWARD_SLASH + fileTitle);
083    
084                                            TrashUtil.deleteEntriesAttachments(
085                                                    group.getCompanyId(), repositoryId, date,
086                                                    attachmentFileNames);
087                                    }
088                            }
089                    }
090            }
091    
092            public void deleteTrashEntries(long[] classPKs, boolean checkPermission)
093                    throws PortalException, SystemException {
094    
095                    for (long classPK : classPKs) {
096                            if (checkPermission) {
097                                    MBThreadServiceUtil.deleteThread(classPK);
098                            }
099                            else {
100                                    MBThreadLocalServiceUtil.deleteThread(classPK);
101                            }
102                    }
103            }
104    
105            public String getClassName() {
106                    return CLASS_NAME;
107            }
108    
109            @Override
110            public String getRestoreLink(PortletRequest portletRequest, long classPK)
111                    throws PortalException, SystemException {
112    
113                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
114    
115                    return MBUtil.getMBControlPanelLink(
116                            portletRequest, thread.getCategoryId());
117            }
118    
119            @Override
120            public String getRestoreMessage(PortletRequest portletRequest, long classPK)
121                    throws PortalException, SystemException {
122    
123                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
124    
125                    return MBUtil.getAbsolutePath(portletRequest, thread.getCategoryId());
126            }
127    
128            @Override
129            public TrashRenderer getTrashRenderer(long classPK)
130                    throws PortalException, SystemException {
131    
132                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
133    
134                    return new MBThreadTrashRenderer(thread);
135            }
136    
137            public boolean isInTrash(long classPK) {
138                    return false;
139            }
140    
141            public void restoreTrashEntries(long[] classPKs)
142                    throws PortalException, SystemException {
143    
144                    for (long classPK : classPKs) {
145                            MBThreadServiceUtil.restoreThreadFromTrash(classPK);
146                    }
147            }
148    
149            @Override
150            protected boolean hasPermission(
151                            PermissionChecker permissionChecker, long classPK, String actionId)
152                    throws PortalException, SystemException {
153    
154                    MBThread thread = MBThreadLocalServiceUtil.getThread(classPK);
155    
156                    return MBMessagePermission.contains(
157                            permissionChecker, thread.getRootMessageId(), actionId);
158            }
159    
160    }