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.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            public Namespace lookupNamespace(
035                    String virtualWiki, String namespaceString) {
036    
037                    String label = _fileNamespace.getLabel(virtualWiki);
038    
039                    if (label.equalsIgnoreCase(namespaceString)) {
040                            return _fileNamespace;
041                    }
042                    else {
043                            return null;
044                    }
045            }
046    
047            public Namespace lookupNamespaceById(int namespaceId) {
048                    return Namespace.DEFAULT_NAMESPACES.get(namespaceId);
049            }
050    
051            public Topic lookupTopic(
052                    String virtualWiki, String topicName, boolean deleteOK,
053                    Connection conn) {
054    
055                    Topic topic = new Topic(virtualWiki, topicName);
056    
057                    topic.setTopicType(TopicType.IMAGE);
058    
059                    return topic;
060            }
061    
062            public Integer lookupTopicId(String virtualWiki, String topicName) {
063                    long nodeId = getNodeId(virtualWiki);
064    
065                    try {
066                            int pagesCount = WikiPageLocalServiceUtil.getPagesCount(
067                                    nodeId, topicName, true);
068    
069                            if (pagesCount > 0) {
070                                    return 1;
071                            }
072                    }
073                    catch (SystemException se) {
074                            _log.error(se, se);
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 static Log _log = LogFactoryUtil.getLog(LiferayDataHandler.class);
087    
088            private Namespace _fileNamespace = Namespace.DEFAULT_NAMESPACES.get(
089                    Namespace.FILE_ID);
090    
091    }