Skip to content

Commit

Permalink
Export DataObject and BusinessObject in #145
Browse files Browse the repository at this point in the history
  • Loading branch information
mauvaisetroupe committed Nov 25, 2023
1 parent 33f4373 commit edddd16
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.mauvaisetroupe.eadesignit.service.importfile;

import com.mauvaisetroupe.eadesignit.domain.BusinessObject;
import com.mauvaisetroupe.eadesignit.domain.DataObject;
import com.mauvaisetroupe.eadesignit.repository.BusinessObjectRepository;
import com.mauvaisetroupe.eadesignit.repository.DataObjectRepository;
import com.mauvaisetroupe.eadesignit.service.importfile.util.ExcelUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BusinessAndDataObjectExportService {

@Autowired
private DataObjectRepository dataObjectRepository;

@Autowired
private BusinessObjectRepository businessObjectRepository;

public ByteArrayOutputStream getBusinessAndDataObjects() throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet appliSheet = workbook.createSheet(DataObjectImportService.DATA_OBJECT_SHEET_NAME);

writeSheets(appliSheet, workbook);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
workbook.write(stream);
workbook.close();
return stream;
}

protected void writeSheets(Sheet dataobjectSheet, Workbook workbook) {
writeDatObjects(dataobjectSheet);
ExcelUtils.autoSizeAllColumns(dataobjectSheet);
ExcelUtils.addHeaderColorAndFilte(workbook, dataobjectSheet);
}

protected void writeDatObjects(Sheet sheet) {
int column = 0;
int rownb = 0;

Row headerRow = sheet.createRow(rownb++);
headerRow.createCell(column++).setCellValue(DataObjectImportService.BUSINESS_OBJECT_GENERALIZATION);
headerRow.createCell(column++).setCellValue(DataObjectImportService.BUSINESS_OBJECT);
headerRow.createCell(column++).setCellValue(DataObjectImportService.DATA_OBJECT);
headerRow.createCell(column++).setCellValue(DataObjectImportService.DATA_OBJECT_TYPE);
headerRow.createCell(column++).setCellValue(DataObjectImportService.DATA_OBJECT_APPLICATION);

List<DataObject> dataObjects = dataObjectRepository.findAllWithAllChildrens();
for (DataObject datObject : dataObjects) {
column = 0;
Row row = sheet.createRow(rownb++);
BusinessObject businessObject = datObject.getBusinessObject();
if (businessObject != null) {
BusinessObject generalization = businessObject.getGeneralization();
if (generalization != null) {
row.createCell(column).setCellValue(generalization.getName());
}
column++;
row.createCell(column).setCellValue(getFullPath(businessObject));
column++;
} else {
column = column + 2;
}
row.createCell(column++).setCellValue(getFullPath(datObject));
if (datObject.getType() != null) {
row.createCell(column).setCellValue(datObject.getType().toString());
}
column++;
if (datObject.getApplication() != null) {
row.createCell(column++).setCellValue(datObject.getApplication().getName());
}
column++;
}

List<BusinessObject> orphanBusinessObjects = businessObjectRepository
.findAll()
.stream()
.filter(bo -> (bo.getDataObjects() == null || bo.getDataObjects().isEmpty()))
.toList();

for (BusinessObject businessObject : orphanBusinessObjects) {
Row row = sheet.createRow(rownb++);
column = 0;
BusinessObject generalization = businessObject.getGeneralization();
if (generalization != null) {
row.createCell(column).setCellValue(generalization.getName());
}
column++;
row.createCell(column).setCellValue(getFullPath(businessObject));
column++;
}
}

private String getFullPath(BusinessObject businessObject) {
StringBuilder builder = new StringBuilder();
BusinessObject tmp = businessObject;
builder.append(businessObject.getName());
while (tmp.getParent() != null) {
builder.insert(0, " > ");
builder.insert(0, tmp.getParent().getName());
tmp = tmp.getParent();
}
return builder.toString();
}

private String getFullPath(DataObject businessObject) {
StringBuilder builder = new StringBuilder();
DataObject tmp = businessObject;
builder.append(businessObject.getName());
while (tmp.getParent() != null) {
builder.insert(0, " > ");
builder.insert(0, tmp.getParent().getName());
tmp = tmp.getParent();
}
return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,23 @@ public DataObjectDTO importExcel(InputStream excel) throws IOException {
}

// Create Business Objects from full path
String[] bos = dto.getBusinessobject().split("\\w*>\\w*");
BusinessObject bo = null;
BusinessObject parent = null;
for (int i = 0; i < bos.length; i++) {
String boName = bos[i].trim();
bo = findOrCreateBO(parent, boName);
parent = bo;
}
bo.setAbstractBusinessObject(dto.isAbstractValue());
businessObjectRepository.save(bo);

// Create Business Object from Generalization
if (StringUtils.hasText(dto.getGeneralization())) {
BusinessObject generalization = findOrCreateBO(null, dto.getGeneralization());
bo.setGeneralization(generalization);
if (dto.getBusinessobject() != null) {
String[] bos = dto.getBusinessobject().split("\\w*>\\w*");
BusinessObject parent = null;
for (int i = 0; i < bos.length; i++) {
String boName = bos[i].trim();
bo = findOrCreateBO(parent, boName);
parent = bo;
}
bo.setAbstractBusinessObject(dto.isAbstractValue());
businessObjectRepository.save(bo);
// Create Business Object from Generalization
if (StringUtils.hasText(dto.getGeneralization())) {
BusinessObject generalization = findOrCreateBO(null, dto.getGeneralization());
bo.setGeneralization(generalization);
businessObjectRepository.save(bo);
}
}

// Create Data Object from fullpath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class ExportFullDataService {
@Autowired
FunctionalFlowRepository flowRepository;

@Autowired
BusinessAndDataObjectExportService businessAndDataObjectExportService;

public ByteArrayOutputStream getallData(
boolean exportApplications,
boolean exportComponents,
Expand All @@ -58,7 +61,8 @@ public ByteArrayOutputStream getallData(
List<Long> landscapesToExport,
List<Long> capabilitiesMappingToExport,
boolean exportCapabilitiesWithNoLandscape,
boolean functionalFlowsWhithNoLandscape
boolean functionalFlowsWhithNoLandscape,
boolean businessAndDataObjects
) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet summarySheet = workbook.createSheet(SummaryImporterService.SUMMARY_SHEET);
Expand All @@ -67,6 +71,7 @@ public ByteArrayOutputStream getallData(
Sheet ownerSheet = workbook.createSheet(ApplicationImportService.OWNER_SHEET_NAME);
Sheet externalSystemSheet = workbook.createSheet(ExternalSystemImportService.SHEET_NAME);
Sheet capabilitiesSheet = workbook.createSheet(CapabilityImportService.CAPABILITY_SHEET_NAME);
Sheet businessObjectsSheet = workbook.createSheet(DataObjectImportService.DATA_OBJECT_SHEET_NAME);

int lineNb = 0;
int nbcolumn = 0;
Expand Down Expand Up @@ -156,6 +161,17 @@ public ByteArrayOutputStream getallData(
);
addCapabilitieMappingsSummary(workbook, summarySheet, capabilityMappingDTOs, lineNb);

// Business and Data Objects
if (businessAndDataObjects) {
addBusinessAndDataObjectsSummary(workbook, summarySheet, businessObjectsSheet.getSheetName(), lineNb);
businessAndDataObjectExportService.writeDatObjects(businessObjectsSheet);
ExcelUtils.autoSizeAllColumns(businessObjectsSheet);
ExcelUtils.addHeaderColorAndFilte(workbook, businessObjectsSheet);
// find business bobjects with no data object
//businessObjectRepository.findAllWithAllChildrens();

}

// Close stream
ExcelUtils.autoSizeAllColumns(summarySheet);
ExcelUtils.addHeaderColorAndFilte(workbook, summarySheet);
Expand Down Expand Up @@ -247,6 +263,15 @@ private void addCapabilitieMappingsSummary(
}
}

private void addBusinessAndDataObjectsSummary(Workbook workbook, Sheet summarySheet, String businessSheetName, int lineNb) {
Row row = summarySheet.createRow(lineNb);
int columnNb = 0;
row.createCell(columnNb++).setCellValue("Business and Data Objects");
// Link to shet
Cell cell = row.createCell(columnNb++);
createHyperlink(workbook, businessSheetName, cell);
}

private void createHyperlink(Workbook workbook, String sheetName, Cell cell) {
cell.setCellValue(sheetName);
CreationHelper helper = workbook.getCreationHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ public ResponseEntity<Resource> downloadAllData(
@RequestParam(value = "landscapes[]", required = false, defaultValue = "") List<Long> landscapes,
@RequestParam(value = "capabilitiesMapping[]", required = false, defaultValue = "") List<Long> capabilitiesMapping,
@RequestParam boolean capabilitiesMappingWithNoLandscape,
@RequestParam boolean functionalFlowsWhithNoLandscape
@RequestParam boolean functionalFlowsWhithNoLandscape,
@RequestParam(defaultValue = "true") boolean businessAndDataObjects
) throws IOException {
ByteArrayOutputStream file = exportFullDataService.getallData(
applications,
Expand All @@ -245,7 +246,8 @@ public ResponseEntity<Resource> downloadAllData(
landscapes,
capabilitiesMapping,
capabilitiesMappingWithNoLandscape,
functionalFlowsWhithNoLandscape
functionalFlowsWhithNoLandscape,
businessAndDataObjects
);
ByteArrayResource byteArrayResource = new ByteArrayResource(file.toByteArray());

Expand Down

0 comments on commit edddd16

Please sign in to comment.