001    /**
002     * Copyright (c) 2000-2013 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(
033                    String output, String languageId, Map<String, String> tokens) {
034    
035                    if (_log.isDebugEnabled()) {
036                            _log.debug("onOutput");
037                    }
038    
039                    return replace(output, tokens);
040            }
041    
042            /**
043             * Replace the counter token with the increment call.
044             *
045             * @return the processed string
046             */
047            protected String replace(String s, Map<String, String> tokens) {
048                    String articleResourcePK = tokens.get("article_resource_pk");
049    
050                    String counterToken = StringPool.AT + "view_counter" + StringPool.AT;
051    
052                    StringBundler sb = new StringBundler(8);
053    
054                    sb.append("<script type=\"text/javascript\">");
055                    sb.append("Liferay.Service.Asset.AssetEntry.incrementViewCounter");
056                    sb.append("({userId:0, className:'");
057                    sb.append("com.liferay.portlet.journal.model.JournalArticle', ");
058                    sb.append("classPK:");
059                    sb.append(articleResourcePK);
060                    sb.append("});");
061                    sb.append("</script>");
062    
063                    s = StringUtil.replace(s, counterToken, sb.toString());
064    
065                    return s;
066            }
067    
068            private static Log _log = LogFactoryUtil.getLog(
069                    ViewCounterTransformerListener.class);
070    
071    }