mirror of
https://github.com/dalbodeule/snap-admin.git
synced 2025-08-06 12:11:13 +00:00
Export raw values option
This commit is contained in:
@@ -30,6 +30,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
|||||||
import tech.ailef.dbadmin.external.DbAdmin;
|
import tech.ailef.dbadmin.external.DbAdmin;
|
||||||
import tech.ailef.dbadmin.external.dbmapping.DbAdminRepository;
|
import tech.ailef.dbadmin.external.dbmapping.DbAdminRepository;
|
||||||
import tech.ailef.dbadmin.external.dbmapping.DbField;
|
import tech.ailef.dbadmin.external.dbmapping.DbField;
|
||||||
|
import tech.ailef.dbadmin.external.dbmapping.DbFieldValue;
|
||||||
import tech.ailef.dbadmin.external.dbmapping.DbObject;
|
import tech.ailef.dbadmin.external.dbmapping.DbObject;
|
||||||
import tech.ailef.dbadmin.external.dbmapping.DbObjectSchema;
|
import tech.ailef.dbadmin.external.dbmapping.DbObjectSchema;
|
||||||
import tech.ailef.dbadmin.external.dto.DataExportFormat;
|
import tech.ailef.dbadmin.external.dto.DataExportFormat;
|
||||||
@@ -50,8 +51,8 @@ public class DataExportController {
|
|||||||
@GetMapping("/{className}")
|
@GetMapping("/{className}")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseEntity<byte[]> export(@PathVariable String className, @RequestParam(required = false) String query,
|
public ResponseEntity<byte[]> export(@PathVariable String className, @RequestParam(required = false) String query,
|
||||||
@RequestParam(required = false) String format, @RequestParam MultiValueMap<String, String> otherParams) {
|
@RequestParam String format, @RequestParam(required=false) Boolean raw,
|
||||||
|
@RequestParam MultiValueMap<String, String> otherParams) {
|
||||||
if (format == null)
|
if (format == null)
|
||||||
format = "CSV";
|
format = "CSV";
|
||||||
DataExportFormat exportFormat = null;
|
DataExportFormat exportFormat = null;
|
||||||
@@ -72,17 +73,20 @@ public class DataExportController {
|
|||||||
|
|
||||||
List<DbObject> results = repository.search(schema, query, queryFilters);
|
List<DbObject> results = repository.search(schema, query, queryFilters);
|
||||||
|
|
||||||
|
if (raw == null) raw = false;
|
||||||
|
|
||||||
switch (exportFormat) {
|
switch (exportFormat) {
|
||||||
case CSV:
|
case CSV:
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||||
"attachment; filename=\"export_" + schema.getJavaClass().getSimpleName() + ".csv\"")
|
"attachment; filename=\"export_" + schema.getJavaClass().getSimpleName() + ".csv\"")
|
||||||
.body(toCsv(results, fields).getBytes());
|
.body(toCsv(results, fields, raw).getBytes());
|
||||||
case XLSX:
|
case XLSX:
|
||||||
|
String sheetName = schema.getJavaClass().getSimpleName();
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||||
"attachment; filename=\"export_" + schema.getJavaClass().getSimpleName() + ".xlsx\"")
|
"attachment; filename=\"export_" + schema.getJavaClass().getSimpleName() + ".xlsx\"")
|
||||||
.body(toXlsx(results, fields));
|
.body(toXlsx(sheetName, results, fields, raw));
|
||||||
case JSON:
|
case JSON:
|
||||||
throw new DbAdminException("JSON TODO");
|
throw new DbAdminException("JSON TODO");
|
||||||
default:
|
default:
|
||||||
@@ -91,10 +95,10 @@ public class DataExportController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] toXlsx(List<DbObject> items, List<DbField> fields) {
|
private byte[] toXlsx(String sheetName, List<DbObject> items, List<DbField> fields, boolean raw) {
|
||||||
Workbook workbook = new XSSFWorkbook();
|
Workbook workbook = new XSSFWorkbook();
|
||||||
|
|
||||||
Sheet sheet = workbook.createSheet("SchemaName");
|
Sheet sheet = workbook.createSheet(sheetName);
|
||||||
|
|
||||||
int rowIndex = 0;
|
int rowIndex = 0;
|
||||||
for (DbObject item : items) {
|
for (DbObject item : items) {
|
||||||
@@ -102,6 +106,20 @@ public class DataExportController {
|
|||||||
int cellIndex = 0;
|
int cellIndex = 0;
|
||||||
for (DbField field : fields) {
|
for (DbField field : fields) {
|
||||||
Cell cell = row.createCell(cellIndex++);
|
Cell cell = row.createCell(cellIndex++);
|
||||||
|
|
||||||
|
if (raw) {
|
||||||
|
if (field.isForeignKey()) {
|
||||||
|
String cellValue = "";
|
||||||
|
DbObject traverse = item.traverse(field);
|
||||||
|
if (traverse != null) cellValue = traverse.getPrimaryKeyValue().toString();
|
||||||
|
cell.setCellValue(cellValue);
|
||||||
|
} else {
|
||||||
|
String cellValue = "";
|
||||||
|
DbFieldValue fieldValue = item.get(field);
|
||||||
|
if (fieldValue.getValue() != null) cellValue = fieldValue.getValue().toString();
|
||||||
|
cell.setCellValue(cellValue);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if (field.isForeignKey()) {
|
if (field.isForeignKey()) {
|
||||||
DbObject linkedItem = item.traverse(field);
|
DbObject linkedItem = item.traverse(field);
|
||||||
cell.setCellValue(linkedItem.getPrimaryKeyValue() + " (" + linkedItem.getDisplayName() + ")");
|
cell.setCellValue(linkedItem.getPrimaryKeyValue() + " (" + linkedItem.getDisplayName() + ")");
|
||||||
@@ -110,18 +128,22 @@ public class DataExportController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// lets write the excel data to file now
|
// lets write the excel data to file now
|
||||||
ByteArrayOutputStream fos = new ByteArrayOutputStream();
|
ByteArrayOutputStream fos = new ByteArrayOutputStream();
|
||||||
try {
|
try {
|
||||||
workbook.write(fos);
|
workbook.write(fos);
|
||||||
fos.close();
|
fos.close();
|
||||||
|
workbook.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new DbAdminException("Error writing XLSX file");
|
throw new DbAdminException("Error writing XLSX file");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return fos.toByteArray();
|
return fos.toByteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String toCsv(List<DbObject> items, List<DbField> fields) {
|
private String toCsv(List<DbObject> items, List<DbField> fields, boolean raw) {
|
||||||
if (items.isEmpty())
|
if (items.isEmpty())
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
@@ -134,12 +156,25 @@ public class DataExportController {
|
|||||||
try (final CSVPrinter printer = new CSVPrinter(sw, csvFormat)) {
|
try (final CSVPrinter printer = new CSVPrinter(sw, csvFormat)) {
|
||||||
for (DbObject item : items) {
|
for (DbObject item : items) {
|
||||||
printer.printRecord(fields.stream().map(f -> {
|
printer.printRecord(fields.stream().map(f -> {
|
||||||
|
if (raw) {
|
||||||
|
if (f.isForeignKey()) {
|
||||||
|
DbObject traverse = item.traverse(f);
|
||||||
|
if (traverse == null) return "";
|
||||||
|
else return traverse.getPrimaryKeyValue().toString();
|
||||||
|
} else {
|
||||||
|
DbFieldValue fieldValue = item.get(f);
|
||||||
|
if (fieldValue.getValue() == null) return "";
|
||||||
|
else return fieldValue.getValue().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
if (f.isForeignKey()) {
|
if (f.isForeignKey()) {
|
||||||
DbObject linkedItem = item.traverse(f);
|
DbObject linkedItem = item.traverse(f);
|
||||||
return linkedItem.getPrimaryKeyValue() + " (" + linkedItem.getDisplayName() + ")";
|
return linkedItem.getPrimaryKeyValue() + " (" + linkedItem.getDisplayName() + ")";
|
||||||
} else {
|
} else {
|
||||||
return item.get(f).getFormattedValue();
|
return item.get(f).getFormattedValue();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -57,7 +57,14 @@
|
|||||||
<option value="csv">CSV</option>
|
<option value="csv">CSV</option>
|
||||||
<option value="xlsx">XLSX</option>
|
<option value="xlsx">XLSX</option>
|
||||||
</select>
|
</select>
|
||||||
|
<div class="form-check mt-3">
|
||||||
|
<input class="form-check-input" type="checkbox"
|
||||||
|
id="__check_raw"
|
||||||
|
th:name="raw">
|
||||||
|
<label class="form-check-label" for="__check_raw">
|
||||||
|
Export raw values
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="modal-footer">
|
<div class="modal-footer">
|
||||||
|
Reference in New Issue
Block a user