Skip to content

Commit c6a2252

Browse files
committed
Move api-1.9
1 parent adc7cc5 commit c6a2252

File tree

4 files changed

+345
-1
lines changed

4 files changed

+345
-1
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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;
11+
12+
import org.openmrs.BaseOpenmrsMetadata;
13+
import org.openmrs.BaseOpenmrsObject;
14+
import org.openmrs.module.reporting.common.Localized;
15+
import org.openmrs.module.reporting.definition.configuration.ConfigurationProperty;
16+
import org.openmrs.module.reporting.definition.configuration.ConfigurationPropertyCachingStrategy;
17+
import org.openmrs.module.reporting.evaluation.caching.Caching;
18+
import org.openmrs.reporting.export.DataExportReportObject;
19+
20+
/**
21+
* Definition of a dataset that runs a Data Export (of the sort created in the reportingcompatibility
22+
* module.
23+
* @see DataExportDataSetDefinition
24+
*/
25+
@Caching(strategy=ConfigurationPropertyCachingStrategy.class)
26+
@SuppressWarnings("deprecation")
27+
@Localized("reporting.DataExportDataSetDefinition")
28+
public class DataExportDataSetDefinition extends BaseDataSetDefinition {
29+
30+
public static final long serialVersionUID = -2572061676651616176L;
31+
32+
//***** PROPERTIES *****
33+
34+
@ConfigurationProperty
35+
private DataExportReportObject dataExport;
36+
37+
//***** CONSTRUCTORS *****
38+
39+
/**
40+
* Default public constructor
41+
*/
42+
public DataExportDataSetDefinition() {
43+
super();
44+
}
45+
46+
/**
47+
* Full constructor
48+
*/
49+
public DataExportDataSetDefinition(DataExportReportObject dataExport) {
50+
this.dataExport = dataExport;
51+
}
52+
53+
//***** INSTANCE METHODS *****
54+
55+
/**
56+
* @see BaseDataSetDefinition#getId()
57+
*/
58+
@Override
59+
public Integer getId() {
60+
return (getDataExport() == null ? null : getDataExport().getId());
61+
}
62+
63+
/**
64+
* @see BaseOpenmrsObject#getUuid()
65+
*/
66+
@Override
67+
public String getUuid() {
68+
return (getDataExport() == null ? null : getDataExport().getUuid());
69+
}
70+
71+
/**
72+
* @see BaseOpenmrsMetadata#getName()
73+
*/
74+
@Override
75+
public String getName() {
76+
return (getDataExport() == null ? null : getDataExport().getName());
77+
}
78+
79+
/**
80+
* @see BaseOpenmrsMetadata#getDescription()
81+
*/
82+
@Override
83+
public String getDescription() {
84+
return (getDataExport() == null ? null : getDataExport().getDescription());
85+
}
86+
87+
//***** PROPERTY ACCESS *****
88+
89+
/**
90+
* @return the dataExport
91+
*/
92+
public DataExportReportObject getDataExport() {
93+
return dataExport;
94+
}
95+
96+
/**
97+
* @param dataExport the dataExport to set
98+
*/
99+
public void setDataExport(DataExportReportObject dataExport) {
100+
this.dataExport = dataExport;
101+
}
102+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 org.apache.commons.logging.Log;
13+
import org.apache.commons.logging.LogFactory;
14+
import org.openmrs.annotation.Handler;
15+
import org.openmrs.module.reporting.common.ObjectUtil;
16+
import org.openmrs.module.reporting.dataset.DataSet;
17+
import org.openmrs.module.reporting.dataset.DataSetColumn;
18+
import org.openmrs.module.reporting.dataset.DataSetRow;
19+
import org.openmrs.module.reporting.dataset.SimpleDataSet;
20+
import org.openmrs.module.reporting.dataset.definition.DataExportDataSetDefinition;
21+
import org.openmrs.module.reporting.dataset.definition.DataSetDefinition;
22+
import org.openmrs.module.reporting.evaluation.EvaluationContext;
23+
import org.openmrs.reporting.export.DataExportReportObject;
24+
import org.openmrs.reporting.export.DataExportUtil;
25+
import org.openmrs.util.OpenmrsUtil;
26+
27+
import java.io.File;
28+
import java.util.HashMap;
29+
import java.util.Map;
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+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.persister;
11+
12+
import org.openmrs.annotation.Handler;
13+
import org.openmrs.api.context.Context;
14+
import org.openmrs.module.ModuleFactory;
15+
import org.openmrs.module.reporting.ReportingConstants;
16+
import org.openmrs.module.reporting.dataset.definition.DataExportDataSetDefinition;
17+
import org.openmrs.module.reporting.dataset.definition.DataSetDefinition;
18+
import org.openmrs.reporting.AbstractReportObject;
19+
import org.openmrs.reporting.ReportObjectService;
20+
import org.openmrs.reporting.export.DataExportReportObject;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Vector;
25+
26+
/**
27+
* Class which manages persistence of a DataExportDataSetDefinition using legacy tables
28+
*/
29+
@Handler(supports={DataExportDataSetDefinition.class}, order=50)
30+
@SuppressWarnings("deprecation")
31+
public class DataExportDataSetDefinitionPersister implements DataSetDefinitionPersister {
32+
33+
/**
34+
* Public constructor
35+
*/
36+
public DataExportDataSetDefinitionPersister() { }
37+
38+
/**
39+
* @see DataSetDefinitionPersister#getDefinition(Integer)
40+
*/
41+
public DataSetDefinition getDefinition(Integer id) {
42+
ReportObjectService ros = Context.getService(ReportObjectService.class);
43+
DataExportReportObject dataExport = (DataExportReportObject) ros.getReportObject(id);
44+
return (dataExport != null) ? new DataExportDataSetDefinition(dataExport) : null;
45+
}
46+
47+
/**
48+
* @see DataSetDefinitionPersister#getDefinitionByUuid(String)
49+
*/
50+
public DataSetDefinition getDefinitionByUuid(String uuid) {
51+
for(DataSetDefinition dsd : getAllDefinitions(false)) { // NOTE: This is very slow. We could speed it up significantly with a custom dao
52+
if (dsd.getUuid() != null && dsd.getUuid().equals(uuid)) {
53+
return dsd;
54+
}
55+
}
56+
return null;
57+
}
58+
59+
/**
60+
* @see DataSetDefinitionPersister#getAllDefinitions(boolean)
61+
*/
62+
public List<DataSetDefinition> getAllDefinitions(boolean includeRetired) {
63+
List <DataSetDefinition> dataSetDefinitions = new Vector<DataSetDefinition>();
64+
if (ModuleFactory.getStartedModulesMap().containsKey("reportingcompatibility") &&
65+
ReportingConstants.GLOBAL_PROPERTY_INCLUDE_DATA_EXPORTS()) {
66+
ReportObjectService ros = Context.getService(ReportObjectService.class);
67+
List<AbstractReportObject> dataExports = ros.getReportObjectsByType("Data Export");
68+
69+
for (AbstractReportObject obj : dataExports) {
70+
DataExportReportObject dataExport = (DataExportReportObject) obj;
71+
dataExport.setUuid(obj.getUuid()); // hack to get uuids into data exports
72+
dataSetDefinitions.add(new DataExportDataSetDefinition(dataExport));
73+
}
74+
}
75+
return dataSetDefinitions;
76+
}
77+
78+
/**
79+
* @see DataSetDefinitionPersister#getNumberOfDefinitions(boolean)
80+
*/
81+
public int getNumberOfDefinitions(boolean includeRetired) {
82+
if (ModuleFactory.getStartedModulesMap().containsKey("reportingcompatibility")) {
83+
ReportObjectService ros = Context.getService(ReportObjectService.class);
84+
List<AbstractReportObject> dataExports = ros.getReportObjectsByType("Data Export");
85+
return dataExports.size();
86+
}
87+
return 0;
88+
}
89+
90+
/**
91+
* @see DataSetDefinitionPersister#getDefinitions(String, boolean)
92+
*/
93+
public List<DataSetDefinition> getDefinitions(String name, boolean exactMatchOnly) {
94+
List<DataSetDefinition> ret = new ArrayList<DataSetDefinition>();
95+
for(DataSetDefinition dsd : getAllDefinitions(false)) {
96+
if (dsd.getName() != null) {
97+
if (exactMatchOnly) {
98+
if (dsd.getName().equalsIgnoreCase(name)) {
99+
ret.add(dsd);
100+
}
101+
}
102+
else {
103+
if (dsd.getName().toUpperCase().contains(name.toUpperCase())) {
104+
ret.add(dsd);
105+
}
106+
}
107+
}
108+
}
109+
return ret;
110+
}
111+
112+
/**
113+
* @see DataSetDefinitionPersister#saveDefinition(DataSetDefinition)
114+
*/
115+
public DataSetDefinition saveDefinition(DataSetDefinition dataSetDefinition) {
116+
DataExportDataSetDefinition dsd = (DataExportDataSetDefinition) dataSetDefinition;
117+
ReportObjectService ros = Context.getService(ReportObjectService.class);
118+
DataExportReportObject dataExport = (DataExportReportObject) ros.saveReportObject(dsd.getDataExport());
119+
dsd.setDataExport(dataExport);
120+
return dsd;
121+
}
122+
123+
/**
124+
* @see DataSetDefinitionPersister#purgeDefinition(DataSetDefinition)
125+
*/
126+
public void purgeDefinition(DataSetDefinition dataSetDefinition) {
127+
DataExportDataSetDefinition dsd = (DataExportDataSetDefinition) dataSetDefinition;
128+
Context.getService(ReportObjectService.class).purgeReportObject(dsd.getDataExport());
129+
}
130+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
<modules>
3535
<module>api</module>
36-
<module>api-1.9</module>
36+
<!-- <module>api-1.9</module>-->
3737
<module>api-1.10</module>
3838
<module>api-2.0</module>
3939
<module>api-2.2</module>

0 commit comments

Comments
 (0)