diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/domain/CodeDomain.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/domain/CodeDomain.kt index ed97f9c7b..d38f7a359 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/domain/CodeDomain.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/domain/CodeDomain.kt @@ -33,12 +33,15 @@ import org.kopi.galite.visual.dsl.chart.ChartMeasure import org.kopi.galite.visual.dsl.common.CodeDescription import org.kopi.galite.visual.dsl.common.LocalizationWriter import org.kopi.galite.visual.dsl.form.FormField +import org.kopi.galite.visual.dsl.pivottable.Dimension +import org.kopi.galite.visual.dsl.pivottable.PivotTableField import org.kopi.galite.visual.dsl.report.ReportField import org.kopi.galite.visual.form.VBooleanCodeField import org.kopi.galite.visual.form.VField import org.kopi.galite.visual.form.VDecimalCodeField import org.kopi.galite.visual.form.VIntegerCodeField import org.kopi.galite.visual.form.VStringCodeField +import org.kopi.galite.visual.pivottable.VPivotTableColumn import org.kopi.galite.visual.report.VBooleanCodeColumn import org.kopi.galite.visual.report.VCalculateColumn import org.kopi.galite.visual.report.VCellFormat @@ -224,6 +227,54 @@ open class CodeDomain?> : Domain() { } } + /** + * Builds the pivot table column model + */ + override fun buildPivotTableFieldModel( + field: PivotTableField<*>, + position: Dimension.Position?, + ): VPivotTableColumn { + return with(field) { + when (kClass) { + Boolean::class -> org.kopi.galite.visual.pivottable.VBooleanCodeColumn( + ident, + position, + this@CodeDomain.ident.ifEmpty { ident }, + field.source, + codes.map { it.ident }.toTypedArray(), + codes.map { it.value as Boolean }.toBooleanArray() + ) + BigDecimal::class -> org.kopi.galite.visual.pivottable.VDecimalCodeColumn( + ident, + position, + this@CodeDomain.ident.ifEmpty { ident }, + field.source, + codes.map { it.ident }.toTypedArray(), + codes.map { it.value as? BigDecimal }.toTypedArray() + ) + Int::class, Long::class -> org.kopi.galite.visual.pivottable.VIntegerCodeColumn( + ident, + position, + this@CodeDomain.ident.ifEmpty { ident }, + field.source!!, + codes.map { it.ident }.toTypedArray(), + codes.map { it.value as Int }.toIntArray() + ) + String::class -> org.kopi.galite.visual.pivottable.VStringCodeColumn( + ident, + position, + this@CodeDomain.ident.ifEmpty { ident }, + field.source, + codes.map { it.ident }.toTypedArray(), + codes.map { it.value as? String }.toTypedArray() + ) + else -> throw RuntimeException("Type ${kClass!!.qualifiedName} is not supported") + }.also { + it.initLabels(codes.map { it.label }.toTypedArray()) + } + } + } + /** * Sets a mapping between the values that the domain can take * and a corresponding text to be displayed in a field. diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/domain/Domain.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/domain/Domain.kt index c12200c2e..ea63a7415 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/domain/Domain.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/domain/Domain.kt @@ -36,12 +36,13 @@ import org.kopi.galite.visual.dsl.chart.ChartDimension import org.kopi.galite.visual.dsl.chart.ChartMeasure import org.kopi.galite.visual.dsl.common.LocalizationWriter import org.kopi.galite.visual.dsl.form.FormField -import org.kopi.galite.visual.dsl.report.ReportField -import org.kopi.galite.visual.form.* -import org.kopi.galite.visual.report.* import org.kopi.galite.visual.dsl.pivottable.Dimension import org.kopi.galite.visual.dsl.pivottable.PivotTableField +import org.kopi.galite.visual.dsl.report.ReportField +import org.kopi.galite.visual.form.* import org.kopi.galite.visual.pivottable.VPivotTableColumn +import org.kopi.galite.visual.report.* + /** * A domain is a data type with predefined list of allowed values. @@ -182,8 +183,8 @@ open class Domain(val width: Int? = null, BigDecimal::class -> VDecimalDimension(ident, format, height ?: 6, true) String::class -> VStringDimension(ident, format) Boolean::class -> VBooleanDimension(ident, format) - org.joda.time.LocalDate::class, LocalDate::class, java.sql.Date::class, java.util.Date::class -> - VDateDimension(ident, format) + org.joda.time.LocalDate::class, LocalDate::class, java.sql.Date::class, java.util.Date::class + -> VDateDimension(ident, format) Month::class -> VMonthDimension(ident, format) Week::class -> VWeekDimension(ident, format) org.joda.time.LocalTime::class, LocalTime::class -> VTimeDimension(ident, format) @@ -247,11 +248,24 @@ open class Domain(val width: Int? = null, open fun buildPivotTableFieldModel(field: PivotTableField<*>, position: Dimension.Position?): VPivotTableColumn { return with(field) { when (kClass) { - Int::class, Long::class, String::class, BigDecimal::class, Boolean::class, org.joda.time.LocalDate::class, - LocalDate::class, java.sql.Date::class, java.util.Date::class, Month::class, Week::class, org.joda.time.LocalTime::class, - LocalTime::class, Instant::class, LocalDateTime::class, DateTime::class -> - VPivotTableColumn(ident, position) - + Int::class, Long::class-> + org.kopi.galite.visual.pivottable.VIntegerColumn(ident, position) + String::class-> + org.kopi.galite.visual.pivottable.VStringColumn(ident, position) + BigDecimal::class-> + org.kopi.galite.visual.pivottable.VDecimalColumn(ident, position) + Boolean::class -> + org.kopi.galite.visual.pivottable.VBooleanColumn(ident, position) + org.joda.time.LocalDate::class, LocalDate::class, java.sql.Date::class, java.util.Date::class -> + org.kopi.galite.visual.pivottable.VDateColumn(ident, position) + Month::class -> + org.kopi.galite.visual.pivottable.VMonthColumn(ident, position) + Week::class -> + org.kopi.galite.visual.pivottable.VWeekColumn(ident, position) + org.joda.time.LocalTime::class, LocalTime::class -> + org.kopi.galite.visual.pivottable.VTimeColumn(ident, position) + Instant::class, LocalDateTime::class, DateTime::class -> + org.kopi.galite.visual.pivottable.VTimestampColumn(ident, position) else -> throw java.lang.RuntimeException("Type ${kClass!!.qualifiedName} is not supported") } } diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/MPivotTable.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/MPivotTable.kt index 6b4393173..4bde74bb1 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/MPivotTable.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/MPivotTable.kt @@ -21,7 +21,6 @@ package org.kopi.galite.visual.pivottable import java.io.Serializable import org.kopi.galite.visual.MessageCode -import org.kopi.galite.visual.report.VReportRow class MPivotTable : Serializable { @@ -81,4 +80,22 @@ class MPivotTable : Serializable { * @return the desired row */ fun getRow(row: Int): VPivotTableRow? = userRows!![row] + + /** + * Returns an attribute value for a cell. + * + * @param row the index of the row whose value is to be looked up + * @param column the index of the column whose value is to be looked up (column of the model) + * @return the value Object at the specified cell + */ + fun getValueAt(row: Int, column: Int): Any? { + var x: Any? = null + + try { + x = userRows?.get(row)!!.getValueAt(column) + } catch (e: Exception) { + e.printStackTrace() + } + return x + } } diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VBooleanCodeColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VBooleanCodeColumn.kt new file mode 100644 index 000000000..aaa5aad8f --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VBooleanCodeColumn.kt @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.util.base.InconsistencyException +import org.kopi.galite.visual.dsl.pivottable.Dimension + +class VBooleanCodeColumn (ident: String?, + position: Dimension.Position?, + type: String?, + source: String?, + name: Array, + private val codes: BooleanArray) + : VCodeColumn(ident, + position, + type, + source, + name) { + + init { + if (codes.size > 2) { + throw InconsistencyException("Can't define more than two codes for a boolean column") + } + } + + override fun compareTo(object1: Any, object2: Any): Int { + return if (object1 == object2) 0 else if (true == object1) 1 else -1 + } + + override fun getIndex(value: Any?): Int { + return if ((value as Boolean) == codes[0]) 0 else 1 + } +} \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VBooleanColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VBooleanColumn.kt new file mode 100644 index 000000000..7ea673a13 --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VBooleanColumn.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.visual.dsl.pivottable.Dimension + +/** + * Represents a pivot table column description + * @param ident The identifier of the field + * @param position The position of the dimension field + */ +class VBooleanColumn(ident: String?, + position: Dimension.Position?) + : VPivotTableColumn(ident, + position) { + + /** + * Compare two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int { + return if (object1 == object2) 0 else if (true == object1) 1 else -1 + } +} \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VCodeColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VCodeColumn.kt new file mode 100644 index 000000000..1b4bd5d2f --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VCodeColumn.kt @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.visual.dsl.pivottable.Dimension +import org.kopi.galite.visual.l10n.FieldLocalizer +import org.kopi.galite.visual.l10n.TypeLocalizer + +/** + * Represents a pivot table column description + * @param ident The identifier of the field + * @param position The position of the dimension field + */ +abstract class VCodeColumn(ident: String?, + position : Dimension.Position?, + private val type: String?, + private val source: String?, + private val idents: Array) + : VPivotTableColumn(ident, + position) { + + protected var names: Array? = null // array of external representations + + /** + * Compares two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + abstract override fun compareTo(object1: Any, object2: Any): Int + + /** + * Return a string representation. + */ + override fun format(o: Any?): String { + return if (names != null) names!![getIndex(o)]!! else idents[getIndex(o)] + } + + /** + * Get the index of the value. + */ + abstract fun getIndex(value: Any?): Int + + // ---------------------------------------------------------------------- + // LOCALIZATION + // ---------------------------------------------------------------------- + /** + * Localizes this field + * + * @param parentLocalizer the caller localizer + */ + override fun localize(parentLocalizer: FieldLocalizer) { + val loc: TypeLocalizer = parentLocalizer.manager.getTypeLocalizer(source, type) + names = Array(idents.size) { i -> + val label = loc.getCodeLabel(idents[i]) + label + } + } + + fun initLabels(labels: Array) { + names = labels.map { + it + }.toTypedArray() + } +} diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VDateColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VDateColumn.kt new file mode 100644 index 000000000..471d7f221 --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VDateColumn.kt @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.visual.dsl.pivottable.Dimension +import java.time.LocalDate + +/** + * Represents a pivot table column description + * @param ident The identifier of the field + * @param position The position of the dimension field + */ +class VDateColumn (ident: String?, + position: Dimension.Position?) + : VPivotTableColumn(ident, + position) { + + /** + * Compare two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int = (object1 as LocalDate).compareTo(object2 as LocalDate) +} \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VDecimalCodeColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VDecimalCodeColumn.kt new file mode 100644 index 000000000..30932f38b --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VDecimalCodeColumn.kt @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import java.math.BigDecimal + +import org.kopi.galite.util.base.InconsistencyException +import org.kopi.galite.visual.dsl.pivottable.Dimension + +class VDecimalCodeColumn (ident: String?, + position: Dimension.Position?, + type: String?, + source: String?, + name: Array, + private val codes: Array) + : VCodeColumn(ident, + position, + type, + source, + name) { + + /** + * Compares two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int = (object1 as BigDecimal) + .compareTo(object2 as BigDecimal) + + /** + * Get the index of the value. + */ + override fun getIndex(value: Any?): Int { + for (i in codes.indices) { + if (value == codes[i]) { + return i + } + } + throw InconsistencyException(">>>>$value") + } + } \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VDecimalColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VDecimalColumn.kt new file mode 100644 index 000000000..5f84bc77f --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VDecimalColumn.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.visual.dsl.pivottable.Dimension +import java.math.BigDecimal + +/** + * Represents a pivot table column description + * @param ident The identifier of the field + * @param position The position of the dimension field + */ +class VDecimalColumn(ident: String?, + position: Dimension.Position?) + : VPivotTableColumn(ident, + position) { + + /** + * Compare two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int = + (object1 as BigDecimal).compareTo(object2 as BigDecimal) +} diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VIntegerCodeColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VIntegerCodeColumn.kt new file mode 100644 index 000000000..3bd63d2b6 --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VIntegerCodeColumn.kt @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.visual.dsl.pivottable.Dimension + +class VIntegerCodeColumn (ident: String?, + position: Dimension.Position?, + type: String?, + source: String?, + name: Array, + private val codes: IntArray) + : VCodeColumn(ident, + position, + type, + source, + name) { + + private var fastIndex = -1 // if array = {fastIndex, fastIndex + 1, ...} + + init { + fastIndex = codes[0] + for (i in 1 until codes.size) { + if (codes[i] != fastIndex + i) { + fastIndex = -1 + break + } + } + } + + /** + * Compares two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int { + val v1 = (object1 as Int) + val v2 = (object2 as Int) + + return if (v1 < v2) -1 else if (v1 > v2) 1 else 0 + } + + /** + * Get the index of the value. + */ + override fun getIndex(value: Any?): Int { + if (fastIndex != -1) { + return (value as Int) - fastIndex + } + for (i in codes.indices) { + if ((value as Int) == codes[i]) { + return i + } + } + return -1 + } +} \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VIntegerColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VIntegerColumn.kt new file mode 100644 index 000000000..8643d11bf --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VIntegerColumn.kt @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.visual.dsl.pivottable.Dimension + +class VIntegerColumn(ident: String?, + position: Dimension.Position?) + : VPivotTableColumn(ident, + position) { + + /** + * Compare two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int { + val v1 = (object1 as Int) + val v2 = (object2 as Int) + + return if (v1 < v2) -1 else if (v1 > v2) 1 else 0 + } +} \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VMonthColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VMonthColumn.kt new file mode 100644 index 000000000..346ef333e --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VMonthColumn.kt @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.type.Month +import org.kopi.galite.visual.dsl.pivottable.Dimension + +/** + * Represents a pivot table column description + * @param ident The identifier of the field + * @param position The position of the dimension field + */ +class VMonthColumn(ident: String?, + position: Dimension.Position?) + : VPivotTableColumn(ident, + position) { + + /** + * Compare two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int { + return (object1 as Month).compareTo(object2 as Month) + } +} \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTableColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTableColumn.kt index 27e826d9d..d2bd4e6e1 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTableColumn.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VPivotTableColumn.kt @@ -27,7 +27,8 @@ import org.kopi.galite.visual.l10n.PivotTableLocalizer * @param ident The identifier of the field * @param position The position of the dimension field */ -class VPivotTableColumn(val ident: String?, val position: Dimension.Position?) { +abstract class VPivotTableColumn(val ident: String?, + val position: Dimension.Position?) { // ---------------------------------------------------------------------- // DATA MEMBERS @@ -35,6 +36,20 @@ class VPivotTableColumn(val ident: String?, val position: Dimension.Position?) { var label: String = "" var help: String? = null + open fun format(o: Any?): String { + return o.toString() + } + /** + * Compare two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + abstract fun compareTo(object1: Any, object2: Any): Int + // ---------------------------------------------------------------------- // LOCALIZATION // ---------------------------------------------------------------------- @@ -51,6 +66,15 @@ class VPivotTableColumn(val ident: String?, val position: Dimension.Position?) { } } + /** + * Localizes this field + * + * @param parentLocalizer the caller localizer + */ + protected open fun localize(parentLocalizer: FieldLocalizer) { + // by default nothing to do + } + fun helpOnColumn(help: VHelpGenerator) { help.helpOnColumn(label, this.help) } diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VStringCodeColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VStringCodeColumn.kt new file mode 100644 index 000000000..3497d1e90 --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VStringCodeColumn.kt @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.util.base.InconsistencyException +import org.kopi.galite.visual.dsl.pivottable.Dimension + +class VStringCodeColumn (ident: String?, + position: Dimension.Position?, + type: String?, + source: String?, + name: Array, + private val codes: Array) + : VCodeColumn(ident, + position, + type, + source, + name) { + + /** + * Compares two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int { + return (object1 as String).compareTo((object2 as String)) + } + + override fun getIndex(value: Any?): Int { + codes.forEachIndexed { index, code -> + if (value == code) { + return index + } + } + throw InconsistencyException(">>>>$value") + } +} \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VStringColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VStringColumn.kt new file mode 100644 index 000000000..504f53be3 --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VStringColumn.kt @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.visual.dsl.pivottable.Dimension + +/** + * Represents a pivot table column description + * @param ident The identifier of the field + * @param position The position of the dimension field + */ +class VStringColumn(ident: String?, + position: Dimension.Position?) + : VPivotTableColumn(ident, + position) { + + /** + * Compare two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int = (object1 as String).compareTo((object2 as String)) +} \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VTimeColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VTimeColumn.kt new file mode 100644 index 000000000..53dcec4e5 --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VTimeColumn.kt @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.visual.dsl.pivottable.Dimension +import java.time.LocalTime + +/** + * Represents a pivot table column description + * @param ident The identifier of the field + * @param position The position of the dimension field + */ +class VTimeColumn(ident: String?, + position: Dimension.Position?) + : VPivotTableColumn(ident, + position) { + + /** + * Compares two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int { + return (object1 as LocalTime).compareTo(object2 as LocalTime) + } +} \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VTimestampColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VTimestampColumn.kt new file mode 100644 index 000000000..4a7abf1a4 --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VTimestampColumn.kt @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.visual.dsl.pivottable.Dimension +import java.time.Instant + +/** + * Represents a pivot table column description + * @param ident The identifier of the field + * @param position The position of the dimension field + */ +class VTimestampColumn(ident: String?, + position: Dimension.Position?) + : VPivotTableColumn(ident, + position) { + + /** + * Compares two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int { + return (object1 as Instant).compareTo(object2 as Instant) + } +} \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VWeekColumn.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VWeekColumn.kt new file mode 100644 index 000000000..a7ba2410c --- /dev/null +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/pivottable/VWeekColumn.kt @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2013-2023 kopiLeft Services SARL, Tunis TN + * Copyright (c) 1990-2023 kopiRight Managed Solutions GmbH, Wien AT + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package org.kopi.galite.visual.pivottable + +import org.kopi.galite.type.Week +import org.kopi.galite.visual.dsl.pivottable.Dimension + +/** + * Represents a pivot table column description + * @param ident The identifier of the field + * @param position The position of the dimension field + */ +class VWeekColumn(ident: String?, + position: Dimension.Position?) + : VPivotTableColumn(ident, + position) { + + /** + * Compare two objects. + * + * @param object1 the first operand of the comparison + * @param object2 the second operand of the comparison + * @return -1 if the first operand is smaller than the second + * 1 if the second operand if smaller than the first + * 0 if the two operands are equal + */ + override fun compareTo(object1: Any, object2: Any): Int = (object1 as Week).compareTo(object2 as Week) +} \ No newline at end of file diff --git a/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt b/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt index 96c924ca1..ae4e8eb85 100644 --- a/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt +++ b/galite-core/src/main/kotlin/org/kopi/galite/visual/ui/vaadin/pivottable/DPivotTable.kt @@ -35,6 +35,7 @@ class DPivotTable(private val pivotTable: VPivotTable) : DWindow(pivotTable), UP private val model: MPivotTable = pivotTable.model // pivot table model private val pivotData = PivotTable.PivotData() private val pivotOptions = PivotTable.PivotOptions() + private val rowsValues = mutableListOf() private val rows = mutableListOf() private val columns = mutableListOf() @@ -60,11 +61,10 @@ class DPivotTable(private val pivotTable: VPivotTable) : DWindow(pivotTable), UP columns.add(it.label) } } - - model.userRows - ?.flatMap { it.data.asIterable() } - ?.chunked(model.columns.count()) { rows -> - pivotData.addRow(*rows.map { it ?: "" }.toTypedArray())} + buildRows() + rowsValues + .chunked(model.columns.count()) { rows -> + pivotData.addRow(*rows.map{ it }.toTypedArray())} // Pivot table dimension pivotOptions.setRows(*rows.toTypedArray()) @@ -93,4 +93,16 @@ class DPivotTable(private val pivotTable: VPivotTable) : DWindow(pivotTable), UP add(pivot) } + + private fun buildRows(){ + for (i in 0 until model.userRows!!.count()) { + for (j in 0 until model.columns.count()) { + rowsValues.add(getValueAt(j, i)) + } + } + } + + fun getValueAt(columnIndex: Int, rowIndex: Int): String { + return model.columns[columnIndex]!!.format(model.getValueAt(rowIndex, columnIndex)) + } } diff --git a/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/product/ProductP.kt b/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/product/ProductP.kt index 10a48ea4c..a07260abf 100644 --- a/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/product/ProductP.kt +++ b/galite-demo/galite-vaadin/src/main/kotlin/org/kopi/galite/demo/product/ProductP.kt @@ -26,8 +26,8 @@ import org.kopi.galite.visual.domain.DECIMAL import org.kopi.galite.visual.domain.STRING import org.kopi.galite.visual.dsl.common.Icon import org.kopi.galite.visual.dsl.form.Key -import org.kopi.galite.visual.dsl.pivottable.PivotTable import org.kopi.galite.visual.dsl.pivottable.Dimension.Position +import org.kopi.galite.visual.dsl.pivottable.PivotTable /** * Product Report @@ -54,11 +54,11 @@ class ProductP : PivotTable(title = "Products", locale = Locale.UK) { label = "Supplier" help = "The supplier" } - val tax = dimension(STRING(10), Position.ROW) { + val tax = dimension(Tax, Position.ROW) { label = "Tax" help = "The product tax name" } - val category = dimension(STRING(10), Position.ROW) { + val category = dimension(Category, Position.ROW) { label = "Category" help = "The product category" } @@ -76,42 +76,11 @@ class ProductP : PivotTable(title = "Products", locale = Locale.UK) { this[product] = result[Product.description] this[department] = result[Product.department].orEmpty() this[supplier] = result[Product.supplier].orEmpty() - this[category] = decodeCategory(result[Product.category]) - this[tax] = decodeTax(result[Product.taxName]) + this[category] = result[Product.category] + this[tax] = result[Product.taxName] this[price] = result[Product.price] } } } } - - /** - * Decode category code - * !!! FIXME : Fix pivot table to accept CodeDomain type and automatically convert a code to its value - */ - fun decodeCategory(category: Int) : String { - return when (category) { - 1 -> "shoes" - 2 -> "shirts" - 3 -> "glasses" - 4 -> "pullovers" - 5 -> "jeans" - else -> "UNKNOWN" - } - } - - /** - * Decode Tax code - * !!! FIXME : Fix pivot table to accept CodeDomain type and automatically convert a code to its value - */ - fun decodeTax(taxe: String) : String { - return when (taxe) { - "tax 0" -> "0%" - "tax 1" -> "19%" - "tax 2" -> "9%" - "tax 3" -> "13%" - "tax 4" -> "22%" - "tax 5" -> "11%" - else -> "UNKNOWN" - } - } }