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