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