001    /**
002     * Copyright (c) 2000-2011 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                    addModelValidators();
055    
056                    return super.doStartTag();
057            }
058    
059            protected void addModelValidators() {
060                    Class<?> model = getModel();
061    
062                    if (model == null) {
063                            model = (Class<?>)pageContext.getAttribute(
064                                    "aui:model-context:model");
065                    }
066    
067                    if ((model == null) || Validator.isNotNull(getType())) {
068                            return;
069                    }
070    
071                    String field = getField();
072    
073                    if (Validator.isNull(field)) {
074                            field = getName();
075                    }
076    
077                    List<Tuple> modelValidators = ModelHintsUtil.getValidators(
078                            model.getName(), field);
079    
080                    if (modelValidators == null) {
081                            return;
082                    }
083    
084                    for (Tuple modelValidator : modelValidators) {
085                            String validatorName = (String)modelValidator.getObject(1);
086                            String validatorErrorMessage = (String)modelValidator.getObject(2);
087                            String validatorValue = (String)modelValidator.getObject(3);
088                            boolean customValidator = (Boolean)modelValidator.getObject(4);
089    
090                            ValidatorTag validatorTag = new ValidatorTagImpl(
091                                    validatorName, validatorErrorMessage, validatorValue,
092                                    customValidator);
093    
094                            addValidatorTag(validatorName, validatorTag);
095                    }
096            }
097    
098            protected void addValidatorTag(
099                    String validatorName, ValidatorTag validatorTag) {
100    
101                    if (_validators == null) {
102                            _validators = new HashMap<String, ValidatorTag>();
103                    }
104    
105                    _validators.put(validatorName, validatorTag);
106            }
107    
108            @Override
109            protected void cleanUp() {
110                    super.cleanUp();
111    
112                    _forLabel = null;
113                    _validators = null;
114            }
115    
116            @Override
117            protected boolean isCleanUpSetAttributes() {
118                    return _CLEAN_UP_SET_ATTRIBUTES;
119            }
120    
121            @Override
122            protected void setAttributes(HttpServletRequest request) {
123                    super.setAttributes(request);
124    
125                    Object bean = getBean();
126    
127                    if (bean == null) {
128                            bean = pageContext.getAttribute("aui:model-context:bean");
129                    }
130    
131                    String name = getName();
132    
133                    int pos = name.indexOf(StringPool.DOUBLE_DASH);
134    
135                    if (pos != -1) {
136                            name = name.substring(pos + 2, name.length() - 2);
137                    }
138    
139                    String field = getField();
140    
141                    if (Validator.isNull(field)) {
142                            field = getName();
143                    }
144    
145                    String formName = getFormName();
146    
147                    if (formName == null) {
148                            FormTag formTag = (FormTag)findAncestorWithClass(
149                                    this, FormTag.class);
150    
151                            if (formTag != null) {
152                                    formName = formTag.getName();
153                            }
154                    }
155    
156                    String id = getId();
157                    String type = getType();
158    
159                    if (Validator.isNull(id)) {
160                            if (!Validator.equals(type, "assetTags") &&
161                                    !Validator.equals(type, "radio")) {
162    
163                                    id = name;
164                            }
165                            else {
166                                    id = PwdGenerator.getPassword(PwdGenerator.KEY3, 4);
167                            }
168                    }
169    
170                    String label = getLabel();
171    
172                    if (label == null) {
173                            label = TextFormatter.format(name, TextFormatter.K);
174                    }
175    
176                    Class<?> model = getModel();
177    
178                    if (model == null) {
179                            model = (Class<?>)pageContext.getAttribute(
180                                    "aui:model-context:model");
181                    }
182    
183                    _forLabel = id;
184                    _inputName = getName();
185    
186                    String baseType = null;
187    
188                    if ((model != null) && Validator.isNull(type)) {
189                            baseType = ModelHintsUtil.getType(model.getName(), field);
190    
191                            String fieldParam = getFieldParam();
192    
193                            if (Validator.isNotNull(fieldParam)) {
194                                    _forLabel = fieldParam;
195                                    _inputName = fieldParam;
196                            }
197    
198                            if (ModelHintsUtil.isLocalized(model.getName(), field)) {
199                                    Locale defaultLocale = LocaleUtil.getDefault();
200                                    String defaultLanguageId = LocaleUtil.toLanguageId(
201                                            defaultLocale);
202    
203                                    _forLabel += StringPool.UNDERLINE + defaultLanguageId;
204                                    _inputName += StringPool.UNDERLINE + defaultLanguageId;
205                            }
206                    }
207                    else if (Validator.isNotNull(type)) {
208                            if (Validator.equals(type, "checkbox") ||
209                                    Validator.equals(type, "radio")) {
210    
211                                    baseType = type;
212                            }
213                    }
214    
215                    if (Validator.isNull(baseType)) {
216                            baseType = "text";
217                    }
218    
219                    setNamespacedAttribute(request, "baseType", baseType);
220                    setNamespacedAttribute(request, "bean", bean);
221                    setNamespacedAttribute(request, "field", field);
222                    setNamespacedAttribute(request, "forLabel", _forLabel);
223                    setNamespacedAttribute(request, "formName", formName);
224                    setNamespacedAttribute(request, "id", id);
225                    setNamespacedAttribute(request, "label", label);
226                    setNamespacedAttribute(request, "model", model);
227    
228                    request.setAttribute(getAttributeNamespace() + "value", getValue());
229            }
230    
231            protected void setEndAttributes() {
232                    HttpServletRequest request =
233                            (HttpServletRequest)pageContext.getRequest();
234    
235                    boolean required = false;
236    
237                    if ((_validators != null) && (_validators.get("required") != null)) {
238                            required = true;
239                    }
240    
241                    setNamespacedAttribute(request, "required", String.valueOf(required));
242            }
243    
244            protected void updateFormValidators() {
245                    if (_validators == null) {
246                            return;
247                    }
248    
249                    HttpServletRequest request =
250                            (HttpServletRequest)pageContext.getRequest();
251    
252                    Map<String, List<ValidatorTag>> validatorTagsMap =
253                            (Map<String, List<ValidatorTag>>)request.getAttribute(
254                                    "aui:form:validatorTagsMap");
255    
256                    List<ValidatorTag> validatorTags = ListUtil.fromMapValues(_validators);
257    
258                    validatorTagsMap.put(_inputName, validatorTags);
259            }
260    
261            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
262    
263            private String _forLabel;
264            private String _inputName;
265            private Map<String, ValidatorTag> _validators;
266    
267    }