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                    }
080    
081                    if (Validator.isNull(baseType)) {
082                            baseType = "text";
083                    }
084    
085                    return baseType;
086            }
087    
088            @Override
089            public String getField() {
090                    String field = super.getField();
091    
092                    if (Validator.isNull(field)) {
093                            field = getName();
094                    }
095    
096                    return field;
097            }
098    
099            @Override
100            public String getInputName() {
101                    String inputName = getName();
102    
103                    Class<?> model = getModel();
104    
105                    String type = getType();
106    
107                    if ((model != null) && Validator.isNull(type)) {
108                            String fieldParam = getFieldParam();
109    
110                            if (Validator.isNotNull(fieldParam)) {
111                                    inputName = fieldParam;
112                            }
113                    }
114    
115                    return inputName;
116            }
117    
118            @Override
119            public Class<?> getModel() {
120                    Class<?> model = super.getModel();
121    
122                    if (model == null) {
123                            model = (Class<?>)pageContext.getAttribute(
124                                    "aui:model-context:model");
125                    }
126    
127                    return model;
128            }
129    
130            protected void addModelValidatorTags() {
131                    Class<?> model = getModel();
132    
133                    if ((model == null) || Validator.isNotNull(getType())) {
134                            return;
135                    }
136    
137                    List<Tuple> modelValidators = ModelHintsUtil.getValidators(
138                            model.getName(), getField());
139    
140                    if (modelValidators == null) {
141                            return;
142                    }
143    
144                    for (Tuple modelValidator : modelValidators) {
145                            String validatorName = (String)modelValidator.getObject(1);
146                            String validatorErrorMessage = (String)modelValidator.getObject(2);
147                            String validatorValue = (String)modelValidator.getObject(3);
148                            boolean customValidator = (Boolean)modelValidator.getObject(4);
149    
150                            ValidatorTag validatorTag = new ValidatorTagImpl(
151                                    validatorName, validatorErrorMessage, validatorValue,
152                                    customValidator);
153    
154                            addValidatorTag(validatorName, validatorTag);
155                    }
156            }
157    
158            @Override
159            protected boolean isCleanUpSetAttributes() {
160                    return _CLEAN_UP_SET_ATTRIBUTES;
161            }
162    
163            @Override
164            protected void setAttributes(HttpServletRequest request) {
165                    super.setAttributes(request);
166    
167                    Object bean = getBean();
168    
169                    if (bean == null) {
170                            bean = pageContext.getAttribute("aui:model-context:bean");
171                    }
172    
173                    Class<?> model = getModel();
174    
175                    String defaultLanguageId = getDefaultLanguageId();
176    
177                    if (Validator.isNull(defaultLanguageId)) {
178                            defaultLanguageId = (String)pageContext.getAttribute(
179                                    "aui:model-context:defaultLanguageId");
180                    }
181    
182                    if (Validator.isNull(defaultLanguageId)) {
183                            if ((model != null) &&
184                                    ModelHintsUtil.hasField(model.getName(), "groupId")) {
185    
186                                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
187                                            WebKeys.THEME_DISPLAY);
188    
189                                    defaultLanguageId = LocaleUtil.toLanguageId(
190                                            themeDisplay.getSiteDefaultLocale());
191                            }
192                    }
193    
194                    if (Validator.isNull(defaultLanguageId)) {
195                            Locale defaultLocale = LocaleUtil.getDefault();
196    
197                            defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
198                    }
199    
200                    String name = getName();
201    
202                    int pos = name.indexOf(StringPool.DOUBLE_DASH);
203    
204                    if (pos != -1) {
205                            name = name.substring(pos + 2, name.length() - 2);
206                    }
207    
208                    String field = getField();
209    
210                    if (Validator.isNull(field)) {
211                            field = getName();
212                    }
213    
214                    String formName = getFormName();
215    
216                    if (formName == null) {
217                            FormTag formTag = (FormTag)findAncestorWithClass(
218                                    this, FormTag.class);
219    
220                            if (formTag != null) {
221                                    formName = formTag.getName();
222                            }
223                    }
224    
225                    String id = getId();
226                    String type = getType();
227    
228                    if (Validator.isNull(id)) {
229                            String fieldParam = getFieldParam();
230    
231                            if ((model != null) && Validator.isNull(type) &&
232                                    Validator.isNotNull(fieldParam)) {
233    
234                                    id = AUIUtil.normalizeId(fieldParam);
235                            }
236                            else if (!Validator.equals(type, "assetTags") &&
237                                             !Validator.equals(type, "radio")) {
238    
239                                    id = AUIUtil.normalizeId(name);
240                            }
241                            else {
242                                    id = PortalUtil.getUniqueElementId(
243                                            request, StringPool.BLANK, AUIUtil.normalizeId(name));
244                            }
245                    }
246    
247                    String forLabel = id;
248    
249                    if (Validator.equals(type,"assetTags")) {
250                            forLabel = forLabel.concat("assetTagNames");
251                    }
252    
253                    String languageId = getLanguageId();
254    
255                    if (Validator.isNotNull(languageId)) {
256                            forLabel = LocalizationUtil.getLocalizedName(forLabel, languageId);
257                    }
258    
259                    String label = getLabel();
260    
261                    if (label == null) {
262                            label = TextFormatter.format(name, TextFormatter.P);
263                    }
264    
265                    String title = getTitle();
266    
267                    if ((title == null) && (Validator.isNull(label) ||
268                             Validator.equals(type, "image"))) {
269    
270                            title = TextFormatter.format(name, TextFormatter.P);
271                    }
272    
273                    boolean wrappedField = getWrappedField();
274    
275                    FieldWrapperTag fieldWrapper = (FieldWrapperTag)findAncestorWithClass(
276                            this, FieldWrapperTag.class);
277    
278                    if (fieldWrapper != null) {
279                            wrappedField = true;
280                    }
281    
282                    setNamespacedAttribute(request, "baseType", getBaseType());
283                    setNamespacedAttribute(request, "bean", bean);
284                    setNamespacedAttribute(request, "defaultLanguageId", defaultLanguageId);
285                    setNamespacedAttribute(request, "field", field);
286                    setNamespacedAttribute(request, "forLabel", forLabel);
287                    setNamespacedAttribute(request, "formName", formName);
288                    setNamespacedAttribute(request, "id", id);
289                    setNamespacedAttribute(request, "label", label);
290                    setNamespacedAttribute(request, "model", model);
291                    setNamespacedAttribute(request, "title", String.valueOf(title));
292                    setNamespacedAttribute(request, "wrappedField", wrappedField);
293    
294                    request.setAttribute(getAttributeNamespace() + "value", getValue());
295    
296                    Map<String, ValidatorTag> validatorTags = getValidatorTags();
297    
298                    if ((validatorTags != null) &&
299                            (validatorTags.get("required") != null)) {
300    
301                            setNamespacedAttribute(
302                                    request, "required", Boolean.TRUE.toString());
303                    }
304            }
305    
306            protected void updateFormCheckboxNames() {
307                    if (!Validator.equals(getType(), "checkbox")) {
308                            return;
309                    }
310    
311                    List<String> checkboxNames = (List<String>)request.getAttribute(
312                            "aui:form:checkboxNames");
313    
314                    if (checkboxNames != null) {
315                            String inputName = getInputName();
316    
317                            String languageId = getLanguageId();
318    
319                            if (Validator.isNotNull(languageId)) {
320                                    inputName = LocalizationUtil.getLocalizedName(
321                                            inputName, languageId);
322                            }
323    
324                            checkboxNames.add(inputName);
325                    }
326            }
327    
328            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
329    
330    }