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.kernel.workflow.StatusConstants;
25  import com.liferay.portal.model.CompanyConstants;
26  import com.liferay.portlet.wiki.model.WikiNode;
27  import com.liferay.portlet.wiki.model.WikiPage;
28  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
29  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * <a href="WikiPageImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Jorge Ferrer
39   */
40  public class WikiPageImpl extends WikiPageModelImpl implements WikiPage {
41  
42      public WikiPageImpl() {
43      }
44  
45      public String getAttachmentsDir() {
46          if (_attachmentDirs == null) {
47              _attachmentDirs = "wiki/" + getResourcePrimKey();
48          }
49  
50          return _attachmentDirs;
51      }
52  
53      public String[] getAttachmentsFiles()
54          throws PortalException, SystemException {
55  
56          String[] fileNames = new String[0];
57  
58          try {
59              fileNames = DLServiceUtil.getFileNames(
60                  getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
61          }
62          catch (NoSuchDirectoryException nsde) {
63          }
64  
65          return fileNames;
66      }
67  
68      public List<WikiPage> getChildPages() {
69          List<WikiPage> pages = null;
70  
71          try {
72              pages = WikiPageLocalServiceUtil.getChildren(
73                  getNodeId(), true, getTitle());
74          }
75          catch (Exception e) {
76              pages = new ArrayList<WikiPage>();
77  
78              _log.error(e);
79          }
80  
81          return pages;
82      }
83  
84      public WikiNode getNode() {
85          WikiNode node = null;
86  
87          try {
88              node = WikiNodeLocalServiceUtil.getNode(getNodeId());
89          }
90          catch (Exception e) {
91              node = new WikiNodeImpl();
92  
93              _log.error(e);
94          }
95  
96          return node;
97      }
98  
99      public WikiPage getParentPage() {
100         if (Validator.isNull(getParentTitle())) {
101             return null;
102         }
103 
104         WikiPage page = null;
105 
106         try {
107             page = WikiPageLocalServiceUtil.getPage(
108                 getNodeId(), getParentTitle());
109         }
110         catch (Exception e) {
111             _log.error(e);
112         }
113 
114         return page;
115     }
116 
117     public List<WikiPage> getParentPages() {
118         List<WikiPage> parentPages = new ArrayList<WikiPage>();
119 
120         WikiPage parentPage = getParentPage();
121 
122         if (parentPage != null) {
123             parentPages.addAll(parentPage.getParentPages());
124             parentPages.add(parentPage);
125         }
126 
127         return parentPages;
128     }
129 
130     public WikiPage getRedirectPage() {
131         if (Validator.isNull(getRedirectTitle())) {
132             return null;
133         }
134 
135         WikiPage page = null;
136 
137         try {
138             page = WikiPageLocalServiceUtil.getPage(
139                 getNodeId(), getRedirectTitle());
140         }
141         catch (Exception e) {
142             _log.error(e);
143         }
144 
145         return page;
146     }
147 
148     public boolean isApproved() {
149         if (getStatus() == StatusConstants.APPROVED) {
150             return true;
151         }
152         else {
153             return false;
154         }
155     }
156 
157     public void setAttachmentsDir(String attachmentsDir) {
158         _attachmentDirs = attachmentsDir;
159     }
160 
161     private static Log _log = LogFactoryUtil.getLog(WikiPageImpl.class);
162 
163     private String _attachmentDirs;
164 
165 }