001    /**
002     * Copyright (c) 2000-present 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 java.util.regex.Matcher;
025    import java.util.regex.Pattern;
026    
027    import org.jamwiki.model.Namespace;
028    import org.jamwiki.model.Topic;
029    import org.jamwiki.model.TopicType;
030    
031    /**
032     * @author Jonathan Potter
033     */
034    public class LiferayDataHandler extends DummyDataHandler {
035    
036            @Override
037            public Namespace lookupNamespace(
038                    String virtualWiki, String namespaceString) {
039    
040                    String label = _fileNamespace.getLabel(virtualWiki);
041    
042                    if (StringUtil.equalsIgnoreCase(label, namespaceString)) {
043                            return _fileNamespace;
044                    }
045                    else {
046                            return null;
047                    }
048            }
049    
050            @Override
051            public Namespace lookupNamespaceById(int namespaceId) {
052                    return Namespace.DEFAULT_NAMESPACES.get(namespaceId);
053            }
054    
055            @Override
056            public Topic lookupTopic(
057                    String virtualWiki, String topicName, boolean deleteOK,
058                    Connection conn) {
059    
060                    Topic topic = new Topic(virtualWiki, topicName);
061    
062                    topic.setTopicType(TopicType.IMAGE);
063    
064                    return topic;
065            }
066    
067            @Override
068            public String lookupTopicName(String virtualWiki, String topicName) {
069                    long nodeId = getNodeId(virtualWiki);
070    
071                    try {
072                            WikiPage page = WikiPageLocalServiceUtil.getPage(
073                                    nodeId, topicName, true);
074    
075                            return page.getTitle();
076                    }
077                    catch (Exception e) {
078                    }
079    
080                    return null;
081            }
082    
083            protected long getNodeId(String virtualWiki) {
084                    Matcher matcher = _pattern.matcher(virtualWiki);
085    
086                    if (matcher.find()) {
087                            return GetterUtil.getLong(matcher.group(1));
088                    }
089    
090                    return 0;
091            }
092    
093            private final Namespace _fileNamespace = Namespace.DEFAULT_NAMESPACES.get(
094                    Namespace.FILE_ID);
095            private final Pattern _pattern = Pattern.compile("Special:Node:(\\d+)");
096    
097    }