package org.sigmah.server.report.model.generator;
/*
* #%L
* Sigmah
* %%
* Copyright (C) 2010 - 2016 URD
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.google.inject.Inject;
import org.sigmah.server.dao.PivotDAO;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.sigmah.server.domain.User;
import org.sigmah.shared.dto.pivot.content.FilterDescription;
import org.sigmah.shared.dto.pivot.model.Report;
import org.sigmah.shared.dto.pivot.model.ReportElement;
import org.sigmah.shared.dto.referential.DimensionType;
import org.sigmah.shared.util.DateRange;
import org.sigmah.shared.util.Filter;
/**
* Implements functionality common to all generators.
*
* @param <T> The type of <code>ReportElement</code> generated by the subclass.
*/
public abstract class BaseGenerator<T extends ReportElement> implements ContentGenerator<T> {
@Inject
protected PivotDAO pivotDAO;
/**
* Resolves an element's filter into a the effective filter, taking into
* account inherited restrictions and the overall <code>DateRange</code> of the
* report.
*
* Interaction between the report's date range <code>DateRange</code> and the
* element's filter is specified in {@link ReportElement#getFilter()}
*
* @param element The report element for which to resolve the filter
* @param inheritedFilter The <code>Filter</code> that is inherited from the enclosing <code>Report</code> or other container
* @param dateRange The overall <code>DateRange</code> of the report. This may be <code>null</code>, for example if generation is not
* occuring in the context of an individual element.
* @return the effective <code>Filter</code>
*/
protected Filter resolveEffectiveFilter(T element, Filter inheritedFilter, DateRange dateRange) {
Filter filter;
if (inheritedFilter != null) {
filter = new Filter(element.getFilter(), inheritedFilter);
} else {
filter = new Filter(element.getFilter());
}
return resolveElementFilter(element, dateRange);
}
protected Filter resolveElementFilter(T element, DateRange dateRange) {
Filter filter = new Filter(element.getFilter());
if (dateRange != null) {
if (filter.getMinDate() == null) {
filter.setMinDate(dateRange.getMinDate());
}
if (filter.getMaxDate() == null) {
filter.setMaxDate(dateRange.getMaxDate());
}
}
return filter;
}
protected List<FilterDescription> generateFilterDescriptions(Filter filter, Set<DimensionType> excludeDims, User user) {
List<FilterDescription> list = new ArrayList<FilterDescription>();
Set<DimensionType> filterDims = filter.getRestrictedDimensions();
filterDims.removeAll(excludeDims);
for (DimensionType type : filterDims) {
list.add(new FilterDescription(
type,
pivotDAO.getFilterLabels(type, filter.getRestrictions(type))));
}
if (filter.getMinDate() != null || filter.getMaxDate() != null) {
DateRangeFormat format = new DateRangeFormat(user.getLocaleInstance());
list.add(new FilterDescription(
DimensionType.Date,
format.format(filter.getMinDate(), filter.getMaxDate())));
}
return list;
}
/**
* Resolves a templated string using the supplied parameters. Parameters can be referenced
* within the template as ${DATE_RANGE}. Case sensitive.
*
* @param template The file name template
* @param range The date range of the report
* @param user The user for whom the report will be generated (required for the locale)
* @return The resolved file name
* @see Report#getFileName()
*/
protected String resolveTemplate(String template, DateRange range, User user) {
if (template.contains("${DATE_RANGE}")) {
DateRangeFormat format = new DateRangeFormat(user.getLocaleInstance());
String rangeText = format.format(range);
return template.replace("${DATE_RANGE}", rangeText);
} else {
return template;
}
}
}