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 authored and adamsaghy committed Jun 26, 2024
1 parent 009e9de commit b482fbd
Show file tree
Hide file tree
Showing 31 changed files with 169 additions and 261 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
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
*/
package org.apache.fineract.portfolio.accountdetails.domain;

import java.util.Arrays;
import java.util.List;
import org.apache.fineract.infrastructure.core.data.EnumOptionData;
import org.apache.fineract.portfolio.accountdetails.service.AccountEnumerations;

/**
* Enum representation of account types .
*/
Expand All @@ -27,7 +32,8 @@ public enum AccountType {
INDIVIDUAL(1, "accountType.individual"), //
GROUP(2, "accountType.group"), //
JLG(3, "accountType.jlg"), // JLG account given in group context
GLIM(4, "accountType.glim"), GSIM(5, "accountType.gsim");
GLIM(4, "accountType.glim"), //
GSIM(5, "accountType.gsim");

private final Integer value;
private final String code;
Expand Down Expand Up @@ -71,6 +77,10 @@ public static AccountType fromName(final String name) {
return accountType;
}

public static List<EnumOptionData> toEnumOptionData() {
return Arrays.stream(values()).sequential().map(AccountEnumerations::loanType).toList();
}

public Integer getValue() {
return this.value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public static NoteType fromApiUrl(final String url) {
return BY_API.get(url);
}

public static EnumOptionData toEnumOptionData(final Integer id) {
return toEnumOptionData(NoteType.fromInt(id));
}

public static EnumOptionData toEnumOptionData(final NoteType noteType) {
return noteType == null ? null : noteType.toEnumOptionData();
}

public EnumOptionData toEnumOptionData() {
return new EnumOptionData(getValue().longValue(), getCode(), getDescription());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class EntityDatatableChecksApiResource {
+ "Example Request:\n" + "\n" + "entityDatatableChecks?offset=0&limit=15")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = EntityDatatableChecksApiResourceSwagger.GetEntityDatatableChecksResponse.class)))) })
public String retrieveAll(@Context final UriInfo uriInfo, @QueryParam("status") @Parameter(description = "status") final Long status,
public String retrieveAll(@Context final UriInfo uriInfo, @QueryParam("status") @Parameter(description = "status") final Integer status,
@QueryParam("entity") @Parameter(description = "entity") final String entity,
@QueryParam("productId") @Parameter(description = "productId") final Long productId,
@QueryParam("offset") @Parameter(description = "offset") final Integer offset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class EntityDatatableChecks extends AbstractPersistableCustom<Long> {
private String datatableName;

@Column(name = "status_enum", nullable = false)
private Long status;
private Integer status;

@Column(name = "system_defined")
private boolean systemDefined = false;
Expand All @@ -50,29 +50,19 @@ public class EntityDatatableChecks extends AbstractPersistableCustom<Long> {
private Long productId;

public static EntityDatatableChecks fromJson(final JsonCommand command) {

final String entity = command.stringValueOfParameterNamed("entity");
final Long status = command.longValueOfParameterNamed("status");
final Integer status = command.integerValueSansLocaleOfParameterNamed("status");
final String datatableName = command.stringValueOfParameterNamed("datatableName");

boolean systemDefined = false;
if (command.parameterExists("systemDefined")) {
systemDefined = command.booleanObjectValueOfParameterNamed("systemDefined");
} else {
systemDefined = false;
}

boolean systemDefined = command.booleanPrimitiveValueOfParameterNamed("systemDefined");
Long productId = null;
if (command.parameterExists("productId")) {
productId = command.longValueOfParameterNamed("productId");
}

return new EntityDatatableChecks(entity, datatableName, status, systemDefined, productId);

}

public boolean isSystemDefined() {
return this.systemDefined;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ List<EntityDatatableChecks> findByEntityAndStatusAndSubtype(@Param("entity") Str
@Param("subtype") String subtype);

@Query("select t from EntityDatatableChecks t WHERE t.status =:status and t.entity=:entity and t.productId = :productId ")
List<EntityDatatableChecks> findByEntityStatusAndProduct(@Param("entity") String entity, @Param("status") Long status,
List<EntityDatatableChecks> findByEntityStatusAndProduct(@Param("entity") String entity, @Param("status") Integer status,
@Param("productId") Long productId);

@Query("select t from EntityDatatableChecks t WHERE t.status =:status and t.entity=:entity and t.productId IS NULL ")
List<EntityDatatableChecks> findByEntityStatusAndNoProduct(@Param("entity") String entity, @Param("status") Long status);
List<EntityDatatableChecks> findByEntityStatusAndNoProduct(@Param("entity") String entity, @Param("status") Integer status);

@Query("select t from EntityDatatableChecks t WHERE t.status =:status "
+ "and t.entity=:entity and t.datatableName = :datatableName AND t.productId = :productId")
List<EntityDatatableChecks> findByEntityStatusAndDatatableIdAndProductId(@Param("entity") String entityName,
@Param("status") Long status, @Param("datatableName") String datatableName, @Param("productId") Long productId);
@Param("status") Integer status, @Param("datatableName") String datatableName, @Param("productId") Long productId);

@Query("select t from EntityDatatableChecks t WHERE t.status =:status and t.entity=:entity "
+ " and t.datatableName = :datatableName AND t.productId IS NULL")
List<EntityDatatableChecks> findByEntityStatusAndDatatableIdAndNoProduct(@Param("entity") String entityName,
@Param("status") Long status, @Param("datatableName") String datatableName);
@Param("status") Integer status, @Param("datatableName") String datatableName);

}
Loading

0 comments on commit b482fbd

Please sign in to comment.