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