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.nio.charset.StandardCharsets;
025import java.util.Collections;
026import java.util.ResourceBundle;
027import java.util.stream.Collectors;
028import org.jgrapes.core.Channel;
029import org.jgrapes.core.Component;
030import org.jgrapes.core.annotation.Handler;
031import org.jgrapes.webconlet.markdowndisplay.MarkdownDisplayConlet;
032import org.jgrapes.webconsole.base.Conlet.RenderMode;
033import org.jgrapes.webconsole.base.ConsoleConnection;
034import org.jgrapes.webconsole.base.events.AddConletRequest;
035import org.jgrapes.webconsole.base.events.ConsoleConfigured;
036import org.jgrapes.webconsole.base.events.ConsoleReady;
037import org.jgrapes.webconsole.base.events.RenderConlet;
038
039/**
040 * 
041 */
042public class AvoidEmptyPolicy extends Component {
043
044    private final String renderedFlagName = getClass().getName() + ".rendered";
045
046    /**
047     * Creates a new component with its channel set to the given channel.
048     * 
049     * @param componentChannel
050     */
051    public AvoidEmptyPolicy(Channel componentChannel) {
052        super(componentChannel);
053    }
054
055    /**
056     * On console ready.
057     *
058     * @param event the event
059     * @param connection the connection
060     */
061    @Handler
062    public void onConsoleReady(ConsoleReady event,
063            ConsoleConnection connection) {
064        connection.session().put(renderedFlagName, false);
065    }
066
067    /**
068     * On render conlet.
069     *
070     * @param event the event
071     * @param connection the connection
072     */
073    @Handler(priority = 100)
074    public void onRenderConlet(RenderConlet event,
075            ConsoleConnection connection) {
076        if (event.renderAs().contains(RenderMode.Preview)
077            || event.renderAs().contains(RenderMode.View)) {
078            connection.session().put(renderedFlagName, true);
079        }
080    }
081
082    /**
083     * On console configured.
084     *
085     * @param event the event
086     * @param connection the console connection
087     * @throws InterruptedException the interrupted exception
088     */
089    @Handler(priority = -100)
090    public void onConsoleConfigured(ConsoleConfigured event,
091            ConsoleConnection connection) throws InterruptedException,
092            IOException {
093        if ((Boolean) connection.session().getOrDefault(renderedFlagName,
094            false)) {
095            return;
096        }
097        var resourceBundle = ResourceBundle.getBundle(
098            getClass().getPackage().getName() + ".l10n", connection.locale(),
099            getClass().getClassLoader(),
100            ResourceBundle.Control.getNoFallbackControl(
101                ResourceBundle.Control.FORMAT_DEFAULT));
102        var locale = resourceBundle.getLocale().toString();
103        String shortDesc;
104        try (BufferedReader shortDescReader
105            = new BufferedReader(new InputStreamReader(
106                AvoidEmptyPolicy.class.getResourceAsStream(
107                    "ManagerIntro-Preview" + (locale.isEmpty() ? ""
108                        : "_" + locale) + ".md"),
109                StandardCharsets.UTF_8))) {
110            shortDesc
111                = shortDescReader.lines().collect(Collectors.joining("\n"));
112        }
113        fire(new AddConletRequest(event.event().event().renderSupport(),
114            MarkdownDisplayConlet.class.getName(),
115            RenderMode.asSet(RenderMode.Preview))
116                .addProperty(MarkdownDisplayConlet.CONLET_ID,
117                    getClass().getName())
118                .addProperty(MarkdownDisplayConlet.TITLE,
119                    resourceBundle.getString("consoleTitle"))
120                .addProperty(MarkdownDisplayConlet.PREVIEW_SOURCE,
121                    shortDesc)
122                .addProperty(MarkdownDisplayConlet.DELETABLE, true)
123                .addProperty(MarkdownDisplayConlet.EDITABLE_BY,
124                    Collections.EMPTY_SET),
125            connection);
126    }
127
128}