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