Skip to content

Commit

Permalink
WINDUP-734 + WINDUP-728: Fix exportCSV bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mbriskar committed Sep 1, 2015
1 parent 7dd5e13 commit e76cc62
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.jboss.windup.reporting.service.ClassificationService;
import org.jboss.windup.reporting.service.InlineHintService;
import org.jboss.windup.util.Logging;
import org.jboss.windup.util.PathUtil;
import org.jboss.windup.util.exception.WindupException;
import org.ocpsoft.rewrite.config.Configuration;
import org.ocpsoft.rewrite.config.ConfigurationBuilder;
import org.ocpsoft.rewrite.context.EvaluationContext;
Expand All @@ -43,6 +45,7 @@ public class ExportCSVFileRuleProvider extends AbstractRuleProvider
public static final int COMMIT_INTERVAL = 750;
public static final int LOG_INTERVAL = 250;
private static Logger LOG = Logging.get(ExportCSVFileRuleProvider.class);
Map<String, CSVWriter> projectToFile;

public ExportCSVFileRuleProvider()
{
Expand All @@ -56,48 +59,29 @@ public ExportCSVFileRuleProvider()
public Configuration getConfiguration(GraphContext context)
{
return ConfigurationBuilder.begin()
.addRule()
.when(Query.fromType(WindupConfigurationModel.class).withProperty(WindupConfigurationModel.CSV_MODE,true))
.perform(
Iteration.over(Iteration.DEFAULT_VARIABLE_LIST_STRING).perform(
new ExportCSVReportOperation()).endIteration());
.addRule()
.when(Query.fromType(WindupConfigurationModel.class).withProperty(WindupConfigurationModel.CSV_MODE, true))
.perform(
Iteration.over(Iteration.DEFAULT_VARIABLE_LIST_STRING).perform(
new ExportCSVReportOperation()).endIteration());
}
// @formatter:on

private final class ExportCSVReportOperation extends AbstractIterationOperation<WindupConfigurationModel>
{
@Override public void perform(GraphRewrite event, EvaluationContext context, WindupConfigurationModel config)
@Override
public void perform(GraphRewrite event, EvaluationContext context, WindupConfigurationModel config)
{
projectToFile = new HashMap<>();
InlineHintService hintService = new InlineHintService(event.getGraphContext());
String outputFolderPath = config.getOutputPath().getFilePath() + File.separator;
ClassificationService classificationService = new ClassificationService(event.getGraphContext());
ProjectService projectService = new ProjectService(event.getGraphContext());
final Iterable<InlineHintModel> hints = hintService.findAll();
final Iterable<ProjectModel> projects = projectService.findAll();
final Iterable<ClassificationModel> classifications = classificationService.findAll();
final Iterable<ClassificationModel> classifications = classificationService.findAll();

Map<String, CSVWriter> rootProjectWriters = new HashMap<>();
//find all the root project models and open an .export file for it
for (ProjectModel project : projects)
{
ProjectModel root = project.getRootProjectModel();
if (!rootProjectWriters.containsKey(root.getName()))
{
try
{
CSVWriter writer = new CSVWriter(
new FileWriter(config.getOutputPath().getFilePath() + File.separator + root.getName() + ".csv"), ';');
String[] headerLine = new String[] {"Rule Id", "Problem type", "Title", "Description", "Links", "Application", "File Name", "File Path", "Line", "Story points"};
writer.writeNext(headerLine);
rootProjectWriters.put(root.getName(), writer);
}
catch (IOException e)
{
e.printStackTrace();
}

}
}
//in case something bad happens, we need to close files
//try{} in case something bad happens, we need to close files
try
{
for (InlineHintModel hint : hints)
Expand All @@ -124,7 +108,7 @@ private final class ExportCSVReportOperation extends AbstractIterationOperation<
projectNameString,
fileName, filePath, String.valueOf(
hint.getLineNumber()), String.valueOf(hint.getEffort()) };
rootProjectWriters.get(parentRootProjectModel.getName()).writeNext(strings);
writeCsvRecordForProject(outputFolderPath, parentRootProjectModel, strings);

}
for (ClassificationModel classification : classifications)
Expand All @@ -151,14 +135,14 @@ private final class ExportCSVReportOperation extends AbstractIterationOperation<
projectNameString, fileName, filePath, "N/A",
String.valueOf(
classification.getEffort()) };
rootProjectWriters.get(parentRootProjectModel.getName()).writeNext(strings);
writeCsvRecordForProject(outputFolderPath, parentRootProjectModel, strings);

}
}
}
finally
{
for (CSVWriter csvWriter : rootProjectWriters.values())
for (CSVWriter csvWriter : projectToFile.values())
{
try
{
Expand All @@ -174,21 +158,6 @@ private final class ExportCSVReportOperation extends AbstractIterationOperation<

}

private void addProjectReportToProject(WindupVertexFrame frame, ProjectModel rootProjectModel, Map<String, LinkedList<WindupVertexFrame>> map)
{
String projectName = rootProjectModel.getName();
if (map.containsKey(projectName))
{
map.get(projectName).add(frame);
}
else
{
LinkedList<WindupVertexFrame> linkedList = new LinkedList<>();
linkedList.add(frame);
map.put(projectName, linkedList);
}
}

private String buildLinkString(Iterable<LinkModel> links)
{
StringBuilder linksString = new StringBuilder();
Expand All @@ -202,5 +171,34 @@ private String buildLinkString(Iterable<LinkModel> links)
linksString.toString();
return linksString.toString();
}

private void writeCsvRecordForProject(String outputFolderPath, ProjectModel projectModel, String[] line)
{
if (!projectToFile.containsKey(projectModel.getName()))
{
CSVWriter writer = initCSVWriter(outputFolderPath + PathUtil.cleanFileName(projectModel.getName()) + ".csv");
projectToFile.put(projectModel.getName(), writer);
}
projectToFile.get(projectModel.getName()).writeNext(line);

}

private CSVWriter initCSVWriter(String path)
{
try
{
CSVWriter writer = new CSVWriter(
new FileWriter(path), ';');
String[] headerLine = new String[] { "Rule Id", "Problem type", "Title", "Description", "Links", "Application", "File Name",
"File Path", "Line", "Story points" };
writer.writeNext(headerLine);
return writer;
}
catch (IOException e)
{
System.err.println("Windup was not able to create a CSV file " + path + ". CSV Export will not be generated.");
throw new WindupException("Unable to create file " + path, e);
}
}
}
}
1 change: 1 addition & 0 deletions utils/src/main/java/org/jboss/windup/util/PathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public static Path getWindupRulesDir()
/**
* Conservative approach to insuring that a given filename only contains characters that are legal for use in
* filenames on the disk. Other characters are replaced with underscore _ .
* Note that this should only be used with the filename itself, not the entire path, because it removes the '/' characters as well.
*/
public static String cleanFileName(String badFileName)
{
Expand Down

0 comments on commit e76cc62

Please sign in to comment.