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 java.io.IOException;
022import java.io.Writer;
023import java.util.Arrays;
024
025/**
026 * A notification (as defined by the JSON RPC specification) to be sent to
027 * the web console component view (the browser).
028 */
029@SuppressWarnings("PMD.DataClass")
030public class NotifyConletView extends ConsoleCommand {
031
032    private final String conletType;
033    private final String conletId;
034    private final String method;
035    private final Object[] params;
036
037    /**
038     * Creates a new event.
039     *  
040     * @param conletType the web console component type (used by the console 
041     * core JS to look up the available functions, see {@link AddConletType})
042     * @param conletId the web console component (view) instance that the 
043     * notification is directed at
044     * @param method the method (function) to be executed, must
045     * have been registered by handling {@link AddConletType}
046     * @param params the parameters
047     */
048    public NotifyConletView(String conletType,
049            String conletId, String method, Object... params) {
050        this.conletType = conletType;
051        this.conletId = conletId;
052        this.method = method;
053        this.params = Arrays.copyOf(params, params.length);
054    }
055
056    /**
057     * Returns the web console component class.
058     * 
059     * @return the web console component class
060     */
061    public String conletType() {
062        return conletType;
063    }
064
065    /**
066     * Returns the web console component id.
067     * 
068     * @return the web console component id
069     */
070    public String conletId() {
071        return conletId;
072    }
073
074    /**
075     * Returns the method to be executed.
076     * 
077     * @return the method
078     */
079    public String method() {
080        return method;
081    }
082
083    /**
084     * Returns the parameters.
085     * 
086     * @return the parameters
087     */
088    public Object[] params() {
089        return Arrays.copyOf(params, params.length);
090    }
091
092    @Override
093    public void toJson(Writer writer) throws IOException {
094        toJson(writer, "notifyConletView", conletType(), conletId(),
095            method(), params());
096    }
097}