001    /**
002     * Copyright (c) 2000-present 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 setDisplayStyle(String displayStyle) {
042                    _displayStyle = displayStyle;
043            }
044    
045            public void setIcon(String icon) {
046                    _icon = icon;
047            }
048    
049            public void setTarget(String target) {
050                    _target = target;
051            }
052    
053            public void setTitle(String title) {
054                    _title = title;
055            }
056    
057            public void setType(String type) {
058                    _type = type;
059            }
060    
061            public void setUrl(String url) {
062                    _url = url;
063            }
064    
065            @Override
066            protected void cleanUp() {
067                    _contentId = null;
068                    _target = null;
069                    _title = null;
070                    _type = null;
071                    _url = null;
072            }
073    
074            protected String getDisplayStyle() {
075                    String displayStyle = _displayStyle;
076    
077                    if (Validator.isNull(displayStyle)) {
078                            String[] displayStyles = PropsUtil.getArray(
079                                    PropsKeys.SOCIAL_BOOKMARK_DISPLAY_STYLES);
080    
081                            displayStyle = displayStyles[0];
082                    }
083    
084                    return displayStyle;
085            }
086    
087            @Override
088            protected String getPage() {
089                    String[] socialTypes = PropsUtil.getArray(
090                            PropsKeys.SOCIAL_BOOKMARK_TYPES);
091    
092                    if (ArrayUtil.contains(socialTypes, _type)) {
093                            String displayStyle = getDisplayStyle();
094    
095                            if (!displayStyle.equals("menu") && Validator.isNotNull(_jspPath)) {
096                                    return _jspPath;
097                            }
098                            else {
099                                    return _PAGE;
100                            }
101                    }
102                    else {
103                            return null;
104                    }
105            }
106    
107            protected String getPostUrl() {
108                    Map<String, String> vars = new HashMap<>();
109    
110                    vars.put("liferay:social-bookmark:title", HttpUtil.encodeURL(_title));
111                    vars.put("liferay:social-bookmark:url", _url);
112    
113                    String postUrl = PropsUtil.get(
114                            PropsKeys.SOCIAL_BOOKMARK_POST_URL, new Filter(_type, vars));
115    
116                    return postUrl;
117            }
118    
119            @Override
120            protected void setAttributes(HttpServletRequest request) {
121                    String jspPath = _jspPaths.get(_type);
122    
123                    if (jspPath == null) {
124                            jspPath = PropsUtil.get(
125                                    PropsKeys.SOCIAL_BOOKMARK_JSP, new Filter(_type));
126    
127                            _jspPaths.put(_type, jspPath);
128                    }
129    
130                    _jspPath = jspPath;
131    
132                    String icon = _icon;
133    
134                    String displayStyle = getDisplayStyle();
135    
136                    if (displayStyle.equals("menu") || Validator.isNull(_jspPath)) {
137                            if (Validator.isNull(icon)) {
138                                    icon = PropsUtil.get(
139                                            PropsKeys.SOCIAL_BOOKMARK_ICON, new Filter(_type));
140    
141                                    if (Validator.isNull(icon)) {
142                                            icon = "../aui/share-sign";
143                                    }
144                            }
145    
146                            request.setAttribute("liferay-ui:social-bookmark:icon", icon);
147                            request.setAttribute(
148                                    "liferay-ui:social-bookmark:postUrl", getPostUrl());
149                    }
150    
151                    request.setAttribute(
152                            "liferay-ui:social-bookmark:contentId", _contentId);
153                    request.setAttribute(
154                            "liferay-ui:social-bookmark:displayStyle", _displayStyle);
155                    request.setAttribute("liferay-ui:social-bookmark:target", _target);
156                    request.setAttribute("liferay-ui:social-bookmark:title", _title);
157                    request.setAttribute("liferay-ui:social-bookmark:type", _type);
158                    request.setAttribute("liferay-ui:social-bookmark:url", _url);
159            }
160    
161            private static final String _PAGE =
162                    "/html/taglib/ui/social_bookmark/page.jsp";
163    
164            private static final Map<String, String> _jspPaths = new HashMap<>();
165    
166            private String _contentId;
167            private String _displayStyle;
168            private String _icon;
169            private String _jspPath;
170            private String _target;
171            private String _title;
172            private String _type;
173            private String _url;
174    
175    }