<%@ page import="com.atlassian.jira.ManagerFactory,
                 com.atlassian.jira.appconsistency.BuildAndLicenseEventCheck,
                 com.atlassian.jira.config.properties.APKeys,
                 com.atlassian.jira.config.properties.ApplicationProperties,
                 com.atlassian.jira.util.BuildUtils,
                 com.atlassian.johnson.JohnsonEventContainer,
                 com.atlassian.johnson.event.Event,
                 com.atlassian.johnson.event.EventType,
                 com.opensymphony.util.TextUtils,
                 java.util.Collection,
                 java.util.Iterator"%>
<%@ taglib uri="sitemesh-page" prefix="page" %>
<%@ taglib uri="sitemesh-page" prefix="page" %>
<%@ taglib uri="webwork" prefix="webwork" %>
<%@ taglib uri="sitemesh-decorator" prefix="decorator" %>
<html>
	<head>
	<title>JIRA</title>
    <%@ include file="/includes/decorators/stylesheettag.jsp" %>
	</head>
<%
    ApplicationProperties ap = ManagerFactory.getApplicationProperties();
    final LookAndFeelBean lookAndFeelBeanErrors = LookAndFeelBean.getInstance(ap);
    String topBgColour = lookAndFeelBeanErrors.getTopBackgroundColour();
    String menuBgColour = lookAndFeelBeanErrors.getMenuBackgroundColour();
    String linkColour = lookAndFeelBeanErrors.getTextLinkColour();
    String linkAColour = lookAndFeelBeanErrors.getTextActiveLinkColour();

    String jiraLogo = lookAndFeelBeanErrors.getLogoUrl();
    if (jiraLogo != null && !jiraLogo.startsWith("http://") && !jiraLogo.startsWith("https://"))
    {
        jiraLogo = request.getContextPath() + jiraLogo;
    }
%>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" link="<%=linkColour %>" vlink="<%=linkColour %>" alink="<%=linkAColour %>">
<meta http-equiv="Refresh" content="30;"/>
    <table border="0" cellpadding="0" cellspacing="0" width=100%>
      <tr>
        <%-- handle user defined top colour --%>
          <td bgcolor="<%= topBgColour %>">
            <table width=100% cellpadding=0 cellspacing=0 border=0><tr>
                <td valign=top width=5% nowrap>
                    <a href="<%= request.getContextPath() %>/secure/Dashboard.jspa"><img src="<%= jiraLogo %>" width="<%= lookAndFeelBeanErrors.getLogoWidth() %>" height="<%= lookAndFeelBeanErrors.getLogoHeight() %>" border=0 alt="<%= ap.getString(APKeys.JIRA_TITLE) %>"></a>
                </td>
            </tr></table>
          </td>
      </tr>
    </table>
    <table width=100% cellpadding=0 cellspacing=0 border=0>
        <tr> <td bgcolor="<%= menuBgColour %>" align=left>
                <table border=0 cellspacing=0 cellpadding=0>
                    <tr>
                        <td width=5><img src="<%= request.getContextPath() %>/images/border/spacer.gif" width=5 height=20 border=0></td>
                        <page:applyDecorator name="navitem" title="HOME">
                            <page:param name="url"><%= request.getContextPath() %>/secure/Dashboard.jspa</page:param>
                            <page:param name="selected">/secure/views/dashboard.jsp</page:param>
                            <page:param name="tooltip">Your Dashboard covering JIRA</page:param>
                        </page:applyDecorator>
                     </tr>
                 </table>
            </td>
        </tr>
    </table>
    <table border="0" cellpadding="0" cellspacing="0" width=100%>
        <tr>
            <td height=10></td>
            <page:applyDecorator name="jirapanel" >
                <page:param name="title">JIRA Access Constraints</page:param>
                <page:param name="width">80%</page:param>
                <page:param name="action">DeleteGroup.jspa</page:param>
                <page:param name="submitName">Delete</page:param>
                <%
                    JohnsonEventContainer appEventContainer = JohnsonEventContainer.get(pageContext.getServletContext());
                    //if there are Events outstanding then display them in a table
                    if (appEventContainer.hasEvents() ) {
                %>
                    <page:param name="description">
                        <font color="cc0000">You cannot access JIRA at present. Look at the table below to identify the reasons</font>
                        <p></p>
                    </page:param>
                    <br>
                            <table class="grid" width="100%">

                                <tr bgcolor="#f0f0f0">
                                    <td class="colHeaderLink" nowrap>
                                        <b>Time</b>
                                    </td>
                                    <td class="colHeaderLink">
                                        <b>Level</b>
                                    </td>
                                    <td class="colHeaderLink" width=40%>
                                        <b>Description</b>
                                    </td>
                                    <td class="colHeaderLink" width=40%>
                                        <b>Exception</b>
                                    </td>
                                </tr>
                                <%
                                    com.atlassian.jira.web.util.ExternalLinkUtil externalLinkUtil = com.atlassian.jira.web.util.ExternalLinkUtilImpl.getInstance();
                                    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().getLevel()%> </td>
                                            <td>
                                        <% if (EventType.get("export-illegal-xml").equals(event.getKey())) { %>
                                                <webwork:component template="help.jsp" name="'autoexport'"><webwork:param name="'helpURLFragment'"></webwork:param></webwork:component><br>
                                        <% } %>
                                        <%=event.getDesc()%><p>
                                        <% if (event.hasProgress()) {%>
                                        This process is <%=event.getProgress()%>% Completed.
                                         <%}%>
                                        <% if (EventType.get(BuildAndLicenseEventCheck.STANDARD_IN_ENTERPRISE).equals(event.getKey()) ||
                                                EventType.get(BuildAndLicenseEventCheck.STANDARD_IN_PROFESSIONAL).equals(event.getKey()) ||
                                                EventType.get(BuildAndLicenseEventCheck.PROFESSIONAL_IN_ENTERPRISE).equals(event.getKey()))
                                           { %><p>
                                            <a href="<%= request.getContextPath() %>/secure/SetupLicense!default.jspa">Edit License</a>
                                        <% }
                                           else if (EventType.get(BuildAndLicenseEventCheck.ENTERPRISE_IN_PROFESSIONAL).equals(event.getKey()))
                                           { %>
                                              Please install JIRA Enterprise, or <a href="<%= externalLinkUtil.getProperty("external.link.jira.enterprise.to.pro", BuildUtils.getVersion())%>">downgrade your Enterprise data to Professional</a>.
                                        <% }
                                           else if (EventType.get(BuildAndLicenseEventCheck.ENTERPRISE_IN_STANDARD).equals(event.getKey()))
                                           { %>
                                              Please install JIRA Enterprise, or <a href="<%= externalLinkUtil.getProperty("external.link.jira.enterprise.to.standard", BuildUtils.getVersion())%>">downgrade your Enterprise data to Standard</a>.
                                        <% }
                                           else if (EventType.get(BuildAndLicenseEventCheck.PROFESSIONAL_IN_STANDARD).equals(event.getKey()))
                                           { %>
                                              Please install JIRA Professional, or <a href="<%= externalLinkUtil.getProperty("external.link.jira.pro.to.standard", BuildUtils.getVersion())%>">downgrade your Professional data to Standard</a>.
                                        <% }
                                            else if (EventType.get("license-region-mismatch").equals(event.getKey()))
                                           { %>
                                               <br><a href="<%= request.getContextPath() %>/secure/ConfirmInstallationWithLicense!default.jspa">Edit License</a>
                                        <% }
                                           else if (EventType.get("license-too-old").equals(event.getKey()))
                                           { %>
                                               <br><a href="<%= request.getContextPath() %>/secure/ConfirmNewInstallationWithOldLicense!default.jspa">Edit License or proceed under Evaluation Terms</a>
                                        <% }
                                           else if (EventType.get("export-illegal-xml").equals(event.getKey()))
                                           { %>
                                               <br><a href="<%= request.getContextPath() %>/secure/CleanData!default.jspa">Clean these characters from database.</a><br>
                                               To disable the export on upgrade, start JIRA with the property <b>jira.autoexport</b> set to false &nbsp;
                                        <% }
                                           else if (EventType.get("index-lock-already-exists").equals(event.getKey()))
                                           { %>
                                                <p>
                                                An index lock file was found unexpectedly. This occurs either because JIRA was not cleanly shutdown
                                                or because there is another instance of this JIRA installation currently running. Please ensure that no
                                                other instance of this JIRA installation is running and then remove the following lock file(s) and restart JIRA:
                                                <br>
                                                <br>
                                                <%
                                                   Object lockFiles = event.getAttribute("lockfiles");
                                                   if (lockFiles != null)
                                                   {
                                                       out.println(lockFiles);
                                                   }
                                                %>
                                                <br>
                                                <br>
                                                Once restarted you will need to reindex your data to ensure that indexes are up to date.
                                                </p>
                                                <p>
                                                <strong>Do not delete the lock file(s) if there is another JIRA running with the same index directory</strong>,
                                                instead cleanly shutdown the other instance.
                                                </p>
                                        <% }
                                           else if (EventType.get("upgrade").equals(event.getKey()))
                                           {
                                               String exportFilePath = ManagerFactory.getUpgradeManager().getExportFilePath();
                                               if (TextUtils.stringSet(exportFilePath))
                                               {
                                               %>
                                               <br>The data before the upgrade has been exported to <%=exportFilePath%>
                                            <% } %>
                                        <% } %>

                                            <hr>(type: <%=event.getKey().getType()%>)

                                            </td>
                                            <td><pre><%=event.getException()==null?"":event.getException()%></pre></td>
                                        </tr>
                                        <% counter++;
                                        }
                                    %>
                            </table>
                    <br>
                <% } else { %>
                    <page:param name="description">
                        <p>There are no problems accessing JIRA at present.</p>
                        <p><img src="<%= request.getContextPath() %>/images/icons/bullet_creme.gif" height=8 width=8 border=0 align=absmiddle>
                            <a href="<%=request.getContextPath()%>/secure/Dashboard.jspa">Go to Dashboard</a></p>
                    </page:param>
                 <% } %>
            </page:applyDecorator>
        </tr>
    </table>
    </body>
</html>
