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;
020
021import java.io.IOException;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.jdrupes.json.JsonArray;
028import org.jgrapes.core.Channel;
029import org.jgrapes.core.Component;
030import org.jgrapes.core.Event;
031import org.jgrapes.core.Manager;
032import org.jgrapes.core.TypedIdKey;
033import org.jgrapes.core.annotation.Handler;
034import org.jgrapes.portal.base.events.JsonInput;
035import org.jgrapes.portal.base.events.PortalReady;
036import org.jgrapes.portal.base.events.SimplePortalCommand;
037import org.jgrapes.util.events.KeyValueStoreQuery;
038import org.jgrapes.util.events.KeyValueStoreUpdate;
039import org.jgrapes.util.events.KeyValueStoreUpdate.Action;
040import org.jgrapes.util.events.KeyValueStoreUpdate.Deletion;
041import org.jgrapes.util.events.KeyValueStoreUpdate.Update;
042
043// TODO: Auto-generated Javadoc
044/**
045 * The Class PortalLocalBackedKVStore.
046 */
047public class PortalLocalBackedKVStore extends Component {
048
049    private final String portalPrefix;
050
051    /**
052     * Create a new key/value store that uses the browser's local storage
053     * for persisting the values.
054     * 
055     * @param componentChannel the channel that the component's 
056     * handlers listen on by default and that 
057     * {@link Manager#fire(Event, Channel...)} sends the event to 
058     * @param portalPrefix the portal's prefix as returned by
059     * {@link PortalWeblet#prefix()}, i.e. staring and ending with a slash
060     */
061    public PortalLocalBackedKVStore(
062            Channel componentChannel, String portalPrefix) {
063        super(componentChannel);
064        this.portalPrefix = portalPrefix;
065    }
066
067    /**
068     * Intercept {@link PortalReady} event to first get data.
069     *
070     * @param event the event
071     * @param channel the channel
072     * @throws InterruptedException the interrupted exception
073     */
074    @Handler(priority = 1000)
075    public void onPortalReady(PortalReady event, PortalSession channel)
076            throws InterruptedException {
077        // Put there by onJsonInput if retrieval has been done.
078        if (TypedIdKey.get(channel.browserSession(), Store.class,
079            portalPrefix)
080            .isPresent()) {
081            // Already have store, nothing to do
082            return;
083        }
084        // Remove portal ready event from queue and save it
085        event.cancel(false);
086        channel.setAssociated(this, event);
087        String keyStart = portalPrefix
088            + PortalLocalBackedKVStore.class.getName() + "/";
089        channel.respond(new SimplePortalCommand("retrieveLocalData", keyStart));
090    }
091
092    private Store getStore(PortalSession channel) {
093        return TypedIdKey
094            .get(channel.browserSession(), Store.class, portalPrefix)
095            .orElseGet(
096                () -> TypedIdKey.put(channel.browserSession(), portalPrefix,
097                    new Store()));
098    }
099
100    /**
101     * Evaluate "retrievedLocalData" response.
102     *
103     * @param event the event
104     * @param channel the channel
105     * @throws InterruptedException the interrupted exception
106     * @throws IOException Signals that an I/O exception has occurred.
107     */
108    @Handler
109    public void onJsonInput(JsonInput event, PortalSession channel)
110            throws InterruptedException, IOException {
111        if (!event.request().method().equals("retrievedLocalData")) {
112            return;
113        }
114        channel.associated(this, PortalReady.class)
115            .ifPresent(origEvent -> {
116                // We have intercepted the portal ready event, fill store.
117                // Having a store now also shows that retrieval has been done.
118                final String keyStart = portalPrefix
119                    + PortalLocalBackedKVStore.class.getName() + "/";
120                @SuppressWarnings("PMD.DataflowAnomalyAnalysis")
121                Store data = getStore(channel);
122                JsonArray params = (JsonArray) event.request().params();
123                params.asArray(0).arrayStream().forEach(item -> {
124                    String key = item.asString(0);
125                    if (key.startsWith(keyStart)) {
126                        data.put(key.substring(
127                            keyStart.length() - 1), item.asString(1));
128                    }
129                });
130                // Don't intercept again
131                channel.setAssociated(this, null);
132                // Let others process the portal ready event
133                fire(new PortalReady(origEvent.renderSupport()), channel);
134            });
135    }
136
137    /**
138     * Handle data update events.
139     *
140     * @param event the event
141     * @param channel the channel
142     * @throws InterruptedException the interrupted exception
143     * @throws IOException Signals that an I/O exception has occurred.
144     */
145    @Handler
146    @SuppressWarnings({ "PMD.DataflowAnomalyAnalysis",
147        "PMD.AvoidInstantiatingObjectsInLoops" })
148    public void onKeyValueStoreUpdate(
149            KeyValueStoreUpdate event, PortalSession channel)
150            throws InterruptedException, IOException {
151        Store data = getStore(channel);
152        List<String[]> actions = new ArrayList<>();
153        String keyStart = portalPrefix
154            + PortalLocalBackedKVStore.class.getName();
155        for (Action action : event.actions()) {
156            @SuppressWarnings("PMD.UselessParentheses")
157            String key = keyStart + (action.key().startsWith("/")
158                ? action.key()
159                : ("/" + action.key()));
160            if (action instanceof Update) {
161                actions.add(new String[] { "u", key,
162                    ((Update) action).value() });
163                data.put(action.key(), ((Update) action).value());
164            } else if (action instanceof Deletion) {
165                actions.add(new String[] { "d", key });
166                data.remove(action.key());
167            }
168        }
169        channel.respond(new SimplePortalCommand("storeLocalData",
170            new Object[] { actions.toArray() }));
171    }
172
173    /**
174     * Handle data query..
175     *
176     * @param event the event
177     * @param channel the channel
178     */
179    @Handler
180    @SuppressWarnings("PMD.ConfusingTernary")
181    public void onKeyValueStoreQuery(
182            KeyValueStoreQuery event, PortalSession channel) {
183        @SuppressWarnings("PMD.UseConcurrentHashMap")
184        Map<String, String> result = new HashMap<>();
185        TypedIdKey.get(channel.browserSession(), Store.class, portalPrefix)
186            .ifPresent(data -> {
187                if (!event.query().endsWith("/")) {
188                    // Single value
189                    if (data.containsKey(event.query())) {
190                        result.put(event.query(), data.get(event.query()));
191                    }
192                } else {
193                    for (Map.Entry<String, String> e : data.entrySet()) {
194                        if (e.getKey().startsWith(event.query())) {
195                            result.put(e.getKey(), e.getValue());
196                        }
197                    }
198                }
199                event.setResult(result);
200            });
201    }
202
203    /**
204     * The store.
205     */
206    @SuppressWarnings("serial")
207    private class Store extends HashMap<String, String> {
208    }
209}