001    /**
002     * Copyright (c) 2000-2012 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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.CompanyConstants;
023    import com.liferay.portlet.documentlibrary.NoSuchDirectoryException;
024    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
025    import com.liferay.portlet.trash.util.TrashUtil;
026    import com.liferay.portlet.wiki.model.WikiNode;
027    import com.liferay.portlet.wiki.model.WikiPage;
028    import com.liferay.portlet.wiki.model.WikiPageConstants;
029    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
030    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
031    
032    import java.util.ArrayList;
033    import java.util.List;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Jorge Ferrer
038     */
039    public class WikiPageImpl extends WikiPageBaseImpl {
040    
041            public WikiPageImpl() {
042            }
043    
044            public String getAttachmentsDir() {
045                    if (_attachmentDirs == null) {
046                            _attachmentDirs =
047                                    WikiPageConstants.BASE_ATTACHMENTS_DIR + getResourcePrimKey();
048                    }
049    
050                    return _attachmentDirs;
051            }
052    
053            public String[] getAttachmentsFiles()
054                    throws PortalException, SystemException {
055    
056                    String[] fileNames = new String[0];
057    
058                    try {
059                            fileNames = DLStoreUtil.getFileNames(
060                                    getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
061                    }
062                    catch (NoSuchDirectoryException nsde) {
063                    }
064    
065                    return fileNames;
066            }
067    
068            public List<WikiPage> getChildPages() {
069                    List<WikiPage> pages = null;
070    
071                    try {
072                            pages = WikiPageLocalServiceUtil.getChildren(
073                                    getNodeId(), true, getTitle());
074                    }
075                    catch (Exception e) {
076                            pages = new ArrayList<WikiPage>();
077    
078                            _log.error(e);
079                    }
080    
081                    return pages;
082            }
083    
084            public String getDeletedAttachmentsDir() {
085                    if (_deletedAttachmentDirs == null) {
086                            _deletedAttachmentDirs =
087                                    WikiPageConstants.BASE_ATTACHMENTS_DIR +
088                                            TrashUtil.TRASH_ATTACHMENTS_DIR + getResourcePrimKey();
089                    }
090    
091                    return _deletedAttachmentDirs;
092            }
093    
094            public String[] getDeletedAttachmentsFiles()
095                    throws PortalException, SystemException {
096    
097                    String[] fileNames = new String[0];
098    
099                    try {
100                            fileNames = DLStoreUtil.getFileNames(
101                                    getCompanyId(), CompanyConstants.SYSTEM,
102                                    getDeletedAttachmentsDir());
103                    }
104                    catch (NoSuchDirectoryException nsde) {
105                    }
106    
107                    return fileNames;
108            }
109    
110            public WikiNode getNode() {
111                    WikiNode node = null;
112    
113                    try {
114                            node = WikiNodeLocalServiceUtil.getNode(getNodeId());
115                    }
116                    catch (Exception e) {
117                            node = new WikiNodeImpl();
118    
119                            _log.error(e);
120                    }
121    
122                    return node;
123            }
124    
125            public WikiPage getParentPage() {
126                    if (Validator.isNull(getParentTitle())) {
127                            return null;
128                    }
129    
130                    WikiPage page = null;
131    
132                    try {
133                            page = WikiPageLocalServiceUtil.getPage(
134                                    getNodeId(), getParentTitle());
135                    }
136                    catch (Exception e) {
137                            _log.error(e);
138                    }
139    
140                    return page;
141            }
142    
143            public List<WikiPage> getParentPages() {
144                    List<WikiPage> parentPages = new ArrayList<WikiPage>();
145    
146                    WikiPage parentPage = getParentPage();
147    
148                    if (parentPage != null) {
149                            parentPages.addAll(parentPage.getParentPages());
150                            parentPages.add(parentPage);
151                    }
152    
153                    return parentPages;
154            }
155    
156            public WikiPage getRedirectPage() {
157                    if (Validator.isNull(getRedirectTitle())) {
158                            return null;
159                    }
160    
161                    WikiPage page = null;
162    
163                    try {
164                            page = WikiPageLocalServiceUtil.getPage(
165                                    getNodeId(), getRedirectTitle());
166                    }
167                    catch (Exception e) {
168                            _log.error(e);
169                    }
170    
171                    return page;
172            }
173    
174            public boolean isInTrashFolder() {
175                    WikiNode node = getNode();
176    
177                    if (node != null) {
178                            return node.isInTrash();
179                    }
180    
181                    return false;
182            }
183    
184            @Override
185            public boolean isResourceMain() {
186                    return isHead();
187            }
188    
189            public void setAttachmentsDir(String attachmentsDir) {
190                    _attachmentDirs = attachmentsDir;
191            }
192    
193            public void setDeletedAttachmentsDir(String deletedAttachmentsDir) {
194                    _deletedAttachmentDirs = deletedAttachmentsDir;
195            }
196    
197            private static Log _log = LogFactoryUtil.getLog(WikiPageImpl.class);
198    
199            private String _attachmentDirs;
200            private String _deletedAttachmentDirs;
201    
202    }