Skip to content

Commit f76e2af

Browse files
committed
Add the removed files
1 parent 2466738 commit f76e2af

File tree

9 files changed

+1273
-0
lines changed

9 files changed

+1273
-0
lines changed

api-1.10/src/main/java/org/openmrs/module/reporting/cohort/definition/evaluator/DrugOrderCohortDefinitionEvaluator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package org.openmrs.module.reporting.cohort.definition.evaluator;
1212

13+
import org.apache.commons.lang3.time.DateUtils;
1314
import org.openmrs.Cohort;
1415
import org.openmrs.DrugOrder;
1516
import org.openmrs.module.reporting.cohort.definition.CohortDefinition;
@@ -24,6 +25,7 @@
2425

2526
import org.springframework.beans.factory.annotation.Autowired;
2627
import java.util.List;
28+
import java.util.Date;
2729

2830
@Handler(supports = { DrugOrderCohortDefinition.class })
2931
public class DrugOrderCohortDefinitionEvaluator implements CohortDefinitionEvaluator {
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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.report.service.db;
11+
12+
import java.io.Serializable;
13+
import java.sql.PreparedStatement;
14+
import java.sql.ResultSet;
15+
import java.sql.SQLException;
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
import java.util.Properties;
19+
20+
import org.apache.commons.lang.StringUtils;
21+
import org.hibernate.Hibernate;
22+
import org.hibernate.HibernateException;
23+
import org.hibernate.engine.SessionImplementor;
24+
import org.hibernate.type.Type;
25+
import org.hibernate.usertype.CompositeUserType;
26+
import org.hibernate.usertype.ParameterizedType;
27+
import org.hibernate.usertype.UserType;
28+
import org.openmrs.api.context.Context;
29+
import org.openmrs.module.reporting.common.HibernateUtil;
30+
import org.openmrs.module.reporting.definition.DefinitionContext;
31+
import org.openmrs.module.reporting.evaluation.Definition;
32+
import org.openmrs.module.reporting.evaluation.parameter.Mapped;
33+
import org.openmrs.module.reporting.evaluation.parameter.Parameterizable;
34+
import org.openmrs.module.reporting.serializer.ReportingSerializer;
35+
36+
/**
37+
* Custom User-Type for storing Mapped objects in a single table within 2 columns
38+
* This type takes in 2 properties and 1 parameter in the form:
39+
* <pre>
40+
* <property name="reportDefinition">
41+
* <column name="report_definition_uuid"/>
42+
* <column name="report_definition_parameters"/>
43+
* <type name="org.openmrs.module.reporting.report.service.db.MappedDefinitionType">
44+
* <param name="mappedType">org.openmrs.module.reporting.report.definition.ReportDefinition</param>
45+
* </type>
46+
* </property>
47+
* </pre>
48+
*/
49+
@SuppressWarnings({"rawtypes", "unchecked"})
50+
public class MappedDefinitionType implements CompositeUserType, ParameterizedType {
51+
52+
/**
53+
* Property via ParameterizedType for storing the type of the Mapped Parameterizable
54+
*/
55+
private Class<? extends Definition> mappedType;
56+
57+
/**
58+
* @see CompositeUserType#returnedClass()
59+
*/
60+
public Class returnedClass() {
61+
return Mapped.class;
62+
}
63+
64+
/**
65+
* @see CompositeUserType#getPropertyNames()
66+
*/
67+
public String[] getPropertyNames() {
68+
return new String[] {"definition", "parameterMappings"};
69+
}
70+
71+
/**
72+
* @see CompositeUserType#getPropertyTypes()
73+
*/
74+
public Type[] getPropertyTypes() {
75+
return new Type[] { HibernateUtil.standardType("STRING"), HibernateUtil.standardType("TEXT") };
76+
}
77+
78+
/**
79+
* @see CompositeUserType#isMutable()
80+
*/
81+
public boolean isMutable() {
82+
return true;
83+
}
84+
85+
/**
86+
* @see CompositeUserType#getPropertyValue(java.lang.Object, int)
87+
*/
88+
public Object getPropertyValue(Object component, int property) throws HibernateException {
89+
Mapped m = (Mapped) component;
90+
return (property == 0 ? m.getParameterizable() : m.getParameterMappings());
91+
}
92+
93+
/**
94+
* @see CompositeUserType#setPropertyValue(java.lang.Object, int, java.lang.Object)
95+
*/
96+
public void setPropertyValue(Object component, int property, Object value) throws HibernateException {
97+
Mapped m = (Mapped) component;
98+
if (property == 0) {
99+
m.setParameterizable((Parameterizable)value);
100+
}
101+
else {
102+
m.setParameterMappings((Map)value);
103+
}
104+
}
105+
106+
/**
107+
* @see CompositeUserType#deepCopy(java.lang.Object)
108+
*/
109+
public Object deepCopy(Object value) throws HibernateException {
110+
if (value == null) return null;
111+
Mapped toCopy = (Mapped) value;
112+
Mapped m = new Mapped();
113+
m.setParameterizable(toCopy.getParameterizable());
114+
m.setParameterMappings(new HashMap<String, Object>(toCopy.getParameterMappings()));
115+
return m;
116+
}
117+
118+
/**
119+
* @see CompositeUserType#nullSafeGet(ResultSet, String[], SessionImplementor, Object)
120+
*/
121+
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
122+
String parameterizableUuid = (String) HibernateUtil.standardType("STRING").nullSafeGet(rs, names[0], session, owner);
123+
if (StringUtils.isEmpty(parameterizableUuid)) { return null; }
124+
String serializedMappings = (String) HibernateUtil.standardType("STRING").nullSafeGet(rs, names[1], session, owner);
125+
Definition d = DefinitionContext.getDefinitionByUuid(mappedType, parameterizableUuid);
126+
Map<String, Object> mappings = new HashMap<String, Object>();
127+
if (StringUtils.isNotBlank(serializedMappings)) {
128+
try {
129+
mappings = Context.getSerializationService().deserialize(serializedMappings, Map.class, ReportingSerializer.class);
130+
}
131+
catch (Exception e) {
132+
throw new HibernateException("Unable to deserialize parameter mappings for definition", e);
133+
}
134+
}
135+
return new Mapped(d, mappings);
136+
}
137+
138+
/**
139+
* @see CompositeUserType#nullSafeSet(PreparedStatement, Object, int, SessionImplementor)
140+
*/
141+
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
142+
String definitionUuid = null;
143+
String serializedMappings = null;
144+
if (value != null) {
145+
Mapped m = (Mapped) value;
146+
if (m.getParameterizable() != null) {
147+
definitionUuid = m.getParameterizable().getUuid();
148+
if (m.getParameterMappings() != null && !m.getParameterMappings().isEmpty()) {
149+
try {
150+
serializedMappings = Context.getSerializationService().serialize(m.getParameterMappings(), ReportingSerializer.class);
151+
}
152+
catch (Exception e) {
153+
throw new HibernateException("Unable to serialize mappings for definition", e);
154+
}
155+
}
156+
}
157+
}
158+
HibernateUtil.standardType("STRING").nullSafeSet(st, definitionUuid, index, session);
159+
HibernateUtil.standardType("STRING").nullSafeSet(st, serializedMappings, index+1, session);
160+
}
161+
162+
/**
163+
* @see CompositeUserType#replace(Object, Object, SessionImplementor, Object)
164+
*/
165+
public Object replace(Object original, Object target, SessionImplementor session, Object owner) throws HibernateException {
166+
return original;
167+
}
168+
169+
/**
170+
* @see UserType#equals(Object, Object)
171+
*/
172+
public boolean equals(Object x, Object y) throws HibernateException {
173+
return x != null && x.equals(y);
174+
}
175+
176+
/**
177+
* @see UserType#hashCode(Object)
178+
*/
179+
public int hashCode(Object x) throws HibernateException {
180+
return x.hashCode();
181+
}
182+
183+
/**
184+
* @see CompositeUserType#disassemble(Object, SessionImplementor)
185+
*/
186+
public Serializable disassemble(Object value, SessionImplementor session) throws HibernateException {
187+
return (Serializable) deepCopy(value);
188+
}
189+
190+
/**
191+
* @see CompositeUserType#assemble(Serializable, SessionImplementor, Object)
192+
*/
193+
public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException {
194+
return deepCopy(cached);
195+
}
196+
197+
/**
198+
* @see ParameterizedType#setParameterValues(Properties)
199+
*/
200+
public void setParameterValues(Properties parameters) {
201+
String mappedTypeStr = parameters.getProperty("mappedType");
202+
try {
203+
mappedType = (Class<? extends Definition>)Context.loadClass(mappedTypeStr);
204+
}
205+
catch (Exception e) {
206+
throw new HibernateException("Error setting the mappedType property to " + mappedTypeStr, e);
207+
}
208+
}
209+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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.report.service.db;
11+
12+
import org.hibernate.HibernateException;
13+
import org.hibernate.usertype.UserType;
14+
15+
import java.io.IOException;
16+
import java.io.Serializable;
17+
import java.io.StringReader;
18+
import java.io.StringWriter;
19+
import java.sql.PreparedStatement;
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
22+
import java.util.Map;
23+
import java.util.Properties;
24+
25+
import static java.sql.Types.VARCHAR;
26+
27+
/**
28+
* A report definition type
29+
*/
30+
public class PropertiesType implements UserType {
31+
32+
/**
33+
* @see UserType#assemble(Serializable, Object)
34+
*/
35+
public Object assemble(Serializable cached, Object owner) throws HibernateException {
36+
if (cached == null) {
37+
return null;
38+
}
39+
try {
40+
String s = (String) cached;
41+
Properties p = new Properties();
42+
p.load(new StringReader(s));
43+
return p;
44+
}
45+
catch (IOException e) {
46+
throw new IllegalArgumentException("Unable to load properties from string", e);
47+
}
48+
}
49+
50+
/**
51+
* @see UserType#deepCopy(Object)
52+
*/
53+
public Object deepCopy(Object value) throws HibernateException {
54+
if (value != null) {
55+
Properties val = (Properties) value;
56+
Properties copy = new Properties();
57+
for ( Map.Entry<Object, Object> e : val.entrySet() ) {
58+
copy.setProperty((String) e.getKey(), (String) e.getValue());
59+
}
60+
return copy;
61+
} else {
62+
return null;
63+
}
64+
}
65+
66+
/**
67+
* @see UserType#disassemble(Object)
68+
*/
69+
public Serializable disassemble(Object value) throws HibernateException {
70+
if (value == null) {
71+
return null;
72+
}
73+
try {
74+
Properties props = (Properties) value;
75+
StringWriter sw = new StringWriter();
76+
props.store(sw, null);
77+
return sw.toString();
78+
}
79+
catch (IOException e) {
80+
throw new IllegalArgumentException("Unable to store properties as string", e);
81+
}
82+
}
83+
84+
/**
85+
* @see UserType#equals(Object, Object)
86+
*/
87+
public boolean equals(Object x, Object y) throws HibernateException {
88+
return x != null && x.equals(y);
89+
}
90+
91+
/**
92+
* @see UserType#hashCode(Object)
93+
*/
94+
public int hashCode(Object x) throws HibernateException {
95+
return x.hashCode();
96+
}
97+
98+
/**
99+
* @see UserType#isMutable()
100+
*/
101+
public boolean isMutable() {
102+
return true;
103+
}
104+
105+
/**
106+
* @see UserType#nullSafeGet(ResultSet, String[], Object)
107+
*/
108+
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
109+
String s = rs.getString(names[0]);
110+
return assemble(s, null);
111+
}
112+
113+
/**
114+
* @see UserType#nullSafeSet(PreparedStatement, Object, int)
115+
*/
116+
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
117+
String val = (String) disassemble(value);
118+
st.setString(index, val);
119+
}
120+
121+
/**
122+
* @see UserType#replace(Object, Object, Object)
123+
*/
124+
public Object replace(Object original, Object target, Object owner) throws HibernateException {
125+
return original;
126+
}
127+
128+
/**
129+
* @see UserType#returnedClass()
130+
*/
131+
@SuppressWarnings("unchecked")
132+
public Class returnedClass() {
133+
return Properties.class;
134+
}
135+
136+
/**
137+
* @see UserType#sqlTypes()
138+
*/
139+
public int[] sqlTypes() {
140+
return new int[] { VARCHAR };
141+
}
142+
}

0 commit comments

Comments
 (0)