0

Is it possible to do audit trail in common composer than extended in all class? How do I get the name of button that is clicked?

asked 2021-05-18 12:21:29 +0800

soldier gravatar image soldier
1

updated 2021-05-18 15:35:41 +0800

I managed to save it in database, however I dont know how to get the name of button that is clicked. here is my code:

public class CommonComposer extends GenericForwardComposer{

/**
 * 
 */
private static final long serialVersionUID = 6848767592732520915L;
private ActivityService activityService;
private User admin;

@Override
public void doAfterCompose(Component comp) throws Exception {
    // TODO Auto-generated method stub
    super.doAfterCompose(comp);

    if(!SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals("anonymousUser")){
        activityService = (ActivityService) SpringUtil.getBean("activityService");
        admin = ContextHelper.getUser();
    }
}

protected void navigateTo(String url, Map<String,Object> args, Component currentComponent){
    Component parent = currentComponent.getParent();
    currentComponent.detach();
    Executions.createComponents(url, parent, args);

    try {
        if(!SecurityContextHolder.getContext().getAuthentication().getPrincipal().equals("anonymousUser")) {

            admin = ContextHelper.getUser();
            Activity activity = new Activity();
            activity.setDescription("User clicked url: "+url);
            activity.setCreatedDate(new Date());
            activity.setFromUserId(admin.getLoginId());
            activity.setLastUpdateDate(new Date());
            activity.setToUserId(admin.getLoginId());
            activity.setStatus("CLICKED");
            activity.setRole(admin.getType());
            activity.setType(admin.getType()+"_CLICKED");
            activityService.save(activity);

        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

}

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2021-05-19 12:18:58 +0800

hawk gravatar image hawk
2835 1 5
http://hawkphoenix.blogsp... ZK Team

updated 2021-05-19 12:20:46 +0800

In GenericForwardComposer, you can add an event listener on a button's id, e.g. mybutton;

public void onClick$mybutton(Event event) {
    event.getTarget().getId();
}

I assume the name of a button you mean is the button's id.

link publish delete flag offensive edit

Comments

Hi thank you for answering my question. However my GenericForwardComposer file is read only, is there another way to add this event listerner? and how if i want to get the label of the button?

soldier ( 2021-05-19 12:43:55 +0800 )edit
0

answered 2021-05-21 06:17:54 +0800

cor3000 gravatar image cor3000
5920 2 7
ZK Team

updated 2021-05-21 06:22:44 +0800

Another idea: if you need a global place to "monitor" events you can implement the ExecutionMonitor interface (especially the eventStart or eventComplete method) and configure it as a listener in your zk.xml.

<listener>
    <listener-class>my.package.MyExecutionMonitor</listener-class>
</listener>

and some exemplary imlementation

package my.package.example;

import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Execution;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.util.ExecutionMonitor;
import org.zkoss.zul.Button;

public class MyExecutionMonitor implements ExecutionMonitor {
    @Override
    public void executionActivate(Execution exec, Desktop desktop) {}
    @Override
    public void executionWait(Execution exec, Desktop desktop) {}
    @Override
    public void executionDeactivate(Execution exec, Desktop desktop) {}
    @Override
    public void executionAbort(Execution exec, Desktop desktop, Throwable t) {}
    @Override
    public void desktopDestroy(Desktop desktop) {}
    @Override
    public void eventStart(Event event) {
        String eventName = event.getName();
        if(Events.ON_CLICK.equals(eventName)) {
            if(event.getTarget() instanceof Button) {
                Button clickedButton = (Button) event.getTarget();
                String message = String.format("%s on %s with with label %s", 
                        eventName, clickedButton, clickedButton.getLabel());
                System.out.println(message);
            }
        }
    }
    @Override
    public void eventComplete(Event event) {}
    @Override
    public void eventSuspend(Event event) {}    
    @Override
    public void eventResume(Event event) {}
}

Config Docs: zk.xml - Listener Element - ExecutionMonitor

Javadocs: ExecutionMonitor

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

RSS

Stats

Asked: 2021-05-18 12:21:29 +0800

Seen: 13 times

Last updated: May 21

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