/*
 * Copyright (c) 2005-2011 Grameen Foundation USA
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * See also http://www.apache.org/licenses/LICENSE-2.0.html for an
 * explanation of the license and how it is applied.
 */

package org.mifos.framework.persistence;

import org.mifos.db.upgrade.DatabaseUpgradeSupport;
import org.mifos.db.upgrade.DbUpgradeValidationResult;
import org.mifos.framework.ApplicationInitializer;
import org.mifos.framework.DatabaseErrorCode;
import org.mifos.framework.struts.tags.XmlBuilder;
import org.mifos.framework.util.helpers.ServletUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static org.mifos.db.upgrade.DatabaseUpgradeSupport.BEAN_NAME;

public class DatabaseInitFilter implements Filter {

    private static boolean databaseVerified = false;


    public DatabaseInitFilter() {
    }

    @Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
            ServletException {

        if (!databaseVerified) {
            showError(request, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    private void showError(ServletRequest request, ServletResponse response) throws IOException, ServletException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        httpResponse.setContentType("text/html");
        httpResponse.setStatus(500);
        PrintWriter out = httpResponse.getWriter();

        printErrorPage(out);
    }

    void printErrorPage(PrintWriter out) {
        XmlBuilder xml = new XmlBuilder();
        xml.startTag("html");
        xml.startTag("head");
        xml.startTag("title");
        xml.text("Mifos Database Error");
        xml.text("\n");
        xml.endTag("title");
        xml.text("\n");
        xml.endTag("head");
        xml.text("\n");

        xml.startTag("body");
        xml.startTag("h2");
        xml.text("Mifos Database Error");
        xml.endTag("h2");
        xml.text("\n");

        xml.startTag("p");
        xml.text("A database error occurred. ");
        xml.text("Correct the error and restart the application. ");

        xml.text("Details:");

        xml.endTag("p");
        xml.text("\n");

        ApplicationInitializer.printDatabaseError(xml);

        xml.endTag("body");
        xml.text("\n");
        xml.endTag("html");
        out.println(xml.getOutput());
    }

    @Override
	public void init(FilterConfig filterConfig) {
        try {
            DatabaseUpgradeSupport databaseUpgradeSupport = ServletUtils.getBean(filterConfig.getServletContext(), BEAN_NAME);
            DbUpgradeValidationResult validationResult = databaseUpgradeSupport.validate();
            databaseVerified = validationResult.allUpgradesApplied();
            if (!databaseVerified) {
                ApplicationInitializer.setDatabaseError(DatabaseErrorCode.UPGRADE_FAILURE, "There are un applied db upgrades: ",
                        new RuntimeException(validationResult.getUnAppliedChangeSets()));
            }
        } catch (Exception e) {
           filterConfig.getServletContext().log("Failed to check for unapplied upgrades upgrades",e );
        }
    }

    @Override
	public void destroy() {
    }

}

@@ -1473,24 +1473,37 @@
 ide%0A%09public 
+synchronized 
 void doFilte
@@ -3075,24 +3075,37 @@
 ide%0A%09public 
+synchronized 
 void init(Fi

Synchronize access to databaseVerified.