Files
VisualSapfor/src/Visual_DVM_2021/Passes/All/ExportTasksPackageToExcel.java

293 lines
10 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package Visual_DVM_2021.Passes.All;
import Common.Current;
import Common.Global;
import Common.UI.UI;
import Common.Utils.Files.VDirectoryChooser;
import Common.Utils.Utils;
import GlobalData.Tasks.TaskState;
import TestingSystem.DVM.Tasks.TestRunTask;
import TestingSystem.DVM.TasksPackage.TasksPackage;
import TestingSystem.DVM.TasksPackage.TasksPackageState;
import Visual_DVM_2021.Passes.Pass_2021;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.*;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Vector;
public class ExportTasksPackageToExcel extends Pass_2021<TasksPackage> {
//https://tproger.ru/translations/how-to-read-write-excel-file-java-poi-example
File dir;
File res;
VDirectoryChooser directoryChooser = new VDirectoryChooser("Выбор папки для сохранения таблицы Excel");
//---
final int max_columns = 13;
Vector<CellStyle> styles;
//--
//--
Workbook book = null;
Sheet sheet = null;
//--
Vector<TestRunTask> tasks = null;
//--
@Override
public String getIconPath() {
return "/icons/Excel.png";
}
@Override
public String getButtonText() {
return "";
}
@Override
protected boolean needsAnimation() {
return true;
}
@Override
protected boolean canStart(Object... args) throws Exception {
book = null;
sheet = null;
styles = null;
tasks = null;
//--
if (Current.Check(Log, Current.TasksPackage)) {
target = Current.getTasksPackage();
if (!target.state.equals(TasksPackageState.Done)) {
Log.Writeln_("Можно получить таблицу только завершенного пакета.");
return false;
} else {
//---
tasks = new Vector<>();
for (TestRunTask task : Global.testingServer.account_db.testRunTasks.Data.values()) {
if ((task.taskspackage_id == target.id) && (task.isVisible()))
tasks.add(task);
}
if (tasks.isEmpty()) {
Log.Writeln_("Не найдено ни одной видимой задачи.");
return false;
}
//---
dir = directoryChooser.ShowDialog();
if (dir == null) {
Log.Writeln_("Папка не выбрана.");
return false;
}
res = new File(dir, Utils.getDateName("package_" + target.id) + ".xls");
return true;
}
}
return false;
}
protected Row addRow(int row_num, boolean isHeader, Object... values) {
Row row = sheet.createRow(row_num);
int i = 0;
for (Object value : values) {
String cell_text = value.toString();
Cell cell = row.createCell(i);
int style_index = 0;
if (!isHeader) {
switch (i) {
case 4:
case 5:
TaskState state = (TaskState) value;
cell_text = state.getDescription();
switch (state) {
case FailedToQueue:
case NoSuchTask:
case AbortedByUser:
case AbortedByTimeout:
case DoneWithErrors:
case WrongTestFormat:
case InternalError:
case Canceled:
style_index = 3;
break;
case Queued:
case Running:
style_index = 5;
break;
case Done:
style_index = 2;
break;
case Crushed:
style_index = 4;
break;
case Finished:
style_index = 6;
break;
}
break;
default:
style_index = 1;
break;
}
}
cell.setCellStyle(styles.get(style_index));
cell.setCellValue(cell_text);
//--
++i;
}
return row;
}
protected void setBorder(CellStyle style, short border) {
style.setBorderBottom(border);
style.setBorderLeft(border);
style.setBorderRight(border);
style.setBorderTop(border);
}
@Override
protected void body() throws Exception {
File file = Utils.getTempFileName("table");
//--
book = new HSSFWorkbook();
sheet = book.createSheet("Результаты тестирования");
//--
styles = new Vector<>();
//0 - заголовок
//1 - черный
//2 - зеленый
//3 - красный
//4 - темно красный
//5 - оранжевый
//6 - серый
for (int i = 0; i < 6; ++i) {
CellStyle style = book.createCellStyle();
Font font = book.createFont();
switch (i) {
case 0:
font.setBold(true);
setBorder(style, CellStyle.BORDER_THICK);
break;
default:
setBorder(style, CellStyle.BORDER_THIN);
switch (i) {
case 1:
font.setColor(HSSFColor.BLACK.index);
break;
case 2:
font.setColor(HSSFColor.GREEN.index);
break;
case 3:
font.setColor(HSSFColor.RED.index);
break;
case 4:
font.setColor(HSSFColor.DARK_RED.index);
break;
case 5:
font.setColor(HSSFColor.ORANGE.index);
break;
case 6:
font.setColor(HSSFColor.GREY_40_PERCENT.index);
break;
}
break;
}
style.setFont(font);
styles.add(style);
}
//--
Row header = addRow(0, true,
"Группа",
"Тест",
"Язык",
"Флаги",
"Сборка",
"Запуск",
"Время компиляции (с)",
"Матрица",
"Окружение",
"usr.par",
"Время выполнения (с)",
"Чистое время (с)",
"Прогресс (%)");
//--
int i = 1;
for (TestRunTask task : tasks) {
ShowMessage2(task.test_description);
Row row = addRow(i, false,
task.group_description,
task.test_description,
task.language.getDescription(),
task.flags,
task.compilation_state,
task.state,
task.compilation_time,
task.matrix,
task.environments,
task.usr_par,
task.Time,
task.CleanTime,
task.progress
);
++i;
}
//--
for (i = 0; i < max_columns; ++i)
sheet.autoSizeColumn(i);
// Записываем всё в файл
FileOutputStream stream = new FileOutputStream(file);
book.write(stream);
book.close();
stream.close();
///--
FileUtils.copyFile(file, res);
book = null;
stream = null;
file = null;
}
@Override
protected void performDone() throws Exception {
super.performDone();
if (Desktop.isDesktopSupported()) {
if (UI.Question("Таблица сформирована в файле\n" + Utils.DQuotes(res.getAbsolutePath()) + ".\nОткрыть её"
)) {
Desktop.getDesktop().open(res);
}
}
}
/*
short color = HSSFColor.BLACK.index;
String cell_value = null;
switch (i) {
case 4:
case 5:
TaskState state = (TaskState) value;
switch (state) {
case FailedToQueue:
case NoSuchTask:
case AbortedByUser:
case AbortedByTimeout:
case DoneWithErrors:
case WrongTestFormat:
case InternalError:
case Canceled:
color = HSSFColor.RED.index;
break;
case Queued:
case Running:
color = HSSFColor.ORANGE.index;
break;
case Done:
color = HSSFColor.GREEN.index;
break;
case Crushed:
color = HSSFColor.DARK_RED.index;
break;
case Finished:
color = HSSFColor.GREY_40_PERCENT.index;
break;
}
cell_value = state.getDescription();
break;
default:
cell_value = value.toString();
break;
}
font.setColor(color);
regular_style.setFont(font);
*/
}