Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mob 24 b: changes to aid running of groovy from java #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.openmrs.module.amrsmobileforms;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
import org.openmrs.module.amrsmobileforms.groovy.CleanPatientIdentifiers;
import org.openmrs.module.amrsmobileforms.util.MobileFormEntryUtil;
import org.openmrs.scheduler.tasks.AbstractTask;

/**
* A task to clean up patient identifier prior to processing.
*
*
*/
public class DeleteSpacesInPatiendIdTask extends AbstractTask {

private static Log log = LogFactory.getLog(DeleteSpacesInPatiendIdTask.class);
String filePath = MobileFormEntryUtil.getMobileFormsDropDir().getAbsolutePath();
private CleanPatientIdentifiers cleanPatientIdentifiers;

public CleanPatientIdentifiers getCleanPatientIdentifiers() {
return cleanPatientIdentifiers;
}

public void setCleanPatientIdentifiers(CleanPatientIdentifiers cleanPatientIdentifiers) {
this.cleanPatientIdentifiers = cleanPatientIdentifiers;
}

/**
* Call Groovy class that cleans patient identifiers
*/
public void execute() {
Context.openSession();
log.debug("Running task that cleans Patient Identifier prior to form processing ");
try {
if (Context.isAuthenticated() == false)
authenticate();
//It would be good if we pass the full URL to path of the directory with forms to be processed
cleanPatientIdentifiers.getDirFileListing(filePath);
} catch (APIException e) {
log.error("Error running mobile forms relationships task", e);
throw e;
} finally {
Context.closeSession();
}
}

/*
* Resources clean up
*/
public void shutdown() {
super.shutdown();
log.debug("Shutting down task that cleans Patient Identifier");
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.openmrs.module.amrsmobileforms.groovy;

/**
* TODO:please provide a brief description for the class.
*/
public interface CleanPatientIdentifiers {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.openmrs.module.amrsmobileforms.groovy

import groovy.xml.StreamingMarkupBuilder

public class ParseXML{

def getDirFileListing(String pathToFolder){

new File(pathToFolder).eachFile() { file->

def fullFilePath = pathToFolder +"/" + file.getName()
def fileData = new XmlSlurper(false,false).parse(fullFilePath);

if(fileData==null){
//todo: handle blank files
}
else{


def individuals = fileData.household.individuals.individual

individuals.each{
String patient_number = it.patient."patient.medical_record_number"
patient_number = patient_number.toString()

if(patient_number){

patient_number.trim()

String newNum =patient_number.replaceAll("\\s","")

if(!patient_number.equals(newNum)){

it.patient."patient.medical_record_number".replaceBody(newNum)

def writable = new StreamingMarkupBuilder().bind { mkp.yield fileData }
writable.writeTo(new PrintWriter(new FileWriter(fullFilePath)))


}


}
else{

//todo:add code to perfom any task for such a scenario

}

}


}


}



}


}


Empty file.
Loading