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.mediawiki;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portlet.wiki.model.WikiPage;
019    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
020    
021    import java.sql.Connection;
022    
023    import org.jamwiki.model.Namespace;
024    import org.jamwiki.model.Topic;
025    import org.jamwiki.model.TopicType;
026    
027    /**
028     * @author Jonathan Potter
029     */
030    public class LiferayDataHandler extends DummyDataHandler {
031    
032            @Override
033            public Namespace lookupNamespace(
034                    String virtualWiki, String namespaceString) {
035    
036                    String label = _fileNamespace.getLabel(virtualWiki);
037    
038                    if (label.equalsIgnoreCase(namespaceString)) {
039                            return _fileNamespace;
040                    }
041                    else {
042                            return null;
043                    }
044            }
045    
046            @Override
047            public Namespace lookupNamespaceById(int namespaceId) {
048                    return Namespace.DEFAULT_NAMESPACES.get(namespaceId);
049            }
050    
051            @Override
052            public Topic lookupTopic(
053                    String virtualWiki, String topicName, boolean deleteOK,
054                    Connection conn) {
055    
056                    Topic topic = new Topic(virtualWiki, topicName);
057    
058                    topic.setTopicType(TopicType.IMAGE);
059    
060                    return topic;
061            }
062    
063            @Override
064            public String lookupTopicName(String virtualWiki, String topicName) {
065                    long nodeId = getNodeId(virtualWiki);
066    
067                    try {
068                            WikiPage page = WikiPageLocalServiceUtil.getPage(
069                                    nodeId, topicName, true);
070    
071                            return page.getTitle();
072                    }
073                    catch (Exception e) {
074                    }
075    
076                    return null;
077            }
078    
079            protected long getNodeId(String virtualWiki) {
080                    String nodeId = virtualWiki.replaceAll("Special:Node:(\\d+)", "$1");
081    
082                    return GetterUtil.getLong(nodeId);
083            }
084    
085            private Namespace _fileNamespace = Namespace.DEFAULT_NAMESPACES.get(
086                    Namespace.FILE_ID);
087    
088    }