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