001    /**
002     * Copyright (c) 2000-2013 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.StringUtil;
022    import com.liferay.portal.kernel.util.TextFormatter;
023    import com.liferay.portal.kernel.util.Tuple;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.model.ModelHintsUtil;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.taglib.aui.base.BaseInputTag;
029    
030    import java.util.List;
031    import java.util.Locale;
032    import java.util.Map;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.jsp.JspException;
036    
037    /**
038     * @author Julio Camarero
039     * @author Jorge Ferrer
040     * @author Brian Wing Shun Chan
041     */
042    public class InputTag extends BaseInputTag {
043    
044            @Override
045            public int doEndTag() throws JspException {
046                    updateFormValidators();
047    
048                    return super.doEndTag();
049            }
050    
051            @Override
052            public int doStartTag() throws JspException {
053                    addModelValidatorTags();
054    
055                    if (getRequired()) {
056                            addRequiredValidatorTag();
057                    }
058    
059                    return super.doStartTag();
060            }
061    
062            public String getBaseType() {
063                    Class<?> model = getModel();
064    
065                    String type = getType();
066    
067                    String baseType = null;
068    
069                    if ((model != null) && Validator.isNull(type)) {
070                            baseType = ModelHintsUtil.getType(model.getName(), getField());
071                    }
072                    else if (Validator.isNotNull(type)) {
073                            if (Validator.equals(type, "checkbox") ||
074                                    Validator.equals(type, "radio") ||
075                                    Validator.equals(type, "resource")) {
076    
077                                    baseType = type;
078                            }
079                    }
080    
081                    if (Validator.isNull(baseType)) {
082                            baseType = "text";
083                    }
084    
085                    return baseType;
086            }
087    
088            @Override
089            public String getField() {
090                    String field = super.getField();
091    
092                    if (Validator.isNull(field)) {
093                            field = getName();
094                    }
095    
096                    return field;
097            }
098    
099            @Override
100            public String getInputName() {
101                    String inputName = getName();
102    
103                    Class<?> model = getModel();
104    
105                    String type = getType();
106    
107                    if ((model != null) && Validator.isNull(type)) {
108                            String fieldParam = getFieldParam();
109    
110                            if (Validator.isNotNull(fieldParam)) {
111                                    inputName = fieldParam;
112                            }
113                    }
114    
115                    return inputName;
116            }
117    
118            @Override
119            public Class<?> getModel() {
120                    Class<?> model = super.getModel();
121    
122                    if (model == null) {
123                            model = (Class<?>)pageContext.getAttribute(
124                                    "aui:model-context:model");
125                    }
126    
127                    return model;
128            }
129    
130            protected void addModelValidatorTags() {
131                    Class<?> model = getModel();
132    
133                    if ((model == null) || Validator.isNotNull(getType())) {
134                            return;
135                    }
136    
137                    List<Tuple> modelValidators = ModelHintsUtil.getValidators(
138                            model.getName(), getField());
139    
140                    if (modelValidators == null) {
141                            return;
142                    }
143    
144                    for (Tuple modelValidator : modelValidators) {
145                            String validatorName = (String)modelValidator.getObject(1);
146                            String validatorErrorMessage = (String)modelValidator.getObject(2);
147                            String validatorValue = (String)modelValidator.getObject(3);
148                            boolean customValidator = (Boolean)modelValidator.getObject(4);
149                            boolean customValidatorRequired = (Boolean)modelValidator.getObject(
150                                    5);
151    
152                            ValidatorTag validatorTag = new ValidatorTagImpl(
153                                    validatorName, validatorErrorMessage, validatorValue,
154                                    customValidator, customValidatorRequired);
155    
156                            addValidatorTag(validatorName, validatorTag);
157                    }
158            }
159    
160            @Override
161            protected boolean isCleanUpSetAttributes() {
162                    return _CLEAN_UP_SET_ATTRIBUTES;
163            }
164    
165            @Override
166            protected void setAttributes(HttpServletRequest request) {
167                    super.setAttributes(request);
168    
169                    Object bean = getBean();
170    
171                    if (bean == null) {
172                            bean = pageContext.getAttribute("aui:model-context:bean");
173                    }
174    
175                    Class<?> model = getModel();
176    
177                    String defaultLanguageId = getDefaultLanguageId();
178    
179                    if (Validator.isNull(defaultLanguageId)) {
180                            defaultLanguageId = (String)pageContext.getAttribute(
181                                    "aui:model-context:defaultLanguageId");
182                    }
183    
184                    if (Validator.isNull(defaultLanguageId)) {
185                            if ((model != null) &&
186                                    ModelHintsUtil.hasField(model.getName(), "groupId")) {
187    
188                                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
189                                            WebKeys.THEME_DISPLAY);
190    
191                                    defaultLanguageId = LocaleUtil.toLanguageId(
192                                            themeDisplay.getSiteDefaultLocale());
193                            }
194                    }
195    
196                    if (Validator.isNull(defaultLanguageId)) {
197                            Locale defaultLocale = LocaleUtil.getDefault();
198    
199                            defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
200                    }
201    
202                    String name = getName();
203    
204                    int pos = name.indexOf(StringPool.DOUBLE_DASH);
205    
206                    if (pos != -1) {
207                            name = name.substring(pos + 2, name.length() - 2);
208                    }
209    
210                    String field = getField();
211    
212                    if (Validator.isNull(field)) {
213                            field = getName();
214                    }
215    
216                    String formName = getFormName();
217    
218                    if (formName == null) {
219                            FormTag formTag = (FormTag)findAncestorWithClass(
220                                    this, FormTag.class);
221    
222                            if (formTag != null) {
223                                    formName = formTag.getName();
224                            }
225                    }
226    
227                    String id = getId();
228                    String type = getType();
229    
230                    if (Validator.isNull(id)) {
231                            String fieldParam = getFieldParam();
232    
233                            if ((model != null) && Validator.isNull(type) &&
234                                    Validator.isNotNull(fieldParam)) {
235    
236                                    id = fieldParam;
237                            }
238                            else if (!Validator.equals(type, "assetTags") &&
239                                             !Validator.equals(type, "radio")) {
240    
241                                    id = name;
242                            }
243                            else {
244                                    id = StringUtil.randomId();
245                            }
246                    }
247    
248                    String label = getLabel();
249    
250                    if (label == null) {
251                            label = TextFormatter.format(name, TextFormatter.P);
252                    }
253    
254                    String title = getTitle();
255    
256                    if ((title == null) && (Validator.isNull(label) ||
257                             Validator.equals(type, "image"))) {
258    
259                            title = TextFormatter.format(name, TextFormatter.P);
260                    }
261    
262                    String forLabel = id;
263    
264                    if (Validator.equals(type,"assetTags")) {
265                            forLabel = forLabel.concat("assetTagNames");
266                    }
267                    else if (Validator.equals(type, "checkbox")) {
268                            forLabel = forLabel.concat("Checkbox");
269                    }
270    
271                    String languageId = getLanguageId();
272    
273                    if (Validator.isNotNull(languageId)) {
274                            forLabel = forLabel + StringPool.UNDERLINE + languageId;
275                    }
276    
277                    boolean wrappedField = getWrappedField();
278    
279                    FieldWrapperTag fieldWrapper = (FieldWrapperTag)findAncestorWithClass(
280                            this, FieldWrapperTag.class);
281    
282                    if (fieldWrapper != null) {
283                            wrappedField = true;
284                    }
285    
286                    setNamespacedAttribute(request, "baseType", getBaseType());
287                    setNamespacedAttribute(request, "bean", bean);
288                    setNamespacedAttribute(request, "defaultLanguageId", defaultLanguageId);
289                    setNamespacedAttribute(request, "field", field);
290                    setNamespacedAttribute(request, "forLabel", forLabel);
291                    setNamespacedAttribute(request, "formName", formName);
292                    setNamespacedAttribute(request, "id", id);
293                    setNamespacedAttribute(request, "label", label);
294                    setNamespacedAttribute(request, "model", model);
295                    setNamespacedAttribute(request, "title", String.valueOf(title));
296                    setNamespacedAttribute(request, "wrappedField", wrappedField);
297    
298                    request.setAttribute(getAttributeNamespace() + "value", getValue());
299    
300                    Map<String, ValidatorTag> validatorTags = getValidatorTags();
301    
302                    if ((validatorTags != null) &&
303                            (validatorTags.get("required") != null)) {
304    
305                            setNamespacedAttribute(
306                                    request, "required", Boolean.TRUE.toString());
307                    }
308            }
309    
310            protected void updateFormValidators() {
311                    List<ValidatorTag> validatorTags = ListUtil.fromMapValues(
312                            getValidatorTags());
313    
314                    if (validatorTags == null) {
315                            return;
316                    }
317    
318                    HttpServletRequest request =
319                            (HttpServletRequest)pageContext.getRequest();
320    
321                    Map<String, List<ValidatorTag>> validatorTagsMap =
322                            (Map<String, List<ValidatorTag>>)request.getAttribute(
323                                    "aui:form:validatorTagsMap");
324    
325                    if (validatorTagsMap != null) {
326                            String inputName = getInputName();
327    
328                            if (Validator.equals(getType(), "checkbox")) {
329                                    inputName = inputName.concat("Checkbox");
330                            }
331    
332                            String languageId = getLanguageId();
333    
334                            if (Validator.isNotNull(languageId)) {
335                                    inputName = inputName + StringPool.UNDERLINE + languageId;
336                            }
337    
338                            validatorTagsMap.put(inputName, validatorTags);
339                    }
340            }
341    
342            private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
343    
344    }