Changeset 4171 for openmrs-modules/xforms/trunk/src/org/openmrs/module/xforms/DefaultPatientSerializer.java
- Timestamp:
- 05/12/08 07:34:55 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openmrs-modules/xforms/trunk/src/org/openmrs/module/xforms/DefaultPatientSerializer.java
r3055 r4171 1 1 package org.openmrs.module.xforms; 2 3 2 4 3 import java.io.DataInputStream; 5 4 import java.io.DataOutputStream; 6 5 import java.io.IOException; 7 import java.util. *;6 import java.util.List; 8 7 9 8 import org.apache.commons.logging.Log; … … 15 14 16 15 /** 17 * Provides default serialization of patients to clients. 18 * This class is used when sending a list of patients to clients like19 * mobile devices that may wantto collect patient data in for instance offline mode.16 * Provides default serialization of patients to clients. This class is used 17 * when sending a list of patients to clients like mobile devices that may want 18 * to collect patient data in for instance offline mode. 20 19 * 21 * For those who want a different serialization format for patients, 22 * just implement the SerializableData interface and specify the class 23 * using the openmrs global property {xforms.patientSerializer}. 24 * The jar containing this class can then be 25 * put under the webapps/openmrs/web-inf/lib folder. 26 * One of the reasons one could want a different serialization format 27 * is for performance by doing a more optimized and compact format. 28 * Onother reason i can foresee is for non java clients who may say 29 * want the patients in xml or any other format, because the default 30 * implementation assumes java clients. 20 * For those who want a different serialization format for patients, just 21 * implement the SerializableData interface and specify the class using the 22 * openmrs global property {xforms.patientSerializer}. The jar containing this 23 * class can then be put under the webapps/openmrs/web-inf/lib folder. One of 24 * the reasons one could want a different serialization format is for 25 * performance by doing a more optimized and compact format. Onother reason i 26 * can foresee is for non java clients who may say want the patients in xml or 27 * any other format, because the default implementation assumes java clients. 31 28 * 32 29 * @author Daniel 33 * 30 * 34 31 */ 35 public class DefaultPatientSerializer implements SerializableData {32 public class DefaultPatientSerializer implements SerializableData { 36 33 37 34 private Log log = LogFactory.getLog(this.getClass()); 38 39 40 public DefaultPatientSerializer(){ 41 35 36 public DefaultPatientSerializer() { 37 42 38 } 43 39 … … 45 41 * Serializes a patient to the stream. 46 42 * 47 * @param patient - the patient to serialize. 48 * @param dos - the stream to write to. 43 * @param patient - 44 * the patient to serialize. 45 * @param dos - 46 * the stream to write to. 49 47 */ 50 protected void serialize(Patient patient, DataOutputStream dos) {51 try {48 protected void serialize(Patient patient, DataOutputStream dos) { 49 try { 52 50 SerializationUtils.writeInteger(dos, patient.getPatientId()); 53 51 54 52 PersonName personName = patient.getPersonName(); 55 53 SerializationUtils.writeUTF(dos, personName.getPrefix()); … … 59 57 SerializationUtils.writeUTF(dos, patient.getGender()); 60 58 SerializationUtils.writeDate(dos, patient.getBirthdate()); 61 62 if(patient.getPatientIdentifier() != null) 63 SerializationUtils.writeUTF(dos, patient.getPatientIdentifier().toString()); 59 60 if (patient.getPatientIdentifier() != null) 61 SerializationUtils.writeUTF(dos, patient.getPatientIdentifier() 62 .toString()); 64 63 else 65 64 dos.writeBoolean(false); 66 67 dos.writeBoolean(false); // flag to tell whether this is a new patient or not.68 }69 catch(IOException e){65 66 dos.writeBoolean(false); // flag to tell whether this is a new 67 // patient or not. 68 } catch (IOException e) { 70 69 log.error(e); 71 70 } 72 71 } 73 72 74 73 /** 75 * @see org.openmrs.module.xforms.SerializableData#serialize(java.io.DataOutputStream, java.lang.Object) 74 * @see org.openmrs.module.xforms.SerializableData#serialize(java.io.DataOutputStream, 75 * java.lang.Object) 76 76 */ 77 public void serialize(DataOutputStream dos, Object data){78 try {79 if (data == null)77 public void serialize(DataOutputStream dos, Object data) { 78 try { 79 if (data == null) 80 80 return; 81 PatientData patientData = (PatientData)data; //data will always be a patient data. 82 83 List<Patient> patients = patientData.getPatients(); 84 if(patients == null || patients.size() == 0) 81 PatientData patientData = (PatientData) data; // data will always 82 // be a patient 83 // data. 84 85 List<Patient> patients = patientData.getPatients(); 86 if (patients == null || patients.size() == 0) 85 87 return; 86 88 87 89 dos.writeInt(patients.size()); 88 for (Patient patient : patients)89 serialize(patient, dos);90 91 List<PatientTableField> fields = patientData.getFields(); 92 if (fields == null || fields.size() == 0)90 for (Patient patient : patients) 91 serialize(patient, dos); 92 93 List<PatientTableField> fields = patientData.getFields(); 94 if (fields == null || fields.size() == 0) 93 95 return; 94 96 dos.writeByte(fields.size()); 95 for (PatientTableField field : fields){97 for (PatientTableField field : fields) { 96 98 dos.writeInt(field.getId()); 97 99 dos.writeUTF(field.getName()); 98 100 } 99 100 List<PatientTableFieldValue> fieldVals = patientData.getFieldValues(); 101 if(fieldVals == null || fieldVals.size() == 0) 101 102 List<PatientTableFieldValue> fieldVals = patientData 103 .getFieldValues(); 104 if (fieldVals == null || fieldVals.size() == 0) 102 105 return; 103 106 dos.writeInt(fieldVals.size()); 104 for (PatientTableFieldValue fieldVal : fieldVals){107 for (PatientTableFieldValue fieldVal : fieldVals) { 105 108 dos.writeInt(fieldVal.getFieldId()); 106 109 dos.writeInt(fieldVal.getPatientId()); 107 110 dos.writeUTF(fieldVal.getValue().toString()); 108 111 } 109 110 } catch(IOException e){112 113 } catch (IOException e) { 111 114 log.error(e); 112 115 } 113 116 } 114 117 115 118 /** 116 * @see org.openmrs.module.xforms.SerializableData#deSerialize(java.io.DataInputStream, java.lang.Object) 119 * @see org.openmrs.module.xforms.SerializableData#deSerialize(java.io.DataInputStream, 120 * java.lang.Object) 117 121 */ 118 public Object deSerialize(DataInputStream dis,Object data){ 119 return null; //not necessary for now because patients come back as xforms. 122 public Object deSerialize(DataInputStream dis, Object data) { 123 return null; // not necessary for now because patients come back as 124 // xforms. 120 125 } 121 126 }