0

how to communicating error message between dao and zul

asked 2013-07-20 03:32:15 +0800

progamerdotcom gravatar image progamerdotcom
117 5

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,

delete flag offensive retag edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-07-20 12:20:28 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2623 3 8
http://emrpms.blogspot.in...
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;
}

}

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow
1 follower

RSS

Stats

Asked: 2013-07-20 03:32:15 +0800

Seen: 20 times

Last updated: Jul 20 '13

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More