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.portal.kernel.dao.search;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.HtmlUtil;
019    import com.liferay.portal.kernel.util.LocaleThreadLocal;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.theme.ThemeDisplay;
027    
028    import java.util.Locale;
029    import java.util.Map;
030    
031    import javax.portlet.PortletResponse;
032    
033    import javax.servlet.http.HttpServletRequest;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class RowChecker {
039    
040            public static final String ALIGN = "left";
041    
042            public static final String ALL_ROW_IDS = "allRowIds";
043    
044            public static final int COLSPAN = 1;
045    
046            public static final String CSS_CLASS = StringPool.BLANK;
047    
048            public static final String FORM_NAME = "fm";
049    
050            public static final String ROW_IDS = "rowIds";
051    
052            public static final String VALIGN = "middle";
053    
054            public RowChecker(PortletResponse portletResponse) {
055                    _portletResponse = portletResponse;
056                    _allRowIds = _portletResponse.getNamespace() + ALL_ROW_IDS;
057                    _formName = _portletResponse.getNamespace() + FORM_NAME;
058                    _rowIds = _portletResponse.getNamespace() + ROW_IDS;
059            }
060    
061            public String getAlign() {
062                    return _align;
063            }
064    
065            public String getAllRowIds() {
066                    return _allRowIds;
067            }
068    
069            public String getAllRowsCheckBox() {
070                    return getAllRowsCheckBox(null);
071            }
072    
073            public String getAllRowsCheckBox(HttpServletRequest request) {
074                    return getAllRowsCheckbox(
075                            request, _allRowIds, StringUtil.quote(_rowIds));
076            }
077    
078            public String getAllRowsId() {
079                    return getAllRowIds();
080            }
081    
082            public int getColspan() {
083                    return _colspan;
084            }
085    
086            public String getCssClass() {
087                    return _cssClass;
088            }
089    
090            public Map<String, Object> getData(Object obj) {
091                    return _data;
092            }
093    
094            public String getFormName() {
095                    return _formName;
096            }
097    
098            /**
099             * @deprecated As of 6.2.0, replaced by  {@link
100             *             #getRowCheckBox(HttpServletRequest, boolean, boolean,
101             *             String)}
102             */
103            @Deprecated
104            public String getRowCheckBox(
105                    boolean checked, boolean disabled, String primaryKey) {
106    
107                    return getRowCheckBox(null, checked, disabled, primaryKey);
108            }
109    
110            public String getRowCheckBox(
111                    HttpServletRequest request, boolean checked, boolean disabled,
112                    String primaryKey) {
113    
114                    return getRowCheckBox(
115                            request, checked, disabled, _rowIds, primaryKey,
116                            StringUtil.quote(_rowIds), StringUtil.quote(_allRowIds),
117                            StringPool.BLANK);
118            }
119    
120            public String getRowId() {
121                    return getRowIds();
122            }
123    
124            public String getRowIds() {
125                    return _rowIds;
126            }
127    
128            public String getValign() {
129                    return _valign;
130            }
131    
132            public boolean isChecked(Object obj) {
133                    return false;
134            }
135    
136            public boolean isDisabled(Object obj) {
137                    return false;
138            }
139    
140            public void setAlign(String align) {
141                    _align = align;
142            }
143    
144            public void setAllRowIds(String allRowIds) {
145                    _allRowIds = getNamespacedValue(allRowIds);
146            }
147    
148            public void setColspan(int colspan) {
149                    _colspan = colspan;
150            }
151    
152            public void setCssClass(String cssClass) {
153                    _cssClass = cssClass;
154            }
155    
156            public void setData(Map<String, Object> data) {
157                    _data = data;
158            }
159    
160            public void setFormName(String formName) {
161                    _formName = getNamespacedValue(formName);
162            }
163    
164            public void setRowIds(String rowIds) {
165                    _rowIds = getNamespacedValue(rowIds);
166            }
167    
168            public void setValign(String valign) {
169                    _valign = valign;
170            }
171    
172            protected String getAllRowsCheckbox(
173                    HttpServletRequest request, String name, String checkBoxRowIds) {
174    
175                    if (Validator.isNull(name)) {
176                            return StringPool.BLANK;
177                    }
178    
179                    StringBuilder sb = new StringBuilder(10);
180    
181                    sb.append("<input name=\"");
182                    sb.append(name);
183                    sb.append("\" title=\"");
184                    sb.append(LanguageUtil.get(getLocale(request), "select-all"));
185                    sb.append("\" type=\"checkbox\" ");
186                    sb.append(HtmlUtil.buildData(_data));
187                    sb.append("onClick=\"Liferay.Util.checkAll(AUI().one(this).ancestor(");
188                    sb.append("'.table'), ");
189                    sb.append(checkBoxRowIds);
190                    sb.append(", this, 'tr:not(.lfr-template)');\">");
191    
192                    return sb.toString();
193            }
194    
195            protected Locale getLocale(HttpServletRequest request) {
196                    Locale locale = null;
197    
198                    if (request != null) {
199                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
200                                    WebKeys.THEME_DISPLAY);
201    
202                            locale = themeDisplay.getLocale();
203                    }
204                    else {
205                            locale = LocaleThreadLocal.getThemeDisplayLocale();
206                    }
207    
208                    if (locale == null) {
209                            locale = LocaleUtil.getDefault();
210                    }
211    
212                    return locale;
213            }
214    
215            protected String getNamespacedValue(String value) {
216                    if (Validator.isNull(value)) {
217                            return StringPool.BLANK;
218                    }
219    
220                    if (!value.startsWith(_portletResponse.getNamespace())) {
221                            value = _portletResponse.getNamespace() + value;
222                    }
223    
224                    return value;
225            }
226    
227            protected String getOnClick(
228                    String checkBoxRowIds, String checkBoxAllRowIds,
229                    String checkBoxPostOnClick) {
230    
231                    StringBundler sb = new StringBundler(9);
232    
233                    sb.append("onClick=\"Liferay.Util.rowCheckerCheckAllBox(AUI().");
234                    sb.append("one(this).ancestor('.table'), AUI().one(this).");
235                    sb.append("ancestor('tr:not(.lfr-template)'), ");
236                    sb.append(checkBoxRowIds);
237                    sb.append(", ");
238                    sb.append(checkBoxAllRowIds);
239                    sb.append(", 'info');");
240    
241                    if (Validator.isNotNull(checkBoxPostOnClick)) {
242                            sb.append(checkBoxPostOnClick);
243                    }
244    
245                    sb.append("\"");
246    
247                    return sb.toString();
248            }
249    
250            protected String getRowCheckBox(
251                    HttpServletRequest request, boolean checked, boolean disabled,
252                    String name, String value, String checkBoxRowIds,
253                    String checkBoxAllRowIds, String checkBoxPostOnClick) {
254    
255                    StringBundler sb = new StringBundler(14);
256    
257                    sb.append("<input ");
258    
259                    if (checked) {
260                            sb.append("checked ");
261                    }
262    
263                    if (disabled) {
264                            sb.append("disabled ");
265                    }
266    
267                    sb.append("class=\"");
268                    sb.append(_cssClass);
269                    sb.append("\" name=\"");
270                    sb.append(name);
271                    sb.append("\" title=\"");
272                    sb.append(LanguageUtil.get(request.getLocale(), "select"));
273                    sb.append("\" type=\"checkbox\" value=\"");
274                    sb.append(HtmlUtil.escapeAttribute(value));
275                    sb.append("\" ");
276    
277                    if (Validator.isNotNull(_allRowIds)) {
278                            sb.append(
279                                    getOnClick(
280                                            checkBoxRowIds, checkBoxAllRowIds, checkBoxPostOnClick));
281                    }
282    
283                    sb.append(">");
284    
285                    return sb.toString();
286            }
287    
288            private String _align = ALIGN;
289            private String _allRowIds;
290            private int _colspan = COLSPAN;
291            private String _cssClass = CSS_CLASS;
292            private Map<String, Object> _data;
293            private String _formName;
294            private final PortletResponse _portletResponse;
295            private String _rowIds;
296            private String _valign = VALIGN;
297    
298    }