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