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.wiki.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.repository.model.Folder;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
026    import com.liferay.portlet.wiki.model.WikiPage;
027    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
028    
029    /**
030     * @author Eudaldo Alonso
031     */
032    public class WikiPageAttachmentsUtil {
033    
034            public static long getFolderId(
035                            long groupId, long userId, long nodeId, long pageId)
036                    throws PortalException, SystemException {
037    
038                    ServiceContext serviceContext = new ServiceContext();
039    
040                    serviceContext.setAddGroupPermissions(true);
041                    serviceContext.setAddGuestPermissions(true);
042    
043                    long repositoryId = PortletFileRepositoryUtil.getPortletRepository(
044                            groupId, PortletKeys.WIKI, serviceContext);
045    
046                    Folder nodeFolder = PortletFileRepositoryUtil.getPortletFolder(
047                            userId, repositoryId, DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
048                            String.valueOf(nodeId), serviceContext);
049    
050                    Folder pageFolder = PortletFileRepositoryUtil.getPortletFolder(
051                            userId, repositoryId, nodeFolder.getFolderId(),
052                            String.valueOf(pageId), serviceContext);
053    
054                    return pageFolder.getFolderId();
055            }
056    
057            public static WikiPage getPage(long fileEntryId)
058                    throws PortalException, SystemException {
059    
060                    FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
061                            fileEntryId);
062    
063                    Folder folder = PortletFileRepositoryUtil.getPortletFolder(
064                            fileEntry.getFolderId());
065    
066                    long resourcePrimKey = GetterUtil.getLong(folder.getName());
067    
068                    return WikiPageLocalServiceUtil.getPage(resourcePrimKey);
069            }
070    
071    }