001    /**
002     * Copyright (c) 2000-present 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.taglib.aui;
016    
017    import com.liferay.portal.kernel.servlet.taglib.aui.ValidatorTag;
018    import com.liferay.portal.kernel.util.LocaleUtil;
019    import com.liferay.portal.kernel.util.LocalizationUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.TextFormatter;
022    import com.liferay.portal.kernel.util.Tuple;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.util.WebKeys;
025    import com.liferay.portal.model.ModelHintsUtil;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.taglib.aui.base.BaseInputTag;
029    
030    import java.util.List;
031    import java.util.Locale;
032    import java.util.Map;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.jsp.JspException;
036    
037    /**
038     * @author Julio Camarero
039     * @author Jorge Ferrer
040     * @author Brian Wing Shun Chan
041     */
042    public class InputTag extends BaseInputTag {
043    
044            @Override
045            public int doEndTag() throws JspException {
046                    updateFormCheckboxNames();
047    
048                    return super.doEndTag();
049            }
050    
051            @Override
052            public int doStartTag() throws JspException {
053                    addModelValidatorTags();
054    
055                    if (getRequired()) {
056                            addRequiredValidatorTag();
057                    }
058    
059                    return super.doStartTag();
060            }
061    
062            public String getBaseType() {
063                    Class<?> model = getModel();
064    
065                    String type = getType();
066    
067                    String baseType = null;
068    
069                    if ((model != null) && Validator.isNull(type)) {
070                            baseType = ModelHintsUtil.getType(model.getName(), getField());
071                    }
072                    else if (Validator.isNotNull(type)) {
073                            if (Validator.equals(type, "checkbox") ||
074                                    Validator.equals(type, "radio") ||
075                                    Validator.equals(type, "resource")) {
076    
077                                    baseType = type;
078                            }
079                            else if (Validator.equals(type, "toggle-card") ||
080                                             Validator.equals(type, "toggle-switch")) {
081    
082                                    baseType = "checkbox";
083                            }
084                    }
085    
086                    if (Validator.isNull(baseType)) {
087                            baseType = "text";
088                    }
089    
090                    return baseType;
091            }
092    
093            @Override
094            public String getField() {
095                    String field = super.getField();
096    
097                    if (Validator.isNull(field)) {
098                            field = getName();
099                    }
100    
101                    return field;
102            }
103    
104            @Override
105            public String getInputName() {
106                    String inputName = getName();
107    
108                    Class<?> model = getModel();
109    
110                    String type = getType();
111    
112                    if ((model != null) && Validator.isNull(type)) {
113                            String fieldParam = getFieldParam();
114    
115                            if (Validator.isNotNull(fieldParam)) {
116                                    inputName = fieldParam;
117                            }
118                    }
119    
120                    return inputName;
121            }
122    
123            @Override
124            public Class<?> getModel() {
125                    Class<?> model = super.getModel();
126    
127                    if (model == null) {
128                            model = (Class<?>)pageContext.getAttribute(
129                                    "aui:model-context:model");
130                    }
131    
132                    return model;
133            }
134    
135            protected void addModelValidatorTags() {
136                    Class<?> model = getModel();
137    
138                    if ((model == null) || Validator.isNotNull(getType())) {
139                            return;
140                    }
141    
142                    List<Tuple> modelValidators = ModelHintsUtil.getValidators(
143                            model.getName(), getField());
144    
145                    if (modelValidators == null) {
146                            return;
147                    }
148    
149                    for (Tuple modelValidator : modelValidators) {
150                            String validatorName = (String)modelValidator.getObject(1);
151                            String validatorErrorMessage = (String)modelValidator.getObject(2);
152                            String validatorValue = (String)modelValidator.getObject(3);
153                            boolean customValidator = (Boolean)modelValidator.getObject(4);
154    
155                            ValidatorTag validatorTag = new ValidatorTagImpl(
156                                    validatorName, validatorErrorMessage, validatorValue,
157                                    customValidator);
158    
159                            addValidatorTag(validatorName, validatorTag);
160                    }
161            }
162    
163            @Override
164            protected boolean isCleanUpSetAttributes() {
165                    return _CLEAN_UP_SET_ATTRIBUTES;
166            }
167    
168            @Override
169            protected void setAttributes(HttpServletRequest request) {
170                    super.setAttributes(request);
171    
172                    Object bean = getBean();
173    
174                    if (bean == null) {
175                            bean = pageContext.getAttribute("aui:model-context:bean");
176                    }
177    
178                    Class<?> model = getModel();
179    
180                    String defaultLanguageId = getDefaultLanguageId();
181    
182                    if (Validator.isNull(defaultLanguageId)) {
183                            defaultLanguageId = (String)pageContext.getAttribute(
184                                    "aui:model-context:defaultLanguageId");
185                    }
186    
187                    if (Validator.isNull(defaultLanguageId)) {
188                            if ((model != null) &&
189                                    ModelHintsUtil.hasField(model.getName(), "groupId")) {
190    
191                                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
192                                            WebKeys.THEME_DISPLAY);
193    
194                                    defaultLanguageId = LocaleUtil.toLanguageId(
195                                            themeDisplay.getSiteDefaultLocale());
196                            }
197                    }
198    
199                    if (Validator.isNull(defaultLanguageId)) {
200                            Locale defaultLocale = LocaleUtil.getDefault();
201    
202                            defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
203                    }
204    
205                    String name = getName();
206    
207                    int pos = name.indexOf(StringPool.DOUBLE_DASH);
208    
209                    if (pos != -1) {
210                            name = name.substring(pos + 2, name.length() - 2);
211                    }
212    
213                    String field = getField();
214    
215                    if (Validator.isNull(field)) {
216                            field = getName();
217                    }
218    
219                    String formName = getFormName();
220    
221                    if (formName == null) {
222                            FormTag formTag = (FormTag)findAncestorWithClass(
223                                    this, FormTag.class);
224    
225                            if (formTag != null) {
226                                    formName = formTag.getName();
227                            }
228                    }
229    
230                    String id = getId();
231                    String type = getType();
232    
233                    if (Validator.isNull(id)) {
234                            String fieldParam = getFieldParam();
235    
236                            if ((model != null) && Validator.isNull(type) &&
237                                    Validator.isNotNull(fieldParam)) {
238    
239                                    id = AUIUtil.normalizeId(fieldParam);
240                            }
241                            else if (!Validator.equals(type, "assetTags") &&
242                                             !Validator.equals(type, "radio")) {
243    
244                                    id = AUIUtil.normalizeId(name);
245                            }
246                            else {
247                                    id = PortalUtil.getUniqueElementId(
248                                            request, StringPool.BLANK, AUIUtil.normalizeId(name));
249                            }
250                    }
251    
252                    String forLabel = id;
253    
254                    if (Validator.equals(type, "assetTags")) {
255                            forLabel = forLabel.concat("assetTagNames");
256                    }
257    
258                    String languageId = getLanguageId();
259    
260                    if (Validator.isNotNull(languageId)) {
261                            forLabel = LocalizationUtil.getLocalizedName(forLabel, languageId);
262                    }
263    
264                    String label = getLabel();
265    
266                    if (label == null) {
267                            label = TextFormatter.format(name, TextFormatter.P);
268                    }
269    
270                    String title = getTitle();
271    
272                    if ((title == null) &&
273                            (Validator.isNull(label) || Validator.equals(type, "image"))) {
274    
275                            title = TextFormatter.format(name, TextFormatter.P);
276                    }
277    
278                    boolean wrappedField = getWrappedField();
279    
280                    FieldWrapperTag fieldWrapper = (FieldWrapperTag)findAncestorWithClass(
281                            this, FieldWrapperTag.class);
282    
283                    if (fieldWrapper != null) {
284                            wrappedField = true;
285                    }
286    
287                    setNamespacedAttribute(request, "baseType", getBaseType());
288                    setNamespacedAttribute(request, "bean", bean);
289                    setNamespacedAttribute(request, "defaultLanguageId", defaultLanguageId);
290                    setNamespacedAttribute(request, "field", field);
291                    setNamespacedAttribute(request, "forLabel", forLabel);
292                    setNamespacedAttribute(request, "formName", formName);
293                    setNamespacedAttribute(request, "id", id);
294                    setNamespacedAttribute(request, "label", label);
295                    setNamespacedAttribute(request, "model", model);
296                    setNamespacedAttribute(request, "title", String.valueOf(title));
297                    setNamespacedAttribute(request, "wrappedField", wrappedField);
298    
299                    request.setAttribute(getAttributeNamespace() + "value", getValue());
300    
301                    Map<String, ValidatorTag> validatorTags = getValidatorTags();
302    
303                    if ((validatorTags != null) &&
304                            (validatorTags.get("required") != null)) {
305    
306                            setNamespacedAttribute(
307                                    request, "required", Boolean.TRUE.toString());
308                    }
309            }
310    
311            protected void updateFormCheckboxNames() {
312                    if (!Validator.equals(getBaseType(), "checkbox")) {
313                            return;
314                    }
315    
316                    List<String> checkboxNames = (List<String>)request.getAttribute(
317                            "LIFERAY_SHARED_aui:form:checkboxNames");
318    
319                    if (checkboxNames != null) {
320                            String inputName = getInputName();
321    
322                            String languageId = getLanguageId();
323    
324                            if (Validator.isNotNull(languageId)) {
325                                    inputName = LocalizationUtil.getLocalizedName(
326                                            inputName, languageId);
327                            }
328    
329                            checkboxNames.add(inputName);
330                    }
331            }
332    
333            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
334    
335    }