hs.hsadmin/hsarback/src/de/hsadmin/mods/db/PgSqlUserProcessorFactory.java
2011-03-22 22:02:06 +00:00

49 lines
1.8 KiB
Java

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.Processor;
/**
* Factory class which creates Processor instances for dealing with UNIX user
* accounts.
*
* @author mi
*/
public class PgSqlUserProcessorFactory implements EntityProcessorFactory {
public <T extends AbstractEntity> Processor createCreateProcessor(EntityManager em, T entity) {
DatabaseUser dbu = (DatabaseUser) entity;
assert dbu.getInstance().equals("pgsql");
JDBCProcessor aJDBCP = PgSqlDatabaseProcessorFactory.createPostgreSqlAdminProcessor();
String aName = AbstractEntity.escapeString(dbu.getName());
String aPassword = AbstractEntity.escapeString(dbu.getPassword());
aJDBCP.addSQL("CREATE USER " + aName + " NOCREATEDB NOCREATEUSER" + " ENCRYPTED PASSWORD '" + aPassword + "'");
return aJDBCP;
}
public <T extends AbstractEntity> Processor createUpdateProcessor(
EntityManager em, T entity) {
DatabaseUser dbu = (DatabaseUser) entity;
assert dbu.getInstance().equals("pgsql");
JDBCProcessor aJDBCP = PgSqlDatabaseProcessorFactory.createPostgreSqlAdminProcessor();
String aName = AbstractEntity.escapeString(dbu.getName());
String aPassword = AbstractEntity.escapeString(dbu.getPassword());
aJDBCP.addSQL("ALTER USER " + aName + " WITH ENCRYPTED PASSWORD '" + aPassword + "'");
return aJDBCP;
}
public <T extends AbstractEntity> Processor createDeleteProcessor(
EntityManager em, T entity) {
DatabaseUser dbu = (DatabaseUser) entity;
assert dbu.getInstance().equals("pgsql");
JDBCProcessor aJDBCP = PgSqlDatabaseProcessorFactory.createPostgreSqlAdminProcessor();
String aName = AbstractEntity.escapeString(dbu.getName());
aJDBCP.addSQL("DROP USER " + aName);
return aJDBCP;
}
}