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