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.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.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    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
030    
031    import java.util.ArrayList;
032    import java.util.Collections;
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            @Override
045            public String getAttachmentsDir() {
046                    if (_attachmentDirs == null) {
047                            _attachmentDirs = "wiki/" + getResourcePrimKey();
048                    }
049    
050                    return _attachmentDirs;
051            }
052    
053            @Override
054            public String[] getAttachmentsFiles()
055                    throws PortalException, SystemException {
056    
057                    String[] fileNames = new String[0];
058    
059                    try {
060                            fileNames = DLStoreUtil.getFileNames(
061                                    getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
062                    }
063                    catch (NoSuchDirectoryException nsde) {
064                    }
065    
066                    return fileNames;
067            }
068    
069            @Override
070            public List<WikiPage> getChildPages() {
071                    try {
072                            return WikiPageLocalServiceUtil.getChildren(
073                                    getNodeId(), true, getTitle());
074                    }
075                    catch (Exception e) {
076                            _log.error(e, e);
077    
078                            return Collections.emptyList();
079                    }
080            }
081    
082            @Override
083            public WikiNode getNode() {
084                    try {
085                            return WikiNodeLocalServiceUtil.getNode(getNodeId());
086                    }
087                    catch (Exception e) {
088                            _log.error(e, e);
089    
090                            return new WikiNodeImpl();
091                    }
092            }
093    
094            @Override
095            public WikiPage getParentPage() {
096                    if (Validator.isNull(getParentTitle())) {
097                            return null;
098                    }
099    
100                    try {
101                            return WikiPageLocalServiceUtil.getPage(
102                                    getNodeId(), getParentTitle());
103                    }
104                    catch (Exception e) {
105                            _log.error(e, e);
106    
107                            return null;
108                    }
109            }
110    
111            @Override
112            public List<WikiPage> getParentPages() {
113                    List<WikiPage> parentPages = new ArrayList<WikiPage>();
114    
115                    WikiPage parentPage = getParentPage();
116    
117                    if (parentPage != null) {
118                            parentPages.addAll(parentPage.getParentPages());
119                            parentPages.add(parentPage);
120                    }
121    
122                    return parentPages;
123            }
124    
125            @Override
126            public WikiPage getRedirectPage() {
127                    if (Validator.isNull(getRedirectTitle())) {
128                            return null;
129                    }
130    
131                    try {
132                            return WikiPageLocalServiceUtil.getPage(
133                                    getNodeId(), getRedirectTitle());
134                    }
135                    catch (Exception e) {
136                            _log.error(e, e);
137    
138                            return null;
139                    }
140            }
141    
142            @Override
143            public List<WikiPage> getViewableChildPages() {
144                    try {
145                            return WikiPageServiceUtil.getChildren(
146                                    getGroupId(), getNodeId(), true, getTitle());
147                    }
148                    catch (Exception e) {
149                            _log.error(e, e);
150    
151                            return Collections.emptyList();
152                    }
153            }
154    
155            @Override
156            public WikiPage getViewableParentPage() {
157                    if (Validator.isNull(getParentTitle())) {
158                            return null;
159                    }
160    
161                    try {
162                            return WikiPageServiceUtil.getPage(
163                                    getGroupId(), getNodeId(), getParentTitle());
164                    }
165                    catch (Exception e) {
166                            _log.error(e, e);
167    
168                            return null;
169                    }
170            }
171    
172            @Override
173            public List<WikiPage> getViewableParentPages() {
174                    List<WikiPage> pages = new ArrayList<WikiPage>();
175    
176                    WikiPage page = getViewableParentPage();
177    
178                    if (page != null) {
179                            pages.addAll(page.getViewableParentPages());
180                            pages.add(page);
181                    }
182    
183                    return pages;
184            }
185    
186            @Override
187            public boolean isResourceMain() {
188                    return isHead();
189            }
190    
191            @Override
192            public void setAttachmentsDir(String attachmentsDir) {
193                    _attachmentDirs = attachmentsDir;
194            }
195    
196            private static Log _log = LogFactoryUtil.getLog(WikiPageImpl.class);
197    
198            private String _attachmentDirs;
199    
200    }