001    /**
002     * Copyright (c) 2000-2012 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 defaultLanguageId = getDefaultLanguageId();
132    
133                    if (Validator.isNull(defaultLanguageId)) {
134                            defaultLanguageId = (String)pageContext.getAttribute(
135                                    "aui:model-context:defaultLanguageId");
136                    }
137    
138                    if (Validator.isNull(defaultLanguageId)) {
139                            Locale defaultLocale = LocaleUtil.getDefault();
140    
141                            defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
142                    }
143    
144                    String name = getName();
145    
146                    int pos = name.indexOf(StringPool.DOUBLE_DASH);
147    
148                    if (pos != -1) {
149                            name = name.substring(pos + 2, name.length() - 2);
150                    }
151    
152                    String field = getField();
153    
154                    if (Validator.isNull(field)) {
155                            field = getName();
156                    }
157    
158                    String formName = getFormName();
159    
160                    if (formName == null) {
161                            FormTag formTag = (FormTag)findAncestorWithClass(
162                                    this, FormTag.class);
163    
164                            if (formTag != null) {
165                                    formName = formTag.getName();
166                            }
167                    }
168    
169                    String id = getId();
170                    String type = getType();
171    
172                    if (Validator.isNull(id)) {
173                            if (!Validator.equals(type, "assetTags") &&
174                                    !Validator.equals(type, "radio")) {
175    
176                                    id = name;
177                            }
178                            else {
179                                    id = PwdGenerator.getPassword(PwdGenerator.KEY3, 4);
180                            }
181                    }
182    
183                    String label = getLabel();
184    
185                    if (label == null) {
186                            label = TextFormatter.format(name, TextFormatter.K);
187                    }
188    
189                    Class<?> model = getModel();
190    
191                    if (model == null) {
192                            model = (Class<?>)pageContext.getAttribute(
193                                    "aui:model-context:model");
194                    }
195    
196                    _forLabel = id;
197                    _inputName = getName();
198    
199                    String baseType = null;
200    
201                    if ((model != null) && Validator.isNull(type)) {
202                            baseType = ModelHintsUtil.getType(model.getName(), field);
203    
204                            String fieldParam = getFieldParam();
205    
206                            if (Validator.isNotNull(fieldParam)) {
207                                    _forLabel = fieldParam;
208                                    _inputName = fieldParam;
209                            }
210    
211                            if (ModelHintsUtil.isLocalized(model.getName(), field)) {
212                                    _forLabel += StringPool.UNDERLINE + defaultLanguageId;
213                                    _inputName += StringPool.UNDERLINE + defaultLanguageId;
214                            }
215                    }
216                    else if (Validator.isNotNull(type)) {
217                            if (Validator.equals(type, "checkbox") ||
218                                    Validator.equals(type, "radio")) {
219    
220                                    baseType = type;
221                            }
222                    }
223    
224                    if (Validator.isNull(baseType)) {
225                            baseType = "text";
226                    }
227    
228                    setNamespacedAttribute(request, "baseType", baseType);
229                    setNamespacedAttribute(request, "bean", bean);
230                    setNamespacedAttribute(request, "defaultLanguageId", defaultLanguageId);
231                    setNamespacedAttribute(request, "field", field);
232                    setNamespacedAttribute(request, "forLabel", _forLabel);
233                    setNamespacedAttribute(request, "formName", formName);
234                    setNamespacedAttribute(request, "id", id);
235                    setNamespacedAttribute(request, "label", label);
236                    setNamespacedAttribute(request, "model", model);
237    
238                    request.setAttribute(getAttributeNamespace() + "value", getValue());
239            }
240    
241            protected void setEndAttributes() {
242                    HttpServletRequest request =
243                            (HttpServletRequest)pageContext.getRequest();
244    
245                    boolean required = false;
246    
247                    if ((_validators != null) && (_validators.get("required") != null)) {
248                            required = true;
249                    }
250    
251                    setNamespacedAttribute(request, "required", String.valueOf(required));
252            }
253    
254            protected void updateFormValidators() {
255                    if (_validators == null) {
256                            return;
257                    }
258    
259                    HttpServletRequest request =
260                            (HttpServletRequest)pageContext.getRequest();
261    
262                    Map<String, List<ValidatorTag>> validatorTagsMap =
263                            (Map<String, List<ValidatorTag>>)request.getAttribute(
264                                    "aui:form:validatorTagsMap");
265    
266                    List<ValidatorTag> validatorTags = ListUtil.fromMapValues(_validators);
267    
268                    validatorTagsMap.put(_inputName, validatorTags);
269            }
270    
271            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
272    
273            private String _forLabel;
274            private String _inputName;
275            private Map<String, ValidatorTag> _validators;
276    
277    }