Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register

Ticket #1605: PatientDataLoader.java

File PatientDataLoader.java, 8.0 kB (added by catechu, 8 months ago)

nribeka's original patient data loader program - for case (1)

Line 
1 import java.io.BufferedReader;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileReader;
6 import java.io.IOException;
7 import java.io.PrintStream;
8 import java.text.ParseException;
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.List;
12 import java.util.Properties;
13 import org.apache.log4j.Logger;
14 import org.openmrs.Location;
15 import org.openmrs.Patient;
16 import org.openmrs.PatientIdentifier;
17 import org.openmrs.PatientIdentifierType;
18 import org.openmrs.PersonAddress;
19 import org.openmrs.PersonAttribute;
20 import org.openmrs.PersonAttributeType;
21 import org.openmrs.PersonName;
22 import org.openmrs.Privilege;
23 import org.openmrs.User;
24 import org.openmrs.api.APIException;
25 import org.openmrs.api.LocationService;
26 import org.openmrs.api.PatientService;
27 import org.openmrs.api.PersonService;
28 import org.openmrs.api.context.Context;
29 import org.openmrs.util.OpenmrsUtil;
30
31 public class PatientDataLoader
32 {
33   protected static Logger logger = Logger.getLogger(PatientDataLoader.class);
34
35   public static void main(String[] args)
36   {
37     try
38     {
39       logger.info("Started on: " + new Date(System.currentTimeMillis()));
40
41       String filename = "openmrs-runtime.properties";
42       String filepath = OpenmrsUtil.getApplicationDataDirectory() + filename;
43
44       FileInputStream propertyStream = new FileInputStream(filepath);
45
46       Properties properties = new Properties();
47       properties.load(propertyStream);
48
49       Context.startup("jdbc:mysql://localhost:3306/openmrs?autoReconnect=true", args[1], args[2], properties);
50       Context.authenticate(args[3], args[4]);
51       User user = Context.getAuthenticatedUser();
52
53       for (Privilege privilege : user.getPrivileges()) {
54         Context.addProxyPrivilege(privilege.getPrivilege());
55       }
56
57       String[] attributes = { "Credit Card", "CVC Code", "Country", "Email", "Phone", "Mother's Name", "CC Exp", "UID", "SSN" };
58       String[] locations = { "LOCAL" };
59       String[] identifiers = { "UID" };
60
61       PatientService patientService = Context.getPatientService();
62       PersonService personService = Context.getPersonService();
63       LocationService locationService = Context.getLocationService();
64
65       for (String attribute : attributes) {
66         PersonAttributeType attributeType = personService.getPersonAttributeTypeByName(attribute);
67         if (attributeType == null) {
68           attributeType = new PersonAttributeType();
69           attributeType.setName(attribute);
70           attributeType.setFormat("java.lang.String");
71           attributeType.setDescription("Description of : " + attribute);
72         }
73         personService.savePersonAttributeType(attributeType);
74       }
75
76       for (String identifier : identifiers) {
77         PatientIdentifierType identifierType = patientService.getPatientIdentifierTypeByName(identifier);
78         if (identifierType == null) {
79           identifierType = new PatientIdentifierType();
80           identifierType.setName(identifier);
81           identifierType.setDescription("Description of : " + identifier);
82           identifierType.setCheckDigit(new Boolean(false));
83           identifierType.setRequired(new Boolean(false));
84         }
85         patientService.savePatientIdentifierType(identifierType);
86       }
87
88       for (String location : locations) {
89         Location locationName = locationService.getLocation(location);
90         if (locationName == null) {
91           locationName = new Location();
92           locationName.setName(location);
93         }
94         locationService.saveLocation(locationName);
95       }
96
97       PersonAttributeType attributeTypeCC = personService.getPersonAttributeTypeByName("Credit Card");
98       PersonAttributeType attributeTypeCVCCode = personService.getPersonAttributeTypeByName("CVC Code");
99       PersonAttributeType attributeTypeCountry = personService.getPersonAttributeTypeByName("Country");
100       PersonAttributeType attributeTypeEmail = personService.getPersonAttributeTypeByName("Email");
101       PersonAttributeType attributeTypePhone = personService.getPersonAttributeTypeByName("Phone");
102       PersonAttributeType attributeTypeMothersName = personService.getPersonAttributeTypeByName("Mother's Name");
103       PersonAttributeType attributeTypeCCExp = personService.getPersonAttributeTypeByName("CC Exp");
104       PersonAttributeType attributeTypeUID = personService.getPersonAttributeTypeByName("UID");
105       PersonAttributeType attributeTypeSSN = personService.getPersonAttributeTypeByName("SSN");
106
107       PatientIdentifierType identType = patientService.getPatientIdentifierTypeByName("UID");
108       Location patientLocation = locationService.getLocation("LOCAL");
109
110       int counter = patientService.getAllPatients().size();
111
112       SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
113
114       BufferedReader reader = new BufferedReader(new FileReader(new File(args[0])));
115       String s = null;
116       while ((s = reader.readLine()) != null) {
117         String[] element = s.split("[|]", -1);
118
119         Patient patient = new Patient();
120
121         PersonAddress address = new PersonAddress();
122         address.setCityVillage(element[6]);
123         address.setStateProvince(element[7]);
124         address.setAddress1(element[5]);
125         address.setAddress2("");
126         address.setPostalCode(element[8]);
127         patient.addAddress(address);
128
129         PersonName name = new PersonName();
130         name.setGivenName(element[2]);
131         name.setMiddleName(element[3]);
132         name.setFamilyName(element[4]);
133         patient.addName(name);
134
135         PersonAttribute attribute = new PersonAttribute();
136         attribute.setAttributeType(attributeTypeCC);
137         attribute.setValue(element[15]);
138         patient.addAttribute(attribute);
139
140         attribute = new PersonAttribute();
141         attribute.setAttributeType(attributeTypeCVCCode);
142         attribute.setValue(element[16]);
143         patient.addAttribute(attribute);
144
145         attribute = new PersonAttribute();
146         attribute.setAttributeType(attributeTypeCountry);
147         attribute.setValue(element[9]);
148         patient.addAttribute(attribute);
149
150         attribute = new PersonAttribute();
151         attribute.setAttributeType(attributeTypeEmail);
152         attribute.setValue(element[10]);
153         patient.addAttribute(attribute);
154
155         attribute = new PersonAttribute();
156         attribute.setAttributeType(attributeTypePhone);
157         attribute.setValue(element[11]);
158         patient.addAttribute(attribute);
159
160         attribute = new PersonAttribute();
161         attribute.setAttributeType(attributeTypeMothersName);
162         attribute.setValue(element[12]);
163         patient.addAttribute(attribute);
164
165         attribute = new PersonAttribute();
166         attribute.setAttributeType(attributeTypeCCExp);
167         attribute.setValue(element[17]);
168         patient.addAttribute(attribute);
169
170         attribute = new PersonAttribute();
171         attribute.setAttributeType(attributeTypeUID);
172         attribute.setValue(element[0]);
173         patient.addAttribute(attribute);
174
175         attribute = new PersonAttribute();
176         attribute.setAttributeType(attributeTypeSSN);
177         attribute.setValue(element[18]);
178         patient.addAttribute(attribute);
179
180         ++counter;
181         if (counter % 100 == 0) {
182           Context.clearSession();
183         }
184
185         PatientIdentifier patientIdentifier = new PatientIdentifier(
186           String.valueOf(counter), identType,
187           patientLocation);
188         patient.addIdentifier(patientIdentifier);
189
190         Date dateOfBirth = format.parse(element[13]);
191         patient.setBirthdate(dateOfBirth);
192         patient.setGender(element[1]);
193         patient.setCreator(user);
194         patientService.savePatient(patient);
195         System.out.println("Saved patient: " + patient.getGivenName() + " " + patient.getFamilyName());
196         logger.info("Ended on: " + new Date(System.currentTimeMillis()));
197       }
198     }
199     catch (APIException e)
200     {
201       e.printStackTrace();
202     }
203     catch (FileNotFoundException e) {
204       e.printStackTrace();
205     }
206     catch (IOException e) {
207       e.printStackTrace();
208     }
209     catch (ParseException e) {
210       e.printStackTrace();
211     }
212   }
213 }