package accounts;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class VirtuosoUserManager implements UserManager {
  private static final String jdbcDriver = "virtuoso.jdbc4.Driver";
  private String connectionString;
  private String user;
  private String password;
  private Connection connection;

  /**
   * This class manages the User creation in virtuoso for authentication and
   * graph access control
   * 
   * @param connectionString
   *          Connection string to Virtuoso
   * @param user
   *          Virtuoso user
   * @param password
   *          Virtuoso password
   */
  public VirtuosoUserManager(String connectionString, String user, String password) {
    this.connectionString = connectionString;
    this.user = user;
    this.password = password;
  }

  @Override
  public void createUser(String name, String password) throws Exception {
    executeUpdate(getConnection(), "DB.DBA.USER_CREATE('" + name + "', '" + password + "')");
    // executeUpdate(getConnection(), "USER_SET_OPTION('" + name +
    // "', 'DAV_ENABLE', 1)");
  }

  @Override
  public void dropUser(String name) throws Exception {
    executeUpdate(getConnection(), "USER_DROP('" + name + "', 1)");
  }

  @Override
  public void grantRole(String user, String role) throws Exception {
    executeUpdate(getConnection(), "GRANT " + role + " TO \"" + user + "\"");
  }

  @Override
  public void setDefaultRdfPermissions(String user, int permissions) throws Exception {
    executeUpdate(getConnection(), "DB.DBA.RDF_DEFAULT_USER_PERMS_SET ('" + user + "', "
        + permissions + ")");
  }

  @Override
  public void setRdfGraphPermissions(String user, String graph, int permissions) throws Exception {
    executeUpdate(getConnection(), "DB.DBA.RDF_GRAPH_USER_PERMS_SET ('" + graph + "', '" + user
        + "', " + permissions + ")");
  }

  @Override
  public void deleteRdfGraphPermissions(String user, String graph) throws Exception {
    executeUpdate(getConnection(), "DB.DBA.RDF_GRAPH_USER_PERMS_DEL ('" + graph + "', '" + user
        + "')");
  }

  @Override
  public void setDefaultGraphPermissions(String graph, int permissions) throws Exception {
    executeUpdate(getConnection(), "DB.DBA.RDF_GRAPH_USER_PERMS_SET ('" + graph + "', 'nobody', "
        + permissions + ")");
  }

  private Connection getConnection() throws ClassNotFoundException, SQLException {
    if (connection == null || connection.isClosed()) {
      Class.forName(jdbcDriver);
      connection = DriverManager.getConnection(connectionString, user, password);
    }
    return connection;
  }

  public void close() throws SQLException {
    if (connection != null)
      connection.close();
  }

  private void executeUpdate(Connection conn, String query) throws SQLException {
    System.out.println("EXECUTE: " + query);
    Statement stmt = conn.createStatement();
    try {
      stmt.executeUpdate(query);
    } finally {
      stmt.close();
    }
  }
}

@@ -456,21 +456,16 @@
 tion and
-%0A   *
  graph a
@@ -916,32 +916,59 @@
 assword) throws 
+ClassNotFoundException, SQL
 Exception %7B%0A    
@@ -1207,32 +1207,59 @@
 ng name) throws 
+ClassNotFoundException, SQL
 Exception %7B%0A    
@@ -1388,32 +1388,59 @@
 ng role) throws 
+ClassNotFoundException, SQL
 Exception %7B%0A    
@@ -1598,32 +1598,65 @@
 issions) throws 
+ClassNotFoundException,%0A      SQL
 Exception %7B%0A    
@@ -1863,32 +1863,38 @@
 permissions)
+%0A     
  throws 
 Exception %7B%0A
@@ -1873,32 +1873,59 @@
 s)%0A      throws 
+ClassNotFoundException, SQL
 Exception %7B%0A    
@@ -2137,32 +2137,65 @@
 g graph) throws 
+ClassNotFoundException,%0A      SQL
 Exception %7B%0A    
@@ -2391,24 +2391,30 @@
 issions)
+%0A     
  throws 
 Exceptio
@@ -2405,16 +2405,43 @@
  throws 
+ClassNotFoundException, SQL
 Exceptio
@@ -2569,32 +2569,505 @@
 ns + %22)%22);%0A  %7D%0A%0A
+  /**%0A   * This function grant L_O_LOOK in virtuoso which was required to solve the error: Virtuoso 42000%0A   * Error SR186: No permission to execute dpipe DB.DBA.L_O_LOOK with user ID 106, group ID 106%0A   * %0A   * @param user%0A   * @throws ClassNotFoundException%0A   * @throws SQLException%0A   */%0A  public void grantLOLook(String user) throws ClassNotFoundException, SQLException %7B%0A    executeUpdate(getConnection(), %22GRANT EXECUTE ON DB.DBA.L_O_LOOK TO '%22 + user + %22'%22);%0A  %7D%0A%0A
   private Connec

add a function to fix update error