it.delli.mwebc.command.impl.EventCommand.java Source code

Java tutorial

Introduction

Here is the source code for it.delli.mwebc.command.impl.EventCommand.java

Source

/*
 * (C) Copyright 2010, by Francesco Delli Paoli.
 * 
 * Project Info:  http://mwebc.sourceforge.net/
 * 
 * This file is part of mwebc - Model Web Controller.
 * 
 * mwebc 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 of the License, or
 * (at your option) any later version.
 * 
 * mwebc 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 mwebc; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package it.delli.mwebc.command.impl;

import it.delli.mwebc.annotations.CommandDefinition;
import it.delli.mwebc.command.Command;
import it.delli.mwebc.event.Event;
import it.delli.mwebc.event.listener.EventListener;
import it.delli.mwebc.event.listener.PageEventListener;
import it.delli.mwebc.ui.Page;
import it.delli.mwebc.ui.Widget;
import it.delli.mwebc.utils.ReflectionUtils;

import java.lang.reflect.Method;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.gson.JsonObject;

@CommandDefinition("event")
public class EventCommand implements Command {

    private static Log log = LogFactory.getLog(EventCommand.class);

    public void execute(Page page, JsonObject data) {
        String eventName = data.get("event").getAsString();
        String eventType = data.get("eventType").getAsString();
        String method = data.get("forward").getAsString();
        Widget widget = null;
        EventListener eventListener = null;
        if (data.get("id") != null) {
            widget = page.getWidget(data.get("id").getAsString());
            eventListener = widget.getEventListener(eventName);
        } else {
            eventListener = page.getEventListener();
        }
        Class eventTypeClass = page.getApplication().getEventClass(eventType);
        Event event = null;
        try {
            event = (Event) eventTypeClass.getConstructor(String.class, Widget.class, JsonObject.class)
                    .newInstance(eventName, widget, data.get("data").getAsJsonObject());
        } catch (Exception e) {
            log.error("Exception in creating event instance for event type " + eventType);
        }
        ;
        event.setPage(page);
        // bind widgets
        ReflectionUtils.bindWidgets(eventListener, page);
        //
        Method forwardMethod = null;
        if (eventListener instanceof PageEventListener && page.getWidget(method) != null) {
            try {
                it.delli.mwebc.widget.Method widgetMethod = (it.delli.mwebc.widget.Method) page.getWidget(method);
                ScriptEngineManager factory = new ScriptEngineManager();
                ScriptEngine engine = factory.getEngineByName("groovy");
                engine.put("eventListener", null);
                engine.eval(widgetMethod.getScript());
                eventListener = (EventListener) engine.get("eventListener");
                forwardMethod = eventListener.getClass().getMethod(method, Event.class);
                forwardMethod.setAccessible(true);
            } catch (Exception e) {
                log.error("Exception in getting method to execute", e);
            }
        } else {
            try {
                forwardMethod = eventListener.getClass().getMethod(method, Event.class);
                forwardMethod.setAccessible(true);
            } catch (Exception e) {
                log.error("Exception in getting method to execute", e);
            }
        }
        if (forwardMethod != null) {
            if (event.getWidget() != null) {
                log.info("Executing forward method " + eventListener.getClass().getName() + "."
                        + forwardMethod.getName() + " for event " + event.getName() + " on widget "
                        + event.getWidget().getClass().getName() + " (" + event.getWidget().getId() + ")");
            } else {
                log.info("Executing forward method " + eventListener.getClass().getName() + "."
                        + forwardMethod.getName() + " for event " + event.getName());
            }
            try {
                forwardMethod.invoke(eventListener, event);
            } catch (Exception e) {
                if (event.getWidget() != null) {
                    log.error("Exception in executing of forward method " + eventListener.getClass().getName() + "."
                            + forwardMethod.getName() + " for event " + event.getName() + " on widget "
                            + event.getWidget().getClass().getName() + " (" + event.getWidget().getId() + ")", e);
                } else {
                    log.error("Exception in executing of forward method " + eventListener.getClass().getName() + "."
                            + forwardMethod.getName() + " for event " + event.getName(), e);
                }
                log.error("Exception in forward method execution", e);
            }
        } else {
            log.info("Forward method does not exist in event listener " + eventListener.getClass().getName()
                    + " for event " + event.getName());
            log.info("Notifying end of failed execution for event " + event.getName());
        }
    }

}