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.TextFormatter;
022    import com.liferay.portal.kernel.util.Tuple;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.ModelHintsUtil;
025    import com.liferay.taglib.aui.base.BaseInputTag;
026    import com.liferay.util.PwdGenerator;
027    
028    import java.util.HashMap;
029    import java.util.List;
030    import java.util.Locale;
031    import java.util.Map;
032    
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.jsp.JspException;
035    
036    /**
037     * @author Julio Camarero
038     * @author Jorge Ferrer
039     * @author Brian Wing Shun Chan
040     */
041    public class InputTag extends BaseInputTag {
042    
043            @Override
044            public int doEndTag() throws JspException {
045                    updateFormValidators();
046    
047                    setEndAttributes();
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                            Locale defaultLocale = LocaleUtil.getDefault();
159    
160                            defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
161                    }
162    
163                    String name = getName();
164    
165                    int pos = name.indexOf(StringPool.DOUBLE_DASH);
166    
167                    if (pos != -1) {
168                            name = name.substring(pos + 2, name.length() - 2);
169                    }
170    
171                    String field = getField();
172    
173                    if (Validator.isNull(field)) {
174                            field = getName();
175                    }
176    
177                    String formName = getFormName();
178    
179                    if (formName == null) {
180                            FormTag formTag = (FormTag)findAncestorWithClass(
181                                    this, FormTag.class);
182    
183                            if (formTag != null) {
184                                    formName = formTag.getName();
185                            }
186                    }
187    
188                    String id = getId();
189                    String type = getType();
190    
191                    if (Validator.isNull(id)) {
192                            String fieldParam = getFieldParam();
193    
194                            if ((model != null) && Validator.isNull(type) &&
195                                    Validator.isNotNull(fieldParam)) {
196    
197                                    id = fieldParam;
198                            }
199                            else if (!Validator.equals(type, "assetTags") &&
200                                             !Validator.equals(type, "radio")) {
201    
202                                    id = name;
203                            }
204                            else {
205                                    id = PwdGenerator.getPassword(PwdGenerator.KEY3, 4);
206                            }
207                    }
208    
209                    String label = getLabel();
210    
211                    if (label == null) {
212                            label = TextFormatter.format(name, TextFormatter.K);
213                    }
214    
215                    _forLabel = id;
216                    _inputName = getName();
217    
218                    String baseType = null;
219    
220                    if ((model != null) && Validator.isNull(type)) {
221                            baseType = ModelHintsUtil.getType(model.getName(), field);
222    
223                            String fieldParam = getFieldParam();
224    
225                            if (Validator.isNotNull(fieldParam)) {
226                                    _inputName = fieldParam;
227                            }
228    
229                            if (ModelHintsUtil.isLocalized(model.getName(), field)) {
230                                    _forLabel += StringPool.UNDERLINE + defaultLanguageId;
231                                    _inputName += StringPool.UNDERLINE + defaultLanguageId;
232                            }
233                    }
234                    else if (Validator.isNotNull(type)) {
235                            if (Validator.equals(type, "checkbox") ||
236                                    Validator.equals(type, "radio")) {
237    
238                                    baseType = type;
239                            }
240                    }
241    
242                    if (Validator.isNull(baseType)) {
243                            baseType = "text";
244                    }
245    
246                    setNamespacedAttribute(request, "baseType", baseType);
247                    setNamespacedAttribute(request, "bean", bean);
248                    setNamespacedAttribute(request, "defaultLanguageId", defaultLanguageId);
249                    setNamespacedAttribute(request, "field", field);
250                    setNamespacedAttribute(request, "forLabel", _forLabel);
251                    setNamespacedAttribute(request, "formName", formName);
252                    setNamespacedAttribute(request, "id", id);
253                    setNamespacedAttribute(request, "label", label);
254                    setNamespacedAttribute(request, "model", model);
255    
256                    request.setAttribute(getAttributeNamespace() + "value", getValue());
257            }
258    
259            protected void setEndAttributes() {
260                    if ((_validators == null) || (_validators.get("required") == null)) {
261                            return;
262                    }
263    
264                    HttpServletRequest request =
265                            (HttpServletRequest)pageContext.getRequest();
266    
267                    setNamespacedAttribute(request, "required", Boolean.TRUE.toString());
268            }
269    
270            protected void updateFormValidators() {
271                    if (_validators == null) {
272                            return;
273                    }
274    
275                    HttpServletRequest request =
276                            (HttpServletRequest)pageContext.getRequest();
277    
278                    Map<String, List<ValidatorTag>> validatorTagsMap =
279                            (Map<String, List<ValidatorTag>>)request.getAttribute(
280                                    "aui:form:validatorTagsMap");
281    
282                    List<ValidatorTag> validatorTags = ListUtil.fromMapValues(_validators);
283    
284                    validatorTagsMap.put(_inputName, validatorTags);
285            }
286    
287            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
288    
289            private String _forLabel;
290            private String _inputName;
291            private Map<String, ValidatorTag> _validators;
292    
293    }