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                    return getAllRowsCheckbox(_allRowIds, StringUtil.quote(_rowIds));
062            }
063    
064            public String getAllRowsId() {
065                    return getAllRowIds();
066            }
067    
068            public int getColspan() {
069                    return _colspan;
070            }
071    
072            public String getCssClass() {
073                    return _cssClass;
074            }
075    
076            public String getFormName() {
077                    return _formName;
078            }
079    
080            public String getRowCheckBox(
081                    HttpServletRequest request, boolean checked, boolean disabled,
082                    String primaryKey) {
083    
084                    return getRowCheckBox(
085                            checked, disabled, _rowIds, primaryKey, StringUtil.quote(_rowIds),
086                            StringUtil.quote(_allRowIds), StringPool.BLANK);
087            }
088    
089            public String getRowId() {
090                    return getRowIds();
091            }
092    
093            public String getRowIds() {
094                    return _rowIds;
095            }
096    
097            public String getValign() {
098                    return _valign;
099            }
100    
101            public boolean isChecked(Object obj) {
102                    return false;
103            }
104    
105            public boolean isDisabled(Object obj) {
106                    return false;
107            }
108    
109            public void setAlign(String align) {
110                    _align = align;
111            }
112    
113            public void setAllRowIds(String allRowIds) {
114                    _allRowIds = getNamespacedValue(allRowIds);
115            }
116    
117            public void setColspan(int colspan) {
118                    _colspan = colspan;
119            }
120    
121            public void setCssClass(String cssClass) {
122                    _cssClass = cssClass;
123            }
124    
125            public void setFormName(String formName) {
126                    _formName = getNamespacedValue(formName);
127            }
128    
129            public void setRowIds(String rowIds) {
130                    _rowIds = getNamespacedValue(rowIds);
131            }
132    
133            public void setValign(String valign) {
134                    _valign = valign;
135            }
136    
137            protected String getAllRowsCheckbox(String name, String checkBoxRowIds) {
138                    if (Validator.isNull(name)) {
139                            return StringPool.BLANK;
140                    }
141                    else {
142                            StringBuilder sb = new StringBuilder(9);
143    
144                            sb.append("<input name=\"");
145                            sb.append(name);
146                            sb.append("\" type=\"checkbox\" ");
147                            sb.append("onClick=\"Liferay.Util.checkAll(");
148                            sb.append("AUI().one(this).ancestor('");
149                            sb.append(".table'), ");
150                            sb.append(checkBoxRowIds);
151                            sb.append(", this, 'tr:not(.lfr-template)'");
152                            sb.append(");\">");
153    
154                            return sb.toString();
155                    }
156            }
157    
158            protected String getNamespacedValue(String value) {
159                    if (Validator.isNull(value)) {
160                            return StringPool.BLANK;
161                    }
162                    else {
163                            if (!value.startsWith(_portletResponse.getNamespace())) {
164                                    value = _portletResponse.getNamespace() + value;
165                            }
166    
167                            return value;
168                    }
169            }
170    
171            protected String getRowCheckBox(
172                    boolean checked, boolean disabled, String name, String value,
173                    String checkBoxRowIds, String checkBoxAllRowIds,
174                    String checkBoxPostOnClick) {
175    
176                    StringBundler sb = new StringBundler();
177    
178                    sb.append("<input ");
179    
180                    if (checked) {
181                            sb.append("checked ");
182                    }
183    
184                    if (disabled) {
185                            sb.append("disabled ");
186                    }
187    
188                    sb.append("name=\"");
189                    sb.append(name);
190                    sb.append("\" type=\"checkbox\" value=\"");
191                    sb.append(value);
192                    sb.append("\" ");
193    
194                    if (Validator.isNotNull(_allRowIds)) {
195                            sb.append("onClick=\"Liferay.Util.checkAllBox(");
196                            sb.append("AUI().one(this).ancestor('");
197                            sb.append(".table'), ");
198                            sb.append(checkBoxRowIds);
199                            sb.append(", ");
200                            sb.append(checkBoxAllRowIds);
201                            sb.append(");");
202                            sb.append("AUI().one(this).ancestor('tr:not(.lfr-template)').");
203                            sb.append("toggleClass('info');");
204    
205                            if (Validator.isNotNull(checkBoxPostOnClick)) {
206                                    sb.append(checkBoxPostOnClick);
207                            }
208    
209                            sb.append("\"");
210                    }
211    
212                    sb.append(">");
213    
214                    return sb.toString();
215            }
216    
217            private String _align = ALIGN;
218            private String _allRowIds;
219            private int _colspan = COLSPAN;
220            private String _cssClass = CSS_CLASS;
221            private String _formName;
222            private PortletResponse _portletResponse;
223            private String _rowIds;
224            private String _valign = VALIGN;
225    
226    }