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