Skip to content

Instantly share code, notes, and snippets.

Created January 30, 2014 10:29
Show Gist options
  • Select an option

  • Save anonymous/8705979 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/8705979 to your computer and use it in GitHub Desktop.
Fisheye user mapper
import javax.swing.*;
import java.io.File;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
/**
* Created with IntelliJ IDEA.
* User: ludger
* Date: 2013/08/05
* Time: 10:20 PM
* To change this template use File | Settings | File Templates.
*/
public class mapper {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
String connectionURL = "jdbc:mysql://localhost/fisheye";
Connection con = DriverManager.getConnection(connectionURL, "fisheyeuser", "password goes here ");
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM `cru_user`");
Collection<user> users = new ArrayList<user>();
while (rs.next()) {
user tempUser = new user();
tempUser.fullName = rs.getString("cru_displayname").toLowerCase();
tempUser.username = rs.getString("cru_user_name");
users.add(tempUser);
System.out.println("cru_user_name = " + rs.getString("cru_user_name") + " FullName = " + rs.getString("cru_displayname").toLowerCase());
}
JFileChooser j = new JFileChooser();
j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
Integer opt = j.showSaveDialog(null);
if(opt == JFileChooser.APPROVE_OPTION)
{
Collection<String> folders = listFolder(j.getSelectedFile().toPath().toString());
Collection<String> strings = generateSQL(folders, users,statement);
float total = strings.size();
float current = 0;
for(String Query: strings)
{
statement.executeUpdate(Query);
current++;
float percent = current/total;
percent = percent*100;
System.out.println(percent);
}
}
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
private static class user
{
public String username;
public String fullName;
}
public static Collection<String> listFolder(String path) {
Collection<String> temp = new ArrayList<String>();
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (!listOfFiles[i].isFile()) {
String folderName = listOfFiles[i].getName();
temp.add(folderName);
System.out.println(folderName);
}
}
return temp;
}
public static Collection<String> generateSQL(Collection<String> folders, Collection<user> users, Statement statement) throws SQLException {
Collection<String> SQLQueries = new ArrayList<String>();
for(user User:users)
{
for(String folder:folders)
{
ResultSet resultSet = statement.executeQuery("SELECT * " +
"FROM `cru_committer_user_mapping` " +
"WHERE `cru_committer_name` = '" + User.fullName + "'" +
"AND `cru_repository_name` = '" + folder + "'" +
"AND `cru_user_name` = '" + User.username + "'");
int i = 0;
while (resultSet.next()) i++;
if(i != 1)
{
String sqlQuery = "";
sqlQuery = "INSERT INTO `fisheye`.`cru_committer_user_mapping` (`cru_committer_name`, `cru_repository_name`, `cru_user_name`)";
sqlQuery += " VALUES ('"+User.fullName+"', '"+folder+"', '"+User.username+"');";
SQLQueries.add(sqlQuery);
System.out.println(sqlQuery);
}
}
}
return SQLQueries;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment