* @return the modified Project object, which now contains the stats
*/
@Override
public Project fetchStats(Project p) throws Exception {
MySQLConnection conn = new MySQLConnection();
conn.connect();
// Get project's stats for all versions
ResultSet rs1 = conn
.SQLSelect("SELECT * FROM versions JOIN general_stats "
+ "ON general_stats.version = versions.id "
+ "WHERE versions.project = " + p.getId()
+ " ORDER BY versions.version");
ResultSet rs2 = conn
.SQLSelect("SELECT * FROM versions JOIN test_stats "
+ "ON test_stats.version = versions.id "
+ "WHERE versions.project = " + p.getId()
+ " ORDER BY versions.version");
ResultSet rs3 = conn
.SQLSelect("SELECT * FROM versions JOIN findbugs_stats "
+ "ON findbugs_stats.version = versions.id "
+ "WHERE versions.project = " + p.getId()
+ " ORDER BY versions.version");
// fetchProjects() already fetched the nlocs which now might be from an
// old version, so clear them then fetch them again
p.clearNlocs();
// Get project's general stats
while (rs1.next()) {
int nloc = rs1.getInt("nloc");
if (!rs1.wasNull())
p.addNloc(nloc);
else
p.addNloc(-1);
int npkg = rs1.getInt("npkg");
if (!rs1.wasNull())
p.addNpkg(npkg);
else
p.addNpkg(-1);
int nfile = rs1.getInt("nfile");
if (!rs1.wasNull())
p.addNfile(nfile);
else
p.addNfile(-1);
int nfunc = rs1.getInt("nfunc");
if (!rs1.wasNull())
p.addNfunc(nfunc);
else
p.addNfunc(-1);
int ncl = rs1.getInt("ncl");
if (!rs1.wasNull())
p.addNcl(ncl);
else
p.addNcl(-1);
int ncom = rs1.getInt("ncom");
if (!rs1.wasNull())
p.addNcom(ncom);
else
p.addNcom(-1);
int ndupl = rs1.getInt("ndupl");
if (!rs1.wasNull())
p.addNdupl(ndupl);
else
p.addNdupl(-1);
}
// Get project's test stats
while (rs2.next()) {
int ncovl = rs2.getInt("ncovl");
if (!rs2.wasNull())
p.addNcovl(ncovl);
else
p.addNcovl(-1);
}
int currentVersion = 0;
ArrayList<FindbugsBugInstance> bps = null;
ArrayList<FindbugsBugInstance> ps = null;
// Get project's Findbugs bug instances
while (rs3.next()) {
int version = rs3.getInt("version");
// We got to the next version
if (version > currentVersion) {
currentVersion++;
if (bps != null) {
p.addBp(bps);
p.addP(ps);
}
bps = new ArrayList<FindbugsBugInstance>();
ps = new ArrayList<FindbugsBugInstance>();
String category = rs3.getString("category");
// If category is null, Findbugs bug instances are unavailable
// for this version
if (rs3.wasNull()) {
p.addBp(null);
p.addP(null);
} else {
String abbrev = rs3.getString("abbrev");
String type = rs3.getString("type");
String file = rs3.getString("file");
if (rs3.wasNull())
file = null;
int line = rs3.getInt("line");
if (rs3.wasNull())
line = -1;
FindbugsBugInstance fb = new FindbugsBugInstance(category,
abbrev, type, file, line);
if (category.equals(FindbugsCategory.BAD_PRACTICE
.toString())) {
bps.add(fb);
} else {
ps.add(fb);
}
}
}
// We are reading the same version as in the previous loop, ignore
// anything but Findbugs
else {
// Get project's Findbugs bug instances
String category = rs3.getString("category");
String abbrev = rs3.getString("abbrev");
String type = rs3.getString("type");
String file = rs3.getString("file");
if (rs3.wasNull())
file = null;
int line = rs3.getInt("line");
if (rs3.wasNull())
line = -1;
FindbugsBugInstance fb = new FindbugsBugInstance(category,
abbrev, type, file, line);
if (category.equals(FindbugsCategory.BAD_PRACTICE.toString())) {
bps.add(fb);
} else {
ps.add(fb);
}
}
}
p.addBp(bps);
p.addP(ps);
conn.disconnect();
return p;
}