-
FEATURED COMPONENTS
First time here? Check out the FAQ!
morning ZK community,
in zk framework, how to communicating error message that occur in dao layer to view layer ( zul / viewmodel ) ?
for example I have a dao method :
public void saveAllStudents(Set<Student> students){
for (Students student : students){
save(student);
}
}
when retrieve, however some students are already exist in database. In that time I want to send message to view (zul/viewmodel)
thanks,
try {
do Something here
} catch (Exception e) {
ExceptionHandler.handleException(e, "Orders List ", null,
this.moreErrorInfo + ": onSearch");
}
package com.product.webapp.utilities;
import java.sql.SQLException; import java.util.HashMap;
import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.DataAccessException; import org.zkoss.zk.ui.Executions; import org.zkoss.zul.Messagebox;
public final class ExceptionHandler {
static final Logger LOG = LoggerFactory.getLogger(ExceptionHandler.class);
public static void handleException(DataAccessException e, String mtext,
Object Entity, String moreErrorInfo) {
String userMessage = "";
String objectToString = "";
final HashMap<String, Object> map = new HashMap<String, Object>();
final HashMap<String, Object> argmap = new HashMap<String, Object>();
if (Entity != null) {
objectToString = Entity.toString();
map.put("objectToString", objectToString);
}
// First check for Known mysql Error Codes
int errorCode = getSqlErrorCode(e);
switch (errorCode) {
case 1062:
userMessage = "This " + mtext
+ " cannot be added/updated because it is already exists.";
break;
case 1451:
userMessage = "This " + mtext
+ " cannot be deleted because it has been used in other places.";
break;
default:
moreErrorInfo = moreErrorInfo + " Sql Error Code : " + errorCode;
userMessage = null;
}
if (userMessage == null) {
LOG.error(
"User ID : {} \nError : {} \nObject : {} \nMore Info : {}",
FHSessionUtil.getCurrentUser().getID(), e.toString(),
objectToString, moreErrorInfo);
map.put("Exception", e);
moreErrorInfo = "User ID : "
+ FHSessionUtil.getCurrentUser().getID() + "\n\r"
+ moreErrorInfo;
map.put("moreErrorInfo", moreErrorInfo);
argmap.put("map", map);
Executions.createComponents("/WEB-INF/sys/errordialog.zul", null,
argmap);
}
if (StringUtils.isNotBlank(userMessage)) {
Messagebox.show(userMessage, "Error", Messagebox.OK,
Messagebox.ERROR);
}
}
public static void handleException(Exception e, String mtext,
Object Entity, String moreErrorInfo) {
String objectToString = "";
final HashMap<String, Object> map = new HashMap<String, Object>();
final HashMap<String, Object> argmap = new HashMap<String, Object>();
if (e instanceof DataAccessException) {
handleException((DataAccessException) e, mtext, Entity,
moreErrorInfo);
return;
}
if (Entity != null) {
objectToString = Entity.toString();
map.put("objectToString", objectToString);
}
LOG.error("User ID : {} \nError : {} \nObject : {} \nMore Info : {}",
FHSessionUtil.getCurrentUser().getID(), e.toString(),
objectToString, moreErrorInfo);
map.put("Exception", e);
map.put("moreErrorInfo", moreErrorInfo);
argmap.put("map", map);
Executions.createComponents("/WEB-INF/sys/errordialog.zul", null,
argmap);
}
private static int getSqlErrorCode(RuntimeException e) {
Throwable throwable = e;
while (throwable != null && !(throwable instanceof SQLException)) {
throwable = throwable.getCause();
}
if (throwable instanceof SQLException) {
SQLException sqlex = (SQLException) throwable;
int errorCode = sqlex.getErrorCode();
return errorCode;
}
return 0;
}
}
Asked: 2013-07-20 03:32:15 +0800
Seen: 20 times
Last updated: Jul 20 '13
ZK MVC CRUD With Spring 3 + JPA + Hibernate 4 Entity Manager
saving spread sheet data into database?
how to insert data to sql server database using jsp and zk?
Source files for the demo ZK in 10m- User Registration
how to set the selected item in a listbox related to a bandbox
Editable interface to add users