001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.engines.jspwiki;
016    
017    import com.ecyrd.jspwiki.QueryItem;
018    import com.ecyrd.jspwiki.WikiEngine;
019    import com.ecyrd.jspwiki.WikiPage;
020    import com.ecyrd.jspwiki.providers.ProviderException;
021    import com.ecyrd.jspwiki.providers.WikiPageProvider;
022    
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portlet.wiki.NoSuchPageException;
028    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
029    
030    import java.util.ArrayList;
031    import java.util.Arrays;
032    import java.util.Collection;
033    import java.util.Collections;
034    import java.util.Date;
035    import java.util.List;
036    import java.util.Properties;
037    
038    /**
039     * @author Jorge Ferrer
040     */
041    public class LiferayPageProvider implements WikiPageProvider {
042    
043            public static com.ecyrd.jspwiki.WikiPage toJSPWikiPage(
044                    com.liferay.portlet.wiki.model.WikiPage page, WikiEngine engine) {
045    
046                    com.ecyrd.jspwiki.WikiPage jspWikiPage = new com.ecyrd.jspwiki.WikiPage(
047                            engine, page.getTitle());
048    
049                    jspWikiPage.setAuthor(page.getUserName());
050                    jspWikiPage.setVersion((int)(page.getVersion() * 10));
051                    jspWikiPage.setLastModified(page.getCreateDate());
052    
053                    return jspWikiPage;
054            }
055    
056            @Override
057            public void deletePage(String name) {
058                    if (_log.isDebugEnabled()) {
059                            _log.debug("Invoking deletePage(" + name + ")");
060                    }
061            }
062    
063            @Override
064            public void deleteVersion(String title, int version) {
065                    if (_log.isDebugEnabled()) {
066                            _log.debug(
067                                    "Invoking deleteVersion(" + title + ", " + version + ")");
068                    }
069            }
070    
071            @Override
072            public Collection<WikiPage> findPages(QueryItem[] query) {
073                    if (_log.isDebugEnabled()) {
074                            _log.debug("Invoking findPages(" + Arrays.toString(query) + ")");
075                    }
076    
077                    return Collections.emptyList();
078            }
079    
080            @Override
081            public Collection<WikiPage> getAllChangedSince(Date date) {
082                    if (_log.isDebugEnabled()) {
083                            _log.debug("Invoking getAllChangedSince(" + date + ")");
084                    }
085    
086                    try {
087                            return getAllPages();
088                    }
089                    catch (ProviderException pe) {
090                            _log.error("Could not get changed pages", pe);
091    
092                            return Collections.emptyList();
093                    }
094            }
095    
096            @Override
097            public Collection<WikiPage> getAllPages() throws ProviderException {
098                    if (_log.isDebugEnabled()) {
099                            _log.debug("Invoking getAllPages()");
100                    }
101    
102                    List<WikiPage> jspWikiPages = new ArrayList<WikiPage>();
103    
104                    try {
105                            int count = WikiPageLocalServiceUtil.getPagesCount(_nodeId, true);
106    
107                            List<com.liferay.portlet.wiki.model.WikiPage> pages =
108                                    WikiPageLocalServiceUtil.getPages(_nodeId, true, 0, count);
109    
110                            for (com.liferay.portlet.wiki.model.WikiPage page : pages) {
111                                    jspWikiPages.add(toJSPWikiPage(page, _engine));
112                            }
113                    }
114                    catch (SystemException se) {
115                            throw new ProviderException(se.toString());
116                    }
117    
118                    return jspWikiPages;
119            }
120    
121            @Override
122            public int getPageCount() throws ProviderException {
123                    if (_log.isDebugEnabled()) {
124                            _log.debug("Invoking getPageCount()");
125                    }
126    
127                    try {
128                            return WikiPageLocalServiceUtil.getPagesCount(_nodeId);
129                    }
130                    catch (SystemException se) {
131                            throw new ProviderException(se.toString());
132                    }
133            }
134    
135            @Override
136            public com.ecyrd.jspwiki.WikiPage getPageInfo(String title, int version)
137                    throws ProviderException {
138    
139                    if (_log.isDebugEnabled()) {
140                            _log.debug("Invoking getPageInfo(" + title + ", " + version + ")");
141                    }
142    
143                    try {
144                            com.liferay.portlet.wiki.model.WikiPage page =
145                                    WikiPageLocalServiceUtil.getPage(_nodeId, title);
146    
147                            return toJSPWikiPage(page, _engine);
148                    }
149                    catch (NoSuchPageException nspe) {
150                            return null;
151                    }
152                    catch (Exception e) {
153                            throw new ProviderException(e.toString());
154                    }
155            }
156    
157            @Override
158            public String getPageText(String title, int version)
159                    throws ProviderException {
160    
161                    if (_log.isDebugEnabled()) {
162                            _log.debug("Invoking getPageText(" + title + ", " + version + ")");
163                    }
164    
165                    try {
166                            com.liferay.portlet.wiki.model.WikiPage page =
167                                    WikiPageLocalServiceUtil.getPage(_nodeId, title);
168    
169                            return page.getContent();
170                    }
171                    catch (Exception e) {
172                            throw new ProviderException(e.toString());
173                    }
174            }
175    
176            @Override
177            public String getProviderInfo() {
178                    if (_log.isDebugEnabled()) {
179                            _log.debug("Invoking getProviderInfo()");
180                    }
181    
182                    return LiferayPageProvider.class.getName();
183            }
184    
185            @Override
186            public List<WikiPage> getVersionHistory(String title) {
187                    if (_log.isDebugEnabled()) {
188                            _log.debug("Invoking getVersionHistory(" + title + ")");
189                    }
190    
191                    return Collections.emptyList();
192            }
193    
194            @Override
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            @Override
205            public void movePage(String from, String to) {
206                    if (_log.isDebugEnabled()) {
207                            _log.debug("Invoking movePage(" + from + ", " + to + ")");
208                    }
209            }
210    
211            @Override
212            public boolean pageExists(String title) {
213                    if (_log.isDebugEnabled()) {
214                            _log.debug("Invoking pageExists(" + title + ")");
215                    }
216    
217                    try {
218                            int count = WikiPageLocalServiceUtil.getPagesCount(
219                                    _nodeId, JSPWikiEngine.decodeJSPWikiName(title), true);
220    
221                            if (count > 0) {
222                                    return true;
223                            }
224                            else {
225                                    return false;
226                            }
227                    }
228                    catch (Exception e) {
229                            _log.error(e, e);
230                    }
231    
232                    return false;
233            }
234    
235            @Override
236            public void putPageText(com.ecyrd.jspwiki.WikiPage page, String text) {
237                    if (_log.isDebugEnabled()) {
238                            _log.debug("Invoking putPageText(" + page + ", " + text + ")");
239                    }
240            }
241    
242            private static Log _log = LogFactoryUtil.getLog(LiferayPageProvider.class);
243    
244            private WikiEngine _engine;
245            private long _nodeId;
246    
247    }