package org.skyve.impl.web.faces.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.skyve.domain.Bean;
import org.skyve.impl.util.SQLMetaDataUtil;
import org.skyve.impl.util.UtilImpl;

/**
 * Used to assert a public user from admin.Configuration data for use with public forms.
 * 
 * @author mike
 * @param <T>
 */
@ViewScoped
@ManagedBean(name = "skyvePublic")
public class PublicFacesView <T extends Bean> extends FacesView<T> {
	private static final long serialVersionUID = 6102123422562563165L;

	// this is mainly used for public pages that need to assert a customer
	private String bizCustomerParameter;
	public String getBizCustomerParameter() {
		return bizCustomerParameter;
	}
	public void setBizCustomerParameter(String bizCustomerParameter) {
		this.bizCustomerParameter = bizCustomerParameter;
	}
	
	@Override
	public void preRender() {
		FacesContext fc = FacesContext.getCurrentInstance();
		if (! fc.isPostback()) {
			ExternalContext ec = fc.getExternalContext();
			if (ec.getUserPrincipal() == null) {
				if (getUser() == null) {
					String customerName = (UtilImpl.CUSTOMER == null) ? bizCustomerParameter : UtilImpl.CUSTOMER;
					if (customerName != null) {
						String userName = SQLMetaDataUtil.retrievePublicUserName(customerName);
						if (userName != null) {
							setUser(customerName, userName);
						}
					}
				}
			}
		}
		super.preRender();
	}
}

@@ -34,16 +34,45 @@
 beans;%0A%0A
+import java.io.IOException;%0A%0A
 import j
@@ -96,24 +96,24 @@
 anagedBean;%0A
-
 import javax
@@ -220,16 +220,63 @@
 Context;
+%0Aimport javax.servlet.http.HttpServletResponse;
 %0A%0Aimport
@@ -343,16 +343,16 @@
 taUtil;%0A
-
 import o
@@ -379,16 +379,44 @@
 tilImpl;
+%0Aimport org.skyve.util.Util;
 %0A%0A/**%0A *
@@ -1392,25 +1392,25 @@
 ustomerName 
-!
+=
 = null) %7B%0A%09%09
@@ -1404,24 +1404,123 @@
 == null) %7B%0A%09
+%09%09%09%09%09throw new IllegalStateException(%22Malformed URL - this URL must have a 'c' parameter%22);%0A%09%09%09%09%09%7D%0A
 %09%09%09%09%09String 
@@ -1585,25 +1585,24 @@
 Name);%0A%09%09%09%09%09
-%09
 if (userName
@@ -1602,17 +1602,17 @@
 serName 
-!
+=
 = null) 
@@ -1619,16 +1619,366 @@
 %7B%0A%09%09%09%09%09%09
+HttpServletResponse response = (HttpServletResponse) ec.getResponse();%0A%09%09%09%09%09%09try %7B%0A%09%09%09%09%09%09%09response.sendRedirect(response.encodeRedirectURL(Util.getHomeUrl() + %22pages/noPublicUser.jsp%22));%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09catch (IOException e) %7B%0A%09%09%09%09%09%09%09throw new IllegalStateException(%22Could not redirect to /pages/noPublicUser.jsp%22, e);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09return;%0A%09%09%09%09%09%7D%0A%09%09%09%09
 %09setUser
@@ -2006,23 +2006,8 @@
 me);
-%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%7D
 %0A%09%09%09

Throw error in PublicFacesView if no customer on public page and redirect to noPublicUser.jsp if no public user is set in admin.Configuration.