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.engines.jspwiki;
24  
25  import com.ecyrd.jspwiki.QueryItem;
26  import com.ecyrd.jspwiki.WikiEngine;
27  import com.ecyrd.jspwiki.WikiPage;
28  import com.ecyrd.jspwiki.providers.ProviderException;
29  import com.ecyrd.jspwiki.providers.WikiPageProvider;
30  
31  import com.liferay.portal.SystemException;
32  import com.liferay.portal.kernel.log.Log;
33  import com.liferay.portal.kernel.log.LogFactoryUtil;
34  import com.liferay.portal.kernel.util.GetterUtil;
35  import com.liferay.portlet.wiki.NoSuchPageException;
36  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
37  
38  import java.util.ArrayList;
39  import java.util.Collection;
40  import java.util.Collections;
41  import java.util.Date;
42  import java.util.List;
43  import java.util.Properties;
44  
45  /**
46   * <a href="LiferayPageProvider.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Jorge Ferrer
49   */
50  public class LiferayPageProvider implements WikiPageProvider {
51  
52      public static com.ecyrd.jspwiki.WikiPage toJSPWikiPage(
53          com.liferay.portlet.wiki.model.WikiPage page, WikiEngine engine) {
54  
55          com.ecyrd.jspwiki.WikiPage jspWikiPage = new com.ecyrd.jspwiki.WikiPage(
56              engine, page.getTitle());
57  
58          jspWikiPage.setAuthor(page.getUserName());
59          jspWikiPage.setVersion((int)(page.getVersion() * 10));
60          jspWikiPage.setLastModified(page.getCreateDate());
61  
62          return jspWikiPage;
63      }
64  
65      public void deletePage(String name) {
66          if (_log.isDebugEnabled()) {
67              _log.debug("Invoking deletePage(" + name + ")");
68          }
69      }
70  
71      public void deleteVersion(String title, int version) {
72          if (_log.isDebugEnabled()) {
73              _log.debug(
74                  "Invoking deleteVersion(" + title + ", " + version + ")");
75          }
76      }
77  
78      public Collection<WikiPage> findPages(QueryItem[] query) {
79          if (_log.isDebugEnabled()) {
80              _log.debug("Invoking findPages(" + query + ")");
81          }
82  
83          return Collections.EMPTY_LIST;
84      }
85  
86      public Collection<WikiPage> getAllChangedSince(Date date) {
87          if (_log.isDebugEnabled()) {
88              _log.debug("Invoking getAllChangedSince(" + date + ")");
89          }
90  
91          try {
92              return getAllPages();
93          }
94          catch (ProviderException e) {
95              _log.error("Could not get changed pages", e);
96  
97              return Collections.EMPTY_LIST;
98          }
99      }
100 
101     public Collection<WikiPage> getAllPages() throws ProviderException {
102         if (_log.isDebugEnabled()) {
103             _log.debug("Invoking getAllPages()");
104         }
105 
106         List<WikiPage> jspWikiPages = new ArrayList<WikiPage>();
107 
108         try {
109             int count = WikiPageLocalServiceUtil.getPagesCount(_nodeId, true);
110 
111             List<com.liferay.portlet.wiki.model.WikiPage> pages =
112                 WikiPageLocalServiceUtil.getPages(_nodeId, true, 0, count);
113 
114             for (com.liferay.portlet.wiki.model.WikiPage page : pages) {
115                 jspWikiPages.add(toJSPWikiPage(page, _engine));
116             }
117         }
118         catch (SystemException se) {
119             throw new ProviderException(se.toString());
120         }
121 
122         return jspWikiPages;
123     }
124 
125     public int getPageCount() throws ProviderException {
126         if (_log.isDebugEnabled()) {
127             _log.debug("Invoking getPageCount()");
128         }
129 
130         try {
131             return WikiPageLocalServiceUtil.getPagesCount(_nodeId);
132         }
133         catch (SystemException se) {
134             throw new ProviderException(se.toString());
135         }
136     }
137 
138     public com.ecyrd.jspwiki.WikiPage getPageInfo(String title, int version)
139         throws ProviderException {
140 
141         if (_log.isDebugEnabled()) {
142             _log.debug("Invoking getPageInfo(" + title + ", " + version + ")");
143         }
144 
145         try {
146             com.liferay.portlet.wiki.model.WikiPage page =
147                 WikiPageLocalServiceUtil.getPage(_nodeId, title);
148 
149             return toJSPWikiPage(page, _engine);
150         }
151         catch (NoSuchPageException nspe) {
152             return null;
153         }
154         catch (Exception e) {
155             throw new ProviderException(e.toString());
156         }
157     }
158 
159     public String getPageText(String title, int version)
160         throws ProviderException {
161 
162         if (_log.isDebugEnabled()) {
163             _log.debug("Invoking getPageText(" + title + ", " + version + ")");
164         }
165 
166         try {
167             com.liferay.portlet.wiki.model.WikiPage page =
168                 WikiPageLocalServiceUtil.getPage(_nodeId, title);
169 
170             return page.getContent();
171         }
172         catch (Exception e) {
173             throw new ProviderException(e.toString());
174         }
175     }
176 
177     public String getProviderInfo() {
178         if (_log.isDebugEnabled()) {
179             _log.debug("Invoking getProviderInfo()");
180         }
181 
182         return LiferayPageProvider.class.getName();
183     }
184 
185     public List<WikiPage> getVersionHistory(String title) {
186         if (_log.isDebugEnabled()) {
187             _log.debug("Invoking getVersionHistory(" + title + ")");
188         }
189 
190         return Collections.EMPTY_LIST;
191     }
192 
193     public void initialize(WikiEngine engine, Properties props) {
194         if (_log.isDebugEnabled()) {
195             _log.debug("Invoking initialize(" + engine + ", " + props + ")");
196         }
197 
198         _engine = engine;
199         _nodeId = GetterUtil.getLong(props.getProperty("nodeId"));
200     }
201 
202     public void movePage(String from, String to) {
203         if (_log.isDebugEnabled()) {
204             _log.debug("Invoking movePage(" + from + ", " + to + ")");
205         }
206     }
207 
208     public boolean pageExists(String title) {
209         if (_log.isDebugEnabled()) {
210             _log.debug("Invoking pageExists(" + title + ")");
211         }
212 
213         try {
214             if (WikiPageLocalServiceUtil.getPagesCount(
215                     _nodeId, title, true) > 0) {
216 
217                 return true;
218             }
219             else {
220                 return false;
221             }
222         }
223         catch (Exception e) {
224             _log.error(e, e);
225         }
226 
227         return false;
228     }
229 
230     public void putPageText(com.ecyrd.jspwiki.WikiPage page, String text) {
231         if (_log.isDebugEnabled()) {
232             _log.debug("Invoking putPageText(" + page + ", " + text + ")");
233         }
234     }
235 
236     private static Log _log = LogFactoryUtil.getLog(LiferayPageProvider.class);
237 
238     private WikiEngine _engine;
239     private long _nodeId;
240 
241 }