1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.wiki.engines.mediawiki;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
22  
23  import java.sql.Connection;
24  
25  import org.jamwiki.model.Namespace;
26  import org.jamwiki.model.Topic;
27  import org.jamwiki.model.TopicType;
28  
29  /**
30   * <a href="LiferayDataHandler.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Jonathan Potter
33   */
34  public class LiferayDataHandler extends DummyDataHandler {
35  
36      public Namespace lookupNamespace(
37          String virtualWiki, String namespaceString) {
38  
39          String label = _fileNamespace.getLabel(virtualWiki);
40  
41          if (label.equalsIgnoreCase(namespaceString)) {
42              return _fileNamespace;
43          }
44          else {
45              return null;
46          }
47      }
48  
49      public Namespace lookupNamespaceById(int namespaceId) {
50          return Namespace.DEFAULT_NAMESPACES.get(namespaceId);
51      }
52  
53      public Topic lookupTopic(
54          String virtualWiki, String topicName, boolean deleteOK,
55          Connection conn) {
56  
57          Topic topic = new Topic(virtualWiki, topicName);
58  
59          topic.setTopicType(TopicType.IMAGE);
60  
61          return topic;
62      }
63  
64      public Integer lookupTopicId(String virtualWiki, String topicName) {
65          long nodeId = getNodeId(virtualWiki);
66  
67          try {
68              int pagesCount = WikiPageLocalServiceUtil.getPagesCount(
69                  nodeId, topicName, true);
70  
71              if (pagesCount > 0) {
72                  return 1;
73              }
74          }
75          catch (SystemException se) {
76              _log.error(se, se);
77          }
78  
79          return null;
80      }
81  
82      protected long getNodeId(String virtualWiki) {
83          String nodeId = virtualWiki.replaceAll("Special:Node:(\\d+)", "$1");
84  
85          return GetterUtil.getLong(nodeId);
86      }
87  
88      private static Log _log = LogFactoryUtil.getLog(LiferayDataHandler.class);
89  
90      private Namespace _fileNamespace = Namespace.DEFAULT_NAMESPACES.get(
91          Namespace.FILE_ID);
92  
93  }