@Override public void reprocessArchivedForm(String argument,boolean isPath) throws Exception { SerializableFormObject formObject; if(isPath) { formObject = SerializableFormObject.deserializeXml(argument); } else { formObject = SerializableFormObject.deserializeXml(argument,false); } formObject.handleSubmission(); //Save data to database HtmlFormEntryUtil.getService().applyActions(formObject.getSession()); }
public FormEntrySession getSession() throws Exception{ if(session==null)createSession(); return session; }
/** * * @return */ private HttpServletRequest createHttpServletRequest() throws Exception{ if(getParameterMap()==null) { throw new Exception("Could not create Request without parameters"); } HttpServletRequest request = new InnerHttpServletRequestMock(getParameterMap()); return request; }
@Test @Verifies(value = "Should save archived form to the database", method = "reprocessArchivedForm") public void reprocessArchivedForm_shouldProcessForm() throws Exception { EncounterService encService = Context.getEncounterService(); String path = OpenmrsClassLoader.getInstance().getResource("archivedFormData.xml").getPath(); System.out.println("Path: "+path); //Get the SerializableFormObject SerializableFormObject formObject = SerializableFormObject.deserializeXml(path); Assert.assertEquals("da7f524f-27ce-4bb2-86d6-6d1d05312bd5",formObject.getPatientUuid()); Patient patient = Context.getPatientService().getPatientByUuid("da7f524f-27ce-4bb2-86d6-6d1d05312bd5"); int noEnc = encService.getEncountersByPatient(patient).size(); service.reprocessArchivedForm(path); int newNoEnc = encService.getEncountersByPatient(patient).size(); Assert.assertEquals(noEnc+1,newNoEnc); }
/** * This method returns a FormEntrySession object using data in the instance object * @return FormEntrySession object * @throws Exception */ private void createSession() throws Exception { //TODO: Check for null patientUuid and try to parse the xml to obtain the patient ID instead. Patient patient = Context.getPatientService().getPatientByUuid(getPatientUuid()); session = new FormEntrySession(patient,getXmlDefinition(),null); HtmlForm htmlForm = HtmlFormEntryUtil.getService().getHtmlForm(htmlFormId); htmlForm.setXmlData(xmlDefinition); session.setHtmlForm(htmlForm); //getHtmlToDisplay() is called to generate necessary tag handlers and cache the form session.getHtmlToDisplay(); //PrepareForSubmit is called to set patient and encounter if specified in tags session.prepareForSubmit(); }
public static SerializableFormObject deserializeXml(String path) throws Exception { return deserializeXml(path,true); }
public void handleSubmission() throws Exception{ //Get the FormEntrySession & HttpServletRequest if(session==null)createSession(); HttpServletRequest request = createHttpServletRequest(); Map<Widget,String> fields = session.getContext().getFieldNames(); //handle submission & save data session.getSubmissionController().handleFormSubmission(session, request); }
/** * Given argument of the file this method tries to deserialize the contents of the file into SerializableFormObject * @param argument used to pass either a file path or a string representing the archivedData * @param isPath used to indicate whether the first argument is file path (true means it is) * @return equivalent SerializableFormObject representation * @throws Exception */ public static SerializableFormObject deserializeXml(String argument,boolean isPath) throws Exception { if(isPath) { //TODO:Check for existence of the file BufferedReader br = null; try { br = new BufferedReader(new FileReader(argument)); char[] buffer = new char[1024]; StringBuilder xmlSb = new StringBuilder(); int lengthRead; while ((lengthRead = br.read(buffer)) != -1) { xmlSb.append(buffer, 0, lengthRead); } return deserializeXmlString(xmlSb.toString()); } finally { if (br != null) br.close(); } } else { return deserializeXmlString(argument); } }
/** * * @param directoryPath * @throws Exception */ public void serializeToXml(String directoryPath) throws Exception { String filename; if(directoryPath.endsWith(File.separator)) { filename = directoryPath.concat(getFileName()); } else { filename = directoryPath.concat(File.separator + getFileName()); } //Use OpenMRS simpleXStreamSerializer OpenmrsSerializer serializer = Context.getSerializationService().getSerializer(SimpleXStreamSerializer.class); String xmlEquivalent = serializer.serialize(this); BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter(filename+".xml")); bw.write(xmlEquivalent); }finally { if(bw != null) bw.close(); } } /**
SerializableFormObject formObject = null; if (patient != null && encounter != null) { formObject = new SerializableFormObject(session.getXmlDefinition(), submission.getParameterMap(), patient.getPatientIdentifier().getIdentifier(), patient.getUuid(), encounter.getUuid(),session.getHtmlFormId()); formObject = new SerializableFormObject(session.getXmlDefinition(), submission.getParameterMap(), session.getHtmlFormId());