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