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