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 12, 2025
1 parent 3a5888e commit c78a15c
Showing 1 changed file with 46 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
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;
Expand All @@ -39,7 +41,6 @@ public final class ResultsetColumnHeaderData implements Serializable {
private final boolean isColumnPrimaryKey;
private final boolean isColumnUnique;
private final boolean isColumnIndexed;

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

Expand Down Expand Up @@ -80,22 +81,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 +102,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 +150,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 +160,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 +175,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 +209,38 @@ 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;
}

return switch (getJdbcType(columnType)) {
case "TEXT" -> DisplayType.TEXT;
case "STRING" -> DisplayType.STRING;
case "INTEGER" -> DisplayType.INTEGER;
case "FLOAT" -> DisplayType.FLOAT;
case "DECIMAL" -> DisplayType.DECIMAL;
case "DATE" -> DisplayType.DATE;
case "DATETIME" -> DisplayType.DATETIME;
case "TIME" -> DisplayType.TIME;
case "BOOLEAN" -> DisplayType.BOOLEAN;
case "BINARY" -> DisplayType.BINARY;
default -> null;
};
}

private static String getJdbcType(JdbcJavaType columnType) {
if (columnType.isTextType()) return "TEXT";
if (columnType.isStringType()) return "STRING";
if (columnType.isAnyIntegerType()) return "INTEGER";
if (columnType.isAnyFloatType()) return "FLOAT";
if (columnType.isDecimalType()) return "DECIMAL";
if (columnType.isDateType()) return "DATE";
if (columnType.isDateTimeType()) return "DATETIME";
if (columnType.isTimeType()) return "TIME";
if (columnType.isBooleanType()) return "BOOLEAN";
if (columnType.isBinaryType()) return "BINARY";
return "UNKNOWN";
}

// 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 c78a15c

Please sign in to comment.