<%@ page import="com.atlassian.config.ConfigurationException,
                 com.atlassian.confluence.core.ConfluenceSidManager,
                 com.atlassian.johnson.JohnsonEventContainer,
                 com.atlassian.johnson.event.Event,
                 com.atlassian.spring.container.ContainerManager"%>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.Iterator" %>

<html>
<head>
    <title>Errors</title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width=100%>
    <tr>
        <td height=10></td>
        <%
                String sid = null;
                if (ContainerManager.isContainerSetup())
                {
                    ConfluenceSidManager sidManager = (ConfluenceSidManager) ContainerManager.getComponent("sidManager");
                    try
                    {
                        sid = sidManager.getSid();
                    }
                    catch (ConfigurationException e)
                    {
                        // ignore, we just don't want this to bomb out this page 
                    }
                }

                JohnsonEventContainer appEventContainer = JohnsonEventContainer.get(pageContext.getServletContext());
                //if there are Events outstanding then set the HTTP return code to 500 Internal Server Error
                if (appEventContainer != null && appEventContainer.hasEvents())
                {
                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                    response.flushBuffer(); // needed otherwise an exception is thrown
                }
                //if there are Events outstanding then display them in a table
                if (appEventContainer != null && appEventContainer.hasEvents())
                {
            %>
                        <content tag="headsection">
                            <meta http-equiv="Refresh" content="60;"/>
                        </content>
                        
                        <p>
                            <em class="bad">You cannot access Confluence at present. Look at the table below to identify the reasons</em>
                        </p>
                        <p>
                            This page will automatically update every 60 seconds.
                        </p>
                    <br>
                    <table align=center bgcolor="bbbbbb" border="0" cellpadding="0" cellspacing="0" width=100%>
                    <tr>
                        <td>
                            <table  border="0" cellpadding="3" cellspacing="1" width=100%>

                                <tr bgcolor="#f0f0f0">
                                    <td class="colHeaderLink" nowrap>
                                        <strong>Time</strong>
                                    </td>
                                    <td class="colHeaderLink">
                                        <strong>Level</strong>
                                    </td>
                                    <td class="colHeaderLink">
                                        <strong>Type</strong>
                                    </td>
                                    <td class="colHeaderLink" width=40%>
                                        <strong>Description</strong>
                                    </td>
                                    <td class="colHeaderLink" width=40%>
                                        <strong>Exception</strong>
                                    </td>
                                </tr>

                                <%
                                    Collection events  = appEventContainer.getEvents();
                                    int counter = 1;
                                    for (Iterator iterator = events.iterator(); iterator.hasNext();)
                                    {
                                        Event event = (Event) iterator.next();

                                        if (counter % 2 == 1) {
                                        %>
                                            <tr bgcolor="#fffff0">
                                        <%} else { %>
                                            <tr bgcolor="#ffffff">
                                        <% } %>
                                            <td nowrap><%=event.getDate()%></td>
                                            <td nowrap><%=event.getLevel() == null ? "" : event.getLevel().getLevel()%> </td>
                                            <td nowrap><%=event.getKey() == null ? "" : event.getKey().getType()%></td>
                                            <td><%=event.getDesc()%><br/><br/><%= (sid == null ? " No server id found." : " Your server id is: <strong>" + sid + "</strong>.") %></td>
                                            <td><pre><%=event.getException() == null ? "" : event.getException()%></pre></td>
                                        </tr>

                                        <% counter++;
                                        }

                                    %>
                            </table>
                        </td>
                    </tr>
                    </table>
                    <br>
                <% } else { %>
                        <p>This instance of Confluence is now ready to use again. Go to the <a href="<%= request.getContextPath() + "/" %>">Homepage</a>.</p>
                 <% } %>
        </tr>
    </table>
    </body>
</html>
