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
091 ValidatorTag validatorTag = new ValidatorTagImpl(
092 validatorName, validatorErrorMessage, validatorValue,
093 customValidator);
094
095 addValidatorTag(validatorName, validatorTag);
096 }
097 }
098
099 protected void addRequiredValidatorTag() {
100 if (!getRequired()) {
101 return;
102 }
103
104 ValidatorTag validatorTag = new ValidatorTagImpl(
105 "required", null, null, false);
106
107 addValidatorTag("required", validatorTag);
108 }
109
110 protected void addValidatorTag(
111 String validatorName, ValidatorTag validatorTag) {
112
113 if (_validators == null) {
114 _validators = new HashMap<String, ValidatorTag>();
115 }
116
117 _validators.put(validatorName, validatorTag);
118 }
119
120 @Override
121 protected void cleanUp() {
122 super.cleanUp();
123
124 _forLabel = null;
125 _validators = null;
126 }
127
128 @Override
129 protected boolean isCleanUpSetAttributes() {
130 return _CLEAN_UP_SET_ATTRIBUTES;
131 }
132
133 @Override
134 protected void setAttributes(HttpServletRequest request) {
135 super.setAttributes(request);
136
137 Object bean = getBean();
138
139 if (bean == null) {
140 bean = pageContext.getAttribute("aui:model-context:bean");
141 }
142
143 Class<?> model = getModel();
144
145 if (model == null) {
146 model = (Class<?>)pageContext.getAttribute(
147 "aui:model-context:model");
148 }
149
150 String defaultLanguageId = getDefaultLanguageId();
151
152 if (Validator.isNull(defaultLanguageId)) {
153 defaultLanguageId = (String)pageContext.getAttribute(
154 "aui:model-context:defaultLanguageId");
155 }
156
157 if (Validator.isNull(defaultLanguageId)) {
158 if ((model != null) &&
159 ModelHintsUtil.hasField(model.getName(), "groupId")) {
160
161 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
162 WebKeys.THEME_DISPLAY);
163
164 defaultLanguageId = LocaleUtil.toLanguageId(
165 themeDisplay.getSiteDefaultLocale());
166 }
167 }
168
169 if (Validator.isNull(defaultLanguageId)) {
170 Locale defaultLocale = LocaleUtil.getDefault();
171
172 defaultLanguageId = LocaleUtil.toLanguageId(defaultLocale);
173 }
174
175 String name = getName();
176
177 int pos = name.indexOf(StringPool.DOUBLE_DASH);
178
179 if (pos != -1) {
180 name = name.substring(pos + 2, name.length() - 2);
181 }
182
183 String field = getField();
184
185 if (Validator.isNull(field)) {
186 field = getName();
187 }
188
189 String formName = getFormName();
190
191 if (formName == null) {
192 FormTag formTag = (FormTag)findAncestorWithClass(
193 this, FormTag.class);
194
195 if (formTag != null) {
196 formName = formTag.getName();
197 }
198 }
199
200 String id = getId();
201 String type = getType();
202
203 if (Validator.isNull(id)) {
204 String fieldParam = getFieldParam();
205
206 if ((model != null) && Validator.isNull(type) &&
207 Validator.isNotNull(fieldParam)) {
208
209 id = fieldParam;
210 }
211 else if (!Validator.equals(type, "assetTags") &&
212 !Validator.equals(type, "radio")) {
213
214 id = name;
215 }
216 else {
217 id = StringUtil.randomId();
218 }
219 }
220
221 String label = getLabel();
222
223 if (label == null) {
224 label = TextFormatter.format(name, TextFormatter.K);
225 }
226
227 _forLabel = id;
228 _inputName = getName();
229
230 String languageId = getLanguageId();
231
232 if (Validator.isNotNull(languageId)) {
233 _forLabel = _forLabel + StringPool.UNDERLINE + languageId;
234 }
235
236 String baseType = null;
237
238 if ((model != null) && Validator.isNull(type)) {
239 baseType = ModelHintsUtil.getType(model.getName(), field);
240
241 String fieldParam = getFieldParam();
242
243 if (Validator.isNotNull(fieldParam)) {
244 _inputName = fieldParam;
245 }
246 }
247 else if (Validator.isNotNull(type)) {
248 if (Validator.equals(type, "checkbox") ||
249 Validator.equals(type, "radio")) {
250
251 baseType = type;
252 }
253 }
254
255 if (Validator.isNull(baseType)) {
256 baseType = "text";
257 }
258
259 boolean wrappedField = false;
260
261 FieldWrapperTag fieldWrapper = (FieldWrapperTag)findAncestorWithClass(
262 this, FieldWrapperTag.class);
263
264 if (fieldWrapper != null) {
265 wrappedField = true;
266 }
267
268 setNamespacedAttribute(request, "baseType", baseType);
269 setNamespacedAttribute(request, "bean", bean);
270 setNamespacedAttribute(request, "defaultLanguageId", defaultLanguageId);
271 setNamespacedAttribute(request, "field", field);
272 setNamespacedAttribute(request, "forLabel", _forLabel);
273 setNamespacedAttribute(request, "formName", formName);
274 setNamespacedAttribute(request, "id", id);
275 setNamespacedAttribute(request, "label", label);
276 setNamespacedAttribute(request, "model", model);
277 setNamespacedAttribute(request, "wrappedField", wrappedField);
278
279 request.setAttribute(getAttributeNamespace() + "value", getValue());
280
281 if ((_validators != null) && (_validators.get("required") != null)) {
282 setNamespacedAttribute(
283 request, "required", Boolean.TRUE.toString());
284 }
285 }
286
287 protected void updateFormValidators() {
288 if (_validators == null) {
289 return;
290 }
291
292 HttpServletRequest request =
293 (HttpServletRequest)pageContext.getRequest();
294
295 Map<String, List<ValidatorTag>> validatorTagsMap =
296 (Map<String, List<ValidatorTag>>)request.getAttribute(
297 "aui:form:validatorTagsMap");
298
299 if (validatorTagsMap != null) {
300 List<ValidatorTag> validatorTags = ListUtil.fromMapValues(
301 _validators);
302
303 String inputName = _inputName;
304
305 if (Validator.equals(getType(), "checkbox")) {
306 inputName = inputName.concat("Checkbox");
307 }
308
309 String languageId = getLanguageId();
310
311 if (Validator.isNotNull(languageId)) {
312 inputName = inputName + StringPool.UNDERLINE + languageId;
313 }
314
315 validatorTagsMap.put(inputName, validatorTags);
316 }
317 }
318
319 private static final boolean _CLEAN_UP_SET_ATTRIBUTES = true;
320
321 private String _forLabel;
322 private String _inputName;
323 private Map<String, ValidatorTag> _validators;
324
325 }