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.util.ParamUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.TextFormatter;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.taglib.aui.base.BaseSelectTag;
023    
024    import javax.servlet.http.HttpServletRequest;
025    
026    /**
027     * @author Julio Camarero
028     * @author Jorge Ferrer
029     * @author Brian Wing Shun Chan
030     */
031    public class SelectTag extends BaseSelectTag {
032    
033            @Override
034            protected boolean isCleanUpSetAttributes() {
035                    return _CLEAN_UP_SET_ATTRIBUTES;
036            }
037    
038            @Override
039            protected void setAttributes(HttpServletRequest request) {
040                    super.setAttributes(request);
041    
042                    Object bean = getBean();
043    
044                    if (bean == null) {
045                            bean = pageContext.getAttribute("aui:model-context:bean");
046                    }
047    
048                    String name = getName();
049    
050                    int pos = name.indexOf(StringPool.DOUBLE_DASH);
051    
052                    if (pos != -1) {
053                            name = name.substring(pos + 2, name.length() - 2);
054                    }
055    
056                    String id = getId();
057    
058                    if (Validator.isNull(id)) {
059                            id = name;
060                    }
061    
062                    String label = getLabel();
063    
064                    if (label == null) {
065                            label = TextFormatter.format(name, TextFormatter.K);
066                    }
067    
068                    String listType = getListType();
069                    String listTypeFieldName = getListTypeFieldName();
070    
071                    if (Validator.isNotNull(listType) &&
072                            Validator.isNull(listTypeFieldName)) {
073    
074                            listTypeFieldName = "typeId";
075                    }
076    
077                    String value = StringPool.BLANK;
078    
079                    if (Validator.isNull(listType)) {
080                            if (bean != null) {
081                                    value = BeanPropertiesUtil.getStringSilent(bean, name, value);
082                            }
083    
084                            if (!getIgnoreRequestValue()) {
085                                    value = ParamUtil.getString(request, name, value);
086                            }
087                    }
088    
089                    setNamespacedAttribute(request, "bean", bean);
090                    setNamespacedAttribute(request, "id", id);
091                    setNamespacedAttribute(request, "label", label);
092                    setNamespacedAttribute(request, "listTypeFieldName", listTypeFieldName);
093                    setNamespacedAttribute(request, "value", value);
094            }
095    
096            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
097    
098    }