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