001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    
023    import java.util.Map;
024    
025    /**
026     * @author Raymond Augé
027     */
028    public class ViewCounterTransformerListener extends TransformerListener {
029    
030            public String onXml(String s) {
031                    if (_log.isDebugEnabled()) {
032                            _log.debug("onXml");
033                    }
034    
035                    return s;
036            }
037    
038            public String onScript(String s) {
039                    if (_log.isDebugEnabled()) {
040                            _log.debug("onScript");
041                    }
042    
043                    return s;
044            }
045    
046            public String onOutput(String s) {
047                    if (_log.isDebugEnabled()) {
048                            _log.debug("onOutput");
049                    }
050    
051                    return replace(s);
052            }
053    
054            /**
055             * Replace the counter token with the increment call.
056             *
057             * @return the processed string
058             */
059            protected String replace(String s) {
060                    Map<String, String> tokens = getTokens();
061    
062                    String articleResourcePK = tokens.get("article_resource_pk");
063    
064                    String counterToken = StringPool.AT + "view_counter" + StringPool.AT;
065    
066                    StringBundler sb = new StringBundler(8);
067    
068                    sb.append("<script type=\"text/javascript\">");
069                    sb.append("Liferay.Service.Asset.AssetEntry.incrementViewCounter");
070                    sb.append("({userId:0, className:'");
071                    sb.append("com.liferay.portlet.journal.model.JournalArticle', ");
072                    sb.append("classPK:");
073                    sb.append(articleResourcePK);
074                    sb.append("});");
075                    sb.append("</script>");
076    
077                    s = StringUtil.replace(s, counterToken, sb.toString());
078    
079                    return s;
080            }
081    
082            private static Log _log = LogFactoryUtil.getLog(
083                    ViewCounterTransformerListener.class);
084    
085    }