package de.hsadmin.mods.db;

import javax.persistence.EntityManager;

import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.qserv.EntityProcessorFactory;
import de.hsadmin.core.qserv.JDBCProcessor;
import de.hsadmin.core.qserv.Processor;

/**
 * Factory class which creates Processor instances for dealing with UNIX user
 * accounts.
 * 
 * @author mi
 */
public class MySqlDatabaseProcessorFactory implements EntityProcessorFactory {
	public static JDBCProcessor createMySqlProcessor(String database,
			String user, String password) {
		return new JDBCProcessor("com.mysql.jdbc.Driver",
				"jdbc:mysql://localhost/" + database, user, password);
	}

	public static JDBCProcessor createMySqlAdminProcessor() {
		return new JDBCProcessor("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/");
	}

	public <T extends AbstractEntity> Processor createCreateProcessor(
			EntityManager em, T entity) {
		Database db = (Database) entity;
		assert db.getInstance().equals("mysql");
		JDBCProcessor aJDBCP = null;
		String aName = AbstractEntity.escapeString(db.getName());
		String aOwner = AbstractEntity.escapeString(db.getOwner());
		String aEncoding = AbstractEntity.escapeString(db.getSystemEncoding());
		aJDBCP = createMySqlAdminProcessor();
		aJDBCP.addSQL("CREATE DATABASE " + aName + " DEFAULT CHARACTER SET '" + aEncoding + "'");
		aJDBCP.addSQL("GRANT ALL ON " + aName + ".* TO '" + aOwner + "'@'%' WITH GRANT OPTION");
		return aJDBCP;
	}

	public <T extends AbstractEntity> Processor createUpdateProcessor(EntityManager em, T entity) {
		Database db = (Database) entity;
		assert db.getInstance().equals("mysql");
		String aName = AbstractEntity.escapeString(db.getName());
		String aOwner = AbstractEntity.escapeString(db.getOwner());
		JDBCProcessor aJDBCP = null;
		aJDBCP = createMySqlAdminProcessor();
		aJDBCP.addSQL("GRANT ALL ON " + aName + ".* TO '" + aOwner + "'@'%'");
		return aJDBCP;
	}

	public <T extends AbstractEntity> Processor createDeleteProcessor(EntityManager em, T entity) {
		Database db = (Database) entity;
		assert db.getInstance().equals("mysql");
		JDBCProcessor aJDBCP = createMySqlAdminProcessor();
		String aName = AbstractEntity.escapeString(db.getName());
		String aOwner = AbstractEntity.escapeString(db.getOwner());
		aJDBCP.addSQL("REVOKE ALL ON " + aName + ".* FROM '" + aOwner + "'@'%'");
		aJDBCP.addSQL("DROP DATABASE IF EXISTS " + aName);
		return aJDBCP;
	}
}
