class HibernateDHIS2ReportingDAO implements DHIS2ReportingDAO {

	@Override
	public String evaluateDataValueTemplate(DataValueTemplate dvt,
			Period period, Location location, boolean priority)
			throws DHIS2ReportingException {
		String queryString = dvt.getQuery();
		queryString = queryString.replaceAll("\t", " ");
		queryString = queryString.replaceAll("\n", " ");
		queryString = queryString.trim();
		String mappedUuid = dvt.getMappeddefinitionuuid();
		if (queryString == null || queryString.isEmpty()
				|| (priority && mappedUuid != null)) {
			org.openmrs.module.reporting.report.definition.ReportDefinition rrd = Context
					.getService(ReportDefinitionService.class)
					.getDefinitionByUuid(dvt.getMappeddefinitionuuid());
			try {
				return getReport(rrd, dvt.getMappeddefinitionlabel(), location,
						period).toString();
			} catch (EvaluationException ex) {
				log.debug("Evaluation Exception : " + ex.getMessage());
			}
		}

		if (dvt.potentialUpdateDelete()) {
			throw new DHIS2ReportingException(
					"Attempt to execute potential update/delete query for "
							+ dvt.getDataelement().getName() + " : "
							+ dvt.getDisaggregation().getName());
		}

		Query query = getCurrentSession().createSQLQuery(queryString);

		List<String> parameters = new ArrayList<String>(Arrays.asList(query
				.getNamedParameters()));
		// loactionId is optional
		if (parameters.contains("locationId")) {
			query.setParameter("locationId", location.getId().toString());
		}
		query.setParameter("startOfPeriod", period.getStartDate());
		query.setParameter("endOfPeriod", period.getEndDate());
		return query.uniqueResult().toString();
	}

}