Skip to content
Permalink
Browse files
Add displayable fields, customer query, office query and deposit acco…
…unt query for savings accounts
  • Loading branch information
Awasum Yannick committed Aug 21, 2017
1 parent c843bc0 commit 8eba0c024926f7294f2336659338f6922f4f090d
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 28 deletions.
@@ -0,0 +1,21 @@
package io.mifos.reporting;

import io.mifos.reporting.api.v1.domain.ReportDefinition;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class TestSavingListReportSpecification extends AbstractReportingSpecificationTest {
public TestSavingListReportSpecification() {
super();
}

@Test
public void shouldReturnReportDefinition() {
final List<ReportDefinition> reportDefinitions = super.testSubject.fetchReportDefinitions("Deposit");
Assert.assertTrue(
reportDefinitions.stream().anyMatch(reportDefinition -> reportDefinition.getIdentifier().equals("Listing"))
);
}
}
@@ -2,40 +2,49 @@

import io.mifos.reporting.api.v1.domain.*;
import io.mifos.reporting.service.ServiceConstants;
import io.mifos.reporting.service.spi.Report;
import io.mifos.reporting.service.spi.ReportSpecification;
import io.mifos.reporting.service.spi.*;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Report(category = "Saving", identifier = "Listing")
public class SavingListReportSpecification implements ReportSpecification{
private static final String SAVING_ACCOUNT = "Saving account";
@Report(category = "Deposit", identifier = "Listing")
public class SavingListReportSpecification implements ReportSpecification {

private static final String CUSTOMER = "Customer";
private static final String FIRST_NAME = "First name";
private static final String MIDDLE_NAME = "Middle name";
private static final String LAST_NAME = "Last name";
private static final String EMPLOYEE = "Employee";
//private static final String OFFICE = "Office";
private static final String DATE_RANGE = "Date range";
private static final String ACCOUNT_NUMBER = "Account number";
private static final String ACCOUNT_TYPE = "Account type";
private static final String STATE = "State";
private static final String OFFICE = "Office";
private static final String DATE_RANGE = "Date created";

private final EntityManager entityManager;

private final Logger logger;

private final HashMap<String, String> customerColumnMapping = new HashMap<>();
private final HashMap<String, String> savingAccountColumnMapping = new HashMap<>();
// private final HashMap<String, String> officeColumnMapping = new HashMap<>();
private final HashMap<String, String> accountColumnMapping = new HashMap<>();
private final HashMap<String, String> officeColumnMapping = new HashMap<>();
private final HashMap<String, String> employeeColumnMapping = new HashMap<>();
private final HashMap<String, String> allColumnMapping = new HashMap<>();


@Autowired
public SavingListReportSpecification(@Qualifier(ServiceConstants.LOGGER_NAME) final Logger logger, final EntityManager entityManager){
public SavingListReportSpecification(@Qualifier(ServiceConstants.LOGGER_NAME) final Logger logger, final EntityManager entityManager) {
this.entityManager = entityManager;
this.logger = logger;
this.initializeMapping();
}


@@ -53,37 +62,161 @@ public ReportDefinition getReportDefinition() {

@Override
public ReportPage generateReport(ReportRequest reportRequest, int pageIndex, int size) {
final ReportDefinition reportDefinition = this.getReportDefinition();
this.logger.info("Generating report {0} ", reportDefinition.getIdentifier());

final ReportPage reportPage = new ReportPage();
reportPage.setName(reportDefinition.getName());
reportPage.setDescription(reportDefinition.getDescription());
reportPage.setHeader(this.createHeader(reportRequest.getDisplayableFields()));

final Query depositAccountQuery = this.entityManager.createNativeQuery(this.buildAccountQuery(reportRequest, pageIndex, size));

final List<?> depositAccountResultList = depositAccountQuery.getResultList();
reportPage.setRows(this.buildRows(reportRequest, depositAccountResultList));

return null;
}

private List<Row> buildRows(ReportRequest reportRequest, List<?> customerResultList) {
return null;
}

private List<QueryParameter> buildQueryParameters() {
return Arrays.asList(
QueryParameterBuilder.create(DATE_RANGE, Type.DATE).operator(QueryParameter.Operator.BETWEEN).build(),
QueryParameterBuilder.create(STATE, Type.TEXT).operator(QueryParameter.Operator.IN).build()
);
}

@Override
public void validate(ReportRequest reportRequest) throws IllegalArgumentException {
final ArrayList<String> unknownFields = new ArrayList<>();
reportRequest.getQueryParameters().forEach(queryParameter -> {
if (!this.allColumnMapping.keySet().contains(queryParameter.getName())) {
unknownFields.add(queryParameter.getName());
}
});

reportRequest.getDisplayableFields().forEach(displayableField -> {
if (!this.allColumnMapping.keySet().contains(displayableField.getName())) {
unknownFields.add(displayableField.getName());
}
});

if (!unknownFields.isEmpty()) {
throw new IllegalArgumentException(
"Unspecified fields requested: " + unknownFields.stream().collect(Collectors.joining(", "))
);
}

}

public String buildSavingAccountQuery(){
private void initializeMapping() {
this.customerColumnMapping.put(CUSTOMER, "cst.identifier");
this.customerColumnMapping.put(FIRST_NAME, "cst.given_name");
this.customerColumnMapping.put(MIDDLE_NAME, "cst.middle_name");
this.customerColumnMapping.put(LAST_NAME, "cst.surname");

this.officeColumnMapping.put(OFFICE, "cst.assigned_office");

this.employeeColumnMapping.put(EMPLOYEE, "pi.created_by");

return "SELECT ... FROM shed_product_instances pi";
this.accountColumnMapping.put(ACCOUNT_NUMBER, "pi.customer_identifier, pi.account_identifier");
this.accountColumnMapping.put(STATE, " pi.a_state");
this.accountColumnMapping.put(ACCOUNT_TYPE, "pi.product_definition_id");
this.accountColumnMapping.put(DATE_RANGE, "pi.created_on");

this.allColumnMapping.putAll(customerColumnMapping);
this.allColumnMapping.putAll(officeColumnMapping);
this.allColumnMapping.putAll(employeeColumnMapping);
this.allColumnMapping.putAll(accountColumnMapping);
}

public String buildCustomerQuery(){
private String buildAccountQuery(final ReportRequest reportRequest, int pageIndex, int size) {
final StringBuilder query = new StringBuilder("SELECT ");

final List<DisplayableField> displayableFields = reportRequest.getDisplayableFields();
final ArrayList<String> columns = new ArrayList<>();
displayableFields.forEach(displayableField -> {
final String column = this.accountColumnMapping.get(displayableField.getName())
+ this.employeeColumnMapping.get(displayableField.getName());
if (column != null) {
columns.add(column);
}
});

query.append(columns.stream().collect(Collectors.joining(", ")))
.append(" FROM ")
.append("shed_product_instances pi ");
final List<QueryParameter> queryParameters = reportRequest.getQueryParameters();
if (!queryParameters.isEmpty()) {
final ArrayList<String> criteria = new ArrayList<>();
queryParameters.forEach(queryParameter -> {
if (queryParameter.getValue() != null && !queryParameter.getValue().isEmpty()) {
criteria.add(
CriteriaBuilder.buildCriteria(this.accountColumnMapping.get(queryParameter.getName()), queryParameter)
);
criteria.add(
CriteriaBuilder.buildCriteria(this.employeeColumnMapping.get(queryParameter.getName()), queryParameter)
);
}
});

if (!criteria.isEmpty()) {
query.append(" WHERE ");
query.append(criteria.stream().collect(Collectors.joining(" AND ")));
}

}
query.append(" ORDER BY pi.customer_identifier");

query.append(" LIMIT ");
query.append(size);
if (pageIndex > 0) {
query.append(" OFFSET ");
query.append(size * pageIndex);
}

return query.toString();

// return "SELECT ... FROM shed_product_instances pi";

return null;
}

private void initializeMapping(){
this.customerColumnMapping.put(CUSTOMER, "cst.identifier, cst.given_name, cst.middle_name, " +
"cst.surname, cst.assigned_office");
this.employeeColumnMapping.put(EMPLOYEE, "pi.created_by");
this.savingAccountColumnMapping.put(SAVING_ACCOUNT, "pi.customer_identifier, pi.product_definition_id, pi.account_identifier, pi.a_state");
this.savingAccountColumnMapping.put(DATE_RANGE, "pi.created_on");
private String buildCustomerQuery(final ReportRequest reportRequest, final String customerIdentifier) {
final List<DisplayableField> displayableFields = reportRequest.getDisplayableFields();
final ArrayList<String> columns = new ArrayList<>();
displayableFields.forEach(displayableField -> {
final String column = this.customerColumnMapping.get(displayableField.getName());
if (column != null) {
columns.add(column);
}
});
return "SELECT " + columns.stream().collect(Collectors.joining(", ")) + " " +
"FROM maat_customers cst " +
"LEFT JOIN shed_product_instances pi on cst.identifier = pi.customer_identifier " +
"WHERE pi.customer_identifier ='" + customerIdentifier + "' " +
"ORDER BY cst.identifier";
}

this.allColumnMapping.putAll(customerColumnMapping);
this.allColumnMapping.putAll(employeeColumnMapping);
this.allColumnMapping.putAll(savingAccountColumnMapping);
private String buildOfficeQuery(final ReportRequest reportRequest, final String customerIdentifier) {
final List<DisplayableField> displayableFields = reportRequest.getDisplayableFields();
final ArrayList<String> columns = new ArrayList<>();
displayableFields.forEach(displayableField -> {
final String column = this.officeColumnMapping.get(displayableField.getName());
if (column != null) {
columns.add(column);
}
});

return "SELECT " + columns.stream().collect(Collectors.joining(", ")) + " " +
"FROM maat_customers cst " +
"WHERE cst.identifier ='" + customerIdentifier + "' " +
"ORDER BY cst.identifier";
}


private Header createHeader(final List<DisplayableField> displayableFields) {
final Header header = new Header();
header.setColumnNames(
@@ -98,12 +231,18 @@ private Header createHeader(final List<DisplayableField> displayableFields) {

private List<DisplayableField> buildDisplayableFields() {

return null;
}

private List<QueryParameter> buildQueryParameters(){
return Arrays.asList(
DisplayableFieldBuilder.create(CUSTOMER, Type.TEXT).mandatory().build(),
DisplayableFieldBuilder.create(FIRST_NAME, Type.TEXT).build(),
DisplayableFieldBuilder.create(MIDDLE_NAME, Type.TEXT).build(),
DisplayableFieldBuilder.create(LAST_NAME, Type.TEXT).build(),
DisplayableFieldBuilder.create(ACCOUNT_NUMBER, Type.TEXT).mandatory().build(),
DisplayableFieldBuilder.create(EMPLOYEE, Type.TEXT).mandatory().build(),
DisplayableFieldBuilder.create(OFFICE, Type.TEXT).mandatory().build(),
DisplayableFieldBuilder.create(DATE_RANGE, Type.TEXT).build()
);

return null;
}


}

0 comments on commit 8eba0c0

Please sign in to comment.