Search in sources :

Example 1 with RiskFactorCalculator

use of org.eclipse.titanium.markers.utils.RiskFactorCalculator in project titan.EclipsePlug-ins by eclipse.

the class XlsProblemExporter method exportMarkers.

/**
 * Export the code smells of a project to an excel workbook.
 * <p>
 * The first sheet of the workbook is a summary page, showing the number of
 * hits for each code smell, and an expressive bar chart of these data. The
 * further sheets enumerate the specific code smells of each kind, including
 * the message of the code smell, and the file name and line where it
 * occurred.
 * <p>
 * Note: All code smell types are used in the analysis and are written in
 * the output. Some code smells use external settings, which can be fine
 * tuned on the preference page.
 *
 * @param filename
 *            the file to save the xls
 * @param date
 *            the time stamp to write on the summary page
 *
 * @throws IOException
 *             when writing the file fails
 */
@Override
public // guaranteed to be initialized first.
void exportMarkers(final IProgressMonitor monitor, final String filename, final Date date) throws IOException {
    final SubMonitor progress = SubMonitor.convert(monitor, 100);
    final File file = new File(filename);
    POIFSFileSystem fs = null;
    HSSFWorkbook workbook = null;
    try {
        fs = new POIFSFileSystem(XlsProblemExporter.class.getResourceAsStream("ProblemMarkers.xlt"));
        workbook = new HSSFWorkbook(fs, true);
    } catch (IOException e) {
        ErrorReporter.logExceptionStackTrace("Error while exporting to excel", e);
        // one (without the chart).
        if (reportDebugInformation) {
            TITANDebugConsole.println("Error on opening ProblemMarkers.xlt. Chartless xls will be generated");
        }
        workbook = new HSSFWorkbook(new FileInputStream(file));
        workbook.createSheet("Summary");
        workbook.setSheetOrder("Summary", 0);
    } catch (Exception e) {
        ErrorReporter.logExceptionStackTrace("Error while exporting to excel", e);
        return;
    }
    progress.worked(10);
    final List<IProject> projects = ProjectBasedBuilder.getProjectBasedBuilder(project).getAllReachableProjects();
    projects.remove(project);
    try {
        final HSSFSheet summarySheet = workbook.getSheetAt(0);
        createTimeSheet(workbook);
        final Map<String, Integer> smellCount = new HashMap<String, Integer>();
        int summaryRow = 4;
        Cell label = null;
        Cell numberCell = null;
        final Map<TaskType, List<IMarker>> markers = collectMarkers();
        // export the task markers:
        for (final TaskType t : TaskType.values()) {
            createTaskSheet(workbook, t, markers.get(t), projects.size() > 0);
            final Row row1 = summarySheet.createRow(summaryRow++);
            label = row1.createCell(0);
            label.setCellValue(t.getHumanReadableName());
            final int nofMarkers = markers.get(t).size();
            numberCell = row1.createCell(1);
            numberCell.setCellValue(nofMarkers);
            // row-1 is the number of found markers
            smellCount.put(t.name(), nofMarkers);
        }
        progress.worked(20);
        final MarkerHandler mh = AnalyzerCache.withAll().analyzeProject(progress.newChild(30), project);
        progress.setWorkRemaining(CodeSmellType.values().length + 1);
        // export the semantic problem markers:
        for (final CodeSmellType t : CodeSmellType.values()) {
            createCodeSmellSheet(workbook, mh, t, projects.size() > 0);
            final Row row1 = summarySheet.createRow(summaryRow++);
            label = row1.createCell(0);
            label.setCellValue(t.getHumanReadableName());
            smellCount.put(t.name(), mh.numberOfOccurrences(t));
            numberCell = row1.createCell(1);
            numberCell.setCellValue(mh.numberOfOccurrences(t));
            progress.worked(1);
        }
        final StringBuilder nameBuilder = new StringBuilder("Project: ");
        nameBuilder.append(project.getName());
        if (projects.size() > 0) {
            nameBuilder.append(" including ( ");
            for (int i = 0; i < projects.size(); i++) {
                if (i > 0) {
                    nameBuilder.append(", ");
                }
                nameBuilder.append(projects.get(i).getName());
            }
            nameBuilder.append(" )");
        }
        final Row row0 = summarySheet.createRow(0);
        row0.createCell(0).setCellValue(nameBuilder.toString());
        final Row row1 = summarySheet.createRow(1);
        row1.createCell(0).setCellValue("Code smell \\ date");
        final CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("yyyy.mm.dd"));
        label = row1.createCell(1);
        label.setCellValue(date);
        label.setCellStyle(cellStyle);
        final Row row2 = summarySheet.createRow(2);
        row2.createCell(0).setCellValue("Commulative Project Risk Factor");
        final int riskFactor = new RiskFactorCalculator().measure(project, smellCount);
        row2.createCell(1).setCellValue(riskFactor);
        summarySheet.autoSizeColumn(0);
        summarySheet.autoSizeColumn(1);
        progress.worked(1);
    } catch (Exception e) {
        ErrorReporter.logExceptionStackTrace("Error while exporting to excel", e);
    } finally {
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            workbook.write(fileOutputStream);
        } catch (Exception e) {
            ErrorReporter.logExceptionStackTrace("Error while closing the generated excel", e);
        } finally {
            IOUtils.closeQuietly(fileOutputStream);
        }
    }
}
Also used : HashMap(java.util.HashMap) TaskType(org.eclipse.titanium.markers.types.TaskType) List(java.util.List) MarkerHandler(org.eclipse.titanium.markers.handler.MarkerHandler) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) Cell(org.apache.poi.ss.usermodel.Cell) SubMonitor(org.eclipse.core.runtime.SubMonitor) IOException(java.io.IOException) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) FileInputStream(java.io.FileInputStream) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) CodeSmellType(org.eclipse.titanium.markers.types.CodeSmellType) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) FileOutputStream(java.io.FileOutputStream) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) File(java.io.File) RiskFactorCalculator(org.eclipse.titanium.markers.utils.RiskFactorCalculator)

Aggregations

File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)1 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)1 Cell (org.apache.poi.ss.usermodel.Cell)1 CellStyle (org.apache.poi.ss.usermodel.CellStyle)1 Row (org.apache.poi.ss.usermodel.Row)1 IProject (org.eclipse.core.resources.IProject)1 CoreException (org.eclipse.core.runtime.CoreException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 MarkerHandler (org.eclipse.titanium.markers.handler.MarkerHandler)1 CodeSmellType (org.eclipse.titanium.markers.types.CodeSmellType)1 TaskType (org.eclipse.titanium.markers.types.TaskType)1 RiskFactorCalculator (org.eclipse.titanium.markers.utils.RiskFactorCalculator)1