1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.model.impl;
16  
17  import com.liferay.documentlibrary.NoSuchDirectoryException;
18  import com.liferay.documentlibrary.service.DLServiceUtil;
19  import com.liferay.portal.kernel.exception.PortalException;
20  import com.liferay.portal.kernel.exception.SystemException;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.CompanyConstants;
25  import com.liferay.portlet.wiki.model.WikiNode;
26  import com.liferay.portlet.wiki.model.WikiPage;
27  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
28  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * <a href="WikiPageImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Jorge Ferrer
38   */
39  public class WikiPageImpl extends WikiPageModelImpl implements WikiPage {
40  
41      public WikiPageImpl() {
42      }
43  
44      public String getAttachmentsDir() {
45          if (_attachmentDirs == null) {
46              _attachmentDirs = "wiki/" + getResourcePrimKey();
47          }
48  
49          return _attachmentDirs;
50      }
51  
52      public String[] getAttachmentsFiles()
53          throws PortalException, SystemException {
54  
55          String[] fileNames = new String[0];
56  
57          try {
58              fileNames = DLServiceUtil.getFileNames(
59                  getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
60          }
61          catch (NoSuchDirectoryException nsde) {
62          }
63  
64          return fileNames;
65      }
66  
67      public List<WikiPage> getChildPages() {
68          List<WikiPage> pages = null;
69  
70          try {
71              pages = WikiPageLocalServiceUtil.getChildren(
72                  getNodeId(), true, getTitle());
73          }
74          catch (Exception e) {
75              pages = new ArrayList<WikiPage>();
76  
77              _log.error(e);
78          }
79  
80          return pages;
81      }
82  
83      public WikiNode getNode() {
84          WikiNode node = null;
85  
86          try {
87              node = WikiNodeLocalServiceUtil.getNode(getNodeId());
88          }
89          catch (Exception e) {
90              node = new WikiNodeImpl();
91  
92              _log.error(e);
93          }
94  
95          return node;
96      }
97  
98      public WikiPage getParentPage() {
99          if (Validator.isNull(getParentTitle())) {
100             return null;
101         }
102 
103         WikiPage page = null;
104 
105         try {
106             page = WikiPageLocalServiceUtil.getPage(
107                 getNodeId(), getParentTitle());
108         }
109         catch (Exception e) {
110             _log.error(e);
111         }
112 
113         return page;
114     }
115 
116     public List<WikiPage> getParentPages() {
117         List<WikiPage> parentPages = new ArrayList<WikiPage>();
118 
119         WikiPage parentPage = getParentPage();
120 
121         if (parentPage != null) {
122             parentPages.addAll(parentPage.getParentPages());
123             parentPages.add(parentPage);
124         }
125 
126         return parentPages;
127     }
128 
129     public WikiPage getRedirectPage() {
130         if (Validator.isNull(getRedirectTitle())) {
131             return null;
132         }
133 
134         WikiPage page = null;
135 
136         try {
137             page = WikiPageLocalServiceUtil.getPage(
138                 getNodeId(), getRedirectTitle());
139         }
140         catch (Exception e) {
141             _log.error(e);
142         }
143 
144         return page;
145     }
146 
147     public void setAttachmentsDir(String attachmentsDir) {
148         _attachmentDirs = attachmentsDir;
149     }
150 
151     private static Log _log = LogFactoryUtil.getLog(WikiPageImpl.class);
152 
153     private String _attachmentDirs;
154 
155 }