FINERACT-2245: New command processing - mix taxonomy

This commit is contained in:
Aleksandar Vidakovic
2026-02-18 20:26:51 +01:00
committed by Aleksandar Vidakovic
parent fe98aa6a58
commit ae3e2a6343
54 changed files with 1037 additions and 751 deletions
+75
View File
@@ -0,0 +1,75 @@
/**
* 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.
*/
description = 'Fineract Mix'
apply plugin: 'java'
apply plugin: 'eclipse'
compileJava {
dependsOn ':fineract-avro-schemas:buildJavaSdk'
}
configurations {
providedRuntime // needed for Spring Boot executable WAR
providedCompile
compile() {
exclude module: 'hibernate-entitymanager'
exclude module: 'hibernate-validator'
exclude module: 'activation'
exclude module: 'bcmail-jdk14'
exclude module: 'bcprov-jdk14'
exclude module: 'bctsp-jdk14'
exclude module: 'c3p0'
exclude module: 'stax-api'
exclude module: 'jaxb-api'
exclude module: 'jaxb-impl'
exclude module: 'jboss-logging'
exclude module: 'itext-rtf'
exclude module: 'classworlds'
}
runtime
}
apply from: 'dependencies.gradle'
// Configuration for the modernizer plugin
// https://github.com/andygoossens/gradle-modernizer-plugin
modernizer {
ignoreClassNamePatterns = [
'.*AbstractPersistableCustom',
'.*EntityTables',
'.*domain.*'
]
}
// If we are running Gradle within Eclipse to enhance classes with OpenJPA,
// set the classes directory to point to Eclipse's default build directory
if (project.hasProperty('env') && project.getProperty('env') == 'eclipse') {
sourceSets.main.java.outputDir = new File(rootProject.projectDir, "fineract-core/bin/main")
}
if (!(project.hasProperty('env') && project.getProperty('env') == 'dev')) {
sourceSets {
test {
java {
exclude '**/core/boot/tests/**'
}
}
}
}
+70
View File
@@ -0,0 +1,70 @@
/**
* 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.
*/
dependencies {
// Never use "compile" scope, but make all dependencies either 'implementation', 'runtimeOnly' or 'testCompile'.
// Note that we never use 'api', because Fineract at least currently is a simple monolithic application ("WAR"), not a library.
// We also (normally should have) no need to ever use 'compileOnly'.
// implementation dependencies are directly used (compiled against) in src/main (and src/test)
//
implementation(
project(path: ':fineract-core'),
project(path: ':fineract-command'),
)
implementation(
'org.springframework.boot:spring-boot-starter-web',
'org.springframework.boot:spring-boot-starter-validation',
'org.springframework.boot:spring-boot-starter-security',
'org.springframework.boot:spring-boot-starter-data-jdbc',
'jakarta.ws.rs:jakarta.ws.rs-api',
'org.apache.commons:commons-lang3',
'com.google.guava:guava',
// TODO: try to get rid of this as soon as we get hands on the MIX XSD file from 2009; then we can use JAXB
'com.google.code.gson:gson',
'com.github.spotbugs:spotbugs-annotations',
'io.swagger.core.v3:swagger-annotations-jakarta',
'org.mapstruct:mapstruct',
'io.github.resilience4j:resilience4j-spring-boot3',
)
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'org.mapstruct:mapstruct-processor'
implementation('org.dom4j:dom4j') {
exclude group: 'javax.xml.bind'
}
// testCompile dependencies are ONLY used in src/test, not src/main.
// Do NOT repeat dependencies which are ALREADY in implementation or runtimeOnly!
//
testImplementation( 'io.github.classgraph:classgraph' )
testImplementation ('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'com.jayway.jsonpath', module: 'json-path'
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
exclude group: 'jakarta.activation'
exclude group: 'javax.activation'
exclude group: 'org.skyscreamer'
}
testImplementation ('org.mockito:mockito-inline')
}
@@ -0,0 +1,53 @@
/**
* 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.mix.api;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import java.sql.Date;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.mix.service.MixReportXBRLBuilder;
import org.apache.fineract.mix.service.MixReportXBRLResultService;
import org.springframework.stereotype.Component;
@Path("/v1/mixreport")
@Component
@Tag(name = "Mix Report", description = """
""")
@RequiredArgsConstructor
public class MixReportApiResource {
private final MixReportXBRLResultService xbrlResultService;
private final MixReportXBRLBuilder xbrlBuilder;
@GET
@Produces({ MediaType.APPLICATION_XML })
public String retrieveXBRLReport(@QueryParam("startDate") final Date startDate, @QueryParam("endDate") final Date endDate,
@QueryParam("currency") final String currency) {
final var data = xbrlResultService.getXBRLResult(startDate, endDate, currency);
// TODO: make this type safe?
return this.xbrlBuilder.build(data);
}
}
@@ -0,0 +1,47 @@
/**
* 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.mix.api;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.mix.data.MixTaxonomyData;
import org.apache.fineract.mix.service.MixTaxonomyReadService;
import org.springframework.stereotype.Component;
@Path("/v1/mixtaxonomy")
@Component
@Tag(name = "Mix Taxonomy", description = "")
@RequiredArgsConstructor
public class MixTaxonomyApiResource {
private final MixTaxonomyReadService readTaxonomyService;
@GET
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public List<MixTaxonomyData> retrieveAll() {
return readTaxonomyService.retrieveAll();
}
}
@@ -0,0 +1,71 @@
/**
* 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.mix.api;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;
import org.apache.fineract.command.core.CommandPipeline;
import org.apache.fineract.mix.command.MixTaxonomyMappingUpdateCommand;
import org.apache.fineract.mix.data.MixTaxonomyMappingData;
import org.apache.fineract.mix.data.MixTaxonomyMappingUpdateRequest;
import org.apache.fineract.mix.data.MixTaxonomyMappingUpdateResponse;
import org.apache.fineract.mix.service.MixTaxonomyMappingReadService;
import org.springframework.stereotype.Component;
@Path("/v1/mixmapping")
@Component
@Tag(name = "Mix Mapping", description = "")
@RequiredArgsConstructor
public class MixTaxonomyMappingApiResource {
private final MixTaxonomyMappingReadService readTaxonomyMappingService;
private final CommandPipeline commandPipeline;
@GET
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public MixTaxonomyMappingData retrieveTaxonomyMapping() {
return this.readTaxonomyMappingService.retrieveTaxonomyMapping();
}
@PUT
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
public MixTaxonomyMappingUpdateResponse updateTaxonomyMapping(final MixTaxonomyMappingUpdateRequest request) {
// TODO support multiple configuration file loading; this is the legacy behavior
if (request.getId() == null) {
request.setId(1L);
}
final var command = new MixTaxonomyMappingUpdateCommand();
command.setPayload(request);
final Supplier<MixTaxonomyMappingUpdateResponse> response = commandPipeline.send(command);
return response.get();
}
}
@@ -0,0 +1,28 @@
/**
* 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.mix.command;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.mix.data.MixTaxonomyMappingUpdateRequest;
@Data
@EqualsAndHashCode(callSuper = true)
public class MixTaxonomyMappingUpdateCommand extends Command<MixTaxonomyMappingUpdateRequest> {}
@@ -0,0 +1,42 @@
/**
* 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.mix.data;
import java.io.Serial;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class MixReportXBRLContextData implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private String dimensionType;
private String dimension;
private Integer periodType;
}
@@ -0,0 +1,46 @@
/**
* 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.mix.data;
import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class MixReportXBRLData implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private Map<MixTaxonomyData, BigDecimal> resultMap;
private Date startDate;
private Date endDate;
private String currency;
}
@@ -0,0 +1,42 @@
/**
* 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.mix.data;
import java.io.Serial;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class MixReportXBRLNamespaceData implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private Long id;
private String prefix;
private String url;
}
@@ -0,0 +1,57 @@
/**
* 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.mix.data;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serial;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class MixTaxonomyData implements Serializable {
public static final Integer PORTFOLIO = 0;
public static final Integer BALANCE_SHEET = 1;
public static final Integer INCOME = 2;
public static final Integer EXPENSE = 3;
@Serial
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String namespace;
private String dimension;
private Integer type;
private String description;
// TODO: why is this different from the PORTFOLIO constant? This doesn't seem right!
@JsonIgnore
public boolean isPortfolio() {
return this.type == 5;
}
}
@@ -0,0 +1,37 @@
/**
* 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.mix.data;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class MixTaxonomyMappingData {
private String identifier;
private String config;
private String currency;
}
@@ -0,0 +1,41 @@
/**
* 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.mix.data;
import java.io.Serial;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MixTaxonomyMappingUpdateRequest implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private Long id;
private String identifier;
private String config;
private String currency;
}
@@ -0,0 +1,38 @@
/**
* 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.mix.data;
import java.io.Serial;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MixTaxonomyMappingUpdateResponse implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private Long entityId;
}
@@ -0,0 +1,48 @@
/**
* 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.mix.domain;
import java.io.Serial;
import java.io.Serializable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
@Table("mix_xbrl_namespace")
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
public class MixReportXBRLNamespace implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id
@Column("id")
private Long id;
@Column("prefix")
private String prefix;
@Column("url")
private String url;
}
@@ -0,0 +1,29 @@
/**
* 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.mix.domain;
import java.util.Optional;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
public interface MixReportXBRLNamespaceRepository
extends ListCrudRepository<MixReportXBRLNamespace, Long>, QueryByExampleExecutor<MixReportXBRLNamespace> {
Optional<MixReportXBRLNamespace> findOneByPrefix(String prefix);
}
@@ -0,0 +1,63 @@
/**
* 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.mix.domain;
import java.io.Serial;
import java.io.Serializable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
@Table("mix_taxonomy")
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
public final class MixTaxonomy implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id
@Column("id")
private Long id;
@Column("name")
private String name;
@Column("namespace_id")
private Long namespaceId;
@Column("dimension")
private String dimension;
@Column("type")
private Integer type;
@Column("description")
private String description;
// TODO: this is never used, but creates an error on MySQL (tinyint vs boolean mapping)
// @Column("need_mapping")
// private Boolean needMapping;
}
@@ -0,0 +1,53 @@
/**
* 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.mix.domain;
import java.io.Serial;
import java.io.Serializable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
@Table("mix_taxonomy_mapping")
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
public final class MixTaxonomyMapping implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id
@Column("id")
private Long id;
@Column("identifier")
private String identifier;
@Column("config")
private String config;
@Column("currency")
private String currency;
}
@@ -0,0 +1,25 @@
/**
* 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.mix.domain;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
public interface MixTaxonomyMappingRepository
extends ListCrudRepository<MixTaxonomyMapping, Long>, QueryByExampleExecutor<MixTaxonomyMapping> {}
@@ -0,0 +1,28 @@
/**
* 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.mix.domain;
import java.util.List;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
public interface MixTaxonomyRepository extends ListCrudRepository<MixTaxonomy, Long>, QueryByExampleExecutor<MixTaxonomy> {
List<MixTaxonomy> findAllByOrderByIdAsc();
}
@@ -0,0 +1,29 @@
/**
* 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.mix.exception;
import org.apache.fineract.infrastructure.core.exception.AbstractPlatformDomainRuleException;
public class MixReportXBRLMappingInvalidException extends AbstractPlatformDomainRuleException {
public MixReportXBRLMappingInvalidException(final String msg) {
super("error.msg.xbrl.report.mapping.invalid.id", "Mapping does not exist", msg);
}
}
@@ -0,0 +1,51 @@
/**
* 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.mix.handler;
import io.github.resilience4j.retry.annotation.Retry;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.command.core.Command;
import org.apache.fineract.command.core.CommandHandler;
import org.apache.fineract.mix.data.MixTaxonomyMappingUpdateRequest;
import org.apache.fineract.mix.data.MixTaxonomyMappingUpdateResponse;
import org.apache.fineract.mix.service.MixTaxonomyMappingWriteService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@Component
@RequiredArgsConstructor
public class MixTaxonomyMappingUpdateCommandHandler
implements CommandHandler<MixTaxonomyMappingUpdateRequest, MixTaxonomyMappingUpdateResponse> {
private final MixTaxonomyMappingWriteService writeTaxonomyService;
@Retry(name = "commandMixTaxonomyMappingUpdate", fallbackMethod = "fallback")
@Transactional
@Override
public MixTaxonomyMappingUpdateResponse handle(Command<MixTaxonomyMappingUpdateRequest> command) {
return writeTaxonomyService.updateMapping(command.getPayload());
}
@Override
public MixTaxonomyMappingUpdateResponse fallback(Command<MixTaxonomyMappingUpdateRequest> command, Throwable t) {
return CommandHandler.super.fallback(command, t);
}
}
@@ -0,0 +1,30 @@
/**
* 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.mix.mapping;
import org.apache.fineract.infrastructure.core.config.MapstructMapperConfig;
import org.apache.fineract.mix.data.MixReportXBRLNamespaceData;
import org.apache.fineract.mix.domain.MixReportXBRLNamespace;
import org.mapstruct.Mapper;
@Mapper(config = MapstructMapperConfig.class)
public interface MixReportXBRLNamespaceMapper {
MixReportXBRLNamespaceData map(MixReportXBRLNamespace source);
}
@@ -0,0 +1,32 @@
/**
* 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.mix.mapping;
import org.apache.fineract.infrastructure.core.config.MapstructMapperConfig;
import org.apache.fineract.mix.data.MixTaxonomyData;
import org.apache.fineract.mix.domain.MixTaxonomy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@Mapper(config = MapstructMapperConfig.class)
public interface MixTaxonomyMapper {
@Mapping(ignore = true, target = "namespace")
MixTaxonomyData map(MixTaxonomy source);
}
@@ -0,0 +1,30 @@
/**
* 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.mix.mapping;
import org.apache.fineract.infrastructure.core.config.MapstructMapperConfig;
import org.apache.fineract.mix.data.MixTaxonomyMappingData;
import org.apache.fineract.mix.domain.MixTaxonomyMapping;
import org.mapstruct.Mapper;
@Mapper(config = MapstructMapperConfig.class)
public interface MixTaxonomyMappingMapper {
MixTaxonomyMappingData map(MixTaxonomyMapping source);
}
@@ -0,0 +1,30 @@
/**
* 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.mix.mapping;
import org.apache.fineract.infrastructure.core.config.MapstructMapperConfig;
import org.apache.fineract.mix.data.MixTaxonomyMappingUpdateRequest;
import org.apache.fineract.mix.domain.MixTaxonomyMapping;
import org.mapstruct.Mapper;
@Mapper(config = MapstructMapperConfig.class)
public interface MixTaxonomyMappingUpdateRequestMapper {
MixTaxonomyMapping map(MixTaxonomyMappingUpdateRequest source);
}
@@ -0,0 +1,208 @@
/**
* 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.mix.service;
import com.google.common.base.Splitter;
import java.math.BigDecimal;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.mix.data.MixReportXBRLContextData;
import org.apache.fineract.mix.data.MixReportXBRLData;
import org.apache.fineract.mix.data.MixReportXBRLNamespaceData;
import org.apache.fineract.mix.data.MixTaxonomyData;
import org.apache.fineract.mix.exception.MixReportXBRLMappingInvalidException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Component;
@Slf4j
@RequiredArgsConstructor
@Component
public class MixReportXBRLBuilder {
// NOTE: see https://www.xbrl.org/taxonomyrecognition/mx_2009-06-19_summary-page.htm
private static final String SCHEME_URL = "http://www.themix.org";
private static final String IDENTIFIER = "000000";
private static final String UNITID_PURE = "Unit1";
private static final String UNITID_CUR = "Unit2";
private final MixReportXBRLNamespaceReadService readNamespaceService;
// TODO: we should do this with JAXB
public String build(final MixReportXBRLData xbrlData) {
return this.build(xbrlData.getResultMap(), xbrlData.getStartDate(), xbrlData.getEndDate(), xbrlData.getCurrency());
}
public String build(final Map<MixTaxonomyData, BigDecimal> map, final Date startDate, final Date endDate, final String currency) {
Integer instantScenarioCounter = 0;
Integer durationScenarioCounter = 0;
Map<MixReportXBRLContextData, String> contextMap = new HashMap<>();
final Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("xbrl");
root.addElement("schemaRef").addNamespace("link",
"http://www.themix.org/sites/default/files/Taxonomy2010/dct/dc-all_2010-08-31.xsd");
for (final Map.Entry<MixTaxonomyData, BigDecimal> entry : map.entrySet()) {
final MixTaxonomyData taxonomy = entry.getKey();
final BigDecimal value = entry.getValue();
addTaxonomy(root, taxonomy, value, startDate, endDate, instantScenarioCounter, durationScenarioCounter, contextMap);
}
addContexts(root, startDate, endDate, contextMap);
addCurrencyUnit(root, currency);
addNumberUnit(root);
doc.setXMLEncoding("UTF-8");
return doc.asXML();
}
private Element addTaxonomy(final Element rootElement, final MixTaxonomyData taxonomy, final BigDecimal value, final Date startDate,
final Date endDate, Integer instantScenarioCounter, Integer durationScenarioCounter,
final Map<MixReportXBRLContextData, String> contextMap) {
// throw an error is start / endate is null
if (startDate == null || endDate == null) {
throw new MixReportXBRLMappingInvalidException("start date and end date should not be null");
}
final String prefix = taxonomy.getNamespace();
String qname = taxonomy.getName();
if (prefix != null && !prefix.isEmpty()) {
final MixReportXBRLNamespaceData ns = this.readNamespaceService.retrieveNamespaceByPrefix(prefix);
if (ns != null) {
rootElement.addNamespace(prefix, ns.getUrl());
}
qname = prefix + ":" + taxonomy.getName();
}
final Element xmlElement = rootElement.addElement(qname);
final String dimension = taxonomy.getDimension();
final SimpleDateFormat timeFormat = new SimpleDateFormat("MM_dd_yyyy");
MixReportXBRLContextData context = null;
if (dimension != null) {
final List<String> dims = Splitter.on(':').splitToList(dimension);
if (dims.size() == 2) {
context = new MixReportXBRLContextData().setDimensionType(dims.get(0)).setDimension(dims.get(1)).setPeriodType(
taxonomy.getType().equals(MixTaxonomyData.BALANCE_SHEET) || taxonomy.getType().equals(MixTaxonomyData.PORTFOLIO) ? 0
: 1);
}
}
if (context == null) {
context = new MixReportXBRLContextData().setPeriodType(
taxonomy.getType().equals(MixTaxonomyData.BALANCE_SHEET) || taxonomy.getType().equals(MixTaxonomyData.PORTFOLIO) ? 0
: 1);
}
if (!contextMap.containsKey(context)) {
final String startDateStr = timeFormat.format(startDate);
final String endDateStr = timeFormat.format(endDate);
instantScenarioCounter += 1;
durationScenarioCounter += 1;
final String contextRefID = context.getPeriodType() == 0 ? "As_Of_" + endDateStr + instantScenarioCounter
: "Duration_" + startDateStr + "_To_" + endDateStr + durationScenarioCounter;
contextMap.put(context, contextRefID);
}
xmlElement.addAttribute("contextRef", contextMap.get(context));
xmlElement.addAttribute("unitRef", getUnitRef(taxonomy));
xmlElement.addAttribute("decimals", getNumberOfDecimalPlaces(value).toString());
// add the child
xmlElement.addText(value.toPlainString());
return xmlElement;
}
private String getUnitRef(final MixTaxonomyData tx) {
return tx.isPortfolio() ? UNITID_PURE : UNITID_CUR;
}
/**
* Adds the generic number unit
*/
private void addNumberUnit(final Element root) {
final Element numerUnit = root.addElement("unit");
numerUnit.addAttribute("id", UNITID_PURE);
final Element measure = numerUnit.addElement("measure");
measure.addText("xbrli:pure");
}
/**
* Adds the currency unit to the document
*
* @param currencyCode
*/
private void addCurrencyUnit(final Element root, final String currencyCode) {
final Element currencyUnitElement = root.addElement("unit");
currencyUnitElement.addAttribute("id", UNITID_CUR);
final Element measure = currencyUnitElement.addElement("measure");
measure.addText("iso4217:" + currencyCode);
}
private void addContexts(final Element root, final Date startDate, final Date endDate,
final Map<MixReportXBRLContextData, String> contextMap) {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
for (final Map.Entry<MixReportXBRLContextData, String> entry : contextMap.entrySet()) {
final MixReportXBRLContextData context = entry.getKey();
final Element contextElement = root.addElement("context");
contextElement.addAttribute("id", entry.getValue());
contextElement.addElement("entity").addElement("identifier").addAttribute("scheme", SCHEME_URL).addText(IDENTIFIER);
final Element periodElement = contextElement.addElement("period");
if (context.getPeriodType() == 0) {
periodElement.addElement("instant").addText(format.format(endDate));
} else {
periodElement.addElement("startDate").addText(format.format(startDate));
periodElement.addElement("endDate").addText(format.format(endDate));
}
final String dimension = context.getDimension();
final String dimType = context.getDimensionType();
if (dimType != null && dimension != null) {
contextElement.addElement("scenario").addElement("explicitMember").addAttribute("dimension", dimType).addText(dimension);
}
}
}
private Integer getNumberOfDecimalPlaces(final BigDecimal bigDecimal) {
final String string = bigDecimal.stripTrailingZeros().toPlainString();
final int index = string.indexOf(".");
return index < 0 ? 0 : string.length() - index - 1;
}
}
@@ -0,0 +1,26 @@
/**
* 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.mix.service;
import org.apache.fineract.mix.data.MixReportXBRLNamespaceData;
public interface MixReportXBRLNamespaceReadService {
MixReportXBRLNamespaceData retrieveNamespaceByPrefix(String prefix);
}
@@ -0,0 +1,40 @@
/**
* 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.mix.service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.mix.data.MixReportXBRLNamespaceData;
import org.apache.fineract.mix.domain.MixReportXBRLNamespaceRepository;
import org.apache.fineract.mix.mapping.MixReportXBRLNamespaceMapper;
import org.springframework.stereotype.Service;
@Slf4j
@RequiredArgsConstructor
@Service
public class MixReportXBRLNamespaceReadServiceImpl implements MixReportXBRLNamespaceReadService {
private final MixReportXBRLNamespaceRepository repository;
private final MixReportXBRLNamespaceMapper mapper;
@Override
public MixReportXBRLNamespaceData retrieveNamespaceByPrefix(final String prefix) {
return repository.findOneByPrefix(prefix).map(mapper::map).orElse(null);
}
}
@@ -0,0 +1,28 @@
/**
* 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.mix.service;
import java.sql.Date;
import org.apache.fineract.mix.data.MixReportXBRLData;
public interface MixReportXBRLResultService {
MixReportXBRLData getXBRLResult(Date startDate, Date endDate, String currency);
}
@@ -0,0 +1,185 @@
/**
* 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.mix.service;
import com.google.gson.Gson;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.mix.data.MixReportXBRLData;
import org.apache.fineract.mix.data.MixTaxonomyData;
import org.apache.fineract.mix.data.MixTaxonomyMappingData;
import org.apache.fineract.mix.exception.MixReportXBRLMappingInvalidException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class MixReportXBRLResultServiceImpl implements MixReportXBRLResultService {
private static final ScriptEngine SCRIPT_ENGINE = new ScriptEngineManager().getEngineByName("JavaScript");
private final MixTaxonomyMappingReadService readTaxonomyMappingService;
private final MixTaxonomyReadService readTaxonomyService;
private final JdbcTemplate jdbcTemplate;
@Autowired
public MixReportXBRLResultServiceImpl(final JdbcTemplate jdbcTemplate, final MixTaxonomyMappingReadService readTaxonomyMappingService,
final MixTaxonomyReadService readTaxonomyService) {
this.readTaxonomyMappingService = readTaxonomyMappingService;
this.readTaxonomyService = readTaxonomyService;
this.jdbcTemplate = jdbcTemplate;
}
@Override
public MixReportXBRLData getXBRLResult(final Date startDate, final Date endDate, final String currency) {
final Map<MixTaxonomyData, BigDecimal> config = retrieveTaxonomyConfig(startDate, endDate);
if (config == null || config.isEmpty()) {
throw new MixReportXBRLMappingInvalidException("Mapping is empty");
}
return new MixReportXBRLData().setResultMap(config).setStartDate(startDate).setEndDate(endDate).setCurrency(currency);
}
@SuppressWarnings("unchecked")
private Map<MixTaxonomyData, BigDecimal> retrieveTaxonomyConfig(final Date startDate, final Date endDate) {
final MixTaxonomyMappingData taxonomyMapping = this.readTaxonomyMappingService.retrieveTaxonomyMapping();
if (taxonomyMapping == null) {
return null;
}
final String config = taxonomyMapping.getConfig();
if (config != null) {
// <taxonomyId, mapping>
Map<String, String> configMap = new HashMap<>();
configMap = new Gson().fromJson(config, configMap.getClass());
if (configMap == null) {
return null;
}
// <taxonomyId, value>
final HashMap<MixTaxonomyData, BigDecimal> resultMap = new HashMap<>();
Map<String, BigDecimal> accountBalanceMap = setupBalanceMap(getAccountSql(startDate, endDate));
for (final Map.Entry<String, String> entry : configMap.entrySet()) {
final BigDecimal value = processMappingString(accountBalanceMap, entry.getValue());
final MixTaxonomyData taxonomy = this.readTaxonomyService.retrieveOne(Long.parseLong(entry.getKey()));
resultMap.put(taxonomy, value);
}
return resultMap;
}
return null;
}
// TODO: this should at least use prepared statements and not just string concatenate the date objects!
private String getAccountSql(final Date startDate, final Date endDate) {
return "SELECT debits.glcode AS 'glcode', debits.name AS 'name', COALESCE(debits.debitamount,0)-COALESCE(credits.creditamount,0)) AS 'balance' "
+ "FROM (SELECT acc_gl_account.gl_code AS 'glcode',name,SUM(amount) AS 'debitamount' "
+ "FROM acc_gl_journal_entry,acc_gl_account WHERE acc_gl_account.id = acc_gl_journal_entry.account_id "
+ "AND acc_gl_journal_entry.type_enum=2 AND acc_gl_journal_entry.entry_date <= " + endDate
+ " AND acc_gl_journal_entry.entry_date > " + startDate
//
+ " GROUP BY glcode ORDER BY glcode) debits LEFT OUTER JOIN "
+ "(SELECT acc_gl_account.gl_code AS 'glcode',name,SUM(amount) AS 'creditamount' "
+ "FROM acc_gl_journal_entry,acc_gl_account WHERE acc_gl_account.id = acc_gl_journal_entry.account_id "
+ "AND acc_gl_journal_entry.type_enum=1 AND acc_gl_journal_entry.entry_date <= " + endDate
+ " AND acc_gl_journal_entry.entry_date > " + startDate
//
+ " GROUP BY glcode ORDER BY glcode) credits ON debits.glcode=credits.glcode UNION "
+ "SELECT credits.glcode AS 'glcode', credits.name AS 'name', COALESCE(debits.debitamount,0)-COALESCE(credits.creditamount,0)) AS 'balance' "
+ "FROM (SELECT acc_gl_account.gl_code AS 'glcode',name,SUM(amount) AS 'debitamount' "
+ "FROM acc_gl_journal_entry,acc_gl_account WHERE acc_gl_account.id = acc_gl_journal_entry.account_id "
+ "AND acc_gl_journal_entry.type_enum=2 AND acc_gl_journal_entry.entry_date <= " + endDate
+ " AND acc_gl_journal_entry.entry_date > " + startDate
//
+ " GROUP BY glcode ORDER BY glcode) debits RIGHT OUTER JOIN "
+ "(SELECT acc_gl_account.gl_code AS 'glcode',name,SUM(amount) AS 'creditamount' "
+ "FROM acc_gl_journal_entry,acc_gl_account WHERE acc_gl_account.id = acc_gl_journal_entry.account_id "
+ "AND acc_gl_journal_entry.type_enum=1 AND acc_gl_journal_entry.entry_date <= " + endDate
+ " AND acc_gl_journal_entry.entry_date > " + startDate
//
+ " GROUP BY name, glcode ORDER BY glcode) credits ON debits.glcode=credits.glcode;";
}
private Map<String, BigDecimal> setupBalanceMap(final String sql) {
Map<String, BigDecimal> accountBalanceMap = new HashMap<>();
final SqlRowSet rs = this.jdbcTemplate.queryForRowSet(sql);
while (rs.next()) {
accountBalanceMap.put(rs.getString("glcode"), rs.getBigDecimal("balance"));
}
return accountBalanceMap;
}
// Calculate Taxonomy value from expression
private BigDecimal processMappingString(Map<String, BigDecimal> accountBalanceMap, String mappingString) {
final List<String> glCodes = getGLCodes(mappingString);
for (final String glcode : glCodes) {
final BigDecimal balance = accountBalanceMap.get(glcode);
mappingString = mappingString.replaceAll("\\{" + glcode + "\\}", balance != null ? balance.toString() : "0");
}
// evaluate the expression
float eval = 0f;
try {
// TODO: this doesn't work anymore in modern JVMs!!!!
final Number value = (Number) SCRIPT_ENGINE.eval(mappingString);
if (value != null) {
eval = value.floatValue();
}
} catch (final ScriptException e) {
log.error("Problem occurred in processMappingString function", e);
throw new IllegalArgumentException(e.getMessage(), e);
}
return BigDecimal.valueOf(eval);
}
public List<String> getGLCodes(final String template) {
final ArrayList<String> placeholders = new ArrayList<>();
if (template != null) {
final Pattern p = Pattern.compile("\\{(.*?)\\}");
final Matcher m = p.matcher(template);
while (m.find()) { // find next match
final String match = m.group();
final String code = match.substring(1, match.length() - 1);
placeholders.add(code);
}
}
return placeholders;
}
}
@@ -0,0 +1,26 @@
/**
* 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.mix.service;
import org.apache.fineract.mix.data.MixTaxonomyMappingData;
public interface MixTaxonomyMappingReadService {
MixTaxonomyMappingData retrieveTaxonomyMapping();
}
@@ -0,0 +1,40 @@
/**
* 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.mix.service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.mix.data.MixTaxonomyMappingData;
import org.apache.fineract.mix.domain.MixTaxonomyMappingRepository;
import org.apache.fineract.mix.mapping.MixTaxonomyMappingMapper;
import org.springframework.stereotype.Service;
@Slf4j
@RequiredArgsConstructor
@Service
public class MixTaxonomyMappingReadServiceImpl implements MixTaxonomyMappingReadService {
private final MixTaxonomyMappingRepository repository;
private final MixTaxonomyMappingMapper mapper;
@Override
public MixTaxonomyMappingData retrieveTaxonomyMapping() {
return repository.findAll().stream().findFirst().map(mapper::map).orElse(null);
}
}
@@ -0,0 +1,27 @@
/**
* 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.mix.service;
import org.apache.fineract.mix.data.MixTaxonomyMappingUpdateRequest;
import org.apache.fineract.mix.data.MixTaxonomyMappingUpdateResponse;
public interface MixTaxonomyMappingWriteService {
MixTaxonomyMappingUpdateResponse updateMapping(MixTaxonomyMappingUpdateRequest request);
}
@@ -0,0 +1,48 @@
/**
* 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.mix.service;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.mix.data.MixTaxonomyMappingUpdateRequest;
import org.apache.fineract.mix.data.MixTaxonomyMappingUpdateResponse;
import org.apache.fineract.mix.domain.MixTaxonomyMappingRepository;
import org.apache.fineract.mix.mapping.MixTaxonomyMappingUpdateRequestMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Slf4j
@RequiredArgsConstructor
@Service
public class MixTaxonomyMappingWriteServiceImpl implements MixTaxonomyMappingWriteService {
private final MixTaxonomyMappingRepository repository;
private final MixTaxonomyMappingUpdateRequestMapper mapper;
@Transactional
@Override
public MixTaxonomyMappingUpdateResponse updateMapping(@Valid final MixTaxonomyMappingUpdateRequest request) {
final var taxonomyMapping = mapper.map(request);
repository.save(taxonomyMapping);
return MixTaxonomyMappingUpdateResponse.builder().entityId(taxonomyMapping.getId()).build();
}
}
@@ -0,0 +1,29 @@
/**
* 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.mix.service;
import java.util.List;
import org.apache.fineract.mix.data.MixTaxonomyData;
public interface MixTaxonomyReadService {
List<MixTaxonomyData> retrieveAll();
MixTaxonomyData retrieveOne(Long id);
}
@@ -0,0 +1,46 @@
/**
* 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.mix.service;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.fineract.mix.data.MixTaxonomyData;
import org.apache.fineract.mix.domain.MixTaxonomyRepository;
import org.apache.fineract.mix.mapping.MixTaxonomyMapper;
import org.springframework.stereotype.Service;
@Slf4j
@RequiredArgsConstructor
@Service
public class MixTaxonomyReadServiceImpl implements MixTaxonomyReadService {
private final MixTaxonomyRepository repository;
private final MixTaxonomyMapper mapper;
@Override
public List<MixTaxonomyData> retrieveAll() {
return repository.findAllByOrderByIdAsc().stream().map(mapper::map).toList();
}
@Override
public MixTaxonomyData retrieveOne(final Long id) {
return repository.findById(id).map(mapper::map).orElse(null);
}
}