001    /**
002     * Copyright (c) 2000-2012 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                    Class<?> model = getModel();
132    
133                    if (model == null) {
134                            model = (Class<?>)pageContext.getAttribute(
135                                    "aui:model-context:model");
136                    }
137    
138                    String defaultLanguageId = getDefaultLanguageId();
139    
140                    if (Validator.isNull(defaultLanguageId)) {
141                            defaultLanguageId = (String)pageContext.getAttribute(
142                                    "aui:model-context:defaultLanguageId");
143                    }
144    
145                    if (Validator.isNull(defaultLanguageId)) {
146                            Locale defaultLocale = LocaleUtil.getDefault();
147    
148                            defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
149                    }
150    
151                    String name = getName();
152    
153                    int pos = name.indexOf(StringPool.DOUBLE_DASH);
154    
155                    if (pos != -1) {
156                            name = name.substring(pos + 2, name.length() - 2);
157                    }
158    
159                    String field = getField();
160    
161                    if (Validator.isNull(field)) {
162                            field = getName();
163                    }
164    
165                    String formName = getFormName();
166    
167                    if (formName == null) {
168                            FormTag formTag = (FormTag)findAncestorWithClass(
169                                    this, FormTag.class);
170    
171                            if (formTag != null) {
172                                    formName = formTag.getName();
173                            }
174                    }
175    
176                    String id = getId();
177                    String type = getType();
178    
179                    if (Validator.isNull(id) &&
180                            ((model == null) || Validator.isNotNull(type))) {
181    
182                            if (!Validator.equals(type, "assetTags") &&
183                                    !Validator.equals(type, "radio")) {
184    
185                                    id = name;
186                            }
187                            else {
188                                    id = PwdGenerator.getPassword(PwdGenerator.KEY3, 4);
189                            }
190                    }
191    
192                    String label = getLabel();
193    
194                    if (label == null) {
195                            label = TextFormatter.format(name, TextFormatter.K);
196                    }
197    
198                    _forLabel = id;
199                    _inputName = getName();
200    
201                    String baseType = null;
202    
203                    if ((model != null) && Validator.isNull(type)) {
204                            baseType = ModelHintsUtil.getType(model.getName(), field);
205    
206                            String fieldParam = getFieldParam();
207    
208                            if (Validator.isNotNull(fieldParam)) {
209                                    _forLabel = fieldParam;
210                                    _inputName = fieldParam;
211                            }
212    
213                            if (ModelHintsUtil.isLocalized(model.getName(), field)) {
214                                    _forLabel += StringPool.UNDERLINE + defaultLanguageId;
215                                    _inputName += StringPool.UNDERLINE + defaultLanguageId;
216                            }
217                    }
218                    else if (Validator.isNotNull(type)) {
219                            if (Validator.equals(type, "checkbox") ||
220                                    Validator.equals(type, "radio")) {
221    
222                                    baseType = type;
223                            }
224                    }
225    
226                    if (Validator.isNull(baseType)) {
227                            baseType = "text";
228                    }
229    
230                    setNamespacedAttribute(request, "baseType", baseType);
231                    setNamespacedAttribute(request, "bean", bean);
232                    setNamespacedAttribute(request, "defaultLanguageId", defaultLanguageId);
233                    setNamespacedAttribute(request, "field", field);
234                    setNamespacedAttribute(request, "forLabel", _forLabel);
235                    setNamespacedAttribute(request, "formName", formName);
236                    setNamespacedAttribute(request, "id", id);
237                    setNamespacedAttribute(request, "label", label);
238                    setNamespacedAttribute(request, "model", model);
239    
240                    request.setAttribute(getAttributeNamespace() + "value", getValue());
241            }
242    
243            protected void setEndAttributes() {
244                    HttpServletRequest request =
245                            (HttpServletRequest)pageContext.getRequest();
246    
247                    boolean required = false;
248    
249                    if ((_validators != null) && (_validators.get("required") != null)) {
250                            required = true;
251                    }
252    
253                    setNamespacedAttribute(request, "required", String.valueOf(required));
254            }
255    
256            protected void updateFormValidators() {
257                    if (_validators == null) {
258                            return;
259                    }
260    
261                    HttpServletRequest request =
262                            (HttpServletRequest)pageContext.getRequest();
263    
264                    Map<String, List<ValidatorTag>> validatorTagsMap =
265                            (Map<String, List<ValidatorTag>>)request.getAttribute(
266                                    "aui:form:validatorTagsMap");
267    
268                    List<ValidatorTag> validatorTags = ListUtil.fromMapValues(_validators);
269    
270                    validatorTagsMap.put(_inputName, validatorTags);
271            }
272    
273            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
274    
275            private String _forLabel;
276            private String _inputName;
277            private Map<String, ValidatorTag> _validators;
278    
279    }