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