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.taglib.ui;
016    
017    import com.liferay.portal.kernel.configuration.Filter;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.PropsUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.taglib.util.IncludeTag;
024    
025    import java.util.HashMap;
026    import java.util.Map;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author David Truong
032     * @author Jorge Ferrer
033     * @author Brian Wing Shun Chan
034     */
035    public class SocialBookmarkTag extends IncludeTag {
036    
037            public void setContentId(String contentId) {
038                    _contentId = contentId;
039            }
040    
041            public void setTarget(String target) {
042                    _target = target;
043            }
044    
045            public void setTitle(String title) {
046                    _title = title;
047            }
048    
049            public void setType(String type) {
050                    _type = type;
051            }
052    
053            public void setUrl(String url) {
054                    _url = url;
055            }
056    
057            @Override
058            protected void cleanUp() {
059                    _contentId = null;
060                    _target = null;
061                    _title = null;
062                    _type = null;
063                    _url = null;
064            }
065    
066            @Override
067            protected String getPage() {
068                    String[] socialTypes = PropsUtil.getArray(
069                            PropsKeys.SOCIAL_BOOKMARK_TYPES);
070    
071                    if (ArrayUtil.contains(socialTypes, _type)) {
072                            if (Validator.isNotNull(_jspPath)) {
073                                    return _jspPath;
074                            }
075                            else {
076                                    return _PAGE;
077                            }
078                    }
079                    else {
080                            return null;
081                    }
082            }
083    
084            protected String getPostUrl() {
085                    Map<String, String> vars = new HashMap<String, String>();
086    
087                    vars.put("liferay:social-bookmark:title", HttpUtil.encodeURL(_title));
088                    vars.put("liferay:social-bookmark:url", _url);
089    
090                    String postUrl = PropsUtil.get(
091                            PropsKeys.SOCIAL_BOOKMARK_POST_URL, new Filter(_type, vars));
092    
093                    return postUrl;
094            }
095    
096            @Override
097            protected void setAttributes(HttpServletRequest request) {
098                    String jspPath = _jspPaths.get(_type);
099    
100                    if (jspPath == null) {
101                            jspPath = PropsUtil.get(
102                                    PropsKeys.SOCIAL_BOOKMARK_JSP, new Filter(_type));
103    
104                            _jspPaths.put(_type, jspPath);
105                    }
106    
107                    _jspPath = jspPath;
108    
109                    if (Validator.isNull(_jspPath)) {
110                            request.setAttribute(
111                                    "liferay-ui:social-bookmark:postUrl", getPostUrl());
112                    }
113    
114                    request.setAttribute(
115                            "liferay-ui:social-bookmark:contentId", _contentId);
116                    request.setAttribute("liferay-ui:social-bookmark:target", _target);
117                    request.setAttribute("liferay-ui:social-bookmark:title", _title);
118                    request.setAttribute("liferay-ui:social-bookmark:type", _type);
119                    request.setAttribute("liferay-ui:social-bookmark:url", _url);
120            }
121    
122            private static final String _PAGE =
123                    "/html/taglib/ui/social_bookmark/page.jsp";
124    
125            private static Map<String, String> _jspPaths =
126                    new HashMap<String, String>();
127    
128            private String _contentId;
129            private String _jspPath;
130            private String _target;
131            private String _title;
132            private String _type;
133            private String _url;
134    
135    }