Skip to content

Commit

Permalink
FINERACT-2095: Support for string type primary keys - part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Marta Jankovics committed Jun 14, 2024
1 parent f48f94c commit 98af815
Show file tree
Hide file tree
Showing 39 changed files with 371 additions and 272 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,33 @@
/**
* 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;

@Getter
public class GLStringEnumOptionData extends StringEnumOptionData implements Serializable {

private String glAccountType;

public GLStringEnumOptionData(String id, String code, String glAccountType, String description) {
super(id, code, description);
this.glAccountType = glAccountType;
}
}
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 @@ -31,7 +31,7 @@
*/
@Getter
@EqualsAndHashCode
public class ExternalId implements Serializable {
public class ExternalId implements StringValueHolder, Serializable {

@Serial
private static final long serialVersionUID = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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.domain;

public interface StringValueHolder {

String getValue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ public PlatformApiDataValidationException(String globalisationMessageCode, Strin
this.errors = errors;
}

public PlatformApiDataValidationException(String messageCode, String userMessage, String parameterName, Throwable cause,
final Object... userMessageArgs) {
this("validation.msg.validation.errors.exist", "Validation errors exist.",
List.of(ApiParameterError.parameterError(messageCode, userMessage, parameterName, userMessageArgs)), cause);
}

public PlatformApiDataValidationException(String messageCode, String userMessage, String parameterName,
final Object... userMessageArgs) {
this("validation.msg.validation.errors.exist", "Validation errors exist.",
List.of(ApiParameterError.parameterError(messageCode, userMessage, parameterName, userMessageArgs)), null);
}

public List<ApiParameterError> getErrors() {
return this.errors;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.service;

public interface DefaultOption {

boolean isDefault();

static <T extends Enum<T> & DefaultOption> T getDefault(Class<T> clazz) {
for (T enumConstant : clazz.getEnumConstants()) {
if (enumConstant.isDefault()) {
return enumConstant;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.apache.fineract.infrastructure.core.service.database.JdbcJavaType;

public enum EntityTables {

Expand Down Expand Up @@ -65,18 +66,26 @@ public enum EntityTables {
private final String foreignKeyColumnNameOnDatatable;
@NotNull
private final String refColumn; // referenced column name on apptable
@NotNull
private final JdbcJavaType refColumnType; // referenced column type on apptable

private final ImmutableList<StatusEnum> checkStatuses;

EntityTables(@NotNull String name, @NotNull String apptableName, @NotNull String foreignKeyColumnNameOnDatatable,
@NotNull String refColumn, StatusEnum... statuses) {
@NotNull String refColumn, @NotNull JdbcJavaType refColumnType, StatusEnum... statuses) {
this.name = name;
this.apptableName = apptableName;
this.foreignKeyColumnNameOnDatatable = foreignKeyColumnNameOnDatatable;
this.refColumn = refColumn;
this.refColumnType = refColumnType;
this.checkStatuses = statuses == null ? ImmutableList.of() : ImmutableList.copyOf(statuses);
}

EntityTables(@NotNull String name, @NotNull String apptableName, @NotNull String foreignKeyColumnNameOnDatatable,
@NotNull String refColumn, StatusEnum... statuses) {
this(name, apptableName, foreignKeyColumnNameOnDatatable, refColumn, JdbcJavaType.BIGINT, statuses);
}

EntityTables(@NotNull String name, @NotNull String foreignKeyColumnNameOnDatatable, @NotNull String refColumn, StatusEnum... statuses) {
this(name, name, foreignKeyColumnNameOnDatatable, refColumn, statuses);
}
Expand All @@ -101,6 +110,11 @@ public String getRefColumn() {
return refColumn;
}

@NotNull
public JdbcJavaType getRefColumnType() {
return refColumnType;
}

public List<StatusEnum> getCheckStatuses() {
return checkStatuses;
}
Expand Down Expand Up @@ -130,7 +144,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
Loading

0 comments on commit 98af815

Please sign in to comment.