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;
024import org.jgrapes.core.Channel;
025import org.jgrapes.core.Components;
026
027/**
028 * A notification (as defined by the JSON RPC specification) to be sent to
029 * the web console component view (the browser).
030 */
031@SuppressWarnings("PMD.DataClass")
032public class NotifyConletView extends ConsoleCommand {
033
034    private final String conletType;
035    private final String conletId;
036    private final String method;
037    private final Object[] params;
038
039    /**
040     * Creates a new event.
041     *  
042     * @param conletType the web console component type (used by the console 
043     * core JS to look up the available functions, see {@link AddConletType})
044     * @param conletId the web console component (view) instance that the 
045     * notification is directed at
046     * @param method the method (function) to be executed, must
047     * have been registered by handling {@link AddConletType}
048     * @param params the parameters
049     */
050    public NotifyConletView(String conletType,
051            String conletId, String method, Object... params) {
052        this.conletType = conletType;
053        this.conletId = conletId;
054        this.method = method;
055        this.params = Arrays.copyOf(params, params.length);
056    }
057
058    /**
059     * Returns the web console component class.
060     * 
061     * @return the web console component class
062     */
063    public String conletType() {
064        return conletType;
065    }
066
067    /**
068     * Returns the web console component id.
069     * 
070     * @return the web console component id
071     */
072    public String conletId() {
073        return conletId;
074    }
075
076    /**
077     * Returns the method to be executed.
078     * 
079     * @return the method
080     */
081    public String method() {
082        return method;
083    }
084
085    /**
086     * Returns the parameters.
087     * 
088     * @return the parameters
089     */
090    public Object[] params() {
091        return Arrays.copyOf(params, params.length);
092    }
093
094    @Override
095    public void emitJson(Writer writer) throws IOException {
096        emitJson(writer, "notifyConletView", conletType(), conletId(),
097            method(), params());
098    }
099
100    /*
101     * (non-Javadoc)
102     * 
103     * @see java.lang.Object#toString()
104     */
105    @Override
106    public String toString() {
107        StringBuilder builder = new StringBuilder(40);
108        builder.append(Components.objectName(this))
109            .append(" [conletId=").append(conletId)
110            .append(", method=").append(method);
111        if (channels() != null) {
112            builder.append(", channels=").append(Channel.toString(channels()));
113        }
114        builder.append(']');
115        return builder.toString();
116    }
117}