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.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.HashMap;
031 import java.util.List;
032 import java.util.Locale;
033 import java.util.Map;
034
035 import javax.servlet.http.HttpServletRequest;
036 import javax.servlet.jsp.JspException;
037
038
043 public class InputTag extends BaseInputTag {
044
045 @Override
046 public int doEndTag() throws JspException {
047 updateFormValidators();
048
049 return super.doEndTag();
050 }
051
052 @Override
053 public int doStartTag() throws JspException {
054 addModelValidatorTags();
055 addRequiredValidatorTag();
056
057 return super.doStartTag();
058 }
059
060 protected void addModelValidatorTags() {
061 Class<?> model = getModel();
062
063 if (model == null) {
064 model = (Class<?>)pageContext.getAttribute(
065 "aui:model-context:model");
066 }
067
068 if ((model == null) || Validator.isNotNull(getType())) {
069 return;
070 }
071
072 String field = getField();
073
074 if (Validator.isNull(field)) {
075 field = getName();
076 }
077
078 List<Tuple> modelValidators = ModelHintsUtil.getValidators(
079 model.getName(), field);
080
081 if (modelValidators == null) {
082 return;
083 }
084
085 for (Tuple modelValidator : modelValidators) {
086 String validatorName = (String)modelValidator.getObject(1);
087 String validatorErrorMessage = (String)modelValidator.getObject(2);
088 String validatorValue = (String)modelValidator.getObject(3);
089 boolean customValidator = (Boolean)modelValidator.getObject(4);
090 boolean customValidatorRequired = (Boolean)modelValidator.getObject(
091 5);
092
093 ValidatorTag validatorTag = new ValidatorTagImpl(
094 validatorName, validatorErrorMessage, validatorValue,
095 customValidator, customValidatorRequired);
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 label = getLabel();
223
224 if (label == null) {
225 label = TextFormatter.format(name, TextFormatter.P);
226 }
227
228 String title = getTitle();
229
230 if ((title == null) && (Validator.isNull(label) ||
231 Validator.equals(type, "image"))) {
232
233 title = TextFormatter.format(name, TextFormatter.P);
234 }
235
236 String forLabel = id;
237
238 if (Validator.equals(type,"assetTags")) {
239 forLabel = forLabel.concat("assetTagNames");
240 }
241 else if (Validator.equals(type, "checkbox")) {
242 forLabel = forLabel.concat("Checkbox");
243 }
244
245 _inputName = getName();
246
247 String languageId = getLanguageId();
248
249 if (Validator.isNotNull(languageId)) {
250 forLabel = forLabel + StringPool.UNDERLINE + languageId;
251 }
252
253 String baseType = null;
254
255 if ((model != null) && Validator.isNull(type)) {
256 baseType = ModelHintsUtil.getType(model.getName(), field);
257
258 String fieldParam = getFieldParam();
259
260 if (Validator.isNotNull(fieldParam)) {
261 _inputName = fieldParam;
262 }
263 }
264 else if (Validator.isNotNull(type)) {
265 if (Validator.equals(type, "checkbox") ||
266 Validator.equals(type, "radio") ||
267 Validator.equals(type, "resource")) {
268
269 baseType = type;
270 }
271 }
272
273 if (Validator.isNull(baseType)) {
274 baseType = "text";
275 }
276
277 boolean wrappedField = false;
278
279 FieldWrapperTag fieldWrapper = (FieldWrapperTag)findAncestorWithClass(
280 this, FieldWrapperTag.class);
281
282 if (fieldWrapper != null) {
283 wrappedField = true;
284 }
285
286 setNamespacedAttribute(request, "baseType", baseType);
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 if ((_validators != null) && (_validators.get("required") != null)) {
301 setNamespacedAttribute(
302 request, "required", Boolean.TRUE.toString());
303 }
304 }
305
306 protected void updateFormValidators() {
307 if (_validators == null) {
308 return;
309 }
310
311 HttpServletRequest request =
312 (HttpServletRequest)pageContext.getRequest();
313
314 Map<String, List<ValidatorTag>> validatorTagsMap =
315 (Map<String, List<ValidatorTag>>)request.getAttribute(
316 "aui:form:validatorTagsMap");
317
318 if (validatorTagsMap != null) {
319 List<ValidatorTag> validatorTags = ListUtil.fromMapValues(
320 _validators);
321
322 String inputName = _inputName;
323
324 if (Validator.equals(getType(), "checkbox")) {
325 inputName = inputName.concat("Checkbox");
326 }
327
328 String languageId = getLanguageId();
329
330 if (Validator.isNotNull(languageId)) {
331 inputName = inputName + StringPool.UNDERLINE + languageId;
332 }
333
334 validatorTagsMap.put(inputName, validatorTags);
335 }
336 }
337
338 private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
339
340 private String _inputName;
341 private Map<String, ValidatorTag> _validators;
342
343 }