}
}
}
}
return;
}
if (pans != null) {
cans.addAll(pans);
}
cans.add(p);
// all ancestors have it as descendant
for (String pp : cans) {
Set desc = descendants.get(pp);
if (desc == null) {
if (first && newId == null) {
descendants.put(pp, desc = new HashSet());
}
desc.add(c);
}
}
ps.close();
} while (!todo.isEmpty());
// insert descendants into table
sql = String.format("INSERT INTO %s (ID, DESCENDANTID) VALUES (?, ?)",
table);
PreparedStatement ps = conn.prepareStatement(sql);
int n = 0;
for (Entry> e : descendants.entrySet()) {
String p = e.getKey();
for (String c : e.getValue()) {
ps.setString(1, p);
ps.setString(2, c);
// logDebug(sql, p, c);
ps.execute();
n++;
logDebug(String.format("-- inserted %s rows into %s", Long.valueOf(n),
table));
ps.close();
}
protected static String getLocalReadAcl(Connection conn, String id)
throws SQLException {
String sql = "SELECT \"GRANT\", \"USER\"" //
+ " FROM ACLS" //
+ " WHERE" //
+ " \"ID\" = '"
+ id
+ "'"//
+ " AND \"PERMISSION\" IN ('Read', 'ReadWrite', 'Everything', 'Browse')"
+ " ORDER BY \"POS\"";
if (isLogEnabled()) {
logDebug(sql);
}
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
StringBuffer readAcl = new StringBuffer();
try {
while (rs.next()) {
Boolean grant = rs.getBoolean(1);
String op;
if (grant) {
op = rs.getString(2);
} else {
op = '-' + rs.getString(2);
}
if (readAcl.length() == 0) {
readAcl.append(op);
} else {
readAcl.append("," + op);
}
}
} finally {
if (ps != null) {
ps.close();
}
}
return readAcl.toString();
}
public static String getReadAcl(Connection conn, String id)
throws SQLException {
String localReadAcl;
StringBuffer readAcl = new StringBuffer();
PreparedStatement ps1 = null;
PreparedStatement ps2 = null;
try {
ps1 = conn.prepareStatement("SELECT PARENTID FROM HIERARCHY WHERE ID = ?;");
boolean first = true;
do {
// local acl
localReadAcl = getLocalReadAcl(conn, id);
if (localReadAcl != null && (localReadAcl.length() > 0)) {
if (readAcl.length() == 0) {
readAcl.append(localReadAcl);
} else {
readAcl.append(',' + localReadAcl);
}
}
// get parent
ps1.setObject(1, id);
ResultSet rs = ps1.executeQuery();
String newId;
if (rs.next()) {
newId = (String) rs.getObject(1);
if (rs.wasNull()) {
newId = null;
}
} else {
// no such id
newId = null;
// there is no parent for the first level
// we may have a version on our hands, find the live doc
ps2 = conn.prepareStatement("SELECT VERSIONABLEID FROM VERSIONS WHERE ID = ?;");
ps2.setObject(1, id);
rs = ps2.executeQuery();
if (rs.next()) {
newId = (String) rs.getObject(1);
if (rs.wasNull()) {
newId = null;
}
} else {
// no such id
newId = null;
}
}
first = false;
id = newId;
} while (id != null);
} finally {
if (ps1 != null) {
ps1.close();
if (ps2 != null) {
ps2.close();
}
}
return readAcl.toString();
}
public static ResultSet getReadAclsFor(Connection conn, String principals)
throws SQLException {
DatabaseMetaData meta = conn.getMetaData();
SimpleResultSet result = new SimpleResultSet();
result.addColumn("ID", Types.VARCHAR, 0, 0); // String id
if (meta.getURL().startsWith("jdbc:columnlist:")) {
// this is just to query the result set columns
return result;
}
if (principals == null) {
return result;
}
Set principalsList = split(principals);
Set blackList = new HashSet();
for (String user : principalsList) {
blackList.add('-' + user);
}
// log.debug("getReadAclFor " + principals);
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement("SELECT \"ID\", \"ACL\" FROM READ_ACLS");
rs = ps.executeQuery();
while (rs.next()) {
String id = rs.getString(1);
String[] acl = rs.getString(2).split(",");
for (String ace : acl) {
// log.debug("ace: " + ace);
if (principalsList.contains(ace)) {
result.addRow(new Object[] { id });
// log.debug("allowed: " + id);
} else if (blackList.contains(ace)) {
// log.debug("deny: " + id);
break;
}
}
}
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
}
return result;
}
public static class LogAclsModified implements org.h2.api.Trigger,
CloseListener {
private String idName = "ID";
private int idIndex;
private int idType;
/**
* Trigger interface: initialization.
*/
public void init(Connection conn, String schema, String triggerName,
String table, boolean before, int opType) throws SQLException {
ResultSet rs = null;
try {
DatabaseMetaData meta = conn.getMetaData();
rs = meta.getColumns(null, schema, table, idName);
if (!rs.next()) {
throw new SQLException("No id key for: " + schema + '.'
+ table);
}
idType = rs.getInt("DATA_TYPE");
idIndex = rs.getInt("ORDINAL_POSITION") - 1;
} finally {
if (rs != null) {
rs.close();
}
}
}
/**
* Trigger interface.
*/
public void fire(Connection conn, Object[] oldRow, Object[] newRow)
throws SQLException {
String id = null;
if (oldRow != null) {
// update or delete
id = oldRow[idIndex].toString();
} else if (newRow != null) {
// insert
id = newRow[idIndex].toString();
} else {
PreparedStatement ps = null;
try {
ps = conn.prepareStatement("INSERT INTO hierarchy_modified_acl VALUES(?, ?);");
ps.setString(1, id);
ps.setString(2, "f");
ps.executeUpdate();
} finally {
if (ps != null) {
ps.close();
}
} |