001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.webconsole.base.events;
020
021import org.jgrapes.core.Event;
022import org.jgrapes.webconsole.base.RenderSupport;
023
024/**
025 * A decoded notification (as defined by the JSON RPC specification) that
026 * invokes a method on a web console component model. Usually, though 
027 * not necessarily, the web console component component responds by sending a
028 * {@link NotifyConletView} to update the web console component representation.
029 * 
030 * ![Event Sequence](NotifyConletModelSeq.svg)
031 * 
032 * @startuml NotifyConletModelSeq.svg
033 * hide footbox
034 * 
035 * Browser -> WebConsole: "notifyConletModel"
036 * activate WebConsole
037 * WebConsole -> Conlet: NotifyConletModel
038 * deactivate WebConsole
039 * activate Conlet
040 * Conlet -> WebConsole: NotifyConletView
041 * deactivate Conlet
042 * activate WebConsole
043 * WebConsole -> Browser: "notifyConletView"
044 * deactivate WebConsole
045 * 
046 * @enduml
047 */
048@SuppressWarnings("PMD.DataClass")
049public class NotifyConletModel extends Event<Void> {
050
051    private final RenderSupport renderSupport;
052    private final String conletId;
053    private final String method;
054    private final Object[] params;
055
056    /**
057     * Creates a new event.
058     * 
059     * @param renderSupport the render support from the web console in case
060     * the response requires it
061     * @param conletId the web console component model that the notification 
062     * is directed at 
063     * @param method the method to be executed
064     * @param params parameters
065     */
066    @SuppressWarnings({ "PMD.UseVarargs", "PMD.ArrayIsStoredDirectly" })
067    public NotifyConletModel(RenderSupport renderSupport,
068            String conletId, String method, Object[] params) {
069        this.renderSupport = renderSupport;
070        this.conletId = conletId;
071        this.method = method;
072        this.params = params;
073    }
074
075    /**
076     * Returns the render support.
077     * 
078     * @return the render support
079     */
080    public RenderSupport renderSupport() {
081        return renderSupport;
082    }
083
084    /**
085     * Returns the web console component id.
086     * 
087     * @return the web console component id
088     */
089    public String conletId() {
090        return conletId;
091    }
092
093    /**
094     * Returns the method.
095     * 
096     * @return the method
097     */
098    public String method() {
099        return method;
100    }
101
102    /**
103     * Returns the parameters.
104     * 
105     * @return the parameters
106     */
107    @SuppressWarnings("PMD.MethodReturnsInternalArray")
108    public Object[] params() {
109        return params;
110    }
111
112    /**
113     * Returns the selected param, cast to the generic type.
114     *
115     * @param <T> the generic type
116     * @param index the index
117     * @return the t
118     */
119    @SuppressWarnings("unchecked")
120    public <T> T param(int index) {
121        return (T) params[index];
122    }
123}