[Schmitzm-commits] r1686 - in trunk/schmitzm-core/src/main/java/de/schmitzm: geotools/postgres swing/event

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Aug 25 16:56:45 CEST 2011


Author: alfonx
Date: 2011-08-25 16:56:44 +0200 (Thu, 25 Aug 2011)
New Revision: 1686

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/geotools/postgres/PGUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/TextComponentChangeListener.java
Log:
Bei DB-Init werden die WIME-Rollen von Benutzer nicht verworfen.

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/geotools/postgres/PGUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/geotools/postgres/PGUtil.java	2011-08-21 10:28:37 UTC (rev 1685)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/geotools/postgres/PGUtil.java	2011-08-25 14:56:44 UTC (rev 1686)
@@ -12,19 +12,21 @@
 import de.schmitzm.lang.LangUtil;
 
 /**
- * Hilfsmethoden für PostgreSQL. Die Dependency soll nur {@link Connection} sein. In Hibernate kann über #doWork eine
- * {@link Connection} erhalten werden.
+ * Hilfsmethoden für PostgreSQL. Die Dependency soll nur {@link Connection}
+ * sein. In Hibernate kann über #doWork eine {@link Connection} erhalten werden.
  */
 public class PGUtil {
 	static final Logger log = Logger.getLogger(PGUtil.class);
 
 	/**
-	 * PostgreSQL Privilegien die per Grant einer Tabelle zugeordnet werden können.<br/>
+	 * PostgreSQL Privilegien die per Grant einer Tabelle zugeordnet werden
+	 * können.<br/>
 	 * 
 	 * @see http://www.postgresql.org/docs/9.1/static/sql-grant.html
 	 */
 	public enum Privileges {
-		SELECT, INSERT, UPDATE, DELETE, REFERENCES, TEMPORARY, ALL_PRIVILEGES("ALL PRIVILEGES");
+		SELECT, INSERT, UPDATE, DELETE, REFERENCES, TEMPORARY, ALL_PRIVILEGES(
+				"ALL PRIVILEGES");
 
 		String sql;
 
@@ -53,11 +55,12 @@
 	 *            List der zuzuordnenden Privilegien
 	 * @throws SQLException
 	 */
-	public static void grantPrivilegesOnTable(Connection c, String targetRole, String tablename,
-			Privileges... privileges) throws SQLException {
+	public static void grantPrivilegesOnTable(Connection c, String targetRole,
+			String tablename, Privileges... privileges) throws SQLException {
 		// GRANT SELECT, UPDATE, INSERT ON mytable TO admin;
-		String sql = "GRANT " + LangUtil.stringConcatWithSep(",", (Object[]) privileges) + " ON " + tablename + " TO "
-				+ targetRole;
+		String sql = "GRANT "
+				+ LangUtil.stringConcatWithSep(",", (Object[]) privileges)
+				+ " ON " + tablename + " TO " + targetRole;
 		c.createStatement().executeUpdate(sql);
 	}
 
@@ -70,30 +73,33 @@
 	 *            List der zuzuordnenden Privilegien
 	 * @throws SQLException
 	 */
-	public static void revokePrivilegesOnTable(Connection c, String tablename, String targetRole,
-			Privileges... privileges) throws SQLException {
+	public static void revokePrivilegesOnTable(Connection c, String tablename,
+			String targetRole, Privileges... privileges) throws SQLException {
 		// GRANT SELECT, UPDATE, INSERT ON mytable TO admin;
-		String sql = "REVOKE " + LangUtil.stringConcatWithSep(",", (Object[]) privileges) + " ON " + tablename
-				+ " FROM " + targetRole;
+		String sql = "REVOKE "
+				+ LangUtil.stringConcatWithSep(",", (Object[]) privileges)
+				+ " ON " + tablename + " FROM " + targetRole;
 		c.createStatement().executeUpdate(sql);
 	}
 
 	/**
-	 * Erstellt eine neue Rolle ohne besondere Starteigenschaften.
+	 * Erstellt eine neue Rolle
 	 */
-	public static void createRole(Connection c, String rolename) throws SQLException {
+	public static void createRole(Connection c, String rolename)
+			throws SQLException {
 		String sql = "CREATE ROLE " + rolename;
 		c.createStatement().executeUpdate(sql);
 	}
 
 	/**
-	 * Querys the <code>geometry_columns</code> table is part of every POSTGIS installation and must/should describe the
-	 * geometry columns and tables.
+	 * Querys the <code>geometry_columns</code> table is part of every POSTGIS
+	 * installation and must/should describe the geometry columns and tables.
 	 */
 	static public String[] getColumnsDescribedInGeometryColumnsTable(Statement s) {
 		String[] columns = new String[0];
 		try {
-			ResultSet askGeoe = s.executeQuery("SELECT f_table_name FROM geometry_columns;");
+			ResultSet askGeoe = s
+					.executeQuery("SELECT f_table_name FROM geometry_columns;");
 			while (askGeoe.next()) {
 				columns = LangUtil.extendArray(columns, askGeoe.getString(1));
 			}
@@ -114,10 +120,16 @@
 	 */
 	@Deprecated
 	// tut noch nicht
-	public static void createOrUpdateGeometrsColumnsEntry(String tableName, String geoColumnName, int srid) {
+	public static void createOrUpdateGeometrysColumnsEntry(String tableName,
+			String geoColumnName, int srid) {
 
 		String createGeometryEntrySQL = "INSERT INTO geometry_columns (f_table_catalog, f_table_schema, f_table_name, f_geometry_column, coord_dimension, srid, \"type\") VALUES ( '', 'public', '"
-				+ tableName + "', '" + geoColumnName + "', 2, " + srid + ",'MULTIPOLYGON' ) LIMIT 1;";
+				+ tableName
+				+ "', '"
+				+ geoColumnName
+				+ "', 2, "
+				+ srid
+				+ ",'MULTIPOLYGON' ) LIMIT 1;";
 	}
 
 	// TODO isGeoColumn();
@@ -125,8 +137,8 @@
 	/**
 	 * @param binding
 	 *            Eine "einfache" Javaklasse, die in PG abgebildet werden soll.
-	 * @return einen PG-spezifischen Datentypenamen für einen Javatyp zurück, z.b. "double precision" für
-	 *         <code>Double.class</code>
+	 * @return einen PG-spezifischen Datentypenamen für einen Javatyp zurück,
+	 *         z.b. "double precision" für <code>Double.class</code>
 	 */
 	public static String getColTypeName(Class<?> binding) {
 
@@ -140,7 +152,8 @@
 			return "bigint";
 		}
 
-		throw new RuntimeException("DB Type mapping for " + binding + " not yet implemented.");
+		throw new RuntimeException("DB Type mapping for " + binding
+				+ " not yet implemented.");
 	}
 
 	/**
@@ -148,30 +161,35 @@
 	 * 
 	 * @return <code>true</code> wenn das Schema neu angelegt wurde.
 	 */
-	public static boolean createSchemaIfNotExists(Connection c, String schemaname) throws SQLException {
+	public static boolean createSchemaIfNotExists(Connection c,
+			String schemaname) throws SQLException {
 		if (!existsSchema(c, schemaname)) {
-			c.createStatement().execute("create schema " + schemaname.toLowerCase());
+			c.createStatement().execute(
+					"create schema " + schemaname.toLowerCase());
 			return true;
 		}
 		return false;
 	}
 
 	/**
-	 * Liefert <code>true</code> wenn ein Schema mit dem Namen in der PG Datenbank existiert.
+	 * Liefert <code>true</code> wenn ein Schema mit dem Namen in der PG
+	 * Datenbank existiert.
 	 */
-	public static boolean existsSchema(Connection c, String schemaname) throws SQLException {
+	public static boolean existsSchema(Connection c, String schemaname)
+			throws SQLException {
 		ResultSet rs = c.createStatement().executeQuery(
-				"select 1 from pg_catalog.pg_namespace where nspname = '" + schemaname.toLowerCase() + "'");
+				"select 1 from pg_catalog.pg_namespace where nspname = '"
+						+ schemaname.toLowerCase() + "'");
 		return rs.next();
 	}
 
 	/**
-	 * Liefert eine iste der Rollen in denen der Übergebenen USER (Login role) ist. <br/>
+	 * Liefert eine Liste der Rollen in denen der Übergebenen USER (Login role)
+	 * ist. <br/>
 	 * Tested with PG 8.4
-	 * 
-	 * @throws SQLException
 	 */
-	public static List<String> listRolesForUser(Connection c, String username) throws SQLException {
+	public static List<String> listRolesForUser(Connection c, String username)
+			throws SQLException {
 		List<String> roles = new ArrayList<String>();
 		ResultSet rs = c
 				.createStatement()
@@ -185,17 +203,34 @@
 	}
 
 	/**
-	 * Liste aller Tabellennamen in diesem Schema, ohne die Schemaangabe im Namen. (Also 'anreden' und nicht
-	 * 'public.anreden') <br/>
+	 * Liefert alle Benutzer der DB zurück.
+	 * 
+	 * @throws SQLException
+	 */
+	public static List<String> listUsers(Connection c) throws SQLException {
+		List<String> roles = new ArrayList<String>();
+		ResultSet rs = c.createStatement()
+				.executeQuery("select * from pg_user");
+		while (rs.next()) {
+			roles.add(rs.getString(1));
+		}
+		return roles;
+	}
+
+	/**
+	 * Liste aller Tabellennamen in diesem Schema, ohne die Schemaangabe im
+	 * Namen. (Also 'anreden' und nicht 'public.anreden') <br/>
 	 * Tested with PG 8.4
 	 * 
 	 * @param schemaname
 	 *            if <code>null</code>, <code>public</code> is used.
 	 */
-	public static List<String> listTablesInSchema(Connection c, String schemaname) throws SQLException {
+	public static List<String> listTablesInSchema(Connection c,
+			String schemaname) throws SQLException {
 		List<String> tables = new ArrayList<String>();
 
-		String sql = "select tablename from pg_tables where schemaname = '" + schemaname.toLowerCase() + "'";
+		String sql = "select tablename from pg_tables where schemaname = '"
+				+ schemaname.toLowerCase() + "'";
 		ResultSet rs = c.createStatement().executeQuery(sql);
 		while (rs.next()) {
 			tables.add(rs.getString(1));
@@ -206,34 +241,55 @@
 	/**
 	 * Liefert <code>true</code> wenn der Benutzername im DBMS Superuser ist.
 	 */
-	public static boolean isSuperuser(Connection c, String username) throws SQLException {
+	public static boolean isSuperuser(Connection c, String username)
+			throws SQLException {
 		ResultSet rs = c.createStatement().executeQuery(
-				"select usesuper from pg_user where usename = '" + username + "' and usesuper = true");
+				"select usesuper from pg_user where usename = '" + username
+						+ "' and usesuper = true");
 		if (rs.next()) {
 			return true;
 		}
 		return false;
 	}
 
-	public static boolean existsRole(Connection c, String rolename) throws SQLException {
+	public static boolean existsRole(Connection c, String rolename)
+			throws SQLException {
 		ResultSet rs = c.createStatement().executeQuery(
-				"select count(1) from pg_roles where rolname = '" + rolename.toLowerCase() + "'");
+				"select count(1) from pg_roles where rolname = '"
+						+ rolename.toLowerCase() + "'");
 		if (rs.next())
 			return rs.getInt(1) > 0;
 		return false;
 	}
 
-	public static void removeAllPrivilegesFrom(Connection c, String rolename, String tablename) throws SQLException {
+	public static void removeAllPrivilegesFrom(Connection c, String rolename,
+			String tablename) throws SQLException {
 		c.createStatement().executeUpdate(
-				"revoke all on TABLE " + tablename.toLowerCase() + " from " + rolename.toLowerCase() + " cascade");
+				"revoke all on TABLE " + tablename.toLowerCase() + " from "
+						+ rolename.toLowerCase() + " cascade");
 	}
 
-	public static void dropRoleIfExists(Connection c, String rolename) throws SQLException {
-		c.createStatement().executeUpdate("drop role if exists " + rolename.toLowerCase());
+	/**
+	 * Löscht eine Rolle wenn sie vorher existiert
+	 */
+	public static void dropRoleIfExists(Connection c, String rolename)
+			throws SQLException {
+		c.createStatement().executeUpdate(
+				"drop role if exists " + rolename.toLowerCase());
 	}
 
-	public static void dropTriggerIfExists(Connection c, String triggerName, String tableName) throws SQLException {
-		c.createStatement().executeUpdate("drop TRIGGER if exists " + triggerName.toLowerCase() + " ON " + tableName);
+	/**
+	 * Löscht einen Trigger wenn er vorher existierte.
+	 * 
+	 * @param c
+	 * @param triggerName
+	 * @param tableName
+	 */
+	public static void dropTriggerIfExists(Connection c, String triggerName,
+			String tableName) throws SQLException {
+		c.createStatement().executeUpdate(
+				"drop TRIGGER if exists " + triggerName.toLowerCase() + " ON "
+						+ tableName);
 	}
 
 	/**
@@ -242,13 +298,17 @@
 	 * @param name
 	 *            z.B.: keckformel wird dann zu keckformel()
 	 * @param plCommands
-	 *            z.B. NEW.kpk_kreis = NEW.risiko_soz+NEW.risiko_emo+NEW.risiko_moti+NEW.risiko_spr+NEW.risiko_wohl;
+	 *            z.B. NEW.kpk_kreis =
+	 *            NEW.risiko_soz+NEW.risiko_emo+NEW.risiko_moti
+	 *            +NEW.risiko_spr+NEW.risiko_wohl;
 	 * @throws SQLException
 	 */
-	public static void createOrReplaceFunction(Connection c, String name, String plCommands) throws SQLException {
-		System.err.println(plCommands);
+	public static void createOrReplaceFunction(Connection c, String name,
+			String plCommands) throws SQLException {
+		// System.err.println(plCommands);
 		c.createStatement().executeUpdate(
-				"CREATE OR REPLACE FUNCTION " + name + "()" + "RETURNS trigger AS 'BEGIN " + plCommands
+				"CREATE OR REPLACE FUNCTION " + name + "()"
+						+ "RETURNS trigger AS 'BEGIN " + plCommands
 						+ "RETURN NEW; END;' LANGUAGE 'plpgsql'");
 	}
 
@@ -261,12 +321,14 @@
 	 * @param plCommands
 	 *            Simple PSQL commands
 	 */
-	public static void createOrRecreateTrigger(Connection c, String triggerName, String type, String tableName,
-			String plCommands) throws SQLException {
+	public static void createOrRecreateTrigger(Connection c,
+			String triggerName, String type, String tableName, String plCommands)
+			throws SQLException {
 		dropTriggerIfExists(c, triggerName, tableName);
 		createOrReplaceFunction(c, triggerName + "_fn", plCommands);
 		c.createStatement().executeUpdate(
-				"CREATE TRIGGER " + triggerName + " " + type + " ON " + tableName + " FOR EACH ROW EXECUTE PROCEDURE "
+				"CREATE TRIGGER " + triggerName + " " + type + " ON "
+						+ tableName + " FOR EACH ROW EXECUTE PROCEDURE "
 						+ triggerName + "_fn" + "()");
 	}
 
@@ -278,10 +340,12 @@
 	 * @param idxNamePostfix
 	 *            e.g. 'idx1'
 	 * @param unique
-	 *            <code>true</code> if the columns are unique. A UNIQUE INDEX will be created.
+	 *            <code>true</code> if the columns are unique. A UNIQUE INDEX
+	 *            will be created.
 	 */
-	public static void addIndex(Connection c, final String tableName, final String columns, String idxPostfix,
-			final boolean unique) throws SQLException {
+	public static void addIndex(Connection c, final String tableName,
+			final String columns, String idxPostfix, final boolean unique)
+			throws SQLException {
 
 	}
 
@@ -293,18 +357,23 @@
 	 * @param idxNamePostfix
 	 *            e.g. 'idx1'
 	 * @param unique
-	 *            <code>true</code> if the columns are unique. A UNIQUE INDEX will be created.
+	 *            <code>true</code> if the columns are unique. A UNIQUE INDEX
+	 *            will be created.
 	 * @param tablespace
-	 *            <code>null</code> oder der name des tablespace in dem der Index liegen soll.
+	 *            <code>null</code> oder der name des tablespace in dem der
+	 *            Index liegen soll.
 	 */
-	public static void addIndex(Connection c, final String tableName, final String columns, String idxPostfix,
-			final boolean unique, String tablespace) throws SQLException {
+	public static void addIndex(Connection c, final String tableName,
+			final String columns, String idxPostfix, final boolean unique,
+			String tablespace) throws SQLException {
 		final String idxName = (tableName + "_" + idxPostfix).replace(".", "_");
 
 		Statement s = c.createStatement();
 		// TODO DROP FIRST?
-		final String queryString = "CREATE " + (unique ? "UNIQUE " : "") + "INDEX " + idxName + " ON " + tableName
-				+ " (" + columns + ") "+(tablespace == null? "" : " TABLESPACE "+tablespace);
+		final String queryString = "CREATE " + (unique ? "UNIQUE " : "")
+				+ "INDEX " + idxName + " ON " + tableName + " (" + columns
+				+ ") "
+				+ (tablespace == null ? "" : " TABLESPACE " + tablespace);
 		try {
 			s.execute(queryString);
 			c.commit();
@@ -321,9 +390,11 @@
 			// Ex abfangen, wenn IDX schon existiert
 
 			String msg = cause.getMessage();
-			if (msg.endsWith("already exists") || msg.endsWith("existiert bereits")) {
+			if (msg.endsWith("already exists")
+					|| msg.endsWith("existiert bereits")) {
 				// TO NOTHING, IDX already exists
-				log.info("Index existierte bereits, nicht neuerstellt: " + queryString);
+				log.info("Index existierte bereits, nicht neuerstellt: "
+						+ queryString);
 			} else
 				log.error(e.getLocalizedMessage(), e);
 
@@ -331,4 +402,15 @@
 
 	}
 
+	/**
+	 * Fügt einen Benutzer zu einer Rolle hinzu. Wenn der benutzer bereits in
+	 * der Rolle enthalten ist, wird keine Exception geschmisssen.
+	 * @throws SQLException 
+	 */
+	public static void grantRoleToUser(Connection c, String rolename,
+			String username) throws SQLException {
+		c.createStatement().executeUpdate(
+				"GRANT " + rolename + " TO " + username);
+	}
+
 }

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/TextComponentChangeListener.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/TextComponentChangeListener.java	2011-08-21 10:28:37 UTC (rev 1685)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/TextComponentChangeListener.java	2011-08-25 14:56:44 UTC (rev 1686)
@@ -1,6 +1,5 @@
 package de.schmitzm.swing.event;
 
-import java.awt.Component;
 import java.awt.event.FocusAdapter;
 import java.awt.event.FocusEvent;
 import java.awt.event.FocusListener;



More information about the Schmitzm-commits mailing list