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