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                    _validators = null;
125            }
126    
127            @Override
128            protected boolean isCleanUpSetAttributes() {
129                    return _CLEAN_UP_SET_ATTRIBUTES;
130            }
131    
132            @Override
133            protected void setAttributes(HttpServletRequest request) {
134                    super.setAttributes(request);
135    
136                    Object bean = getBean();
137    
138                    if (bean == null) {
139                            bean = pageContext.getAttribute("aui:model-context:bean");
140                    }
141    
142                    Class<?> model = getModel();
143    
144                    if (model == null) {
145                            model = (Class<?>)pageContext.getAttribute(
146                                    "aui:model-context:model");
147                    }
148    
149                    String defaultLanguageId = getDefaultLanguageId();
150    
151                    if (Validator.isNull(defaultLanguageId)) {
152                            defaultLanguageId = (String)pageContext.getAttribute(
153                                    "aui:model-context:defaultLanguageId");
154                    }
155    
156                    if (Validator.isNull(defaultLanguageId)) {
157                            if ((model != null) &&
158                                    ModelHintsUtil.hasField(model.getName(), "groupId")) {
159    
160                                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
161                                            WebKeys.THEME_DISPLAY);
162    
163                                    defaultLanguageId = LocaleUtil.toLanguageId(
164                                            themeDisplay.getSiteDefaultLocale());
165                            }
166                    }
167    
168                    if (Validator.isNull(defaultLanguageId)) {
169                            Locale defaultLocale = LocaleUtil.getDefault();
170    
171                            defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
172                    }
173    
174                    String name = getName();
175    
176                    int pos = name.indexOf(StringPool.DOUBLE_DASH);
177    
178                    if (pos != -1) {
179                            name = name.substring(pos + 2, name.length() - 2);
180                    }
181    
182                    String field = getField();
183    
184                    if (Validator.isNull(field)) {
185                            field = getName();
186                    }
187    
188                    String formName = getFormName();
189    
190                    if (formName == null) {
191                            FormTag formTag = (FormTag)findAncestorWithClass(
192                                    this, FormTag.class);
193    
194                            if (formTag != null) {
195                                    formName = formTag.getName();
196                            }
197                    }
198    
199                    String id = getId();
200                    String type = getType();
201    
202                    if (Validator.isNull(id)) {
203                            String fieldParam = getFieldParam();
204    
205                            if ((model != null) && Validator.isNull(type) &&
206                                    Validator.isNotNull(fieldParam)) {
207    
208                                    id = fieldParam;
209                            }
210                            else if (!Validator.equals(type, "assetTags") &&
211                                             !Validator.equals(type, "radio")) {
212    
213                                    id = name;
214                            }
215                            else {
216                                    id = StringUtil.randomId();
217                            }
218                    }
219    
220                    String label = getLabel();
221    
222                    if (label == null) {
223                            label = TextFormatter.format(name, TextFormatter.P);
224                    }
225    
226                    String title = getTitle();
227    
228                    if ((title == null) && (Validator.isNull(label) ||
229                             Validator.equals(type, "image"))) {
230    
231                            title = TextFormatter.format(name, TextFormatter.P);
232                    }
233    
234                    String forLabel = id;
235    
236                    if (Validator.equals(type,"assetTags")) {
237                            forLabel = forLabel.concat("assetTagNames");
238                    }
239                    else if (Validator.equals(type, "checkbox")) {
240                            forLabel = forLabel.concat("Checkbox");
241                    }
242    
243                    _inputName = getName();
244    
245                    String languageId = getLanguageId();
246    
247                    if (Validator.isNotNull(languageId)) {
248                            forLabel = forLabel + StringPool.UNDERLINE + languageId;
249                    }
250    
251                    String baseType = null;
252    
253                    if ((model != null) && Validator.isNull(type)) {
254                            baseType = ModelHintsUtil.getType(model.getName(), field);
255    
256                            String fieldParam = getFieldParam();
257    
258                            if (Validator.isNotNull(fieldParam)) {
259                                    _inputName = fieldParam;
260                            }
261                    }
262                    else if (Validator.isNotNull(type)) {
263                            if (Validator.equals(type, "checkbox") ||
264                                    Validator.equals(type, "radio") ||
265                                    Validator.equals(type, "resource")) {
266    
267                                    baseType = type;
268                            }
269                    }
270    
271                    if (Validator.isNull(baseType)) {
272                            baseType = "text";
273                    }
274    
275                    boolean wrappedField = false;
276    
277                    FieldWrapperTag fieldWrapper = (FieldWrapperTag)findAncestorWithClass(
278                            this, FieldWrapperTag.class);
279    
280                    if (fieldWrapper != null) {
281                            wrappedField = true;
282                    }
283    
284                    setNamespacedAttribute(request, "baseType", baseType);
285                    setNamespacedAttribute(request, "bean", bean);
286                    setNamespacedAttribute(request, "defaultLanguageId", defaultLanguageId);
287                    setNamespacedAttribute(request, "field", field);
288                    setNamespacedAttribute(request, "forLabel", forLabel);
289                    setNamespacedAttribute(request, "formName", formName);
290                    setNamespacedAttribute(request, "id", id);
291                    setNamespacedAttribute(request, "label", label);
292                    setNamespacedAttribute(request, "model", model);
293                    setNamespacedAttribute(request, "title", String.valueOf(title));
294                    setNamespacedAttribute(request, "wrappedField", wrappedField);
295    
296                    request.setAttribute(getAttributeNamespace() + "value", getValue());
297    
298                    if ((_validators != null) && (_validators.get("required") != null)) {
299                            setNamespacedAttribute(
300                                    request, "required", Boolean.TRUE.toString());
301                    }
302            }
303    
304            protected void updateFormValidators() {
305                    if (_validators == null) {
306                            return;
307                    }
308    
309                    HttpServletRequest request =
310                            (HttpServletRequest)pageContext.getRequest();
311    
312                    Map<String, List<ValidatorTag>> validatorTagsMap =
313                            (Map<String, List<ValidatorTag>>)request.getAttribute(
314                                    "aui:form:validatorTagsMap");
315    
316                    if (validatorTagsMap != null) {
317                            List<ValidatorTag> validatorTags = ListUtil.fromMapValues(
318                                    _validators);
319    
320                            String inputName = _inputName;
321    
322                            if (Validator.equals(getType(), "checkbox")) {
323                                    inputName = inputName.concat("Checkbox");
324                            }
325    
326                            String languageId = getLanguageId();
327    
328                            if (Validator.isNotNull(languageId)) {
329                                    inputName = inputName + StringPool.UNDERLINE + languageId;
330                            }
331    
332                            validatorTagsMap.put(inputName, validatorTags);
333                    }
334            }
335    
336            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
337    
338            private String _inputName;
339            private Map<String, ValidatorTag> _validators;
340    
341    }