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.bean.BeanPropertiesUtil;
018    import com.liferay.portal.kernel.servlet.taglib.aui.ValidatorTag;
019    import com.liferay.portal.kernel.util.ListUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.TextFormatter;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.taglib.aui.base.BaseSelectTag;
025    
026    import java.util.HashMap;
027    import java.util.List;
028    import java.util.Map;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.jsp.JspException;
032    
033    /**
034     * @author Julio Camarero
035     * @author Jorge Ferrer
036     * @author Brian Wing Shun Chan
037     */
038    public class SelectTag extends BaseSelectTag {
039    
040            @Override
041            public int doEndTag() throws JspException {
042                    updateFormValidators();
043    
044                    return super.doEndTag();
045            }
046    
047            @Override
048            public int doStartTag() throws JspException {
049                    addRequiredValidatorTag();
050    
051                    return super.doStartTag();
052            }
053    
054            protected void addRequiredValidatorTag() {
055                    if (!getRequired()) {
056                            return;
057                    }
058    
059                    ValidatorTag validatorTag = new ValidatorTagImpl(
060                            "required", null, null, false);
061    
062                    addValidatorTag("required", validatorTag);
063            }
064    
065            protected void addValidatorTag(
066                    String validatorName, ValidatorTag validatorTag) {
067    
068                    if (_validators == null) {
069                            _validators = new HashMap<String, ValidatorTag>();
070                    }
071    
072                    _validators.put(validatorName, validatorTag);
073            }
074    
075            @Override
076            protected boolean isCleanUpSetAttributes() {
077                    return _CLEAN_UP_SET_ATTRIBUTES;
078            }
079    
080            @Override
081            protected void setAttributes(HttpServletRequest request) {
082                    super.setAttributes(request);
083    
084                    Object bean = getBean();
085    
086                    if (bean == null) {
087                            bean = pageContext.getAttribute("aui:model-context:bean");
088                    }
089    
090                    String name = getName();
091    
092                    int pos = name.indexOf(StringPool.DOUBLE_DASH);
093    
094                    if (pos != -1) {
095                            name = name.substring(pos + 2, name.length() - 2);
096                    }
097    
098                    String id = getId();
099    
100                    if (Validator.isNull(id)) {
101                            id = name;
102                    }
103    
104                    String label = getLabel();
105    
106                    if (label == null) {
107                            label = TextFormatter.format(name, TextFormatter.K);
108                    }
109    
110                    String listType = getListType();
111                    String listTypeFieldName = getListTypeFieldName();
112    
113                    if (Validator.isNotNull(listType) &&
114                            Validator.isNull(listTypeFieldName)) {
115    
116                            listTypeFieldName = "typeId";
117                    }
118    
119                    String value = StringPool.BLANK;
120    
121                    if (Validator.isNull(listType)) {
122                            if (bean != null) {
123                                    value = BeanPropertiesUtil.getStringSilent(bean, name, value);
124                            }
125    
126                            if (!getIgnoreRequestValue()) {
127                                    value = ParamUtil.getString(request, name, value);
128                            }
129                    }
130    
131                    setNamespacedAttribute(request, "bean", bean);
132                    setNamespacedAttribute(request, "id", id);
133                    setNamespacedAttribute(request, "label", label);
134                    setNamespacedAttribute(request, "listTypeFieldName", listTypeFieldName);
135                    setNamespacedAttribute(request, "value", value);
136            }
137    
138            protected void updateFormValidators() {
139                    if (_validators == null) {
140                            return;
141                    }
142    
143                    HttpServletRequest request =
144                            (HttpServletRequest)pageContext.getRequest();
145    
146                    Map<String, List<ValidatorTag>> validatorTagsMap =
147                            (Map<String, List<ValidatorTag>>)request.getAttribute(
148                                    "aui:form:validatorTagsMap");
149    
150                    if (validatorTagsMap != null) {
151                            List<ValidatorTag> validatorTags = ListUtil.fromMapValues(
152                                    _validators);
153    
154                            validatorTagsMap.put(getName(), validatorTags);
155                    }
156            }
157    
158            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
159    
160            private Map<String, ValidatorTag> _validators;
161    
162    }