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.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.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.repository.model.FileEntry;
023    import com.liferay.portal.kernel.repository.model.Folder;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.workflow.WorkflowConstants;
026    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.util.PortletKeys;
029    import com.liferay.portlet.wiki.model.WikiNode;
030    import com.liferay.portlet.wiki.model.WikiPage;
031    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
032    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
033    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
034    
035    import java.util.ArrayList;
036    import java.util.Collections;
037    import java.util.List;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Jorge Ferrer
042     */
043    public class WikiPageImpl extends WikiPageBaseImpl {
044    
045            public WikiPageImpl() {
046            }
047    
048            public List<FileEntry> getAttachmentsFileEntries()
049                    throws PortalException, SystemException {
050    
051                    return getAttachmentsFileEntries(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
052            }
053    
054            public List<FileEntry> getAttachmentsFileEntries(int start, int end)
055                    throws PortalException, SystemException {
056    
057                    return PortletFileRepositoryUtil.getPortletFileEntries(
058                            getGroupId(), getAttachmentsFolderId(),
059                            WorkflowConstants.STATUS_APPROVED, start, end, null);
060            }
061    
062            public int getAttachmentsFileEntriesCount()
063                    throws PortalException, SystemException {
064    
065                    return PortletFileRepositoryUtil.getPortletFileEntriesCount(
066                            getGroupId(), getAttachmentsFolderId(),
067                            WorkflowConstants.STATUS_APPROVED);
068            }
069    
070            public long getAttachmentsFolderId()
071                    throws PortalException, SystemException {
072    
073                    if (_attachmentsFolderId > 0) {
074                            return _attachmentsFolderId;
075                    }
076    
077                    ServiceContext serviceContext = new ServiceContext();
078    
079                    serviceContext.setAddGroupPermissions(true);
080                    serviceContext.setAddGuestPermissions(true);
081    
082                    long repositoryId = PortletFileRepositoryUtil.getPortletRepositoryId(
083                            getGroupId(), PortletKeys.WIKI, serviceContext);
084    
085                    Folder folder = PortletFileRepositoryUtil.getPortletFolder(
086                            getUserId(), repositoryId, getNodeAttachmentsFolderId(),
087                            String.valueOf(getResourcePrimKey()), serviceContext);
088    
089                    _attachmentsFolderId = folder.getFolderId();
090    
091                    return _attachmentsFolderId;
092            }
093    
094            public List<WikiPage> getChildPages() {
095                    try {
096                            return WikiPageLocalServiceUtil.getChildren(
097                                    getNodeId(), true, getTitle());
098                    }
099                    catch (Exception e) {
100                            _log.error(e, e);
101    
102                            return Collections.emptyList();
103                    }
104            }
105    
106            public List<FileEntry> getDeletedAttachmentsFileEntries()
107                    throws PortalException, SystemException {
108    
109                    return getDeletedAttachmentsFileEntries(
110                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
111            }
112    
113            public List<FileEntry> getDeletedAttachmentsFileEntries(int start, int end)
114                    throws PortalException, SystemException {
115    
116                    return PortletFileRepositoryUtil.getPortletFileEntries(
117                            getGroupId(), getAttachmentsFolderId(),
118                            WorkflowConstants.STATUS_IN_TRASH, start, end, null);
119            }
120    
121            public int getDeletedAttachmentsFileEntriesCount()
122                    throws PortalException, SystemException {
123    
124                    return PortletFileRepositoryUtil.getPortletFileEntriesCount(
125                            getGroupId(), getAttachmentsFolderId(),
126                            WorkflowConstants.STATUS_IN_TRASH);
127            }
128    
129            public WikiNode getNode() {
130                    try {
131                            return WikiNodeLocalServiceUtil.getNode(getNodeId());
132                    }
133                    catch (Exception e) {
134                            _log.error(e, e);
135    
136                            return new WikiNodeImpl();
137                    }
138            }
139    
140            public long getNodeAttachmentsFolderId()
141                    throws PortalException, SystemException {
142    
143                    WikiNode node = getNode();
144    
145                    return node.getAttachmentsFolderId();
146            }
147    
148            public WikiPage getParentPage() {
149                    if (Validator.isNull(getParentTitle())) {
150                            return null;
151                    }
152    
153                    try {
154                            return WikiPageLocalServiceUtil.getPage(
155                                    getNodeId(), getParentTitle());
156                    }
157                    catch (Exception e) {
158                            _log.error(e, e);
159    
160                            return null;
161                    }
162            }
163    
164            public List<WikiPage> getParentPages() {
165                    List<WikiPage> parentPages = new ArrayList<WikiPage>();
166    
167                    WikiPage parentPage = getParentPage();
168    
169                    if (parentPage != null) {
170                            parentPages.addAll(parentPage.getParentPages());
171                            parentPages.add(parentPage);
172                    }
173    
174                    return parentPages;
175            }
176    
177            public WikiPage getRedirectPage() {
178                    if (Validator.isNull(getRedirectTitle())) {
179                            return null;
180                    }
181    
182                    try {
183                            return WikiPageLocalServiceUtil.getPage(
184                                    getNodeId(), getRedirectTitle());
185                    }
186                    catch (Exception e) {
187                            _log.error(e, e);
188    
189                            return null;
190                    }
191            }
192    
193            public List<WikiPage> getViewableChildPages() {
194                    try {
195                            return WikiPageServiceUtil.getChildren(
196                                    getGroupId(), getNodeId(), true, getTitle());
197                    }
198                    catch (Exception e) {
199                            _log.error(e, e);
200    
201                            return Collections.emptyList();
202                    }
203            }
204    
205            public WikiPage getViewableParentPage() {
206                    if (Validator.isNull(getParentTitle())) {
207                            return null;
208                    }
209    
210                    try {
211                            return WikiPageServiceUtil.getPage(
212                                    getGroupId(), getNodeId(), getParentTitle());
213                    }
214                    catch (Exception e) {
215                            _log.error(e, e);
216    
217                            return null;
218                    }
219            }
220    
221            public List<WikiPage> getViewableParentPages() {
222                    List<WikiPage> pages = new ArrayList<WikiPage>();
223    
224                    WikiPage page = getViewableParentPage();
225    
226                    if (page != null) {
227                            pages.addAll(page.getViewableParentPages());
228                            pages.add(page);
229                    }
230    
231                    return pages;
232            }
233    
234            public boolean isInTrashContainer() {
235                    WikiNode node = getNode();
236    
237                    if (node != null) {
238                            return node.isInTrash();
239                    }
240    
241                    return false;
242            }
243    
244            @Override
245            public boolean isResourceMain() {
246                    return isHead();
247            }
248    
249            public void setAttachmentsFolderId(long attachmentsFolderId) {
250                    _attachmentsFolderId = attachmentsFolderId;
251            }
252    
253            private static Log _log = LogFactoryUtil.getLog(WikiPageImpl.class);
254    
255            private long _attachmentsFolderId;
256    
257    }