package org.activityinfo.server.report.generator;
/*
* #%L
* ActivityInfo Server
* %%
* Copyright (C) 2009 - 2013 UNICEF
* %%
* 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.activityinfo.legacy.shared.command.DimensionType;
import org.activityinfo.legacy.shared.command.Filter;
import org.activityinfo.legacy.shared.command.GetDimensionLabels;
import org.activityinfo.legacy.shared.command.GetDimensionLabels.DimensionLabels;
import org.activityinfo.legacy.shared.reports.content.FilterDescription;
import org.activityinfo.legacy.shared.reports.model.DateRange;
import org.activityinfo.legacy.shared.reports.model.ReportElement;
import org.activityinfo.server.command.DispatcherSync;
import org.activityinfo.server.database.hibernate.entity.User;
import org.activityinfo.server.util.date.DateFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* 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> {
protected final DispatcherSync dispatcher;
@Inject
public BaseGenerator(DispatcherSync dispatcher) {
this.dispatcher = dispatcher;
}
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) {
DimensionLabels labels = dispatcher.execute(new GetDimensionLabels(type, filter.getRestrictions(type)));
list.add(new FilterDescription(type, labels.getLabels()));
}
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 org.activityinfo.legacy.shared.reports.model.Report#getFileName()
*/
protected String resolveTemplate(String template, DateRange range, User user) {
if (template.indexOf("${DATE_RANGE}") != -1) {
DateFormatter format = new DateFormatter(user.getLocaleObject());
String rangeText = format.format(range);
return template.replace("${DATE_RANGE}", rangeText);
} else {
return template;
}
}
protected final DispatcherSync getDispatcher() {
return dispatcher;
}
}