|
1 | | -///** |
2 | | -// * This Source Code Form is subject to the terms of the Mozilla Public License, |
3 | | -// * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
4 | | -// * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under |
5 | | -// * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. |
6 | | -// * |
7 | | -// * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS |
8 | | -// * graphic logo is a trademark of OpenMRS Inc. |
9 | | -// */ |
10 | | -//package org.openmrs.module.reporting.dataset.definition.evaluator; |
11 | | -// |
12 | | -//import java.io.File; |
13 | | -//import java.util.HashMap; |
14 | | -//import java.util.Map; |
15 | | -// |
16 | | -//import org.apache.commons.logging.Log; |
17 | | -//import org.apache.commons.logging.LogFactory; |
18 | | -//import org.openmrs.annotation.Handler; |
19 | | -//import org.openmrs.module.reporting.common.ObjectUtil; |
20 | | -//import org.openmrs.module.reporting.dataset.DataSet; |
21 | | -//import org.openmrs.module.reporting.dataset.DataSetColumn; |
22 | | -//import org.openmrs.module.reporting.dataset.DataSetRow; |
23 | | -//import org.openmrs.module.reporting.dataset.SimpleDataSet; |
24 | | -//import org.openmrs.module.reporting.dataset.definition.DataExportDataSetDefinition; |
25 | | -//import org.openmrs.module.reporting.dataset.definition.DataSetDefinition; |
26 | | -//import org.openmrs.module.reporting.evaluation.EvaluationContext; |
27 | | -//import org.openmrs.reporting.export.DataExportReportObject; |
28 | | -//import org.openmrs.reporting.export.DataExportUtil; |
29 | | -//import org.openmrs.util.OpenmrsUtil; |
30 | | -// |
31 | | -///** |
32 | | -// * The logic that evaluates a {@link DataExportDataSetDefinition} and produces a {@link DataSet} |
33 | | -// * @see DataExportDataSetDefinition |
34 | | -// * @see DataSet |
35 | | -// */ |
36 | | -//@Handler(supports={DataExportDataSetDefinition.class}) |
37 | | -//@SuppressWarnings("deprecation") |
38 | | -//public class DataExportDataSetEvaluator implements DataSetEvaluator { |
39 | | -// |
40 | | -// private Log log = LogFactory.getLog(this.getClass()); |
41 | | -// |
42 | | -// /** |
43 | | -// * Public constructor |
44 | | -// */ |
45 | | -// public DataExportDataSetEvaluator() {} |
46 | | -// |
47 | | -// /** |
48 | | -// * @see DataSetEvaluator#evaluate(DataSetDefinition, EvaluationContext) |
49 | | -// * @should evaluate a DataExportDataSetDefinition |
50 | | -// */ |
51 | | -// public DataSet evaluate(DataSetDefinition definition, EvaluationContext context) { |
52 | | -// |
53 | | -// context = ObjectUtil.nvl(context, new EvaluationContext()); |
54 | | -// SimpleDataSet dataSet = new SimpleDataSet(definition, context); |
55 | | -// try { |
56 | | -// DataExportDataSetDefinition dataExportDefinition = (DataExportDataSetDefinition) definition; |
57 | | -// DataExportReportObject dataExport = dataExportDefinition.getDataExport(); |
58 | | -// DataExportUtil.generateExport(dataExport, context.getBaseCohort(), null); |
59 | | -// |
60 | | -// File dataFile = DataExportUtil.getGeneratedFile(dataExportDefinition.getDataExport()); |
61 | | -// |
62 | | -// // Get contents as a string |
63 | | -// // TODO Test whether this is faster than another approach |
64 | | -// String contents = OpenmrsUtil.getFileAsString(dataFile); |
65 | | -// String [] rows = contents.split("\\n"); |
66 | | -// |
67 | | -// // Get column names |
68 | | -// String [] columns = rows[0].split("\\t"); |
69 | | -// Map<String, DataSetColumn> cols = new HashMap<String, DataSetColumn>(); |
70 | | -// for (String s : columns) { |
71 | | -// DataSetColumn c = new DataSetColumn(s, s, String.class); |
72 | | -// cols.put(s, c); |
73 | | -// dataSet.getMetaData().addColumn(c); |
74 | | -// } |
75 | | -// |
76 | | -// // Iterate over remaining rows |
77 | | -// for (int i=1; i<rows.length;i++) { |
78 | | -// DataSetRow row = new DataSetRow(); |
79 | | -// String [] cells = rows[i].split("\\t"); |
80 | | -// for (int j=0; j<cells.length; j++) { |
81 | | -// row.addColumnValue(cols.get(columns[j]), cells[j]); |
82 | | -// } |
83 | | -// dataSet.addRow(row); |
84 | | -// } |
85 | | -// } |
86 | | -// catch (Exception e) { |
87 | | -// log.error("An error occurred while generating a data export.", e); |
88 | | -// throw new RuntimeException("An error occurred while generating a data export.", e); |
89 | | -// } |
90 | | -// return dataSet; |
91 | | -// } |
92 | | -//} |
| 1 | +/** |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 3 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 4 | + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under |
| 5 | + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. |
| 6 | + * |
| 7 | + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS |
| 8 | + * graphic logo is a trademark of OpenMRS Inc. |
| 9 | + */ |
| 10 | +package org.openmrs.module.reporting.dataset.definition.evaluator; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import org.apache.commons.logging.Log; |
| 17 | +import org.apache.commons.logging.LogFactory; |
| 18 | +import org.openmrs.annotation.Handler; |
| 19 | +import org.openmrs.module.reporting.common.ObjectUtil; |
| 20 | +import org.openmrs.module.reporting.dataset.DataSet; |
| 21 | +import org.openmrs.module.reporting.dataset.DataSetColumn; |
| 22 | +import org.openmrs.module.reporting.dataset.DataSetRow; |
| 23 | +import org.openmrs.module.reporting.dataset.SimpleDataSet; |
| 24 | +import org.openmrs.module.reporting.dataset.definition.DataExportDataSetDefinition; |
| 25 | +import org.openmrs.module.reporting.dataset.definition.DataSetDefinition; |
| 26 | +import org.openmrs.module.reporting.evaluation.EvaluationContext; |
| 27 | +import org.openmrs.reporting.export.DataExportReportObject; |
| 28 | +import org.openmrs.reporting.export.DataExportUtil; |
| 29 | +import org.openmrs.util.OpenmrsUtil; |
| 30 | + |
| 31 | +/** |
| 32 | + * The logic that evaluates a {@link DataExportDataSetDefinition} and produces a {@link DataSet} |
| 33 | + * @see DataExportDataSetDefinition |
| 34 | + * @see DataSet |
| 35 | + */ |
| 36 | +@Handler(supports={DataExportDataSetDefinition.class}) |
| 37 | +@SuppressWarnings("deprecation") |
| 38 | +public class DataExportDataSetEvaluator implements DataSetEvaluator { |
| 39 | + |
| 40 | + private Log log = LogFactory.getLog(this.getClass()); |
| 41 | + |
| 42 | + /** |
| 43 | + * Public constructor |
| 44 | + */ |
| 45 | + public DataExportDataSetEvaluator() {} |
| 46 | + |
| 47 | + /** |
| 48 | + * @see DataSetEvaluator#evaluate(DataSetDefinition, EvaluationContext) |
| 49 | + * @should evaluate a DataExportDataSetDefinition |
| 50 | + */ |
| 51 | + public DataSet evaluate(DataSetDefinition definition, EvaluationContext context) { |
| 52 | + |
| 53 | + context = ObjectUtil.nvl(context, new EvaluationContext()); |
| 54 | + SimpleDataSet dataSet = new SimpleDataSet(definition, context); |
| 55 | + try { |
| 56 | + DataExportDataSetDefinition dataExportDefinition = (DataExportDataSetDefinition) definition; |
| 57 | + DataExportReportObject dataExport = dataExportDefinition.getDataExport(); |
| 58 | + DataExportUtil.generateExport(dataExport, convertCohort(context.getBaseCohort()), null); |
| 59 | + |
| 60 | + File dataFile = DataExportUtil.getGeneratedFile(dataExportDefinition.getDataExport()); |
| 61 | + |
| 62 | + // Get contents as a string |
| 63 | + // TODO Test whether this is faster than another approach |
| 64 | + String contents = OpenmrsUtil.getFileAsString(dataFile); |
| 65 | + String [] rows = contents.split("\\n"); |
| 66 | + |
| 67 | + // Get column names |
| 68 | + String [] columns = rows[0].split("\\t"); |
| 69 | + Map<String, DataSetColumn> cols = new HashMap<String, DataSetColumn>(); |
| 70 | + for (String s : columns) { |
| 71 | + DataSetColumn c = new DataSetColumn(s, s, String.class); |
| 72 | + cols.put(s, c); |
| 73 | + dataSet.getMetaData().addColumn(c); |
| 74 | + } |
| 75 | + |
| 76 | + // Iterate over remaining rows |
| 77 | + for (int i=1; i<rows.length;i++) { |
| 78 | + DataSetRow row = new DataSetRow(); |
| 79 | + String [] cells = rows[i].split("\\t"); |
| 80 | + for (int j=0; j<cells.length; j++) { |
| 81 | + row.addColumnValue(cols.get(columns[j]), cells[j]); |
| 82 | + } |
| 83 | + dataSet.addRow(row); |
| 84 | + } |
| 85 | + } |
| 86 | + catch (Exception e) { |
| 87 | + log.error("An error occurred while generating a data export.", e); |
| 88 | + throw new RuntimeException("An error occurred while generating a data export.", e); |
| 89 | + } |
| 90 | + return dataSet; |
| 91 | + } |
| 92 | + |
| 93 | + private org.openmrs.cohort.Cohort convertCohort(org.openmrs.Cohort cohort) { |
| 94 | + org.openmrs.cohort.Cohort c = new org.openmrs.cohort.Cohort(); |
| 95 | + c.setCohortId(cohort.getId()); |
| 96 | + c.setName(cohort.getName()); |
| 97 | + c.setDescription(cohort.getDescription()); |
| 98 | + c.setMemberIds(cohort.getMemberIds()); |
| 99 | + |
| 100 | + c.setUuid(cohort.getUuid()); |
| 101 | + c.setCreator(cohort.getCreator()); |
| 102 | + c.setDateCreated(cohort.getDateCreated()); |
| 103 | + c.setChangedBy(cohort.getChangedBy()); |
| 104 | + c.setDateChanged(cohort.getDateChanged()); |
| 105 | + c.setVoided(cohort.getVoided()); |
| 106 | + c.setVoidedBy(cohort.getVoidedBy()); |
| 107 | + c.setDateVoided(cohort.getDateVoided()); |
| 108 | + c.setVoidReason(cohort.getVoidReason()); |
| 109 | + |
| 110 | + return c; |
| 111 | + } |
| 112 | +} |
0 commit comments