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