/*
 * JGrapes Event Driven Framework
 * Copyright (C) 2017  Michael N. Lipp
 * 
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, see <http://www.gnu.org/licenses/>.
 */

package org.jgrapes.http.demo.httpserver;

import java.util.Optional;

import org.jgrapes.core.Channel;
import org.jgrapes.core.Component;
import org.jgrapes.core.annotation.Handler;
import org.jgrapes.http.Session;
import org.jgrapes.http.demo.portlets.helloworld.HelloWorldPortlet;
import org.jgrapes.io.IOSubchannel;
import org.jgrapes.portal.Portlet;
import org.jgrapes.portal.events.AddPortletRequest;
import org.jgrapes.portal.events.PortalConfigured;
import org.jgrapes.portal.events.RenderPortlet;

/**
 * 
 */
public class NewPortalSessionPolicy extends Component {

	private final String renderedFlagName = getClass().getName() + ".rendered";
	
	/**
	 * Creates a new component with its channel set to
	 * itself.
	 */
	public NewPortalSessionPolicy() {
	}

	/**
	 * Creates a new component with its channel set to the given channel.
	 * 
	 * @param componentChannel
	 */
	public NewPortalSessionPolicy(Channel componentChannel) {
		super(componentChannel);
	}

	@Handler
	public void onRenderPortlet(RenderPortlet event, IOSubchannel channel) {
		channel.associated(Session.class).ifPresent(session -> 
			session.put(renderedFlagName, true));
	}
	
	@Handler
	public void onPortalConfigured(PortalConfigured event, IOSubchannel channel) 
			throws InterruptedException {
		Optional<Session> optSession = channel.associated(Session.class);
		if (optSession.isPresent()) {
			if ((Boolean)optSession.get().getOrDefault(
					renderedFlagName, false)) {
				return;
			}
			fire(new AddPortletRequest(event.event().event().renderSupport(), 
					HelloWorldPortlet.class.getName(), 
					Portlet.RenderMode.DeleteablePreview), channel);
		};
	}

}

@@ -790,24 +790,27 @@
 va.util.
-Op
+Collec
 tion
-al
+s
 ;%0A%0Aimpor
@@ -939,124 +939,28 @@
 pes.
-http.Session;%0Aimport org.jgrapes.http.demo.portlets.helloworld.HelloWorldPortlet;%0Aimport org.jgrapes.io.IOSubchannel
+portal.PortalSession
 ;%0Aim
@@ -1095,16 +1095,62 @@
 igured;%0A
+import org.jgrapes.portal.events.PortalReady;%0A
 import o
@@ -1188,16 +1188,160 @@
 Portlet;
+%0Aimport org.jgrapes.portlets.markdowndisplay.MarkdownDisplayPortlet;%0Aimport org.jgrapes.portlets.markdowndisplay.MarkdownDisplayPortlet.Options;
 %0A%0A/**%0A *
@@ -1828,135 +1828,261 @@
 d on
-RenderPortlet(RenderPortlet event, IOSubchannel channel) %7B%0A%09%09channel.associated(Session.class).ifPresent(session -%3E %0A%09%09%09s
+PortalReady(PortalReady event, PortalSession portalsession) %7B%0A%09%09portalsession.browserSession().put(renderedFlagName, false);%0A%09%7D%0A%09%0A%09@Handler%0A%09public void onRenderPortlet(RenderPortlet event, PortalSession portalsession) %7B%0A%09%09portalsession.browserS
 ession
+()
 .put
@@ -2105,17 +2105,16 @@
 e, true)
-)
 ;%0A%09%7D%0A%09%0A%09
@@ -2182,28 +2182,35 @@
 nt, 
-IOSubchannel channel
+PortalSession portalSession
 ) %0A%09
@@ -2247,136 +2247,49 @@
 %7B%0A%09%09
-Optional%3CSession%3E optSession = channel.associated(Session.class);%0A%09%09if (optSession.isPresent()) %7B%0A%09%09%09if ((Boolean)opt
+if ((Boolean)portalSession.browser
 Session
-.get
 ().g
@@ -2337,17 +2337,16 @@
 )) %7B%0A%09%09%09
-%09
 return;%0A
@@ -2347,20 +2347,18 @@
 turn;%0A%09%09
-%09
 %7D%0A
-%09
 %09%09fire(n
@@ -2426,19 +2426,23 @@
 %09%09%09%09
-%09HelloWorld
+MarkdownDisplay
 Port
@@ -2461,19 +2461,17 @@
 tName(),
- %0A%09
+%0A
 %09%09%09%09Port
@@ -2489,41 +2489,197 @@
 ode.
-DeleteablePreview), channel);%0A%09%09%7D
+Preview)%0A%09%09%09%09.addOption(Options.TITLE, %22Demo Portal%22)%0A%09%09%09%09.addOption(Options.PREVIEW_SOURCE, %22A Demo Portal%22)%0A%09%09%09%09.addOption(Options.EDITABLE_BY,  Collections.EMPTY_SET),%0A%09%09%09%09portalSession)
 ;%0A%09%7D

Use markdown portlet as example on empty page.