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.util.HashMap;
022import java.util.Map;
023import java.util.function.BiConsumer;
024
025import org.jdrupes.json.JsonArray;
026import org.jgrapes.core.Event;
027
028/**
029 * Sent to a portlet to update some of its properties. The interpretation
030 * of the properties is completely dependent on the handling portlet.
031 * 
032 * This event has a close relationship to the {@link NotifyPortletModel}
033 * event. The latter is used by portlet's functions to send information
034 * from the portal page to the portlet model. It passes the information 
035 * as a {@link JsonArray}. The interpretation of this information is only 
036 * known by the portlet. The {@link UpdatePortletModel} event should be 
037 * used to to pass information within the application, i.e. on the server
038 * side.
039 * 
040 * Depending on the information passed, it may be good practice to 
041 * write an event handler for the portlet that converts a 
042 * {@link NotifyPortletModel} to a {@link UpdatePortletModel} that is
043 * fired on its channel instead of handling it immediately. This allows 
044 * events sent from the portal page and from other components in the 
045 * application to be handled in a uniform way.
046 */
047public class UpdatePortletModel extends Event<Void> {
048
049    private String portletId;
050    private Map<Object, Object> properties;
051
052    /**
053     * Creates a new event.
054     * 
055     * @param portletId the id of the portlet
056     * @param properties the properties to update
057     */
058    public UpdatePortletModel(String portletId, Map<?, ?> properties) {
059        this.portletId = portletId;
060        @SuppressWarnings("unchecked")
061        Map<Object, Object> props = (Map<Object, Object>) properties;
062        this.properties = props;
063    }
064
065    /**
066     * Creates a new event. This constructor creates an empty map of
067     * properties and is therefore intended to be used together with
068     * {@link #addPreference(Object, Object)}.
069     *
070     * @param portletId the portlet id
071     */
072    public UpdatePortletModel(String portletId) {
073        this(portletId, new HashMap<>());
074    }
075
076    /**
077     * Returns the portlet id.
078     * 
079     * @return the portlet id
080     */
081    public String portletId() {
082        return portletId;
083    }
084
085    /**
086     * Returns the properties. Every event returns a mutable map,
087     * thus allowing event handlers to modify the map even if
088     * none was passed to the constructor.
089     */
090    public Map<Object, Object> properties() {
091        if (properties == null) {
092            properties = new HashMap<>();
093        }
094        return properties;
095    }
096
097    /**
098     * Convenience method for adding properties one-by-one.
099     * 
100     * @param key the property key
101     * @param value the property value
102     * @return the event for easy chaining
103     */
104    public UpdatePortletModel addPreference(Object key, Object value) {
105        properties().put(key, value);
106        return this;
107    }
108
109    /**
110     * Convenience method that performs the given action if a property
111     * with the given key exists.
112     * 
113     * @param key the property key
114     * @param action the action to perform
115     */
116    public UpdatePortletModel ifPresent(
117            Object key, BiConsumer<Object, Object> action) {
118        if (properties().containsKey(key)) {
119            action.accept(key, properties().get(key));
120        }
121        return this;
122    }
123}