Skip to content

Commit

Permalink
[backend] Converting return lines to HTML tags when importing rich te…
Browse files Browse the repository at this point in the history
…xt fields (#2317)
  • Loading branch information
Dimfacion authored Jan 31, 2025
1 parent 5db2059 commit 0deef84
Show file tree
Hide file tree
Showing 4 changed files with 467 additions and 90 deletions.
101 changes: 13 additions & 88 deletions openbas-api/src/main/java/io/openbas/service/InjectImportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.openbas.rest.scenario.response.ImportMessage;
import io.openbas.rest.scenario.response.ImportPostSummary;
import io.openbas.rest.scenario.response.ImportTestSummary;
import io.openbas.service.utils.InjectImportUtils;
import io.openbas.utils.InjectUtils;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -39,7 +40,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.logging.log4j.util.Strings;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
Expand Down Expand Up @@ -492,7 +492,9 @@ private ImportRow importRow(
Matcher matcher =
mapPatternByInjectImport
.get(injectImporter.getId())
.matcher(getValueAsString(row, importMapper.getInjectTypeColumn()));
.matcher(
InjectImportUtils.getValueAsString(
row, importMapper.getInjectTypeColumn()));
return matcher.find();
})
.toList();
Expand Down Expand Up @@ -565,7 +567,7 @@ private ImportRow importRow(
if (triggerTimeRuleAttribute.getColumns() != null) {
dateAsString =
Arrays.stream(triggerTimeRuleAttribute.getColumns().split("\\+"))
.map(column -> getDateAsStringFromCell(row, column, timePattern))
.map(column -> InjectImportUtils.getDateAsStringFromCell(row, column, timePattern))
.collect(Collectors.joining());
}
if (dateAsString.isBlank()) {
Expand All @@ -584,7 +586,7 @@ private ImportRow importRow(
injectTime.setUnformattedDate(dateAsString);
injectTime.setLinkedInject(inject);

Temporal dateTime = getInjectDate(injectTime, timePattern);
Temporal dateTime = InjectImportUtils.getInjectDate(injectTime, timePattern);

if (dateTime == null) {
injectTime.setRelativeDay(relativeDays);
Expand Down Expand Up @@ -758,7 +760,7 @@ private void setAttributeValue(
if (valueCell == null) {
setter.accept(ruleAttribute.getDefaultValue());
} else {
String cellValue = getValueAsString(row, ruleAttribute.getColumns());
String cellValue = InjectImportUtils.getValueAsString(row, ruleAttribute.getColumns());
setter.accept(cellValue.isBlank() ? ruleAttribute.getDefaultValue() : cellValue);
}
}
Expand Down Expand Up @@ -806,9 +808,8 @@ private List<ImportMessage> addFields(
String columnValue = Strings.EMPTY;
if (ruleAttribute.getColumns() != null) {
columnValue =
Arrays.stream(ruleAttribute.getColumns().split("\\+"))
.map(column -> getValueAsString(row, column))
.collect(Collectors.joining());
InjectImportUtils.extractAndConvertStringColumnValue(
row, ruleAttribute, mapFieldByKey);
}
if (columnValue.isBlank()) {
inject.getContent().put(ruleAttribute.getDefaultValue(), columnValue);
Expand All @@ -830,7 +831,7 @@ private List<ImportMessage> addFields(
columnValues =
Arrays.stream(
Arrays.stream(ruleAttribute.getColumns().split("\\+"))
.map(column -> getValueAsString(row, column))
.map(column -> InjectImportUtils.getValueAsString(row, column))
.collect(Collectors.joining(","))
.split(","))
.toList();
Expand Down Expand Up @@ -908,7 +909,7 @@ private List<ImportMessage> addFields(
== CellType.NUMERIC)) {
Double columnValueExpectation =
columns.stream()
.map(column -> getValueAsDouble(row, column))
.map(column -> InjectImportUtils.getValueAsDouble(row, column))
.reduce(0.0, Double::sum);
expectation.get().setExpectedScore(columnValueExpectation.doubleValue());
} else {
Expand Down Expand Up @@ -939,7 +940,7 @@ private List<ImportMessage> addFields(
if (ruleAttribute.getColumns() != null) {
String columnValueExpectation =
Arrays.stream(ruleAttribute.getColumns().split("\\+"))
.map(column -> getValueAsString(row, column))
.map(column -> InjectImportUtils.getValueAsString(row, column))
.collect(Collectors.joining());
expectation
.get()
Expand All @@ -954,7 +955,7 @@ private List<ImportMessage> addFields(
if (ruleAttribute.getColumns() != null) {
String columnValueExpectation =
Arrays.stream(ruleAttribute.getColumns().split("\\+"))
.map(column -> getValueAsString(row, column))
.map(column -> InjectImportUtils.getValueAsString(row, column))
.collect(Collectors.joining());
expectation
.get()
Expand All @@ -975,36 +976,6 @@ private List<ImportMessage> addFields(
return emptyList();
}

private Temporal getInjectDate(InjectTime injectTime, String timePattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
if (timePattern != null && !timePattern.isEmpty()) {
dateTimeFormatter = DateTimeFormatter.ofPattern(timePattern);
try {
return LocalDateTime.parse(injectTime.getUnformattedDate(), dateTimeFormatter);
} catch (DateTimeParseException firstException) {
try {
return LocalTime.parse(injectTime.getUnformattedDate(), dateTimeFormatter);
} catch (DateTimeParseException exception) {
// This is a "probably" a relative date
}
}
} else {
try {
return LocalDateTime.parse(injectTime.getUnformattedDate(), dateTimeFormatter);
} catch (DateTimeParseException firstException) {
// The date is not in ISO_DATE_TIME. Trying just the ISO_TIME
dateTimeFormatter = DateTimeFormatter.ISO_TIME;
try {
return LocalDateTime.parse(injectTime.getUnformattedDate(), dateTimeFormatter);
} catch (DateTimeParseException secondException) {
// Neither ISO_DATE_TIME nor ISO_TIME
}
}
}
injectTime.setFormatter(dateTimeFormatter);
return null;
}

private List<ImportMessage> updateInjectDates(Map<Integer, InjectTime> mapInstantByRowIndex) {
List<ImportMessage> importMessages = new ArrayList<>();
// First of all, are there any absolute date
Expand Down Expand Up @@ -1195,50 +1166,4 @@ private void processDateToAbsolute(Map<Integer, InjectTime> mapInstantByRowIndex
- earliestInstant.getEpochSecond());
}));
}

private String getDateAsStringFromCell(Row row, String cellColumn, String timePattern) {
if (cellColumn != null
&& !cellColumn.isBlank()
&& row.getCell(CellReference.convertColStringToIndex(cellColumn)) != null) {
Cell cell = row.getCell(CellReference.convertColStringToIndex(cellColumn));
if (cell.getCellType() == CellType.STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == CellType.NUMERIC) {
if (timePattern == null || timePattern.isEmpty()) {
return cell.getDateCellValue().toString();
} else {
return DateFormatUtils.format(cell.getDateCellValue(), timePattern);
}
}
}
return "";
}

private String getValueAsString(Row row, String cellColumn) {
if (cellColumn != null
&& !cellColumn.isBlank()
&& row.getCell(CellReference.convertColStringToIndex(cellColumn)) != null) {
Cell cell = row.getCell(CellReference.convertColStringToIndex(cellColumn));
if (cell.getCellType() == CellType.STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == CellType.NUMERIC) {
return Double.valueOf(cell.getNumericCellValue()).toString();
}
}
return "";
}

private Double getValueAsDouble(Row row, String cellColumn) {
if (cellColumn != null
&& !cellColumn.isBlank()
&& row.getCell(CellReference.convertColStringToIndex(cellColumn)) != null) {
Cell cell = row.getCell(CellReference.convertColStringToIndex(cellColumn));
if (cell.getCellType() == CellType.STRING) {
return Double.valueOf(cell.getStringCellValue());
} else if (cell.getCellType() == CellType.NUMERIC) {
return cell.getNumericCellValue();
}
}
return 0.0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package io.openbas.service.utils;

import com.fasterxml.jackson.databind.JsonNode;
import io.openbas.database.model.RuleAttribute;
import io.openbas.service.InjectTime;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.Temporal;
import java.util.*;
import java.util.stream.Collectors;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellReference;

public class InjectImportUtils {

/**
* Returns the date as string from an excel cell
*
* @param row the row
* @param cellColumn the column
* @param timePattern the pattern to use to convert the date
* @return the date as string
*/
public static String getDateAsStringFromCell(Row row, String cellColumn, String timePattern) {
if (cellColumn != null
&& !cellColumn.isBlank()
&& row.getCell(CellReference.convertColStringToIndex(cellColumn)) != null) {
Cell cell = row.getCell(CellReference.convertColStringToIndex(cellColumn));
if (cell.getCellType() == CellType.STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == CellType.NUMERIC) {
if (timePattern == null || timePattern.isEmpty()) {
return cell.getDateCellValue().toString();
} else {
return DateFormatUtils.format(cell.getDateCellValue(), timePattern);
}
}
}
return "";
}

/**
* Get the value of a cell as a string
*
* @param row the row
* @param cellColumn the column
* @return the value of the cell as string
*/
public static String getValueAsString(Row row, String cellColumn) {
if (cellColumn != null
&& !cellColumn.isBlank()
&& row.getCell(CellReference.convertColStringToIndex(cellColumn)) != null) {
Cell cell = row.getCell(CellReference.convertColStringToIndex(cellColumn));
if (cell.getCellType() == CellType.STRING) {
return cell.getStringCellValue();
} else if (cell.getCellType() == CellType.NUMERIC) {
return Double.valueOf(cell.getNumericCellValue()).toString();
}
}
return "";
}

/**
* Get the value of a cell as a double
*
* @param row the row
* @param cellColumn the column
* @return the value of the cell as a double
*/
public static Double getValueAsDouble(Row row, String cellColumn) {
if (cellColumn != null
&& !cellColumn.isBlank()
&& row.getCell(CellReference.convertColStringToIndex(cellColumn)) != null) {
Cell cell = row.getCell(CellReference.convertColStringToIndex(cellColumn));
if (cell.getCellType() == CellType.STRING) {
return Double.valueOf(cell.getStringCellValue());
} else if (cell.getCellType() == CellType.NUMERIC) {
return cell.getNumericCellValue();
}
}
return 0.0;
}

/**
* Extract the value as string and convert it to HTML if need be
*
* @param row the row
* @param ruleAttribute the rule to use to extract the value
* @param mapFieldByKey the map of the fields organized by the name of the field
* @return the value as string
*/
public static String extractAndConvertStringColumnValue(
Row row, RuleAttribute ruleAttribute, Map<String, JsonNode> mapFieldByKey) {
String columnValue =
Arrays.stream(ruleAttribute.getColumns().split("\\+"))
.map(column -> getValueAsString(row, column))
.collect(Collectors.joining());

// Given that richText fields are editable using CKEditor which expects HTML,
// we're converting return line into <br>.
// TODO : convert properly the whole cell into HTML including formatting (bold, ...)
if (mapFieldByKey.get(ruleAttribute.getName()).get("richText") != null
&& mapFieldByKey.get(ruleAttribute.getName()).get("richText").asBoolean()) {
columnValue = columnValue.replaceAll("\n", "<br/>");
}
return columnValue;
}

/**
* Returns a date out of an InjectTime object with a time pattern
*
* @param injectTime the object representing a date in the cells
* @param timePattern a pattern to use to find out what value it is
* @return the date
*/
public static Temporal getInjectDate(InjectTime injectTime, String timePattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
if (timePattern != null && !timePattern.isEmpty()) {
dateTimeFormatter = DateTimeFormatter.ofPattern(timePattern);
try {
return LocalDateTime.parse(injectTime.getUnformattedDate(), dateTimeFormatter);
} catch (DateTimeParseException firstException) {
try {
return LocalTime.parse(injectTime.getUnformattedDate(), dateTimeFormatter);
} catch (DateTimeParseException exception) {
// This is a "probably" a relative date
}
}
} else {
try {
return LocalDateTime.parse(injectTime.getUnformattedDate(), dateTimeFormatter);
} catch (DateTimeParseException firstException) {
// The date is not in ISO_DATE_TIME. Trying just the ISO_TIME
dateTimeFormatter = DateTimeFormatter.ISO_TIME;
try {
return LocalTime.parse(injectTime.getUnformattedDate(), dateTimeFormatter);
} catch (DateTimeParseException secondException) {
// Neither ISO_DATE_TIME nor ISO_TIME
}
}
}
injectTime.setFormatter(dateTimeFormatter);
return null;
}
}
Loading

0 comments on commit 0deef84

Please sign in to comment.