001/*
002 * VM-Operator
003 * Copyright (C) 2023 Michael N. Lipp
004 * 
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.vmoperator.manager;
020
021import java.io.BufferedReader;
022import java.io.IOException;
023import java.io.InputStreamReader;
024import java.util.Collections;
025import java.util.ResourceBundle;
026import java.util.stream.Collectors;
027import org.jgrapes.core.Channel;
028import org.jgrapes.core.Component;
029import org.jgrapes.core.annotation.Handler;
030import org.jgrapes.webconlet.markdowndisplay.MarkdownDisplayConlet;
031import org.jgrapes.webconsole.base.Conlet.RenderMode;
032import org.jgrapes.webconsole.base.ConsoleConnection;
033import org.jgrapes.webconsole.base.events.AddConletRequest;
034import org.jgrapes.webconsole.base.events.ConsoleConfigured;
035import org.jgrapes.webconsole.base.events.ConsoleReady;
036import org.jgrapes.webconsole.base.events.RenderConlet;
037
038/**
039 * 
040 */
041public class AvoidEmptyPolicy extends Component {
042
043    private final String renderedFlagName = getClass().getName() + ".rendered";
044
045    /**
046     * Creates a new component with its channel set to the given channel.
047     * 
048     * @param componentChannel
049     */
050    public AvoidEmptyPolicy(Channel componentChannel) {
051        super(componentChannel);
052    }
053
054    /**
055     * On console ready.
056     *
057     * @param event the event
058     * @param connection the connection
059     */
060    @Handler
061    public void onConsoleReady(ConsoleReady event,
062            ConsoleConnection connection) {
063        connection.session().put(renderedFlagName, false);
064    }
065
066    /**
067     * On render conlet.
068     *
069     * @param event the event
070     * @param connection the connection
071     */
072    @Handler(priority = 100)
073    public void onRenderConlet(RenderConlet event,
074            ConsoleConnection connection) {
075        if (event.renderAs().contains(RenderMode.Preview)
076            || event.renderAs().contains(RenderMode.View)) {
077            connection.session().put(renderedFlagName, true);
078        }
079    }
080
081    /**
082     * On console configured.
083     *
084     * @param event the event
085     * @param connection the console connection
086     * @throws InterruptedException the interrupted exception
087     */
088    @Handler(priority = -100)
089    public void onConsoleConfigured(ConsoleConfigured event,
090            ConsoleConnection connection) throws InterruptedException,
091            IOException {
092        if ((Boolean) connection.session().getOrDefault(renderedFlagName,
093            false)) {
094            return;
095        }
096        var resourceBundle = ResourceBundle.getBundle(
097            getClass().getPackage().getName() + ".l10n", connection.locale(),
098            getClass().getClassLoader(),
099            ResourceBundle.Control.getNoFallbackControl(
100                ResourceBundle.Control.FORMAT_DEFAULT));
101        var locale = resourceBundle.getLocale().toString();
102        String shortDesc;
103        try (BufferedReader shortDescReader
104            = new BufferedReader(new InputStreamReader(
105                AvoidEmptyPolicy.class.getResourceAsStream(
106                    "ManagerIntro-Preview" + (locale.isEmpty() ? ""
107                        : "_" + locale) + ".md"),
108                "utf-8"))) {
109            shortDesc
110                = shortDescReader.lines().collect(Collectors.joining("\n"));
111        }
112        fire(new AddConletRequest(event.event().event().renderSupport(),
113            MarkdownDisplayConlet.class.getName(),
114            RenderMode.asSet(RenderMode.Preview))
115                .addProperty(MarkdownDisplayConlet.CONLET_ID,
116                    getClass().getName())
117                .addProperty(MarkdownDisplayConlet.TITLE,
118                    resourceBundle.getString("consoleTitle"))
119                .addProperty(MarkdownDisplayConlet.PREVIEW_SOURCE,
120                    shortDesc)
121                .addProperty(MarkdownDisplayConlet.DELETABLE, true)
122                .addProperty(MarkdownDisplayConlet.EDITABLE_BY,
123                    Collections.EMPTY_SET),
124            connection);
125    }
126
127}