001    /**
002     * Copyright (c) 2000-2010 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.documentlibrary.NoSuchDirectoryException;
018    import com.liferay.documentlibrary.service.DLServiceUtil;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.exception.SystemException;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.CompanyConstants;
025    import com.liferay.portlet.wiki.model.WikiNode;
026    import com.liferay.portlet.wiki.model.WikiPage;
027    import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
028    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Jorge Ferrer
036     */
037    public class WikiPageImpl extends WikiPageModelImpl implements WikiPage {
038    
039            public WikiPageImpl() {
040            }
041    
042            public String getAttachmentsDir() {
043                    if (_attachmentDirs == null) {
044                            _attachmentDirs = "wiki/" + getResourcePrimKey();
045                    }
046    
047                    return _attachmentDirs;
048            }
049    
050            public String[] getAttachmentsFiles()
051                    throws PortalException, SystemException {
052    
053                    String[] fileNames = new String[0];
054    
055                    try {
056                            fileNames = DLServiceUtil.getFileNames(
057                                    getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
058                    }
059                    catch (NoSuchDirectoryException nsde) {
060                    }
061    
062                    return fileNames;
063            }
064    
065            public List<WikiPage> getChildPages() {
066                    List<WikiPage> pages = null;
067    
068                    try {
069                            pages = WikiPageLocalServiceUtil.getChildren(
070                                    getNodeId(), true, getTitle());
071                    }
072                    catch (Exception e) {
073                            pages = new ArrayList<WikiPage>();
074    
075                            _log.error(e);
076                    }
077    
078                    return pages;
079            }
080    
081            public WikiNode getNode() {
082                    WikiNode node = null;
083    
084                    try {
085                            node = WikiNodeLocalServiceUtil.getNode(getNodeId());
086                    }
087                    catch (Exception e) {
088                            node = new WikiNodeImpl();
089    
090                            _log.error(e);
091                    }
092    
093                    return node;
094            }
095    
096            public WikiPage getParentPage() {
097                    if (Validator.isNull(getParentTitle())) {
098                            return null;
099                    }
100    
101                    WikiPage page = null;
102    
103                    try {
104                            page = WikiPageLocalServiceUtil.getPage(
105                                    getNodeId(), getParentTitle());
106                    }
107                    catch (Exception e) {
108                            _log.error(e);
109                    }
110    
111                    return page;
112            }
113    
114            public List<WikiPage> getParentPages() {
115                    List<WikiPage> parentPages = new ArrayList<WikiPage>();
116    
117                    WikiPage parentPage = getParentPage();
118    
119                    if (parentPage != null) {
120                            parentPages.addAll(parentPage.getParentPages());
121                            parentPages.add(parentPage);
122                    }
123    
124                    return parentPages;
125            }
126    
127            public WikiPage getRedirectPage() {
128                    if (Validator.isNull(getRedirectTitle())) {
129                            return null;
130                    }
131    
132                    WikiPage page = null;
133    
134                    try {
135                            page = WikiPageLocalServiceUtil.getPage(
136                                    getNodeId(), getRedirectTitle());
137                    }
138                    catch (Exception e) {
139                            _log.error(e);
140                    }
141    
142                    return page;
143            }
144    
145            public void setAttachmentsDir(String attachmentsDir) {
146                    _attachmentDirs = attachmentsDir;
147            }
148    
149            private static Log _log = LogFactoryUtil.getLog(WikiPageImpl.class);
150    
151            private String _attachmentDirs;
152    
153    }