Package de.innovationgate.wgpublisher.webtml

Source Code of de.innovationgate.wgpublisher.webtml.EventScript$Status

/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
*
* This file is part of the OpenWGA server platform.
*
* OpenWGA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, a special exception is granted by the copyright holders
* of OpenWGA called "OpenWGA plugin exception". You should have received
* a copy of this exception along with OpenWGA in file COPYING.
* If not, see <http://www.openwga.com/gpl-plugin-exception>.
*
* OpenWGA 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenWGA in file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package de.innovationgate.wgpublisher.webtml;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.collections.map.LinkedMap;

import de.innovationgate.webgate.api.WGAPIException;
import de.innovationgate.wgpublisher.webtml.utils.PortletEvent;
import de.innovationgate.wgpublisher.webtml.utils.TMLAction;
import de.innovationgate.wgpublisher.webtml.utils.TMLException;
import de.innovationgate.wgpublisher.webtml.utils.TMLPortlet;

public class EventScript extends Script {
   
    private String _onevent;
   
    private String _ajax;
   
    private String _portletmode;
   
    private String portletcontext;
   
    public static class Status extends Evaluate.Status {
        private boolean _executeEvent;
    }
   
    @Override
    public BaseTagStatus createTagStatus() {
        return new Status();
    }

    public void tmlStartTag() throws TMLException, WGAPIException {
       
        Status status = (Status) getStatus();
        String name = getOnevent();
        if (name == null) {
            throw new TMLException("Event name was not specified", true);
        }
       
        TMLPortlet portlet = getTMLContext().getportlet();
        if (portlet == null) {
            throw new TMLException("Personalisation is not available", true);
        }
       
        // Test if an event of that name has been issued since last run
        status._executeEvent = false;
        Long beginIndex = (Long) getTMLContext().option(Base.OPTION_PORTLET_EVENT_STARTINDEX);
        if (beginIndex != null) {
            List events = findEventsOfName(name, beginIndex);           
            if (events.size() > 0) {
                getTMLContext().setvar("portletEvents", events);
                getTMLContext().setvar("portletEvent", events.get(events.size() - 1));
                status._executeEvent = true;
            }
        }

        setEvalBody(status._executeEvent);
       
        super.tmlStartTag();
    }

    /**
     * Searches the fired events queue for events of a name, beginning at a specified index
     * @param name The event name to search for
     * @param index The start index in the queue
     * @return a list of found events
     * @throws WGAPIException
     */
    private List findEventsOfName(String name, Long index) throws WGAPIException {

        List foundEvents = new ArrayList();
        HttpSession session = getPageContext().getSession();
        LinkedMap events = TMLPortlet.getFiredEventsQueue(session);
       
        if (events.size() == 0) {
            return foundEvents;
        }
       
        // Find the start index. This is either the index after the last processed index, or - if the last processed
        // index is not available in the queue - the first index in the queue.
        if (events.containsKey(index)) {
            index = (Long) events.nextKey(index);
        }
        else {
            index = (Long) events.firstKey();
        }
       
       
        synchronized (events) {
            PortletEvent event;
            while (index != null) {
                event = (PortletEvent) events.get(index);
                String targetPortletKey = event.getTargetPortletKey();
                if (targetPortletKey == null || targetPortletKey.equals(getTMLContext().getportlet().getportletkey())) {
                    if (event.getName().equalsIgnoreCase(name)) {
                        foundEvents.add(event);
                    }
                }
                index = (Long) events.nextKey(index);
               
            }
        }
       
        return foundEvents;
       
   
    }

    public String getOnevent() {
        return getTagAttributeValue("onevent", _onevent, null);
    }

    public void setOnevent(String name) {
        this._onevent = name;
    }

    public void tmlEndTag() throws TMLException, WGAPIException {
       
        Status status = (Status) getStatus();
       
        // Exec event
        if (status._executeEvent) {
            super.tmlEndTag();
            try {
                getTMLContext().removevar("portletEvent");
            }
            catch (WGAPIException e) {
                throw new TMLException("Exception removing portletEvent variable", e, true);
            }
        }
        else {
            setResult("");
        }
       
        // Register as client-side listener for this event
        String jsFunction;
        if (stringToBoolean(getAjax()) == true) {
            jsFunction = "new Function(\"" + getAjaxJSFunction(new TMLAction("$refresh"), getAjax(), getPortletmode(), getPortletcontext()) + "\")";
        }
        else {
            jsFunction = "new Function(\"" + getJSFunction(new TMLAction("$refresh"), getPortletmode(), getPortletcontext()) + "\")";
        }
       
        StringBuffer eventReg = new StringBuffer();
        eventReg.append("<script language=\"javascript\">\n");
        eventReg.append("<!--\n");
        eventReg.append("WGA.event.register(\"");
        eventReg.append(getTMLContext().getportlet().getportletkey());
        eventReg.append("\", \"");
        eventReg.append(getOnevent());
        eventReg.append("\", " + jsFunction + ");\n");
        eventReg.append("//-->\n");
        eventReg.append("</script>\n");
        setSuffix(eventReg.toString());
       
       
    }

    public String getAjax() {
        return getTagAttributeValue("ajax", _ajax, "true");
    }

    public void setAjax(String ajax) {
        _ajax = ajax;
    }

  public String getPortletmode() {
    return getTagAttributeValue("portletmode", _portletmode, null);
  }

  public void setPortletmode(String portletmode) {
    _portletmode = portletmode;
  }
 
  public String getPortletcontext() {
    return getTagAttributeValue("portletcontext", portletcontext, null);
  }

  public void setPortletcontext(String context) {
    portletcontext = context;
  }

}
TOP

Related Classes of de.innovationgate.wgpublisher.webtml.EventScript$Status

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.