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