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.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  }