1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wiki.model.impl;
24  
25  import com.liferay.documentlibrary.NoSuchDirectoryException;
26  import com.liferay.documentlibrary.service.DLServiceUtil;
27  import com.liferay.portal.PortalException;
28  import com.liferay.portal.SystemException;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.CompanyConstants;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.PropsKeys;
35  import com.liferay.portal.util.PropsUtil;
36  import com.liferay.portlet.expando.model.ExpandoBridge;
37  import com.liferay.portlet.expando.model.impl.ExpandoBridgeImpl;
38  import com.liferay.portlet.wiki.model.WikiNode;
39  import com.liferay.portlet.wiki.model.WikiPage;
40  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
41  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
42  
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  /**
47   * <a href="WikiPageImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Jorge Ferrer
51   *
52   */
53  public class WikiPageImpl extends WikiPageModelImpl implements WikiPage {
54  
55      public static final String DEFAULT_FORMAT =
56          PropsUtil.get(PropsKeys.WIKI_FORMATS_DEFAULT);
57  
58      public static final double DEFAULT_VERSION = 1.0;
59  
60      public static final String[] FORMATS =
61          PropsUtil.getArray(PropsKeys.WIKI_FORMATS);
62  
63      public static final String FRONT_PAGE =
64          PropsUtil.get(PropsKeys.WIKI_FRONT_PAGE_NAME);
65  
66      public static final String MOVED = "Moved";
67  
68      public static final String NEW = "New";
69  
70      public static final String REVERTED = "Reverted";
71  
72      public WikiPageImpl() {
73      }
74  
75      public String getAttachmentsDir() {
76          if (_attachmentDirs == null) {
77              _attachmentDirs = "wiki/" + getResourcePrimKey();
78          }
79  
80          return _attachmentDirs;
81      }
82  
83      public String[] getAttachmentsFiles()
84          throws PortalException, SystemException {
85  
86          String[] fileNames = new String[0];
87  
88          try {
89              fileNames = DLServiceUtil.getFileNames(
90                  getCompanyId(), CompanyConstants.SYSTEM, getAttachmentsDir());
91          }
92          catch (NoSuchDirectoryException nsde) {
93          }
94  
95          return fileNames;
96      }
97  
98      public List<WikiPage> getChildPages() {
99          List<WikiPage> pages = null;
100 
101         try {
102             pages = WikiPageLocalServiceUtil.getChildren(
103                 getNodeId(), true, getTitle());
104         }
105         catch (Exception e) {
106             pages = new ArrayList<WikiPage>();
107 
108             _log.error(e);
109         }
110 
111         return pages;
112     }
113 
114     public ExpandoBridge getExpandoBridge() {
115         if (_expandoBridge == null) {
116             _expandoBridge = new ExpandoBridgeImpl(
117                 WikiPage.class.getName(), getResourcePrimKey());
118         }
119 
120         return _expandoBridge;
121     }
122 
123     public WikiNode getNode() {
124         WikiNode node = null;
125 
126         try {
127             node = WikiNodeLocalServiceUtil.getNode(getNodeId());
128         }
129         catch (Exception e) {
130             node = new WikiNodeImpl();
131 
132             _log.error(e);
133         }
134 
135         return node;
136     }
137 
138     public WikiPage getParentPage() {
139         if (Validator.isNull(getParentTitle())) {
140             return null;
141         }
142 
143         WikiPage page = null;
144 
145         try {
146             page = WikiPageLocalServiceUtil.getPage(
147                 getNodeId(), getParentTitle());
148         }
149         catch (Exception e) {
150             _log.error(e);
151         }
152 
153         return page;
154     }
155 
156     public List<WikiPage> getParentPages() {
157         List<WikiPage> parentPages = new ArrayList<WikiPage>();
158 
159         WikiPage parentPage = getParentPage();
160 
161         if (parentPage != null) {
162             parentPages.addAll(parentPage.getParentPages());
163             parentPages.add(parentPage);
164         }
165 
166         return parentPages;
167     }
168 
169     public WikiPage getRedirectPage() {
170         if (Validator.isNull(getRedirectTitle())) {
171             return null;
172         }
173 
174         WikiPage page = null;
175 
176         try {
177             page = WikiPageLocalServiceUtil.getPage(
178                 getNodeId(), getRedirectTitle());
179         }
180         catch (Exception e) {
181             _log.error(e);
182         }
183 
184         return page;
185     }
186 
187     public String getUserUuid() throws SystemException {
188         return PortalUtil.getUserValue(getUserId(), "uuid", _userUuid);
189     }
190 
191     public void setAttachmentsDir(String attachmentsDir) {
192         _attachmentDirs = attachmentsDir;
193     }
194 
195     public void setUserUuid(String userUuid) {
196         _userUuid = userUuid;
197     }
198 
199     private static Log _log = LogFactoryUtil.getLog(WikiPageImpl.class);
200 
201     private String _attachmentDirs;
202     private ExpandoBridge _expandoBridge;
203     private String _userUuid;
204 
205 }