/*
 * Copyright (C) 2004-2006  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope.console;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import com.exedio.cope.Cope;
import com.exedio.cope.Model;


final class DbCop extends AdminCop
{

	DbCop()
	{
		super("db");
		addParameter(TAB, TAB_DB);
	}
	
	private static final String replaceLineBreaks(final String s)
	{
		return (s==null) ? "n/a" : s.replaceAll("\n", "<br>");
	}

	final void writeBody(final PrintStream out, final Model model, final HttpServletRequest request) throws IOException
	{
		final java.util.Properties current = model.getDatabaseInfo();
		for(Iterator i = current.keySet().iterator(); i.hasNext(); )
		{
			final String name = (String)i.next();
			current.setProperty(name, replaceLineBreaks(current.getProperty(name)));
		}

		Properties_Jspm.writeDatabaseInfo(out, current);
		Properties_Jspm.writeTestInfo(out, current, makeTestedDatabases());
	}
	
	private final HashMap[] makeTestedDatabases()
	{
		final java.util.Properties p = new Properties();
		InputStream in = null;
		try
		{
			in = Cope.class.getResourceAsStream("testprotocol.properties");
			if(in==null)
				return null;
			
			p.load(in);
			in.close();
			in = null;
		}
		catch(IOException e)
		{
			throw new RuntimeException(e);
		}
		finally
		{
			if(in!=null)
			{
				try
				{
					in.close();
				}
				catch(IOException e)
				{
					// oops
				}
			}
		}
		
		final TreeMap testedDatabases = new TreeMap();
		for(Iterator i = p.keySet().iterator(); i.hasNext(); )
		{
			final String name = (String)i.next();
			final String value = replaceLineBreaks(p.getProperty(name));
			
			final int nameDot = name.indexOf('.');
			if(nameDot<=0)
				throw new RuntimeException(name);
			
			final String databaseName = name.substring(0, nameDot);
			HashMap database = (HashMap)testedDatabases.get(databaseName);
			if(database==null)
			{
				database = new HashMap();
				database.put("name", databaseName);
				testedDatabases.put(databaseName, database);
			}
			
			final String key = name.substring(nameDot+1);
			if(key.startsWith("cope."))
			{
				TreeMap previousValue = (TreeMap)database.get("cope.properties");
				if(previousValue==null)
				{
					previousValue = new TreeMap();
					database.put("cope.properties", previousValue);
				}
				previousValue.put(key.substring("cope.".length()), value);
			}
			else
				database.put(key, value);
		}
		
		return (HashMap[])testedDatabases.values().toArray(new HashMap[0]);
	}
	
}

@@ -1911,34 +1911,24 @@
 %0A%09%7B%0A%09%09final 
-java.util.
 Properties p
@@ -2353,38 +2353,104 @@
 eMap
- testedDatabases = new TreeMap
+%3CString, HashMap%3CString, Object%3E%3E testedDatabases = new TreeMap%3CString, HashMap%3CString, Object%3E%3E
 ();%0A
@@ -2790,16 +2790,32 @@
 %09HashMap
+%3CString, Object%3E
  databas
@@ -2822,17 +2822,8 @@
 e = 
-(HashMap)
 test
@@ -2906,16 +2906,32 @@
  HashMap
+%3CString, Object%3E
 ();%0A%09%09%09%09
@@ -3120,16 +3120,32 @@
 %09TreeMap
+%3CString, String%3E
  previou
@@ -3157,17 +3157,20 @@
 e = 
-(
+cast
 TreeMap
-)
+(
 data
@@ -3192,24 +3192,25 @@
 properties%22)
+)
 ;%0A%09%09%09%09if(pre
@@ -3265,16 +3265,32 @@
  TreeMap
+%3CString, String%3E
 ();%0A%09%09%09%09
@@ -3470,19 +3470,8 @@
 urn 
-(HashMap%5B%5D)
 test
@@ -3521,10 +3521,163 @@
 );%0A%09%7D%0A%09%0A
+%09@SuppressWarnings(%22unchecked%22)%0A%09private static final TreeMap%3CString, String%3E castTreeMap(final Object o)%0A%09%7B%0A%09%09return (TreeMap%3CString, String%3E)o;%0A%09%7D%0A%09%0A%09%0A
 %7D%0A

fix warnings