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.theme.ThemeDisplay;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.LocaleThreadLocal;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.util.WebKeys;
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            public String getRememberCheckBoxStateURLRegex() {
099                    return _rememberCheckBoxStateURLRegex;
100            }
101    
102            public String getRowCheckBox(
103                    HttpServletRequest request, boolean checked, boolean disabled,
104                    String primaryKey) {
105    
106                    return getRowCheckBox(
107                            request, checked, disabled, _rowIds, primaryKey,
108                            StringUtil.quote(_rowIds), StringUtil.quote(_allRowIds),
109                            StringPool.BLANK);
110            }
111    
112            public String getRowId() {
113                    return getRowIds();
114            }
115    
116            public String getRowIds() {
117                    return _rowIds;
118            }
119    
120            public String getRowSelector() {
121                    return _rowSelector;
122            }
123    
124            public String getValign() {
125                    return _valign;
126            }
127    
128            public boolean isChecked(Object obj) {
129                    return false;
130            }
131    
132            public boolean isDisabled(Object obj) {
133                    return false;
134            }
135    
136            public boolean isRememberCheckBoxState() {
137                    return _rememberCheckBoxState;
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 setRememberCheckBoxState(boolean rememberCheckBoxState) {
165                    _rememberCheckBoxState = rememberCheckBoxState;
166            }
167    
168            public void setRememberCheckBoxStateURLRegex(
169                    String rememberCheckBoxStateURLRegex) {
170    
171                    _rememberCheckBoxStateURLRegex = rememberCheckBoxStateURLRegex;
172            }
173    
174            public void setRowIds(String rowIds) {
175                    _rowIds = getNamespacedValue(rowIds);
176            }
177    
178            public void setRowSelector(String rowSelector) {
179                    _rowSelector = getNamespacedValue(rowSelector);
180            }
181    
182            public void setValign(String valign) {
183                    _valign = valign;
184            }
185    
186            protected String getAllRowsCheckbox(
187                    HttpServletRequest request, String name, String checkBoxRowIds) {
188    
189                    if (Validator.isNull(name)) {
190                            return StringPool.BLANK;
191                    }
192    
193                    StringBuilder sb = new StringBuilder(10);
194    
195                    sb.append("<label><input name=\"");
196                    sb.append(name);
197                    sb.append("\" title=\"");
198                    sb.append(LanguageUtil.get(getLocale(request), "select-all"));
199                    sb.append("\" type=\"checkbox\" ");
200                    sb.append(HtmlUtil.buildData(_data));
201                    sb.append("onClick=\"Liferay.Util.checkAll(AUI().one(this).ancestor(");
202                    sb.append("'.table'), ");
203                    sb.append(checkBoxRowIds);
204                    sb.append(", this, 'tr:not(.lfr-template)');\"></label>");
205    
206                    return sb.toString();
207            }
208    
209            protected Locale getLocale(HttpServletRequest request) {
210                    Locale locale = null;
211    
212                    if (request != null) {
213                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
214                                    WebKeys.THEME_DISPLAY);
215    
216                            locale = themeDisplay.getLocale();
217                    }
218                    else {
219                            locale = LocaleThreadLocal.getThemeDisplayLocale();
220                    }
221    
222                    if (locale == null) {
223                            locale = LocaleUtil.getDefault();
224                    }
225    
226                    return locale;
227            }
228    
229            protected String getNamespacedValue(String value) {
230                    if (Validator.isNull(value)) {
231                            return StringPool.BLANK;
232                    }
233    
234                    if (!value.startsWith(_portletResponse.getNamespace())) {
235                            value = _portletResponse.getNamespace() + value;
236                    }
237    
238                    return value;
239            }
240    
241            protected String getOnClick(
242                    String checkBoxRowIds, String checkBoxAllRowIds,
243                    String checkBoxPostOnClick) {
244    
245                    StringBundler sb = new StringBundler(9);
246    
247                    sb.append("onClick=\"Liferay.Util.rowCheckerCheckAllBox(AUI().");
248                    sb.append("one(this).ancestor('.table'), AUI().one(this).");
249                    sb.append("ancestor('tr:not(.lfr-template)'), ");
250                    sb.append(checkBoxRowIds);
251                    sb.append(", ");
252                    sb.append(checkBoxAllRowIds);
253                    sb.append(", 'info');");
254    
255                    if (Validator.isNotNull(checkBoxPostOnClick)) {
256                            sb.append(checkBoxPostOnClick);
257                    }
258    
259                    sb.append("\"");
260    
261                    return sb.toString();
262            }
263    
264            protected String getRowCheckBox(
265                    HttpServletRequest request, boolean checked, boolean disabled,
266                    String name, String value, String checkBoxRowIds,
267                    String checkBoxAllRowIds, String checkBoxPostOnClick) {
268    
269                    StringBundler sb = new StringBundler(14);
270    
271                    sb.append("<label><input ");
272    
273                    if (checked) {
274                            sb.append("checked ");
275                    }
276    
277                    if (disabled) {
278                            sb.append("disabled ");
279                    }
280    
281                    sb.append("class=\"");
282                    sb.append(_cssClass);
283                    sb.append("\" name=\"");
284                    sb.append(name);
285                    sb.append("\" title=\"");
286                    sb.append(LanguageUtil.get(request.getLocale(), "select"));
287                    sb.append("\" type=\"checkbox\" value=\"");
288                    sb.append(HtmlUtil.escapeAttribute(value));
289                    sb.append("\" ");
290    
291                    if (Validator.isNotNull(_allRowIds)) {
292                            sb.append(
293                                    getOnClick(
294                                            checkBoxRowIds, checkBoxAllRowIds, checkBoxPostOnClick));
295                    }
296    
297                    sb.append("></label>");
298    
299                    return sb.toString();
300            }
301    
302            private String _align = ALIGN;
303            private String _allRowIds;
304            private int _colspan = COLSPAN;
305            private String _cssClass = CSS_CLASS;
306            private Map<String, Object> _data;
307            private String _formName;
308            private final PortletResponse _portletResponse;
309            private boolean _rememberCheckBoxState = true;
310            private String _rememberCheckBoxStateURLRegex;
311            private String _rowIds;
312            private String _rowSelector;
313            private String _valign = VALIGN;
314    
315    }