Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ public class ReportsDashboard {
private String implementationLevel;
private String procurementSystem;
private String location;
private String country;
private String status;
private String reportingSystem;
private String typeOfAssistance;
private String currency;
//private String year;
private String year;
private BigDecimal actualCommitment;
private BigDecimal actualDisbursement;
private Long activityCount;
Expand Down Expand Up @@ -64,15 +65,14 @@ public void setActivityIds(String activityIds) {
this.activityIds = activityIds;
}

/*
public String getYear() {
return year;
}
public String getYear() {
return year;
}

public void setYear(String year) {
this.year = year;
}

public void setYear(String year) {
this.year = year;
}
*/
public BigDecimal getActualCommitment() {
return actualCommitment;
}
Expand All @@ -82,11 +82,21 @@ public void setActualCommitment(BigDecimal actualCommitment) {
}

public String getLocation() {
return location;
return location != null ? location : country;
}

public void setLocation(String location) {
this.location = location;
this.country = location;
}

public String getCountry() {
return country != null ? country : location;
}

public void setCountry(String country) {
this.country = country;
this.location = country;
}

public String getImplementationLevel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,23 @@ public Response getProcurementSystem() {
}

/**
* List the possible values of 'Funding Status' filter.
* List the possible values of 'Reporting System' filter.
*
* @return filter definition and values of 'funding-status' filter.
* @return filter definition and values of 'reporting-system' filter.
*/
@GET
@Path("/reportingSystem/")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
@ApiMethod(id = FiltersConstants.REPORTING_SYSTEM, name = ColumnConstants.REPORTING_SYSTEM)
@ApiOperation(value = "Retrieve the data needed for building the 'Reporting System' filter.",
notes = "The response contains 2 objects - the filter definition and the values. \n"
+ "The filter widget should create a tree for 'Reporting System' values.")
@FilterDefinition(tab = EPConstants.TAB_OTHER, columns = ColumnConstants.REPORTING_SYSTEM)
public Response getReportingSystem() {
return PublicServices.buildOkResponseWithOriginHeaders(
FiltersManager.getInstance().getCategoryValueFilter(FiltersConstants.REPORTING_SYSTEM));
}

@OPTIONS
@Path("/procurementSystem")
@ApiOperation(
Expand All @@ -397,6 +410,22 @@ public Response getProcurementSystem() {
public Response describeProcurementSystem() {
return PublicServices.buildOkResponseWithOriginHeaders("");
}

@OPTIONS
@Path("/reportingSystem")
@ApiOperation(
value = "Describe options for endpoint",
notes = "Enables Cross-Origin Resource Sharing for endpoint")
public Response describeReportingSystem() {
return PublicServices.buildOkResponseWithOriginHeaders("");
}

/**
* List the possible values of 'Funding Status' filter.
*
* @return filter definition and values of 'funding-status' filter.
*/

@GET
@Path("/fundingStatus/")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public final class CategoryValueFilterListManager implements FilterListManager {
.put(FiltersConstants.PROCUREMENT_SYSTEM,
new CategoryBean(ColumnConstants.PROCUREMENT_SYSTEM,
CategoryConstants.PROCUREMENT_SYSTEM_KEY))
.put(FiltersConstants.REPORTING_SYSTEM,
new CategoryBean(ColumnConstants.REPORTING_SYSTEM,
CategoryConstants.REPORTING_SYSTEM_KEY))
.build();

public static CategoryValueFilterListManager getInstance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.digijava.kernel.ampapi.endpoints.dashboards.services.PublicServices;
import org.digijava.kernel.ampapi.endpoints.errors.ApiErrorResponse;
import org.digijava.kernel.ampapi.endpoints.gis.SettingsAndFiltersParameters;
import org.digijava.kernel.ampapi.endpoints.publicportal.dto.PublicDonorCommitmentsByYear;
import org.digijava.kernel.ampapi.endpoints.publicportal.dto.PublicTotalsByMeasure;
import org.digijava.kernel.ampapi.endpoints.reports.ReportFormParameters;
import org.digijava.kernel.ampapi.endpoints.reports.ReportsUtil;
Expand Down Expand Up @@ -126,6 +127,31 @@ public Response describeTotalByMeasure() {
}


@POST
@Path("/donorCommitmentsByYear")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
@ApiMethod(ui = false, id = "donorCommitmentsByYear")
@ApiOperation("Commitments by donor up to selected year")
@ApiResponses(@ApiResponse(code = HttpServletResponse.SC_OK, message = "Donor commitments by year",
response = PublicDonorCommitmentsByYear.class))
public Response getDonorCommitmentsByYear(SettingsAndFiltersParameters config,
@ApiParam(value = "Include commitments up to this year", required = false)
@QueryParam("year") Integer year) {
return PublicServices.buildOkResponseWithOriginHeaders(
PublicPortalService.getDonorCommitmentsByYear(config, year));
}


@OPTIONS
@Path("/donorCommitmentsByYear")
@ApiOperation(
value = "Describe options for endpoint",
notes = "Enables Cross-Origin Resource Sharing for endpoint")
public Response describeDonorCommitmentsByYear() {
return PublicServices.buildOkResponseWithOriginHeaders("");
}


@POST
@Path("/projectCount")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.digijava.kernel.ampapi.endpoints.errors.ApiErrorResponse;
import org.digijava.kernel.ampapi.endpoints.errors.ApiRuntimeException;
import org.digijava.kernel.ampapi.endpoints.gis.SettingsAndFiltersParameters;
import org.digijava.kernel.ampapi.endpoints.publicportal.dto.PublicDonorCommitment;
import org.digijava.kernel.ampapi.endpoints.publicportal.dto.PublicDonorCommitmentsByYear;
import org.digijava.kernel.ampapi.endpoints.publicportal.dto.PublicTotalsByMeasure;
import org.digijava.kernel.ampapi.endpoints.reports.ReportFormParameters;
import org.digijava.kernel.ampapi.endpoints.reports.ReportsUtil;
Expand All @@ -32,9 +34,12 @@

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
Comment thread
Copilot marked this conversation as resolved.
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -49,6 +54,14 @@
*/
public class PublicPortalService {
protected static final Logger logger = Logger.getLogger(PublicPortalService.class);
private static final String DONOR_AGENCY_FILTER = "donor-agency";
private static final String DONOR_AGENCY_CAMEL_FILTER = "donorAgency";
private static final String DONOR_FILTER = "donor";
private static final String REPORTING_SYSTEM_FILTER = "reporting-system";
private static final String REPORTING_SYSTEM_CAMEL_FILTER = "reportingSystem";
private static final String DATE_FILTER = "date";
private static final String DATE_START = "start";
private static final String DATE_END = "end";

/**
* Retrieves top 'count' projects based on fixed requirements. <br>
Expand Down Expand Up @@ -376,6 +389,133 @@ private static void applyFilterRules(Map<String, Object> filters, ReportSpecific
}
}

public static PublicDonorCommitmentsByYear getDonorCommitmentsByYear(SettingsAndFiltersParameters config,
Integer year) {
PublicDonorCommitmentsByYear result = new PublicDonorCommitmentsByYear();
int targetYear = year == null ? Calendar.getInstance().get(Calendar.YEAR) : year;
result.setYear(targetYear);
Comment thread
brianbrix marked this conversation as resolved.

Map<String, Object> settings = copyMap(config != null ? config.getSettings() : null);
if (!settings.containsKey(SettingsConstants.FUNDING_TYPE_ID)) {
settings.put(SettingsConstants.FUNDING_TYPE_ID, SettingsConstants.DEFAULT_FUNDING_TYPE_ID);
}
Comment thread
brianbrix marked this conversation as resolved.
if (!settings.containsKey(SettingsConstants.CURRENCY_ID)) {
settings.put(SettingsConstants.CURRENCY_ID, "USD");
}
result.setCurrency((String) settings.get(SettingsConstants.CURRENCY_ID));

Map<String, Object> filters = copyMap(config != null ? config.getFilters() : null);
normalizeKnownFilterAliases(filters);

List<Object> donorIds = extractDonorIds(filters);
if (donorIds.isEmpty()) {
return result;
}

Set<Long> requestedDonorIds = new LinkedHashSet<>();
for (Object donorId : donorIds) {
Long parsedDonorId = parseLong(donorId);
if (parsedDonorId != null) {
requestedDonorIds.add(parsedDonorId);
}
}

Map<String, Object> baseFilters = copyMap(filters);
baseFilters.put(DATE_FILTER, buildDateFilter(baseFilters.get(DATE_FILTER), targetYear));

for (Long donorId : requestedDonorIds) {
Map<String, Object> donorFilters = copyMap(baseFilters);
donorFilters.put(DONOR_AGENCY_FILTER, Collections.singletonList(donorId));
BigDecimal donorTotal = getTotalForFilters(settings, donorFilters);
Comment on lines +426 to +429

PublicDonorCommitment donorCommitment = new PublicDonorCommitment();
donorCommitment.setDonorId(donorId);
donorCommitment.setTotal(donorTotal);

result.getDonorTotals().add(donorCommitment);
result.setTotal(result.getTotal().add(donorTotal));
}

return result;
}

private static BigDecimal getTotalForFilters(Map<String, Object> settings, Map<String, Object> filters) {
ReportSpecificationImpl spec = new ReportSpecificationImpl("PublicPortal_getDonorCommitmentsByYearSingle",
ArConstants.DONOR_TYPE);

SettingsUtils.applyExtendedSettings(spec, settings);
ReportsUtil.configureFilters(spec, filters);
spec.setSummaryReport(true);

GeneratedReport report = EndpointUtils.runReport(spec);
if (report == null || report.reportContents == null || report.reportContents.getContents() == null) {
return BigDecimal.ZERO;
}

BigDecimal total = BigDecimal.ZERO;
for (ReportCell cell : report.reportContents.getContents().values()) {
if (cell != null && cell.value instanceof BigDecimal) {
total = total.add((BigDecimal) cell.value);
}
}

return total;
}

private static Map<String, Object> copyMap(Map<String, Object> source) {
return source == null ? new HashMap<>() : new HashMap<>(source);
}

private static void normalizeKnownFilterAliases(Map<String, Object> filters) {
if (filters.containsKey(DONOR_AGENCY_CAMEL_FILTER) && !filters.containsKey(DONOR_AGENCY_FILTER)) {
filters.put(DONOR_AGENCY_FILTER, filters.get(DONOR_AGENCY_CAMEL_FILTER));
}
if (filters.containsKey(DONOR_FILTER) && !filters.containsKey(DONOR_AGENCY_FILTER)) {
filters.put(DONOR_AGENCY_FILTER, filters.get(DONOR_FILTER));
}
if (filters.containsKey(REPORTING_SYSTEM_CAMEL_FILTER) && !filters.containsKey(REPORTING_SYSTEM_FILTER)) {
filters.put(REPORTING_SYSTEM_FILTER, filters.get(REPORTING_SYSTEM_CAMEL_FILTER));
}
}

private static List<Object> extractDonorIds(Map<String, Object> filters) {
Object rawDonors = filters.get(DONOR_AGENCY_FILTER);
if (rawDonors instanceof List<?>) {
return new ArrayList<>((List<?>) rawDonors);
}
if (rawDonors != null) {
List<Object> singleValue = new ArrayList<>();
singleValue.add(rawDonors);
return singleValue;
}
return new ArrayList<>();
}

private static Map<String, Object> buildDateFilter(Object existingDateFilter, Integer year) {
Map<String, Object> dateFilter = new HashMap<>();
if (existingDateFilter instanceof Map<?, ?>) {
((Map<?, ?>) existingDateFilter).forEach((key, value) -> {
if (key != null && !DATE_START.equals(String.valueOf(key))) {
dateFilter.put(String.valueOf(key), value);
}
});
}
dateFilter.put(DATE_END, year + "-12-31");
return dateFilter;
}

private static Long parseLong(Object donorId) {
if (donorId == null) {
return null;
}

try {
return Long.parseLong(String.valueOf(donorId));
} catch (NumberFormatException ex) {
return null;
}
}

public static PublicTotalsByMeasure getCountByMeasure(SettingsAndFiltersParameters config) {
PublicTotalsByMeasure result = new PublicTotalsByMeasure();
result.getMeasure().put("original", "Total Activities");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.digijava.kernel.ampapi.endpoints.publicportal.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;

import java.math.BigDecimal;

public class PublicDonorCommitment {

@JsonProperty("donorId")
@ApiModelProperty(example = "43")
private Long donorId;

@JsonProperty("total")
@ApiModelProperty(example = "1085.4")
private BigDecimal total;

public Long getDonorId() {
return donorId;
}

public void setDonorId(Long donorId) {
this.donorId = donorId;
}

public BigDecimal getTotal() {
return total;
}

public void setTotal(BigDecimal total) {
this.total = total;
}
}
Loading
Loading