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