Skip to content

Commit

Permalink
FINERACT-2081: Support for string type primary keys - refactor existi…
Browse files Browse the repository at this point in the history
…ng enums
  • Loading branch information
Marta Jankovics committed Jun 26, 2024
1 parent db8000b commit 2209ce5
Show file tree
Hide file tree
Showing 35 changed files with 252 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@

import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import org.apache.fineract.infrastructure.core.data.EnumOptionData;
import org.apache.fineract.infrastructure.core.data.StringEnumOptionData;

@Getter
public enum AccountingRuleType {

NONE(1, "accountingRuleType.none"), //
CASH_BASED(2, "accountingRuleType.cash"), //
ACCRUAL_PERIODIC(3, "accountingRuleType.accrual.periodic"), //
ACCRUAL_UPFRONT(4, "accountingRuleType.accrual.upfront"); //
NONE(1, "accountingRuleType.none", "No accounting"), //
CASH_BASED(2, "accountingRuleType.cash", "Cash based accounting"), //
ACCRUAL_PERIODIC(3, "accountingRuleType.accrual.periodic", "Periodic accrual accounting"), //
ACCRUAL_UPFRONT(4, "accountingRuleType.accrual.upfront", "Upfront accrual accounting"); //

private final Integer value;
private final String code;
private final String description;

private static final Map<Integer, AccountingRuleType> intToEnumMap = new HashMap<>();

Expand All @@ -44,21 +49,22 @@ public static AccountingRuleType fromInt(final Integer ruleTypeValue) {
return type;
}

AccountingRuleType(final Integer value, final String code) {
AccountingRuleType(final Integer value, final String code, final String description) {
this.value = value;
this.code = code;
this.description = description;
}

@Override
public String toString() {
return name().toString().replaceAll("_", " ");
return name().replaceAll("_", " ");
}

public Integer getValue() {
return this.value;
public EnumOptionData toEnumOptionData() {
return new EnumOptionData((long) getValue(), getCode(), getDescription());
}

public String getCode() {
return this.code;
public StringEnumOptionData toStringEnumOptionData() {
return new StringEnumOptionData(name(), getCode(), getDescription());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.core.data;

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;

/**
* <p>
* Immutable data object representing generic enumeration value.
* </p>
*/
@Getter
@EqualsAndHashCode
@AllArgsConstructor
public abstract class BaseEnumOptionData<T> implements Serializable {

protected T id;
protected final String code;
protected final String value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
package org.apache.fineract.infrastructure.core.data;

import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;

/**
Expand All @@ -29,11 +27,9 @@
* </p>
*/
@Getter
@EqualsAndHashCode
@AllArgsConstructor
public class EnumOptionData implements Serializable {
public class EnumOptionData extends BaseEnumOptionData<Long> implements Serializable {

private final Long id;
private final String code;
private final String value;
public EnumOptionData(Long id, String code, String description) {
super(id, code, description);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.core.data;

import java.io.Serializable;
import lombok.Getter;

/**
* <p>
* Immutable data object representing generic enumeration value.
* </p>
*/
@Getter
public class StringEnumOptionData extends BaseEnumOptionData<String> implements Serializable {

public StringEnumOptionData(String id, String code, String description) {
super(id, code, description);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static List<StatusEnum> getCheckStatuses(String name) {

@NotNull
public static List<Integer> getCheckStatusCodes(String name) {
return getCheckStatuses(name).stream().map(StatusEnum::getCode).toList();
return getCheckStatuses(name).stream().map(StatusEnum::getValue).toList();
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,55 @@
*/
package org.apache.fineract.infrastructure.dataqueries.data;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.fineract.infrastructure.core.data.EnumOptionData;

public enum StatusEnum {

CREATE("create", 100), APPROVE("approve", 200), ACTIVATE("activate", 300), WITHDRAWN("withdraw", 400), REJECTED("reject", 500), CLOSE(
"close", 600), WRITE_OFF("write off", 601), RESCHEDULE("reschedule", 602), OVERPAY("overpay", 700), DISBURSE("disburse", 800);
CREATE("statusEnum.create", 100), //
APPROVE("statusEnum.approve", 200), //
ACTIVATE("statusEnum.activate", 300), //
WITHDRAWN("statusEnum.withdraw", 400), //
REJECTED("statusEnum.reject", 500), //
CLOSE("statusEnum.close", 600), //
WRITE_OFF("statusEnum.write off", 601), //
RESCHEDULE("statusEnum.reschedule", 602), //
OVERPAY("statusEnum.overpay", 700), //
DISBURSE("statusEnum.disburse", 800), //
;

private final String name;
private static final StatusEnum[] VALUES = values();

private final Integer code;
private static final Map<Integer, StatusEnum> BY_ID = Arrays.stream(VALUES).collect(Collectors.toMap(StatusEnum::getValue, v -> v));

public Integer getCode() {
return code;
}

StatusEnum(String name, Integer code) {
private final String code;

this.name = name;
this.code = code;
private final Integer value;

public Integer getValue() {
return value;
}

public static List<DatatableCheckStatusData> getStatusList() {

List<DatatableCheckStatusData> data = new ArrayList<DatatableCheckStatusData>();

for (StatusEnum status : StatusEnum.values()) {
data.add(new DatatableCheckStatusData(status.name, status.code));
}

return data;

StatusEnum(String code, Integer value) {
this.code = code;
this.value = value;
}

public static StatusEnum fromInt(final Integer code) {
StatusEnum ret = null;
switch (code) {
case 100:
ret = StatusEnum.CREATE;
break;
case 200:
ret = StatusEnum.APPROVE;
break;
case 300:
ret = StatusEnum.ACTIVATE;
break;
case 400:
ret = StatusEnum.WITHDRAWN;
break;
case 500:
ret = StatusEnum.REJECTED;
break;
case 600:
ret = StatusEnum.CLOSE;
break;
case 601:
ret = StatusEnum.WRITE_OFF;
break;
case 602:
ret = StatusEnum.RESCHEDULE;
break;
case 700:
ret = StatusEnum.OVERPAY;
break;
case 800:
ret = StatusEnum.DISBURSE;
break;
default:
break;
}
return ret;
public static StatusEnum fromInt(final Integer value) {
return BY_ID.get(value);
}

public static EnumOptionData statusTypeEnum(final Integer id) {
return statusType(StatusEnum.fromInt(id));
public static EnumOptionData toEnumOptionData(final Integer id) {
return toEnumOptionData(StatusEnum.fromInt(id));
}

public static EnumOptionData statusType(final StatusEnum statusType) {
final EnumOptionData optionData = new EnumOptionData(statusType.getCode().longValue(), statusType.name(), statusType.name());
return optionData;
public static EnumOptionData toEnumOptionData(final StatusEnum statusType) {
return statusType == null ? null : statusType.toEnumOptionData();
}

public EnumOptionData toEnumOptionData() {
return new EnumOptionData(getValue().longValue(), code, name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public enum JobName {
LOAN_DELINQUENCY_CLASSIFICATION("Loan Delinquency Classification"), //
SEND_ASYNCHRONOUS_EVENTS("Send Asynchronous Events"), //
PURGE_EXTERNAL_EVENTS("Purge External Events"), //
PURGE_PROCESSED_COMMANDS("Purge Processed Commands");
PURGE_PROCESSED_COMMANDS("Purge Processed Commands"), //
;

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Map;
import java.util.stream.Collectors;
import lombok.Getter;
import org.apache.fineract.infrastructure.core.data.StringEnumOptionData;

@Getter
public enum InteropIdentifierType {
Expand Down Expand Up @@ -65,4 +66,8 @@ public static InteropIdentifierType resolveName(String name) {
InteropIdentifierType idType = BY_ALIAS.get(name);
return idType == null ? BY_NAME.get(name) : idType;
}

public StringEnumOptionData toStringEnumOptionData() {
return new StringEnumOptionData(name(), getCode(), getDescription());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,25 @@
*/
package org.apache.fineract.portfolio;

import lombok.Getter;
import org.apache.fineract.infrastructure.core.data.EnumOptionData;
import org.apache.fineract.infrastructure.core.data.StringEnumOptionData;

@Getter
public enum TransactionEntryType {

CREDIT(1, "transactionEntryType.credit"), //
DEBIT(2, "transactionEntryType.debit"), //
CREDIT(1, "transactionEntryType.credit", "Credit transaction"), //
DEBIT(2, "transactionEntryType.debit", "Debit transaction"), //
;

private final Integer value;
private final String code;
private final String description;

TransactionEntryType(final Integer value, final String code) {
TransactionEntryType(final Integer value, final String code, final String description) {
this.value = value;
this.code = code;
}

public Integer getValue() {
return this.value;
}

public String getCode() {
return this.code;
this.description = description;
}

public boolean isCredit() {
Expand All @@ -51,4 +50,12 @@ public boolean isDebit() {
public TransactionEntryType getReversal() {
return this == CREDIT ? DEBIT : CREDIT;
}

public EnumOptionData toEnumOptionData() {
return new EnumOptionData((long) getValue(), getCode(), getDescription());
}

public StringEnumOptionData toStringEnumOptionData() {
return new StringEnumOptionData(name(), getCode(), getDescription());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public enum PortfolioAccountType {

INVALID(0, "accountType.invalid"), //
LOAN(1, "accountType.loan"), //
SAVINGS(2, "accountType.savings");
SAVINGS(2, "accountType.savings"), //
;

private final Integer value;
private final String code;
Expand Down
Loading

0 comments on commit 2209ce5

Please sign in to comment.