001    /**
002     * Copyright (c) 2000-2013 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            public String getRowCheckBox(
093                    boolean checked, boolean disabled, String primaryKey) {
094    
095                    return getRowCheckBox(null, checked, disabled, primaryKey);
096            }
097    
098            public String getRowCheckBox(
099                    HttpServletRequest request, boolean checked, boolean disabled,
100                    String primaryKey) {
101    
102                    return getRowCheckBox(
103                            request, checked, disabled, _rowIds, primaryKey,
104                            StringUtil.quote(_rowIds), StringUtil.quote(_allRowIds),
105                            StringPool.BLANK);
106            }
107    
108            public String getRowId() {
109                    return getRowIds();
110            }
111    
112            public String getRowIds() {
113                    return _rowIds;
114            }
115    
116            public String getValign() {
117                    return _valign;
118            }
119    
120            public boolean isChecked(Object obj) {
121                    return false;
122            }
123    
124            public boolean isDisabled(Object obj) {
125                    return false;
126            }
127    
128            public void setAlign(String align) {
129                    _align = align;
130            }
131    
132            public void setAllRowIds(String allRowIds) {
133                    _allRowIds = getNamespacedValue(allRowIds);
134            }
135    
136            public void setColspan(int colspan) {
137                    _colspan = colspan;
138            }
139    
140            public void setCssClass(String cssClass) {
141                    _cssClass = cssClass;
142            }
143    
144            public void setFormName(String formName) {
145                    _formName = getNamespacedValue(formName);
146            }
147    
148            public void setRowIds(String rowIds) {
149                    _rowIds = getNamespacedValue(rowIds);
150            }
151    
152            public void setValign(String valign) {
153                    _valign = valign;
154            }
155    
156            protected String getAllRowsCheckbox(
157                    HttpServletRequest request, String name, String checkBoxRowIds) {
158    
159                    if (Validator.isNull(name)) {
160                            return StringPool.BLANK;
161                    }
162    
163                    StringBuilder sb = new StringBuilder(11);
164    
165                    sb.append("<input name=\"");
166                    sb.append(name);
167                    sb.append("\" title=\"");
168                    sb.append(LanguageUtil.get(request.getLocale(), "select-all"));
169                    sb.append("\" type=\"checkbox\" ");
170                    sb.append("onClick=\"Liferay.Util.checkAll(");
171                    sb.append("AUI().one(this).ancestor('");
172                    sb.append(".table'), ");
173                    sb.append(checkBoxRowIds);
174                    sb.append(", this, 'tr:not(.lfr-template)'");
175                    sb.append(");\">");
176    
177                    return sb.toString();
178            }
179    
180            protected String getNamespacedValue(String value) {
181                    if (Validator.isNull(value)) {
182                            return StringPool.BLANK;
183                    }
184    
185                    if (!value.startsWith(_portletResponse.getNamespace())) {
186                            value = _portletResponse.getNamespace() + value;
187                    }
188    
189                    return value;
190            }
191    
192            protected String getRowCheckBox(
193                    HttpServletRequest request, boolean checked, boolean disabled,
194                    String name, String value, String checkBoxRowIds,
195                    String checkBoxAllRowIds, String checkBoxPostOnClick) {
196    
197                    StringBundler sb = new StringBundler();
198    
199                    sb.append("<input ");
200    
201                    if (checked) {
202                            sb.append("checked ");
203                    }
204    
205                    if (disabled) {
206                            sb.append("disabled ");
207                    }
208    
209                    sb.append("name=\"");
210                    sb.append(name);
211                    sb.append("\" title=\"");
212                    sb.append(LanguageUtil.get(request.getLocale(), "select"));
213                    sb.append("\" type=\"checkbox\" value=\"");
214                    sb.append(HtmlUtil.escapeAttribute(value));
215                    sb.append("\" ");
216    
217                    if (Validator.isNotNull(_allRowIds)) {
218                            sb.append("onClick=\"Liferay.Util.checkAllBox(");
219                            sb.append("AUI().one(this).ancestor('");
220                            sb.append(".table'), ");
221                            sb.append(checkBoxRowIds);
222                            sb.append(", ");
223                            sb.append(checkBoxAllRowIds);
224                            sb.append(");");
225                            sb.append("AUI().one(this).ancestor('tr:not(.lfr-template)').");
226                            sb.append("toggleClass('info');");
227    
228                            if (Validator.isNotNull(checkBoxPostOnClick)) {
229                                    sb.append(checkBoxPostOnClick);
230                            }
231    
232                            sb.append("\"");
233                    }
234    
235                    sb.append(">");
236    
237                    return sb.toString();
238            }
239    
240            private String _align = ALIGN;
241            private String _allRowIds;
242            private int _colspan = COLSPAN;
243            private String _cssClass = CSS_CLASS;
244            private String _formName;
245            private PortletResponse _portletResponse;
246            private String _rowIds;
247            private String _valign = VALIGN;
248    
249    }