001    /**
002     * Copyright (c) 2000-present 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.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.Folder;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.model.Repository;
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.documentlibrary.model.DLFolderConstants;
030    import com.liferay.portlet.wiki.model.WikiNode;
031    import com.liferay.portlet.wiki.model.WikiPage;
032    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
033    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
034    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
035    
036    import java.util.ArrayList;
037    import java.util.Collections;
038    import java.util.List;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Jorge Ferrer
043     */
044    public class WikiPageImpl extends WikiPageBaseImpl {
045    
046            public WikiPageImpl() {
047            }
048    
049            @Override
050            public Folder addAttachmentsFolder() throws PortalException {
051                    if (_attachmentsFolderId !=
052                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
053    
054                            return PortletFileRepositoryUtil.getPortletFolder(
055                                    _attachmentsFolderId);
056                    }
057    
058                    ServiceContext serviceContext = new ServiceContext();
059    
060                    serviceContext.setAddGroupPermissions(true);
061                    serviceContext.setAddGuestPermissions(true);
062    
063                    Repository repository = PortletFileRepositoryUtil.addPortletRepository(
064                            getGroupId(), PortletKeys.WIKI, serviceContext);
065    
066                    WikiNode node = getNode();
067    
068                    Folder nodeFolder = node.addAttachmentsFolder();
069    
070                    Folder folder = PortletFileRepositoryUtil.addPortletFolder(
071                            getUserId(), repository.getRepositoryId(), nodeFolder.getFolderId(),
072                            String.valueOf(getResourcePrimKey()), serviceContext);
073    
074                    _attachmentsFolderId = folder.getFolderId();
075    
076                    return folder;
077            }
078    
079            @Override
080            public WikiPage fetchParentPage() {
081                    if (Validator.isNull(getParentTitle())) {
082                            return null;
083                    }
084    
085                    return WikiPageLocalServiceUtil.fetchPage(
086                            getNodeId(), getParentTitle());
087            }
088    
089            @Override
090            public WikiPage fetchRedirectPage() {
091                    if (Validator.isNull(getRedirectTitle())) {
092                            return null;
093                    }
094    
095                    return WikiPageLocalServiceUtil.fetchPage(
096                            getNodeId(), getRedirectTitle());
097            }
098    
099            @Override
100            public List<FileEntry> getAttachmentsFileEntries() {
101                    return getAttachmentsFileEntries(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
102            }
103    
104            @Override
105            public List<FileEntry> getAttachmentsFileEntries(int start, int end) {
106                    List<FileEntry> fileEntries = new ArrayList<FileEntry>();
107    
108                    long attachmentsFolderId = getAttachmentsFolderId();
109    
110                    if (attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
111                            fileEntries = PortletFileRepositoryUtil.getPortletFileEntries(
112                                    getGroupId(), attachmentsFolderId,
113                                    WorkflowConstants.STATUS_APPROVED, start, end, null);
114                    }
115    
116                    return fileEntries;
117            }
118    
119            @Override
120            public int getAttachmentsFileEntriesCount() {
121                    int attachmentsFileEntriesCount = 0;
122    
123                    long attachmentsFolderId = getAttachmentsFolderId();
124    
125                    if (attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
126                            attachmentsFileEntriesCount =
127                                    PortletFileRepositoryUtil.getPortletFileEntriesCount(
128                                            getGroupId(), attachmentsFolderId,
129                                            WorkflowConstants.STATUS_APPROVED);
130                    }
131    
132                    return attachmentsFileEntriesCount;
133            }
134    
135            @Override
136            public long getAttachmentsFolderId() {
137                    if (_attachmentsFolderId !=
138                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
139    
140                            return _attachmentsFolderId;
141                    }
142    
143                    ServiceContext serviceContext = new ServiceContext();
144    
145                    serviceContext.setAddGroupPermissions(true);
146                    serviceContext.setAddGuestPermissions(true);
147    
148                    Repository repository =
149                            PortletFileRepositoryUtil.fetchPortletRepository(
150                                    getGroupId(), PortletKeys.WIKI);
151    
152                    long nodeAttachmentsFolderId = getNodeAttachmentsFolderId();
153    
154                    if ((repository == null) ||
155                            (nodeAttachmentsFolderId ==
156                                    DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {
157    
158                            return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
159                    }
160    
161                    try {
162                            Folder folder = PortletFileRepositoryUtil.getPortletFolder(
163                                    getUserId(), repository.getRepositoryId(),
164                                    nodeAttachmentsFolderId, String.valueOf(getResourcePrimKey()),
165                                    serviceContext);
166    
167                            _attachmentsFolderId = folder.getFolderId();
168                    }
169                    catch (Exception e) {
170                    }
171    
172                    return _attachmentsFolderId;
173            }
174    
175            @Override
176            public List<WikiPage> getChildPages() {
177                    try {
178                            return WikiPageLocalServiceUtil.getChildren(
179                                    getNodeId(), true, getTitle());
180                    }
181                    catch (Exception e) {
182                            _log.error(e, e);
183    
184                            return Collections.emptyList();
185                    }
186            }
187    
188            @Override
189            public List<FileEntry> getDeletedAttachmentsFileEntries() {
190                    return getDeletedAttachmentsFileEntries(
191                            QueryUtil.ALL_POS, QueryUtil.ALL_POS);
192            }
193    
194            @Override
195            public List<FileEntry> getDeletedAttachmentsFileEntries(
196                    int start, int end) {
197    
198                    List<FileEntry> fileEntries = new ArrayList<FileEntry>();
199    
200                    long attachmentsFolderId = getAttachmentsFolderId();
201    
202                    if (attachmentsFolderId != 0) {
203                            fileEntries = PortletFileRepositoryUtil.getPortletFileEntries(
204                                    getGroupId(), attachmentsFolderId,
205                                    WorkflowConstants.STATUS_IN_TRASH, start, end, null);
206                    }
207    
208                    return fileEntries;
209            }
210    
211            @Override
212            public int getDeletedAttachmentsFileEntriesCount() {
213                    int deletedAttachmentsFileEntriesCount = 0;
214    
215                    long attachmentsFolderId = getAttachmentsFolderId();
216    
217                    if (attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
218                            return PortletFileRepositoryUtil.getPortletFileEntriesCount(
219                                    getGroupId(), attachmentsFolderId,
220                                    WorkflowConstants.STATUS_IN_TRASH);
221                    }
222    
223                    return deletedAttachmentsFileEntriesCount;
224            }
225    
226            @Override
227            public WikiNode getNode() {
228                    try {
229                            return WikiNodeLocalServiceUtil.getNode(getNodeId());
230                    }
231                    catch (Exception e) {
232                            _log.error(e, e);
233    
234                            return new WikiNodeImpl();
235                    }
236            }
237    
238            @Override
239            public long getNodeAttachmentsFolderId() {
240                    WikiNode node = getNode();
241    
242                    return node.getAttachmentsFolderId();
243            }
244    
245            @Override
246            public WikiPage getParentPage() throws PortalException {
247                    if (Validator.isNull(getParentTitle())) {
248                            return null;
249                    }
250    
251                    return WikiPageLocalServiceUtil.getPage(getNodeId(), getParentTitle());
252            }
253    
254            @Override
255            public List<WikiPage> getParentPages() {
256                    List<WikiPage> parentPages = new ArrayList<WikiPage>();
257    
258                    WikiPage parentPage = fetchParentPage();
259    
260                    if (parentPage != null) {
261                            parentPages.addAll(parentPage.getParentPages());
262                            parentPages.add(parentPage);
263                    }
264    
265                    return parentPages;
266            }
267    
268            @Override
269            public WikiPage getRedirectPage() throws PortalException {
270                    if (Validator.isNull(getRedirectTitle())) {
271                            return null;
272                    }
273    
274                    return WikiPageLocalServiceUtil.getPage(
275                            getNodeId(), getRedirectTitle());
276            }
277    
278            @Override
279            public long getTrashEntryClassPK() {
280                    return getResourcePrimKey();
281            }
282    
283            @Override
284            public List<WikiPage> getViewableChildPages() {
285                    try {
286                            return WikiPageServiceUtil.getChildren(
287                                    getGroupId(), getNodeId(), true, getTitle());
288                    }
289                    catch (Exception e) {
290                            _log.error(e, e);
291    
292                            return Collections.emptyList();
293                    }
294            }
295    
296            @Override
297            public WikiPage getViewableParentPage() {
298                    if (Validator.isNull(getParentTitle())) {
299                            return null;
300                    }
301    
302                    try {
303                            return WikiPageServiceUtil.getPage(
304                                    getGroupId(), getNodeId(), getParentTitle());
305                    }
306                    catch (Exception e) {
307                            _log.error(e, e);
308    
309                            return null;
310                    }
311            }
312    
313            @Override
314            public List<WikiPage> getViewableParentPages() {
315                    List<WikiPage> pages = new ArrayList<WikiPage>();
316    
317                    WikiPage page = getViewableParentPage();
318    
319                    if (page != null) {
320                            pages.addAll(page.getViewableParentPages());
321                            pages.add(page);
322                    }
323    
324                    return pages;
325            }
326    
327            @Override
328            public boolean isResourceMain() {
329                    return isHead();
330            }
331    
332            @Override
333            public void setAttachmentsFolderId(long attachmentsFolderId) {
334                    _attachmentsFolderId = attachmentsFolderId;
335            }
336    
337            private static final Log _log = LogFactoryUtil.getLog(WikiPageImpl.class);
338    
339            private long _attachmentsFolderId;
340    
341    }