001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 name = getName();
132    
133                    int pos = name.indexOf(StringPool.DOUBLE_DASH);
134    
135                    if (pos != -1) {
136                            name = name.substring(pos + 2, name.length() - 2);
137                    }
138    
139                    String field = getField();
140    
141                    if (Validator.isNull(field)) {
142                            field = getName();
143                    }
144    
145                    String formName = getFormName();
146    
147                    if (formName == null) {
148                            FormTag formTag = (FormTag)findAncestorWithClass(
149                                    this, FormTag.class);
150    
151                            if (formTag != null) {
152                                    formName = formTag.getName();
153                            }
154                    }
155    
156                    String id = getId();
157                    String type = getType();
158    
159                    if (Validator.isNull(id)) {
160                            if (!Validator.equals(type, "assetTags") &&
161                                    !Validator.equals(type, "radio")) {
162    
163                                    id = name;
164                            }
165                            else {
166                                    id = PwdGenerator.getPassword(PwdGenerator.KEY3, 4);
167                            }
168                    }
169    
170                    String label = getLabel();
171    
172                    if (label == null) {
173                            label = TextFormatter.format(name, TextFormatter.K);
174                    }
175    
176                    Class<?> model = getModel();
177    
178                    if (model == null) {
179                            model = (Class<?>)pageContext.getAttribute(
180                                    "aui:model-context:model");
181                    }
182    
183                    _forLabel = id;
184    
185                    String baseType = null;
186    
187                    if ((model != null) && Validator.isNull(type)) {
188                            baseType = ModelHintsUtil.getType(model.getName(), field);
189    
190                            String fieldParam = getFieldParam();
191    
192                            if (Validator.isNotNull(fieldParam)) {
193                                    _forLabel = fieldParam;
194                            }
195    
196                            if (ModelHintsUtil.isLocalized(model.getName(), field)) {
197                                    Locale defaultLocale = LocaleUtil.getDefault();
198                                    String defaultLanguageId = LocaleUtil.toLanguageId(
199                                            defaultLocale);
200    
201                                    _forLabel += StringPool.UNDERLINE + defaultLanguageId;
202                            }
203                    }
204                    else if (Validator.isNotNull(type)) {
205                            if (Validator.equals(type, "checkbox") ||
206                                    Validator.equals(type, "radio")) {
207    
208                                    baseType = type;
209                            }
210                    }
211    
212                    if (Validator.isNull(baseType)) {
213                            baseType = "text";
214                    }
215    
216                    setNamespacedAttribute(request, "baseType", baseType);
217                    setNamespacedAttribute(request, "bean", bean);
218                    setNamespacedAttribute(request, "field", field);
219                    setNamespacedAttribute(request, "forLabel", _forLabel);
220                    setNamespacedAttribute(request, "formName", formName);
221                    setNamespacedAttribute(request, "id", id);
222                    setNamespacedAttribute(request, "label", label);
223                    setNamespacedAttribute(request, "model", model);
224    
225                    request.setAttribute(getAttributeNamespace() + "value", getValue());
226            }
227    
228            protected void setEndAttributes() {
229                    HttpServletRequest request =
230                            (HttpServletRequest)pageContext.getRequest();
231    
232                    boolean required = false;
233    
234                    if ((_validators != null) && (_validators.get("required") != null)) {
235                            required = true;
236                    }
237    
238                    setNamespacedAttribute(request, "required", String.valueOf(required));
239            }
240    
241            protected void updateFormValidators() {
242                    if (_validators == null) {
243                            return;
244                    }
245    
246                    HttpServletRequest request =
247                            (HttpServletRequest)pageContext.getRequest();
248    
249                    Map<String, List<ValidatorTag>> validatorTagsMap =
250                            (Map<String, List<ValidatorTag>>)request.getAttribute(
251                                    "aui:form:validatorTagsMap");
252    
253                    List<ValidatorTag> validatorTags = ListUtil.fromMapValues(_validators);
254    
255                    validatorTagsMap.put(getName(), validatorTags);
256            }
257    
258            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
259    
260            private String _forLabel;
261            private Map<String, ValidatorTag> _validators;
262    
263    }