Author: chris.laprun(a)jboss.com
Date: 2012-01-25 12:15:54 -0500 (Wed, 25 Jan 2012)
New Revision: 8309
Added:
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/AbstractApplicationMessage.java
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/CompoundApplicationMessage.java
Modified:
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/ApplicationMessage.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/core/UIApplication.java
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/core/UIPopupMessages.java
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/exception/MessageException.java
Log:
- Added CompoundApplicationMessage and AbstractApplicationMessage to be able to collect
all messages (in case there might be several) before reporting the errors.
Added:
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/AbstractApplicationMessage.java
===================================================================
---
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/AbstractApplicationMessage.java
(rev 0)
+++
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/AbstractApplicationMessage.java 2012-01-25
17:15:54 UTC (rev 8309)
@@ -0,0 +1,98 @@
+/**
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.web.application;
+
+import org.exoplatform.commons.utils.PropertyManager;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+
+import java.io.Serializable;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+/**
+ * Created by The eXo Platform SARL
+ * Author : Dang Van Minh
+ * minhdv81(a)yahoo.com
+ * Jun 7, 2006
+ */
+public abstract class AbstractApplicationMessage implements Serializable
+{
+ private static Log log = ExoLogger.getLogger(ApplicationMessage.class);
+
+ final public static int ERROR = 0, WARNING = 1, INFO = 2;
+
+ private int type_ = INFO;
+
+ private transient ResourceBundle resourceBundle;
+
+ private boolean argsLocalized = true;
+
+
+ public abstract String getMessage();
+
+ public void setResourceBundle(ResourceBundle resourceBundle)
+ {
+ this.resourceBundle = resourceBundle;
+ }
+
+ public int getType()
+ {
+ return type_;
+ }
+
+ public void setType(int type)
+ {
+ this.type_ = type;
+ }
+
+ public void setArgsLocalized(boolean argsLocalized)
+ {
+ this.argsLocalized = argsLocalized;
+ }
+
+ public boolean isArgsLocalized()
+ {
+ return argsLocalized;
+ }
+
+ protected String resolveMessage(String key)
+ {
+ if (key == null && resourceBundle == null)
+ {
+ return key;
+ }
+
+ String value;
+ try
+ {
+ value = resourceBundle.getString(key);
+ }
+ catch (MissingResourceException ex)
+ {
+ if (PropertyManager.isDevelopping())
+ {
+ log.warn("Can not find resource bundle for key : " + key);
+ }
+ value = key.substring(key.lastIndexOf('.') + 1);
+ }
+ return value;
+ }
+}
Modified:
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/ApplicationMessage.java
===================================================================
---
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/ApplicationMessage.java 2012-01-24
02:52:00 UTC (rev 8308)
+++
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/ApplicationMessage.java 2012-01-25
17:15:54 UTC (rev 8309)
@@ -1,64 +1,46 @@
-/**
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
+/*
+* JBoss, a division of Red Hat
+* Copyright 2008, Red Hat Middleware, LLC, and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+*/
package org.exoplatform.web.application;
-import org.exoplatform.commons.utils.PropertyManager;
-import org.exoplatform.services.log.ExoLogger;
-import org.exoplatform.services.log.Log;
-
import java.io.Serializable;
-import java.util.MissingResourceException;
-import java.util.ResourceBundle;
-/**
- * Created by The eXo Platform SARL
- * Author : Dang Van Minh
- * minhdv81(a)yahoo.com
- * Jun 7, 2006
- */
-public class ApplicationMessage implements Serializable
+/** @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a> */
+public class ApplicationMessage extends AbstractApplicationMessage implements
Serializable
{
- private static Log log = ExoLogger.getLogger(ApplicationMessage.class);
-
- final public static int ERROR = 0, WARNING = 1, INFO = 2;
+ private final String messageKey_;
+ private final Object[] messageArgs_;
- private int type_ = INFO;
-
- private String messageKey_;
-
- private ResourceBundle resourceBundle;
-
- private Object[] messageArgs_;
-
- private boolean argsLocalized = true;
-
public ApplicationMessage(String key, Object[] args)
{
- messageKey_ = key;
- messageArgs_ = args;
+ this.messageKey_ = key;
+ this.messageArgs_ = args;
}
public ApplicationMessage(String key, Object[] args, int type)
{
- this(key, args);
- type_ = type;
+ this.messageKey_ = key;
+ this.messageArgs_ = args;
+ setType(type);
}
public String getMessage()
@@ -66,75 +48,17 @@
String msg = resolveMessage(messageKey_);
if (msg != null && messageArgs_ != null)
{
- for(int i = 0; i < messageArgs_.length; i++)
+ for (int i = 0; i < messageArgs_.length; i++)
{
- String arg = messageArgs_ [i].toString();
- if (isArgsLocalized())
+ String arg = messageArgs_[i].toString();
+ if (isArgsLocalized())
{
arg = resolveMessage(arg);
}
- msg = msg.replace("{" + i + "}", arg);
- }
- }
-
- return msg;
- }
-
- public void setResourceBundle(ResourceBundle resourceBundle)
- {
- this.resourceBundle = resourceBundle;
- }
-
- public String getMessageKey()
- {
- return messageKey_;
- }
-
- public Object[] getMessageAruments()
- {
- return messageArgs_;
- }
-
- public int getType()
- {
- return type_;
- }
-
- public void setType(int type)
- {
- this.type_ = type;
- }
-
- public void setArgsLocalized(boolean argsLocalized)
- {
- this.argsLocalized = argsLocalized;
- }
-
- public boolean isArgsLocalized()
- {
- return argsLocalized;
- }
-
- private String resolveMessage(String key)
- {
- if (key == null && resourceBundle == null)
- {
- return key;
- }
-
- String value;
- try
- {
- value = resourceBundle.getString(key);
- }
- catch (MissingResourceException ex)
- {
- if (PropertyManager.isDevelopping())
- {
- log.warn("Can not find resource bundle for key : " + key);
+ msg = msg.replace("{" + i + "}", arg);
}
- value = key.substring(key.lastIndexOf('.') + 1);
}
- return value;
+
+ return msg;
}
}
Added:
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/CompoundApplicationMessage.java
===================================================================
---
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/CompoundApplicationMessage.java
(rev 0)
+++
portal/trunk/component/web/controller/src/main/java/org/exoplatform/web/application/CompoundApplicationMessage.java 2012-01-25
17:15:54 UTC (rev 8309)
@@ -0,0 +1,68 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2008, Red Hat Middleware, LLC, and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+*/
+
+package org.exoplatform.web.application;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/** @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a> */
+public class CompoundApplicationMessage extends AbstractApplicationMessage implements
Serializable
+{
+ private List<AbstractApplicationMessage> messages = new
ArrayList<AbstractApplicationMessage>(5);
+
+ public CompoundApplicationMessage()
+ {
+ this(null);
+ }
+
+ public CompoundApplicationMessage(AbstractApplicationMessage initialMessage)
+ {
+ if(initialMessage != null)
+ {
+ messages.add(initialMessage);
+ }
+ }
+
+ @Override
+ public String getMessage()
+ {
+ StringBuilder sb = new StringBuilder(255);
+ for (AbstractApplicationMessage message : messages)
+ {
+ sb.append(message.getMessage()).append('\n');
+ }
+
+ return sb.toString();
+ }
+
+ public void addMessage(String messageKey, Object[] args, int type)
+ {
+ messages.add(new ApplicationMessage(messageKey, args, type));
+ }
+
+ public boolean isEmpty()
+ {
+ return messages.isEmpty();
+ }
+}
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java 2012-01-24
02:52:00 UTC (rev 8308)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/form/validator/UsernameValidator.java 2012-01-25
17:15:54 UTC (rev 8309)
@@ -18,6 +18,7 @@
import org.exoplatform.commons.serialization.api.annotations.Serialized;
import org.exoplatform.web.application.ApplicationMessage;
+import org.exoplatform.web.application.CompoundApplicationMessage;
import org.exoplatform.webui.core.UIComponent;
import org.exoplatform.webui.exception.MessageException;
import org.exoplatform.webui.form.UIForm;
@@ -43,46 +44,39 @@
this.min = min;
this.max = max;
}
-
- @Override
+
public void validate(UIFormInput uiInput) throws Exception
{
- if (uiInput.getValue() == null || ((String)uiInput.getValue()).trim().length() ==
0)
+ String value = (String)uiInput.getValue();
+ if (value == null || value.trim().length() == 0)
{
return;
}
+
UIComponent uiComponent = (UIComponent)uiInput;
UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class);
- String label;
- try
+ String label = uiInput.getName();
+ if(uiForm != null)
{
- label = uiForm.getId() + ".label." + uiInput.getName();
+ label = uiForm.getLabel(label);
}
- catch (Exception e)
- {
- label = uiInput.getName();
- }
-
- char[] buff = ((String)uiInput.getValue()).toCharArray();
+
+ CompoundApplicationMessage messages = new CompoundApplicationMessage();
+
+ char[] buff = value.toCharArray();
if(buff.length < min || buff.length > max)
{
- Object[] args = {label, min.toString(), max.toString()};
- throw new MessageException(new
ApplicationMessage("StringLengthValidator.msg.length-invalid", args,
- ApplicationMessage.WARNING));
+ messages.addMessage("StringLengthValidator.msg.length-invalid", new
Object[]{label, min.toString(), max.toString()}, ApplicationMessage.WARNING);
}
if(!isAlphabet(buff[0]))
{
- Object[] args = {label};
- throw new MessageException(new
ApplicationMessage("FirstCharacterNameValidator.msg", args,
- ApplicationMessage.WARNING));
+ messages.addMessage("FirstCharacterNameValidator.msg", new
Object[]{label}, ApplicationMessage.WARNING);
}
if(!isAlphabetOrDigit(buff[buff.length - 1]))
{
- Object[] args = {label, buff[buff.length - 1]};
- throw new MessageException(new
ApplicationMessage("LastCharacterUsernameValidator.msg", args,
- ApplicationMessage.WARNING));
+ messages.addMessage("LastCharacterUsernameValidator.msg", new
Object[]{label, buff[buff.length - 1]}, ApplicationMessage.WARNING);
}
for(int i = 1; i < buff.length -1; i++)
@@ -99,22 +93,23 @@
char next = buff[i + 1];
if (isSymbol(next))
{
- Object[] args = {label, buff[i], buff[i + 1]};
- throw new MessageException(new
ApplicationMessage("ConsecutiveSymbolValidator.msg", args,
- ApplicationMessage.WARNING));
+ messages.addMessage("ConsecutiveSymbolValidator.msg", new
Object[]{label, buff[i], buff[i + 1]}, ApplicationMessage.WARNING);
}
else if (!isAlphabetOrDigit(next))
{
- Object[] args = {label};
- throw new MessageException(new
ApplicationMessage("UsernameValidator.msg.Invalid-char", args,
ApplicationMessage.WARNING));
+ messages.addMessage("UsernameValidator.msg.Invalid-char", new
Object[]{label}, ApplicationMessage.WARNING);
}
}
else
{
- Object[] args = {label};
- throw new MessageException(new
ApplicationMessage("UsernameValidator.msg.Invalid-char", args,
ApplicationMessage.WARNING));
+ messages.addMessage("UsernameValidator.msg.Invalid-char", new
Object[]{label}, ApplicationMessage.WARNING);
}
}
+
+ if(!messages.isEmpty())
+ {
+ throw new MessageException(messages);
+ }
}
private boolean isAlphabet(char c)
Modified:
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/core/UIApplication.java
===================================================================
---
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/core/UIApplication.java 2012-01-24
02:52:00 UTC (rev 8308)
+++
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/core/UIApplication.java 2012-01-25
17:15:54 UTC (rev 8309)
@@ -21,6 +21,7 @@
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
+import org.exoplatform.web.application.AbstractApplicationMessage;
import org.exoplatform.web.application.ApplicationMessage;
import org.exoplatform.webui.application.WebuiRequestContext;
import org.exoplatform.commons.serialization.api.annotations.Serialized;
@@ -82,7 +83,7 @@
return uiPopupMessages_;
}
- public void addMessage(ApplicationMessage message)
+ public void addMessage(AbstractApplicationMessage message)
{
getUIPopupMessages().addMessage(message);
}
Modified:
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/core/UIPopupMessages.java
===================================================================
---
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/core/UIPopupMessages.java 2012-01-24
02:52:00 UTC (rev 8308)
+++
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/core/UIPopupMessages.java 2012-01-25
17:15:54 UTC (rev 8309)
@@ -19,6 +19,7 @@
package org.exoplatform.webui.core;
+import org.exoplatform.web.application.AbstractApplicationMessage;
import org.exoplatform.web.application.ApplicationMessage;
import org.exoplatform.web.application.RequestContext;
import org.exoplatform.webui.application.WebuiRequestContext;
@@ -48,53 +49,53 @@
/**
* The error messages
*/
- private List<ApplicationMessage> errors_;
+ private List<AbstractApplicationMessage> errors_;
/**
* The warning messages
*/
- private List<ApplicationMessage> warnings_;
+ private List<AbstractApplicationMessage> warnings_;
/**
* The info messages
*/
- private List<ApplicationMessage> infos_;
+ private List<AbstractApplicationMessage> infos_;
public UIPopupMessages()
{
- errors_ = new ArrayList<ApplicationMessage>();
- warnings_ = new ArrayList<ApplicationMessage>();
- infos_ = new ArrayList<ApplicationMessage>();
+ errors_ = new ArrayList<AbstractApplicationMessage>();
+ warnings_ = new ArrayList<AbstractApplicationMessage>();
+ infos_ = new ArrayList<AbstractApplicationMessage>();
setShowMask(true);
setShow(true);
}
- public List<ApplicationMessage> getErrors()
+ public List<AbstractApplicationMessage> getErrors()
{
return errors_;
}
- public void setErrors(List<ApplicationMessage> errors_)
+ public void setErrors(List<AbstractApplicationMessage> errors_)
{
this.errors_ = errors_;
}
- public List<ApplicationMessage> getInfos()
+ public List<AbstractApplicationMessage> getInfos()
{
return infos_;
}
- public void setInfos(List<ApplicationMessage> infos_)
+ public void setInfos(List<AbstractApplicationMessage> infos_)
{
this.infos_ = infos_;
}
- public List<ApplicationMessage> getWarnings()
+ public List<AbstractApplicationMessage> getWarnings()
{
return warnings_;
}
- public void setWarnings(List<ApplicationMessage> warnings_)
+ public void setWarnings(List<AbstractApplicationMessage> warnings_)
{
this.warnings_ = warnings_;
}
@@ -104,7 +105,7 @@
super.processRender(context);
}
- public void addMessage(ApplicationMessage msg)
+ public void addMessage(AbstractApplicationMessage msg)
{
msg.setResourceBundle(getResourceBundle());
switch (msg.getType())
Modified:
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/exception/MessageException.java
===================================================================
---
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/exception/MessageException.java 2012-01-24
02:52:00 UTC (rev 8308)
+++
portal/trunk/webui/framework/src/main/java/org/exoplatform/webui/exception/MessageException.java 2012-01-25
17:15:54 UTC (rev 8309)
@@ -19,6 +19,7 @@
package org.exoplatform.webui.exception;
+import org.exoplatform.web.application.AbstractApplicationMessage;
import org.exoplatform.web.application.ApplicationMessage;
/**
@@ -32,15 +33,20 @@
public class MessageException extends Exception
{
- private ApplicationMessage message;
+ private AbstractApplicationMessage message;
- public MessageException(ApplicationMessage message)
+ public MessageException(AbstractApplicationMessage message)
{
this.message = message;
}
- public ApplicationMessage getDetailMessage()
+ /*public MessageException(ApplicationMessage message)
{
+ this.message = message;
+ }*/
+
+ public AbstractApplicationMessage getDetailMessage()
+ {
return message;
}