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