Skip to content

Commit

Permalink
Refactored ResultColumnHeaderData using Lombok and modern Java new fe…
Browse files Browse the repository at this point in the history
…atures
  • Loading branch information
a7med3del1973 committed Feb 10, 2025
1 parent 3a5888e commit 2b77e08
Showing 1 changed file with 41 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand All @@ -21,25 +21,34 @@
import jakarta.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.fineract.infrastructure.core.exception.PlatformDataIntegrityException;
import org.apache.fineract.infrastructure.core.service.database.DatabaseType;
import org.apache.fineract.infrastructure.core.service.database.JdbcJavaType;

/**
* Immutable data object representing a resultset column.
*/
@Getter
public final class ResultsetColumnHeaderData implements Serializable {

private final String columnName;
private JdbcJavaType columnType;
private final Long columnLength;
private final DisplayType columnDisplayType;
@Getter(AccessLevel.NONE)
private final boolean isColumnNullable;
@Getter(AccessLevel.NONE)
private final boolean isColumnPrimaryKey;
@Getter(AccessLevel.NONE)
private final boolean isColumnUnique;
@Getter(AccessLevel.NONE)
private final boolean isColumnIndexed;

private final List<ResultsetColumnValueData> columnValues;
private final String columnCode;

Expand Down Expand Up @@ -80,22 +89,11 @@ private ResultsetColumnHeaderData(final String columnName, String columnType, fi
this.columnDisplayType = calcDisplayType();
}

public String getColumnName() {
return this.columnName;
}

// used in many classes
public boolean isNamed(final String columnName) {
return this.columnName.equalsIgnoreCase(columnName);
}

public JdbcJavaType getColumnType() {
return this.columnType;
}

public Long getColumnLength() {
return this.columnLength;
}

public boolean getIsColumnNullable() {
return isColumnNullable;
}
Expand All @@ -112,18 +110,6 @@ public boolean getIsColumnIndexed() {
return isColumnIndexed;
}

public DisplayType getColumnDisplayType() {
return this.columnDisplayType;
}

public String getColumnCode() {
return this.columnCode;
}

public List<ResultsetColumnValueData> getColumnValues() {
return this.columnValues;
}

public boolean isDateDisplayType() {
return columnDisplayType == DisplayType.DATE;
}
Expand Down Expand Up @@ -172,6 +158,7 @@ public boolean hasColumnValues() {
return columnValues != null && !columnValues.isEmpty();
}

// is used in searchUtil class
public boolean isColumnValueAllowed(final String match) {
for (final ResultsetColumnValueData allowedValue : this.columnValues) {
if (allowedValue.matches(match)) {
Expand All @@ -181,6 +168,7 @@ public boolean isColumnValueAllowed(final String match) {
return false;
}

// is used in searchUtil class
public boolean isColumnCodeAllowed(final Integer match) {
for (final ResultsetColumnValueData allowedValue : this.columnValues) {
if (allowedValue.codeMatches(match)) {
Expand All @@ -195,27 +183,17 @@ public boolean hasPrecision(@NotNull DatabaseType dialect) {
}

// --- Calculation ---

private String adjustColumnType(String type) {
type = type.toUpperCase();
switch (type) {
case "CLOB":
case "ENUM":
case "SET":
return "VARCHAR";
case "NEWDECIMAL":
return "DECIMAL";
case "LONGLONG":
return "BIGINT";
case "SHORT":
return "SMALLINT";
case "TINY":
return "TINYINT";
case "INT24":
return "INT";
default:
return type;
}
return switch (type) {
case "CLOB", "ENUM", "SET" -> "VARCHAR";
case "NEWDECIMAL" -> "DECIMAL";
case "LONGLONG" -> "BIGINT";
case "SHORT" -> "SMALLINT";
case "TINY" -> "TINYINT";
case "INT24" -> "INT";
default -> type;
};
}

@NotNull
Expand All @@ -239,40 +217,25 @@ private DisplayType calcDisplayType() {
}

public static DisplayType calcColumnDisplayType(JdbcJavaType columnType) {
if (columnType.isTextType()) {
return DisplayType.TEXT;
}
if (columnType.isStringType()) {
return DisplayType.STRING;
}
if (columnType.isAnyIntegerType()) {
return DisplayType.INTEGER;
}
if (columnType.isAnyFloatType()) {
return DisplayType.FLOAT;
}
if (columnType.isDecimalType()) { // Refer org.drizzle.jdbc.internal.mysql.MySQLType.java
return DisplayType.DECIMAL;
}
if (columnType.isDateType()) {
return DisplayType.DATE;
}
if (columnType.isDateTimeType()) {
return DisplayType.DATETIME;
}
if (columnType.isTimeType()) {
return DisplayType.TIME;
}
if (columnType.isBooleanType()) {
return DisplayType.BOOLEAN;
}
if (columnType.isBinaryType()) {
return DisplayType.BINARY;
}
return null;
Map<Predicate<JdbcJavaType>, DisplayType> typeMap = new LinkedHashMap<>();

typeMap.put(JdbcJavaType::isTextType, DisplayType.TEXT);
typeMap.put(JdbcJavaType::isStringType, DisplayType.STRING);
typeMap.put(JdbcJavaType::isAnyIntegerType, DisplayType.INTEGER);
typeMap.put(JdbcJavaType::isAnyFloatType, DisplayType.FLOAT);
typeMap.put(JdbcJavaType::isDecimalType, DisplayType.DECIMAL);
typeMap.put(JdbcJavaType::isDateType, DisplayType.DATE);
typeMap.put(JdbcJavaType::isDateTimeType, DisplayType.DATETIME);
typeMap.put(JdbcJavaType::isTimeType, DisplayType.TIME);
typeMap.put(JdbcJavaType::isBooleanType, DisplayType.BOOLEAN);
typeMap.put(JdbcJavaType::isBinaryType, DisplayType.BINARY);

return typeMap.entrySet().stream().filter(entry -> entry.getKey().test(columnType)).map(Map.Entry::getValue).findFirst()
.orElse(null);
}

// Enum representing the different ways a column can be displayed.
public enum DisplayType {
TEXT, STRING, INTEGER, FLOAT, DECIMAL, DATE, TIME, DATETIME, BOOLEAN, BINARY, CODELOOKUP, CODEVALUE,;
TEXT, STRING, INTEGER, FLOAT, DECIMAL, DATE, TIME, DATETIME, BOOLEAN, BINARY, CODELOOKUP, CODEVALUE;
}
}

0 comments on commit 2b77e08

Please sign in to comment.