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.journal.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.templateparser.BaseTransformerListener;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.util.Map;
025    
026    /**
027     * @author Raymond Augé
028     */
029    public class ViewCounterTransformerListener extends BaseTransformerListener {
030    
031            @Override
032            public String onOutput(String s) {
033                    if (_log.isDebugEnabled()) {
034                            _log.debug("onOutput");
035                    }
036    
037                    return replace(s);
038            }
039    
040            @Override
041            public String onScript(String s) {
042                    if (_log.isDebugEnabled()) {
043                            _log.debug("onScript");
044                    }
045    
046                    return s;
047            }
048    
049            @Override
050            public String onXml(String s) {
051                    if (_log.isDebugEnabled()) {
052                            _log.debug("onXml");
053                    }
054    
055                    return s;
056            }
057    
058            /**
059             * Replace the counter token with the increment call.
060             *
061             * @return the processed string
062             */
063            protected String replace(String s) {
064                    Map<String, String> tokens = getTokens();
065    
066                    String articleResourcePK = tokens.get("article_resource_pk");
067    
068                    String counterToken = StringPool.AT + "view_counter" + StringPool.AT;
069    
070                    StringBundler sb = new StringBundler(8);
071    
072                    sb.append("<script type=\"text/javascript\">");
073                    sb.append("Liferay.Service.Asset.AssetEntry.incrementViewCounter");
074                    sb.append("({userId:0, className:'");
075                    sb.append("com.liferay.portlet.journal.model.JournalArticle', ");
076                    sb.append("classPK:");
077                    sb.append(articleResourcePK);
078                    sb.append("});");
079                    sb.append("</script>");
080    
081                    s = StringUtil.replace(s, counterToken, sb.toString());
082    
083                    return s;
084            }
085    
086            private static Log _log = LogFactoryUtil.getLog(
087                    ViewCounterTransformerListener.class);
088    
089    }