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