1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  
23  import java.util.Map;
24  
25  /**
26   * <a href="ViewCounterTransformerListener.java.html"><b><i>View Source</i></b>
27   * </a>
28   *
29   * @author Raymond Augé
30   */
31  public class ViewCounterTransformerListener extends TransformerListener {
32  
33      public String onXml(String s) {
34          if (_log.isDebugEnabled()) {
35              _log.debug("onXml");
36          }
37  
38          return s;
39      }
40  
41      public String onScript(String s) {
42          if (_log.isDebugEnabled()) {
43              _log.debug("onScript");
44          }
45  
46          return s;
47      }
48  
49      public String onOutput(String s) {
50          if (_log.isDebugEnabled()) {
51              _log.debug("onOutput");
52          }
53  
54          return replace(s);
55      }
56  
57      /**
58       * Replace the counter token with the increment call.
59       *
60       * @return the processed string
61       */
62      protected String replace(String s) {
63          Map<String, String> tokens = getTokens();
64  
65          String articleResourcePK = tokens.get("article_resource_pk");
66  
67          String counterToken = StringPool.AT + "view_counter" + StringPool.AT;
68  
69          StringBundler sb = new StringBundler(8);
70  
71          sb.append("<script type=\"text/javascript\">");
72          sb.append("Liferay.Service.Tags.TagsAsset.incrementViewCounter");
73          sb.append("({className:'");
74          sb.append("com.liferay.portlet.journal.model.JournalArticle', ");
75          sb.append("classPK:");
76          sb.append(articleResourcePK);
77          sb.append("});");
78          sb.append("</script>");
79  
80          s = StringUtil.replace(s, counterToken, sb.toString());
81  
82          return s;
83      }
84  
85      private static Log _log = LogFactoryUtil.getLog(
86          ViewCounterTransformerListener.class);
87  
88  }