From ec2ae198354ce33ae682481ad8678387a7f7541e Mon Sep 17 00:00:00 2001 From: Alwin Egger Date: Sun, 3 Oct 2021 16:53:31 +0200 Subject: [PATCH 1/2] Export different types in multiple sheets --- .../ch/openolitor/core/RouteService.scala | 97 +++++++++++-------- 1 file changed, 55 insertions(+), 42 deletions(-) diff --git a/src/main/scala/ch/openolitor/core/RouteService.scala b/src/main/scala/ch/openolitor/core/RouteService.scala index ff7865131..b33578d90 100644 --- a/src/main/scala/ch/openolitor/core/RouteService.scala +++ b/src/main/scala/ch/openolitor/core/RouteService.scala @@ -327,6 +327,12 @@ trait DefaultRouteService extends HttpService with ActorReferences with BaseJson val sheet = dataDocument.getSheetByIndex(0) sheet.setCellStyleInheritance(false) + def createNewSheet(name: String): Table = { + val newSheet = dataDocument.appendSheet(name) + newSheet.setCellStyleInheritance(false) + newSheet + } + def writeToRow(row: Row, element: Any, cellIndex: Int): Unit = { element match { case null => @@ -343,62 +349,69 @@ trait DefaultRouteService extends HttpService with ActorReferences with BaseJson } result match { - case genericList: List[Any] => + case genericList: List[Any] => { if (genericList.nonEmpty) { - genericList.head match { - case firstMapEntry: Map[_, _] => - val listOfMaps = genericList.asInstanceOf[List[Map[String, Any]]] - val row = sheet.getRowByIndex(0); - - listOfMaps.head.zipWithIndex foreach { - case ((fieldName, value), index) => - row.getCellByIndex(index).setStringValue(fieldName) - val font = row.getCellByIndex(index).getFont - font.setFontStyle(StyleTypeDefinitions.FontStyle.BOLD) - font.setSize(10) - row.getCellByIndex(index).setFont(font) - } + genericList.groupBy(_.getClass) map { + case (clazz, clazzSortedList) => { + var clazzSheet = createNewSheet(clazz.getName()) + clazzSortedList.head match { + case firstMapEntry: Map[_, _] => + val listOfMaps = clazzSortedList.asInstanceOf[List[Map[String, Any]]] + val row = clazzSheet.getRowByIndex(0); + + listOfMaps.head.zipWithIndex foreach { + case ((fieldName, value), index) => + row.getCellByIndex(index).setStringValue(fieldName) + val font = row.getCellByIndex(index).getFont + font.setFontStyle(StyleTypeDefinitions.FontStyle.BOLD) + font.setSize(10) + row.getCellByIndex(index).setFont(font) + } - listOfMaps.zipWithIndex foreach { - case (entry, index) => - val row = sheet.getRowByIndex(index + 1); + listOfMaps.zipWithIndex foreach { + case (entry, index) => + val row = clazzSheet.getRowByIndex(index + 1); - entry.zipWithIndex foreach { - case ((fieldName, value), colIndex) => - fieldName match { - case "passwort" => writeToRow(row, "Not available", colIndex) - case _ => writeToRow(row, value, colIndex) + entry.zipWithIndex foreach { + case ((fieldName, value), colIndex) => + fieldName match { + case "passwort" => writeToRow(row, "Not available", colIndex) + case _ => writeToRow(row, value, colIndex) + } } } - } - case firstProductEntry: Product => - val listOfProducts = genericList.asInstanceOf[List[Product]] - val row = sheet.getRowByIndex(0); + case firstProductEntry: Product => + val listOfProducts = clazzSortedList.asInstanceOf[List[Product]] + val row = clazzSheet.getRowByIndex(0); - def getCCParams(cc: Product) = cc.getClass.getDeclaredFields.map(_.getName) // all field names - .zip(cc.productIterator.to).toMap // zipped with all values + def getCCParams(cc: Product) = cc.getClass.getDeclaredFields.map(_.getName) // all field names + .zip(cc.productIterator.to).toMap // zipped with all values - getCCParams(listOfProducts.head).zipWithIndex foreach { - case ((fieldName, value), index) => - row.getCellByIndex(index).setStringValue(fieldName) - val font = row.getCellByIndex(index).getFont - font.setFontStyle(StyleTypeDefinitions.FontStyle.BOLD) - font.setSize(10) - row.getCellByIndex(index).setFont(font) - } + getCCParams(listOfProducts.head).zipWithIndex foreach { + case ((fieldName, value), index) => + row.getCellByIndex(index).setStringValue(fieldName) + val font = row.getCellByIndex(index).getFont + font.setFontStyle(StyleTypeDefinitions.FontStyle.BOLD) + font.setSize(10) + row.getCellByIndex(index).setFont(font) + } - listOfProducts.zipWithIndex foreach { - case (entry, index) => - val row = sheet.getRowByIndex(index + 1); + listOfProducts.zipWithIndex foreach { + case (entry, index) => + val row = clazzSheet.getRowByIndex(index + 1); - getCCParams(entry).zipWithIndex foreach { - case ((fieldName, value), colIndex) => - writeToRow(row, value, colIndex) + getCCParams(entry).zipWithIndex foreach { + case ((fieldName, value), colIndex) => + writeToRow(row, value, colIndex) + } } } + } } } + dataDocument.removeSheet(0) + } case x => sheet.getRowByIndex(0).getCellByIndex(0).setStringValue("Data of type" + x.toString + " could not be transfered to ODS file.") } From f071d30270534716dcd210e02b63b144475a8655 Mon Sep 17 00:00:00 2001 From: Alwin Egger Date: Sun, 3 Oct 2021 18:51:20 +0200 Subject: [PATCH 2/2] Use SimpleName --- src/main/scala/ch/openolitor/core/RouteService.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/ch/openolitor/core/RouteService.scala b/src/main/scala/ch/openolitor/core/RouteService.scala index b33578d90..23d460b7f 100644 --- a/src/main/scala/ch/openolitor/core/RouteService.scala +++ b/src/main/scala/ch/openolitor/core/RouteService.scala @@ -353,7 +353,7 @@ trait DefaultRouteService extends HttpService with ActorReferences with BaseJson if (genericList.nonEmpty) { genericList.groupBy(_.getClass) map { case (clazz, clazzSortedList) => { - var clazzSheet = createNewSheet(clazz.getName()) + var clazzSheet = createNewSheet(clazz.getSimpleName()) clazzSortedList.head match { case firstMapEntry: Map[_, _] => val listOfMaps = clazzSortedList.asInstanceOf[List[Map[String, Any]]]