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 getRowSelector() {
129                    return _rowSelector;
130            }
131    
132            public String getValign() {
133                    return _valign;
134            }
135    
136            public boolean isChecked(Object obj) {
137                    return false;
138            }
139    
140            public boolean isDisabled(Object obj) {
141                    return false;
142            }
143    
144            public void setAlign(String align) {
145                    _align = align;
146            }
147    
148            public void setAllRowIds(String allRowIds) {
149                    _allRowIds = getNamespacedValue(allRowIds);
150            }
151    
152            public void setColspan(int colspan) {
153                    _colspan = colspan;
154            }
155    
156            public void setCssClass(String cssClass) {
157                    _cssClass = cssClass;
158            }
159    
160            public void setData(Map<String, Object> data) {
161                    _data = data;
162            }
163    
164            public void setFormName(String formName) {
165                    _formName = getNamespacedValue(formName);
166            }
167    
168            public void setRowIds(String rowIds) {
169                    _rowIds = getNamespacedValue(rowIds);
170            }
171    
172            public void setRowSelector(String rowSelector) {
173                    _rowSelector = getNamespacedValue(rowSelector);
174            }
175    
176            public void setValign(String valign) {
177                    _valign = valign;
178            }
179    
180            protected String getAllRowsCheckbox(
181                    HttpServletRequest request, String name, String checkBoxRowIds) {
182    
183                    if (Validator.isNull(name)) {
184                            return StringPool.BLANK;
185                    }
186    
187                    StringBuilder sb = new StringBuilder(10);
188    
189                    sb.append("<label><input name=\"");
190                    sb.append(name);
191                    sb.append("\" title=\"");
192                    sb.append(LanguageUtil.get(getLocale(request), "select-all"));
193                    sb.append("\" type=\"checkbox\" ");
194                    sb.append(HtmlUtil.buildData(_data));
195                    sb.append("onClick=\"Liferay.Util.checkAll(AUI().one(this).ancestor(");
196                    sb.append("'.table'), ");
197                    sb.append(checkBoxRowIds);
198                    sb.append(", this, 'tr:not(.lfr-template)');\"></label>");
199    
200                    return sb.toString();
201            }
202    
203            protected Locale getLocale(HttpServletRequest request) {
204                    Locale locale = null;
205    
206                    if (request != null) {
207                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
208                                    WebKeys.THEME_DISPLAY);
209    
210                            locale = themeDisplay.getLocale();
211                    }
212                    else {
213                            locale = LocaleThreadLocal.getThemeDisplayLocale();
214                    }
215    
216                    if (locale == null) {
217                            locale = LocaleUtil.getDefault();
218                    }
219    
220                    return locale;
221            }
222    
223            protected String getNamespacedValue(String value) {
224                    if (Validator.isNull(value)) {
225                            return StringPool.BLANK;
226                    }
227    
228                    if (!value.startsWith(_portletResponse.getNamespace())) {
229                            value = _portletResponse.getNamespace() + value;
230                    }
231    
232                    return value;
233            }
234    
235            protected String getOnClick(
236                    String checkBoxRowIds, String checkBoxAllRowIds,
237                    String checkBoxPostOnClick) {
238    
239                    StringBundler sb = new StringBundler(9);
240    
241                    sb.append("onClick=\"Liferay.Util.rowCheckerCheckAllBox(AUI().");
242                    sb.append("one(this).ancestor('.table'), AUI().one(this).");
243                    sb.append("ancestor('tr:not(.lfr-template)'), ");
244                    sb.append(checkBoxRowIds);
245                    sb.append(", ");
246                    sb.append(checkBoxAllRowIds);
247                    sb.append(", 'info');");
248    
249                    if (Validator.isNotNull(checkBoxPostOnClick)) {
250                            sb.append(checkBoxPostOnClick);
251                    }
252    
253                    sb.append("\"");
254    
255                    return sb.toString();
256            }
257    
258            protected String getRowCheckBox(
259                    HttpServletRequest request, boolean checked, boolean disabled,
260                    String name, String value, String checkBoxRowIds,
261                    String checkBoxAllRowIds, String checkBoxPostOnClick) {
262    
263                    StringBundler sb = new StringBundler(14);
264    
265                    sb.append("<label><input ");
266    
267                    if (checked) {
268                            sb.append("checked ");
269                    }
270    
271                    if (disabled) {
272                            sb.append("disabled ");
273                    }
274    
275                    sb.append("class=\"");
276                    sb.append(_cssClass);
277                    sb.append("\" name=\"");
278                    sb.append(name);
279                    sb.append("\" title=\"");
280                    sb.append(LanguageUtil.get(request.getLocale(), "select"));
281                    sb.append("\" type=\"checkbox\" value=\"");
282                    sb.append(HtmlUtil.escapeAttribute(value));
283                    sb.append("\" ");
284    
285                    if (Validator.isNotNull(_allRowIds)) {
286                            sb.append(
287                                    getOnClick(
288                                            checkBoxRowIds, checkBoxAllRowIds, checkBoxPostOnClick));
289                    }
290    
291                    sb.append("></label>");
292    
293                    return sb.toString();
294            }
295    
296            private String _align = ALIGN;
297            private String _allRowIds;
298            private int _colspan = COLSPAN;
299            private String _cssClass = CSS_CLASS;
300            private Map<String, Object> _data;
301            private String _formName;
302            private final PortletResponse _portletResponse;
303            private String _rowIds;
304            private String _rowSelector;
305            private String _valign = VALIGN;
306    
307    }