001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    import com.liferay.portlet.wiki.util.WikiUtil;
030    
031    import java.util.ArrayList;
032    import java.util.Arrays;
033    import java.util.Collection;
034    import java.util.Collections;
035    import java.util.Date;
036    import java.util.List;
037    import java.util.Properties;
038    
039    /**
040     * @author Jorge Ferrer
041     */
042    public class LiferayPageProvider implements WikiPageProvider {
043    
044            public static com.ecyrd.jspwiki.WikiPage toJSPWikiPage(
045                    com.liferay.portlet.wiki.model.WikiPage page, WikiEngine engine) {
046    
047                    com.ecyrd.jspwiki.WikiPage jspWikiPage = new com.ecyrd.jspwiki.WikiPage(
048                            engine, page.getTitle());
049    
050                    jspWikiPage.setAuthor(page.getUserName());
051                    jspWikiPage.setVersion((int)(page.getVersion() * 10));
052                    jspWikiPage.setLastModified(page.getCreateDate());
053    
054                    return jspWikiPage;
055            }
056    
057            public void deletePage(String name) {
058                    if (_log.isDebugEnabled()) {
059                            _log.debug("Invoking deletePage(" + name + ")");
060                    }
061            }
062    
063            public void deleteVersion(String title, int version) {
064                    if (_log.isDebugEnabled()) {
065                            _log.debug(
066                                    "Invoking deleteVersion(" + title + ", " + version + ")");
067                    }
068            }
069    
070            public Collection<WikiPage> findPages(QueryItem[] query) {
071                    if (_log.isDebugEnabled()) {
072                            _log.debug("Invoking findPages(" + Arrays.toString(query) + ")");
073                    }
074    
075                    return Collections.EMPTY_LIST;
076            }
077    
078            public Collection<WikiPage> getAllChangedSince(Date date) {
079                    if (_log.isDebugEnabled()) {
080                            _log.debug("Invoking getAllChangedSince(" + date + ")");
081                    }
082    
083                    try {
084                            return getAllPages();
085                    }
086                    catch (ProviderException e) {
087                            _log.error("Could not get changed pages", e);
088    
089                            return Collections.EMPTY_LIST;
090                    }
091            }
092    
093            public Collection<WikiPage> getAllPages() throws ProviderException {
094                    if (_log.isDebugEnabled()) {
095                            _log.debug("Invoking getAllPages()");
096                    }
097    
098                    List<WikiPage> jspWikiPages = new ArrayList<WikiPage>();
099    
100                    try {
101                            int count = WikiPageLocalServiceUtil.getPagesCount(_nodeId, true);
102    
103                            List<com.liferay.portlet.wiki.model.WikiPage> pages =
104                                    WikiPageLocalServiceUtil.getPages(_nodeId, true, 0, count);
105    
106                            for (com.liferay.portlet.wiki.model.WikiPage page : pages) {
107                                    jspWikiPages.add(toJSPWikiPage(page, _engine));
108                            }
109                    }
110                    catch (SystemException se) {
111                            throw new ProviderException(se.toString());
112                    }
113    
114                    return jspWikiPages;
115            }
116    
117            public int getPageCount() throws ProviderException {
118                    if (_log.isDebugEnabled()) {
119                            _log.debug("Invoking getPageCount()");
120                    }
121    
122                    try {
123                            return WikiPageLocalServiceUtil.getPagesCount(_nodeId);
124                    }
125                    catch (SystemException se) {
126                            throw new ProviderException(se.toString());
127                    }
128            }
129    
130            public com.ecyrd.jspwiki.WikiPage getPageInfo(String title, int version)
131                    throws ProviderException {
132    
133                    if (_log.isDebugEnabled()) {
134                            _log.debug("Invoking getPageInfo(" + title + ", " + version + ")");
135                    }
136    
137                    try {
138                            com.liferay.portlet.wiki.model.WikiPage page =
139                                    WikiPageLocalServiceUtil.getPage(_nodeId, title);
140    
141                            return toJSPWikiPage(page, _engine);
142                    }
143                    catch (NoSuchPageException nspe) {
144                            return null;
145                    }
146                    catch (Exception e) {
147                            throw new ProviderException(e.toString());
148                    }
149            }
150    
151            public String getPageText(String title, int version)
152                    throws ProviderException {
153    
154                    if (_log.isDebugEnabled()) {
155                            _log.debug("Invoking getPageText(" + title + ", " + version + ")");
156                    }
157    
158                    try {
159                            com.liferay.portlet.wiki.model.WikiPage page =
160                                    WikiPageLocalServiceUtil.getPage(_nodeId, title);
161    
162                            return page.getContent();
163                    }
164                    catch (Exception e) {
165                            throw new ProviderException(e.toString());
166                    }
167            }
168    
169            public String getProviderInfo() {
170                    if (_log.isDebugEnabled()) {
171                            _log.debug("Invoking getProviderInfo()");
172                    }
173    
174                    return LiferayPageProvider.class.getName();
175            }
176    
177            public List<WikiPage> getVersionHistory(String title) {
178                    if (_log.isDebugEnabled()) {
179                            _log.debug("Invoking getVersionHistory(" + title + ")");
180                    }
181    
182                    return Collections.EMPTY_LIST;
183            }
184    
185            public void initialize(WikiEngine engine, Properties props) {
186                    if (_log.isDebugEnabled()) {
187                            _log.debug("Invoking initialize(" + engine + ", " + props + ")");
188                    }
189    
190                    _engine = engine;
191                    _nodeId = GetterUtil.getLong(props.getProperty("nodeId"));
192            }
193    
194            public void movePage(String from, String to) {
195                    if (_log.isDebugEnabled()) {
196                            _log.debug("Invoking movePage(" + from + ", " + to + ")");
197                    }
198            }
199    
200            public boolean pageExists(String title) {
201                    if (_log.isDebugEnabled()) {
202                            _log.debug("Invoking pageExists(" + title + ")");
203                    }
204    
205                    try {
206                            if (WikiPageLocalServiceUtil.getPagesCount(
207                                            _nodeId, WikiUtil.decodeJSPWikiName(title), true) > 0) {
208    
209                                    return true;
210                            }
211                            else {
212                                    return false;
213                            }
214                    }
215                    catch (Exception e) {
216                            _log.error(e, e);
217                    }
218    
219                    return false;
220            }
221    
222            public void putPageText(com.ecyrd.jspwiki.WikiPage page, String text) {
223                    if (_log.isDebugEnabled()) {
224                            _log.debug("Invoking putPageText(" + page + ", " + text + ")");
225                    }
226            }
227    
228            private static Log _log = LogFactoryUtil.getLog(LiferayPageProvider.class);
229    
230            private WikiEngine _engine;
231            private long _nodeId;
232    
233    }