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