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.portal.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 portlet view (the browser).
028 */
029@SuppressWarnings("PMD.DataClass")
030public class NotifyPortletView extends PortalCommand {
031
032    private final String portletType;
033    private final String portletId;
034    private final String method;
035    private final Object[] params;
036
037    /**
038     * Creates a new event.
039     *  
040     * @param portletType the portlet type (used by the portal 
041     * core JS to look up the available functions, see {@link AddPortletType})
042     * @param portletId the portlet (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 AddPortletType}
046     * @param params the parameters
047     */
048    public NotifyPortletView(String portletType,
049            String portletId, String method, Object... params) {
050        this.portletType = portletType;
051        this.portletId = portletId;
052        this.method = method;
053        this.params = Arrays.copyOf(params, params.length);
054    }
055
056    /**
057     * Returns the portlet class.
058     * 
059     * @return the portlet class
060     */
061    public String portletType() {
062        return portletType;
063    }
064
065    /**
066     * Returns the portlet id.
067     * 
068     * @return the portlet id
069     */
070    public String portletId() {
071        return portletId;
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, "notifyPortletView", portletType(), portletId(),
095            method(), params());
096    }
097}