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