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.wiki.model.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.Folder;
022    import com.liferay.portal.model.Repository;
023    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
027    import com.liferay.portlet.wiki.model.WikiPage;
028    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class WikiNodeImpl extends WikiNodeBaseImpl {
037    
038            public WikiNodeImpl() {
039            }
040    
041            public Folder addAttachmentsFolder()
042                    throws PortalException, SystemException {
043    
044                    if (_attachmentsFolderId !=
045                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
046    
047                            return PortletFileRepositoryUtil.getPortletFolder(
048                                    _attachmentsFolderId);
049                    }
050    
051                    ServiceContext serviceContext = new ServiceContext();
052    
053                    serviceContext.setAddGroupPermissions(true);
054                    serviceContext.setAddGuestPermissions(true);
055    
056                    Repository repository = PortletFileRepositoryUtil.addPortletRepository(
057                            getGroupId(), PortletKeys.WIKI, serviceContext);
058    
059                    Folder folder = PortletFileRepositoryUtil.addPortletFolder(
060                            getUserId(), repository.getRepositoryId(),
061                            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
062                            String.valueOf(getNodeId()), serviceContext);
063    
064                    _attachmentsFolderId = folder.getFolderId();
065    
066                    return folder;
067            }
068    
069            public long getAttachmentsFolderId() throws SystemException {
070                    if (_attachmentsFolderId !=
071                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
072    
073                            return _attachmentsFolderId;
074                    }
075    
076                    ServiceContext serviceContext = new ServiceContext();
077    
078                    serviceContext.setAddGroupPermissions(true);
079                    serviceContext.setAddGuestPermissions(true);
080    
081                    Repository repository =
082                            PortletFileRepositoryUtil.fetchPortletRepository(
083                                    getGroupId(), PortletKeys.WIKI);
084    
085                    if (repository == null) {
086                            return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
087                    }
088    
089                    try {
090                            Folder folder = PortletFileRepositoryUtil.getPortletFolder(
091                                    getUserId(), repository.getRepositoryId(),
092                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
093                                    String.valueOf(getNodeId()), serviceContext);
094    
095                            _attachmentsFolderId = folder.getFolderId();
096                    }
097                    catch (Exception e) {
098                    }
099    
100                    return _attachmentsFolderId;
101            }
102    
103            public List<FileEntry> getDeletedAttachmentsFiles() throws SystemException {
104                    List<WikiPage> wikiPages = WikiPageLocalServiceUtil.getPages(
105                            getNodeId(), true, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
106    
107                    List<FileEntry> fileEntries = new ArrayList<FileEntry>();
108    
109                    for (WikiPage wikiPage : wikiPages) {
110                            fileEntries.addAll(wikiPage.getDeletedAttachmentsFileEntries());
111                    }
112    
113                    return fileEntries;
114            }
115    
116            private long _attachmentsFolderId;
117    
118    }