001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.dao.search;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.HtmlUtil;
019    import com.liferay.portal.kernel.util.LocaleThreadLocal;
020    import com.liferay.portal.kernel.util.LocaleUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.theme.ThemeDisplay;
027    
028    import java.util.Locale;
029    
030    import javax.portlet.PortletResponse;
031    
032    import javax.servlet.http.HttpServletRequest;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class RowChecker {
038    
039            public static final String ALIGN = "left";
040    
041            public static final String ALL_ROW_IDS = "allRowIds";
042    
043            public static final int COLSPAN = 1;
044    
045            public static final String CSS_CLASS = StringPool.BLANK;
046    
047            public static final String FORM_NAME = "fm";
048    
049            public static final String ROW_IDS = "rowIds";
050    
051            public static final String VALIGN = "middle";
052    
053            public RowChecker(PortletResponse portletResponse) {
054                    _portletResponse = portletResponse;
055                    _allRowIds = _portletResponse.getNamespace() + ALL_ROW_IDS;
056                    _formName = _portletResponse.getNamespace() + FORM_NAME;
057                    _rowIds = _portletResponse.getNamespace() + ROW_IDS;
058            }
059    
060            public String getAlign() {
061                    return _align;
062            }
063    
064            public String getAllRowIds() {
065                    return _allRowIds;
066            }
067    
068            public String getAllRowsCheckBox() {
069                    return getAllRowsCheckBox(null);
070            }
071    
072            public String getAllRowsCheckBox(HttpServletRequest request) {
073                    return getAllRowsCheckbox(
074                            request, _allRowIds, StringUtil.quote(_rowIds));
075            }
076    
077            public String getAllRowsId() {
078                    return getAllRowIds();
079            }
080    
081            public int getColspan() {
082                    return _colspan;
083            }
084    
085            public String getCssClass() {
086                    return _cssClass;
087            }
088    
089            public String getFormName() {
090                    return _formName;
091            }
092    
093            /**
094             * @deprecated As of 6.2.0, replaced by  {@link
095             *             #getRowCheckBox(HttpServletRequest, boolean, boolean,
096             *             String)}
097             */
098            public String getRowCheckBox(
099                    boolean checked, boolean disabled, String primaryKey) {
100    
101                    return getRowCheckBox(null, checked, disabled, primaryKey);
102            }
103    
104            public String getRowCheckBox(
105                    HttpServletRequest request, boolean checked, boolean disabled,
106                    String primaryKey) {
107    
108                    return getRowCheckBox(
109                            request, checked, disabled, _rowIds, primaryKey,
110                            StringUtil.quote(_rowIds), StringUtil.quote(_allRowIds),
111                            StringPool.BLANK);
112            }
113    
114            public String getRowId() {
115                    return getRowIds();
116            }
117    
118            public String getRowIds() {
119                    return _rowIds;
120            }
121    
122            public String getValign() {
123                    return _valign;
124            }
125    
126            public boolean isChecked(Object obj) {
127                    return false;
128            }
129    
130            public boolean isDisabled(Object obj) {
131                    return false;
132            }
133    
134            public void setAlign(String align) {
135                    _align = align;
136            }
137    
138            public void setAllRowIds(String allRowIds) {
139                    _allRowIds = getNamespacedValue(allRowIds);
140            }
141    
142            public void setColspan(int colspan) {
143                    _colspan = colspan;
144            }
145    
146            public void setCssClass(String cssClass) {
147                    _cssClass = cssClass;
148            }
149    
150            public void setFormName(String formName) {
151                    _formName = getNamespacedValue(formName);
152            }
153    
154            public void setRowIds(String rowIds) {
155                    _rowIds = getNamespacedValue(rowIds);
156            }
157    
158            public void setValign(String valign) {
159                    _valign = valign;
160            }
161    
162            protected String getAllRowsCheckbox(
163                    HttpServletRequest request, String name, String checkBoxRowIds) {
164    
165                    if (Validator.isNull(name)) {
166                            return StringPool.BLANK;
167                    }
168    
169                    StringBuilder sb = new StringBuilder(11);
170    
171                    sb.append("<input name=\"");
172                    sb.append(name);
173                    sb.append("\" title=\"");
174                    sb.append(LanguageUtil.get(getLocale(request), "select-all"));
175                    sb.append("\" type=\"checkbox\" ");
176                    sb.append("onClick=\"Liferay.Util.checkAll(");
177                    sb.append("AUI().one(this).ancestor('");
178                    sb.append(".table'), ");
179                    sb.append(checkBoxRowIds);
180                    sb.append(", this, 'tr:not(.lfr-template)'");
181                    sb.append(");\">");
182    
183                    return sb.toString();
184            }
185    
186            protected Locale getLocale(HttpServletRequest request) {
187                    Locale locale = null;
188    
189                    if (request != null) {
190                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
191                                    WebKeys.THEME_DISPLAY);
192    
193                            locale = themeDisplay.getLocale();
194                    }
195                    else {
196                            locale = LocaleThreadLocal.getThemeDisplayLocale();
197                    }
198    
199                    if (locale == null) {
200                            locale = LocaleUtil.getDefault();
201                    }
202    
203                    return locale;
204            }
205    
206            protected String getNamespacedValue(String value) {
207                    if (Validator.isNull(value)) {
208                            return StringPool.BLANK;
209                    }
210    
211                    if (!value.startsWith(_portletResponse.getNamespace())) {
212                            value = _portletResponse.getNamespace() + value;
213                    }
214    
215                    return value;
216            }
217    
218            protected String getRowCheckBox(
219                    HttpServletRequest request, boolean checked, boolean disabled,
220                    String name, String value, String checkBoxRowIds,
221                    String checkBoxAllRowIds, String checkBoxPostOnClick) {
222    
223                    StringBundler sb = new StringBundler();
224    
225                    sb.append("<input ");
226    
227                    if (checked) {
228                            sb.append("checked ");
229                    }
230    
231                    if (disabled) {
232                            sb.append("disabled ");
233                    }
234    
235                    sb.append("name=\"");
236                    sb.append(name);
237                    sb.append("\" title=\"");
238                    sb.append(LanguageUtil.get(request.getLocale(), "select"));
239                    sb.append("\" type=\"checkbox\" value=\"");
240                    sb.append(HtmlUtil.escapeAttribute(value));
241                    sb.append("\" ");
242    
243                    if (Validator.isNotNull(_allRowIds)) {
244                            sb.append("onClick=\"Liferay.Util.checkAllBox(");
245                            sb.append("AUI().one(this).ancestor('");
246                            sb.append(".table'), ");
247                            sb.append(checkBoxRowIds);
248                            sb.append(", ");
249                            sb.append(checkBoxAllRowIds);
250                            sb.append(");");
251                            sb.append("AUI().one(this).ancestor('tr:not(.lfr-template)').");
252                            sb.append("toggleClass('info');");
253    
254                            if (Validator.isNotNull(checkBoxPostOnClick)) {
255                                    sb.append(checkBoxPostOnClick);
256                            }
257    
258                            sb.append("\"");
259                    }
260    
261                    sb.append(">");
262    
263                    return sb.toString();
264            }
265    
266            private String _align = ALIGN;
267            private String _allRowIds;
268            private int _colspan = COLSPAN;
269            private String _cssClass = CSS_CLASS;
270            private String _formName;
271            private PortletResponse _portletResponse;
272            private String _rowIds;
273            private String _valign = VALIGN;
274    
275    }