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.P);
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 title = getTitle();
120    
121                    if ((title == null) && Validator.isNull(label)) {
122                            title = TextFormatter.format(name, TextFormatter.P);
123                    }
124    
125                    String value = StringPool.BLANK;
126    
127                    if (Validator.isNull(listType)) {
128                            if (bean != null) {
129                                    value = BeanPropertiesUtil.getStringSilent(bean, name, value);
130                            }
131    
132                            if (!getIgnoreRequestValue()) {
133                                    value = ParamUtil.getString(request, name, value);
134                            }
135                    }
136    
137                    setNamespacedAttribute(request, "bean", bean);
138                    setNamespacedAttribute(request, "id", id);
139                    setNamespacedAttribute(request, "label", label);
140                    setNamespacedAttribute(request, "listTypeFieldName", listTypeFieldName);
141                    setNamespacedAttribute(request, "title", String.valueOf(title));
142                    setNamespacedAttribute(request, "value", value);
143            }
144    
145            protected void updateFormValidators() {
146                    if (_validators == null) {
147                            return;
148                    }
149    
150                    HttpServletRequest request =
151                            (HttpServletRequest)pageContext.getRequest();
152    
153                    Map<String, List<ValidatorTag>> validatorTagsMap =
154                            (Map<String, List<ValidatorTag>>)request.getAttribute(
155                                    "aui:form:validatorTagsMap");
156    
157                    if (validatorTagsMap != null) {
158                            List<ValidatorTag> validatorTags = ListUtil.fromMapValues(
159                                    _validators);
160    
161                            validatorTagsMap.put(getName(), validatorTags);
162                    }
163            }
164    
165            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
166    
167            private Map<String, ValidatorTag> _validators;
168    
169    }