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