diff --git a/api/src/main/java/org/openmrs/module/amrsmobileforms/DeleteSpacesInPatiendIdTask.java b/api/src/main/java/org/openmrs/module/amrsmobileforms/DeleteSpacesInPatiendIdTask.java new file mode 100644 index 0000000..f3e682c --- /dev/null +++ b/api/src/main/java/org/openmrs/module/amrsmobileforms/DeleteSpacesInPatiendIdTask.java @@ -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"); + } + + +} \ No newline at end of file diff --git a/api/src/main/java/org/openmrs/module/amrsmobileforms/groovy/CleanPatientIdentifiers.java b/api/src/main/java/org/openmrs/module/amrsmobileforms/groovy/CleanPatientIdentifiers.java new file mode 100644 index 0000000..1815444 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/amrsmobileforms/groovy/CleanPatientIdentifiers.java @@ -0,0 +1,7 @@ +package org.openmrs.module.amrsmobileforms.groovy; + +/** + * TODO:please provide a brief description for the class. + */ +public interface CleanPatientIdentifiers { +} diff --git a/api/src/main/java/org/openmrs/module/amrsmobileforms/groovy/ParseXML.groovy b/api/src/main/java/org/openmrs/module/amrsmobileforms/groovy/ParseXML.groovy new file mode 100644 index 0000000..d037d01 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/amrsmobileforms/groovy/ParseXML.groovy @@ -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 + + } + + } + + + } + + + } + + + + } + + +} + + diff --git a/api/src/main/resources/ParseXML.groovy b/api/src/main/resources/ParseXML.groovy new file mode 100644 index 0000000..e69de29 diff --git a/omod/src/main/java/org/openmrs/module/amrsmobileforms/web/controller/ResolveErrorsController.java b/omod/src/main/java/org/openmrs/module/amrsmobileforms/web/controller/ResolveErrorsController.java index 64a16d6..c9844a6 100644 --- a/omod/src/main/java/org/openmrs/module/amrsmobileforms/web/controller/ResolveErrorsController.java +++ b/omod/src/main/java/org/openmrs/module/amrsmobileforms/web/controller/ResolveErrorsController.java @@ -55,12 +55,141 @@ public class ResolveErrorsController { /** * Controller for Error list jsp page */ - @RequestMapping(value = "/module/amrsmobileforms/resolveErrors") + @RequestMapping(value = "/module/amrsmobileforms/resolveErrors",method = RequestMethod.GET) public String showErrorList() { return "/module/amrsmobileforms/resolveErrors"; } - - /** + /*controller method used to resolve an error from resolveErrors dialog window*/ + @RequestMapping(value = "/module/amrsmobileforms/resolveErrorsFromDialog") + public String resolveErrorfromDialog(HttpSession httpSession, @RequestParam("householdId") String householdId, + @RequestParam("errorId") Integer errorId, @RequestParam("errorItemAction") String errorItemAction, + @RequestParam("birthDate") String birthDate, @RequestParam("patientIdentifier") String patientIdentifier, + @RequestParam("providerId") String providerId, @RequestParam("householdIdentifier") String householdIdentifier) { + MobileFormEntryService mobileService; + String filePath; + + log.debug("Error ID is "+errorId); + + // user must be authenticated (avoids authentication errors) + if (Context.isAuthenticated()) { + if (!Context.getAuthenticatedUser().hasPrivilege( + MobileFormEntryConstants.PRIV_RESOLVE_MOBILE_FORM_ENTRY_ERROR)) { + httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "amrsmobileforms.action.noRights"); + return "redirect:resolveErrors.list"; + } + + mobileService = Context.getService(MobileFormEntryService.class); + + // fetch the MobileFormEntryError item from the database + MobileFormEntryError errorItem = mobileService.getErrorById(errorId); + filePath = MobileFormEntryUtil.getMobileFormsErrorDir().getAbsolutePath() + errorItem.getFormName(); + if ("linkHousehold".equals(errorItemAction)) { + if (mobileService.getHousehold(householdId) == null) { + httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "amrsmobileforms.resolveErrors.action.createLink.error"); + return "redirect:resolveErrors.list"; + } else { + if (XFormEditor.editNode(filePath, + MobileFormEntryConstants.PATIENT_NODE + "/" + MobileFormEntryConstants.PATIENT_HOUSEHOLD_IDENTIFIER, householdId)) { + // put form in queue for normal processing + moveAndDeleteError(MobileFormEntryUtil.getMobileFormsQueueDir().getAbsolutePath(), errorItem); + } + } + } else if ("assignBirthdate".equals(errorItemAction)) { + // format provided birthdate and insert into patient data like so: + // 2009-12-25 + if (StringUtils.isNotEmpty(birthDate)) { + DateFormat reader = DateFormat.getDateInstance(DateFormat.SHORT, Context.getLocale()); + DateFormat writer = new SimpleDateFormat("yyyy-MM-dd"); + try { + String formattedDate = writer.format(reader.parse(birthDate)); + if (XFormEditor.editNode(filePath, + MobileFormEntryConstants.PATIENT_NODE + "/" + MobileFormEntryConstants.PATIENT_BIRTHDATE, formattedDate)) { + // put form in queue for normal processing + moveAndDeleteError(MobileFormEntryUtil.getMobileFormsQueueDir().getAbsolutePath(), errorItem); + } + } catch (ParseException e) { + String error = "Birthdate was not assigned, Invalid date entered: " + birthDate; + httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, error); + log.error(error, e); + return "redirect:resolveErrors.list"; + } + } else { + httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Birthdate was not assigned, Null object entered"); + return "redirect:resolveErrors.list"; + } + } else if ("newIdentifier".equals(errorItemAction)) { + if (patientIdentifier != null && patientIdentifier.trim() != "") { + if (reverseNodes(filePath, patientIdentifier)) { + // put form in queue for normal processing + moveAndDeleteError(MobileFormEntryUtil.getMobileFormsQueueDir().getAbsolutePath(), errorItem); + } + } else { + httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "amrsmobileforms.resolveErrors.action.newIdentifier.error"); + return "redirect:resolveErrors.list"; + } + } else if ("linkProvider".equals(errorItemAction)) { + if (providerId != null && providerId.trim() != "") { + //providerId = Context.getUserService().getUser(Integer.parseInt(providerId)).getSystemId(); + if (XFormEditor.editNode(filePath, + MobileFormEntryConstants.ENCOUNTER_NODE + "/" + MobileFormEntryConstants.ENCOUNTER_PROVIDER, providerId)) { + // put form in queue for normal processing + moveAndDeleteError(MobileFormEntryUtil.getMobileFormsQueueDir().getAbsolutePath(), errorItem); + } + } else { + httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "(Null) Invalid provider ID"); + return "redirect:resolveErrors.list"; + } + } else if ("createPatient".equals(errorItemAction)) { + // put form in queue for normal processing + moveAndDeleteError(MobileFormEntryUtil.getMobileFormsQueueDir().getAbsolutePath(), errorItem); + } else if ("deleteError".equals(errorItemAction)) { + // delete the mobileformentry error queue item + mobileService.deleteError(errorItem); + //and delete from the file system + MobileFormEntryUtil.deleteFile(filePath); + + } else if ("deleteComment".equals(errorItemAction)) { + //set comment to null and save + errorItem.setComment(null); + mobileService.saveErrorInDatabase(errorItem); + } else if ("newHousehold".equals(errorItemAction)) { + if (householdIdentifier != null && householdIdentifier.trim() != "") { + // first change household id + if (XFormEditor.editNode(filePath, + MobileFormEntryConstants.HOUSEHOLD_PREFIX + MobileFormEntryConstants.HOUSEHOLD_META_PREFIX + "/" + + MobileFormEntryConstants.HOUSEHOLD_META_HOUSEHOLD_ID, householdIdentifier)) { + } else { + httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Error assigning new household identififer"); + return "redirect:resolveErrors.list"; + } + + // then change all patient household pointers + if (XFormEditor.editNodeList(filePath, + MobileFormEntryConstants.HOUSEHOLD_PREFIX + MobileFormEntryConstants.HOUSEHOLD_INDIVIDUALS_PREFIX, + "patient/" + MobileFormEntryConstants.PATIENT_HOUSEHOLD_IDENTIFIER, householdIdentifier)) { + // drop form in queue for normal processing + moveAndDeleteError(MobileFormEntryUtil.getMobileFormsDropDir().getAbsolutePath(), errorItem); + } else { + httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Error assigning new household identififer"); + return "redirect:resolveErrors.list"; + } + } else { + httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Error assigning new household identififer"); + return "redirect:resolveErrors.list"; + } + } else if ("noChange".equals(errorItemAction)) { + // do nothing here + } else { + throw new APIException("Invalid action selected for: " + errorId); + } + } + + httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "amrsmobileforms.resolveErrors.action.success"); + return "redirect:resolveErrors.list"; + } + + + /** * Controller for commentOnError jsp Page */ @ModelAttribute("errorFormComment") @@ -96,136 +225,6 @@ public List populateErrorForm(@RequestParam Integer e return getErrorObject(errorId); } - /** - * Controller for resolveError post jsp Page - */ - @RequestMapping(value = "/module/amrsmobileforms/resolveError", method = RequestMethod.POST) - public String resolveError(HttpSession httpSession, @RequestParam("householdId") String householdId, - @RequestParam("errorId") Integer errorId, @RequestParam("errorItemAction") String errorItemAction, - @RequestParam("birthDate") String birthDate, @RequestParam("patientIdentifier") String patientIdentifier, - @RequestParam("providerId") String providerId, @RequestParam("householdIdentifier") String householdIdentifier) { - MobileFormEntryService mobileService; - String filePath; - - log.debug("Error ID is "+errorId); - - // user must be authenticated (avoids authentication errors) - if (Context.isAuthenticated()) { - if (!Context.getAuthenticatedUser().hasPrivilege( - MobileFormEntryConstants.PRIV_RESOLVE_MOBILE_FORM_ENTRY_ERROR)) { - httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "amrsmobileforms.action.noRights"); - return "redirect:resolveErrors.list"; - } - - mobileService = Context.getService(MobileFormEntryService.class); - - // fetch the MobileFormEntryError item from the database - MobileFormEntryError errorItem = mobileService.getErrorById(errorId); - filePath = MobileFormEntryUtil.getMobileFormsErrorDir().getAbsolutePath() + errorItem.getFormName(); - if ("linkHousehold".equals(errorItemAction)) { - if (mobileService.getHousehold(householdId) == null) { - httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "amrsmobileforms.resolveErrors.action.createLink.error"); - return "redirect:resolveErrors.list"; - } else { - if (XFormEditor.editNode(filePath, - MobileFormEntryConstants.PATIENT_NODE + "/" + MobileFormEntryConstants.PATIENT_HOUSEHOLD_IDENTIFIER, householdId)) { - // put form in queue for normal processing - moveAndDeleteError(MobileFormEntryUtil.getMobileFormsQueueDir().getAbsolutePath(), errorItem); - } - } - } else if ("assignBirthdate".equals(errorItemAction)) { - // format provided birthdate and insert into patient data like so: - // 2009-12-25 - if (StringUtils.isNotEmpty(birthDate)) { - DateFormat reader = DateFormat.getDateInstance(DateFormat.SHORT, Context.getLocale()); - DateFormat writer = new SimpleDateFormat("yyyy-MM-dd"); - try { - String formattedDate = writer.format(reader.parse(birthDate)); - if (XFormEditor.editNode(filePath, - MobileFormEntryConstants.PATIENT_NODE + "/" + MobileFormEntryConstants.PATIENT_BIRTHDATE, formattedDate)) { - // put form in queue for normal processing - moveAndDeleteError(MobileFormEntryUtil.getMobileFormsQueueDir().getAbsolutePath(), errorItem); - } - } catch (ParseException e) { - String error = "Birthdate was not assigned, Invalid date entered: " + birthDate; - httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, error); - log.error(error, e); - return "redirect:resolveErrors.list"; - } - } else { - httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Birthdate was not assigned, Null object entered"); - return "redirect:resolveErrors.list"; - } - } else if ("newIdentifier".equals(errorItemAction)) { - if (patientIdentifier != null && patientIdentifier.trim() != "") { - if (reverseNodes(filePath, patientIdentifier)) { - // put form in queue for normal processing - moveAndDeleteError(MobileFormEntryUtil.getMobileFormsQueueDir().getAbsolutePath(), errorItem); - } - } else { - httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "amrsmobileforms.resolveErrors.action.newIdentifier.error"); - return "redirect:resolveErrors.list"; - } - } else if ("linkProvider".equals(errorItemAction)) { - if (providerId != null && providerId.trim() != "") { - providerId = Context.getUserService().getUser(Integer.parseInt(providerId)).getSystemId(); - if (XFormEditor.editNode(filePath, - MobileFormEntryConstants.ENCOUNTER_NODE + "/" + MobileFormEntryConstants.ENCOUNTER_PROVIDER, providerId)) { - // put form in queue for normal processing - moveAndDeleteError(MobileFormEntryUtil.getMobileFormsQueueDir().getAbsolutePath(), errorItem); - } - } else { - httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "(Null) Invalid provider ID"); - return "redirect:resolveErrors.list"; - } - } else if ("createPatient".equals(errorItemAction)) { - // put form in queue for normal processing - moveAndDeleteError(MobileFormEntryUtil.getMobileFormsQueueDir().getAbsolutePath(), errorItem); - } else if ("deleteError".equals(errorItemAction)) { - // delete the mobileformentry error queue item - mobileService.deleteError(errorItem); - //and delete from the file system - MobileFormEntryUtil.deleteFile(filePath); - - } else if ("deleteComment".equals(errorItemAction)) { - //set comment to null and save - errorItem.setComment(null); - mobileService.saveErrorInDatabase(errorItem); - } else if ("newHousehold".equals(errorItemAction)) { - if (householdIdentifier != null && householdIdentifier.trim() != "") { - // first change household id - if (XFormEditor.editNode(filePath, - MobileFormEntryConstants.HOUSEHOLD_PREFIX + MobileFormEntryConstants.HOUSEHOLD_META_PREFIX + "/" - + MobileFormEntryConstants.HOUSEHOLD_META_HOUSEHOLD_ID, householdIdentifier)) { - } else { - httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Error assigning new household identififer"); - return "redirect:resolveErrors.list"; - } - - // then change all patient household pointers - if (XFormEditor.editNodeList(filePath, - MobileFormEntryConstants.HOUSEHOLD_PREFIX + MobileFormEntryConstants.HOUSEHOLD_INDIVIDUALS_PREFIX, - "patient/" + MobileFormEntryConstants.PATIENT_HOUSEHOLD_IDENTIFIER, householdIdentifier)) { - // drop form in queue for normal processing - moveAndDeleteError(MobileFormEntryUtil.getMobileFormsDropDir().getAbsolutePath(), errorItem); - } else { - httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Error assigning new household identififer"); - return "redirect:resolveErrors.list"; - } - } else { - httpSession.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "Error assigning new household identififer"); - return "redirect:resolveErrors.list"; - } - } else if ("noChange".equals(errorItemAction)) { - // do nothing here - } else { - throw new APIException("Invalid action selected for: " + errorId); - } - } - - httpSession.setAttribute(WebConstants.OPENMRS_MSG_ATTR, "amrsmobileforms.resolveErrors.action.success"); - return "redirect:resolveErrors.list"; - } /** * Given an id, this method creates an error model @@ -290,17 +289,32 @@ private static String getAbsoluteFilePath(String formName, MobileFormEntryServic /** * Stores a form in a specified folder */ - private static void saveForm(String oldFormPath, String newFormPath) { + private static boolean saveForm(String oldFormPath, String newFormPath) { + try { if (oldFormPath != null) { File file = new File(oldFormPath); + File newDestination = new File(newFormPath); //move the file to specified new directory - file.renameTo(new File(newFormPath)); + /*check to see if the new file path exists. renameTo does nothing if same path is encountered*/ + if(newDestination.exists()) + newDestination.deleteOnExit();//safely deletes the file. + + if(file.renameTo(new File(newFormPath))){ + log.info("Moving error file to drop directory was successful"); + return true; + } + else { + log.info("Failed to move file to drop directory"); + return false; + } } } catch (Exception e) { + e.printStackTrace(); log.error(e.getMessage(), e); } + return false; } @@ -420,9 +434,14 @@ private void moveAndDeleteError(String destination, MobileFormEntryError error) // find error location String filePath = MobileFormEntryUtil.getMobileFormsErrorDir().getAbsolutePath() + error.getFormName(); // put form in queue for normal processing - saveForm(filePath, destination + error.getFormName()); - // delete the mobileformentry error queue item - getMobileFormEntryService().deleteError(error); + if(saveForm(filePath, destination + error.getFormName())){ + // delete the mobileformentry error queue item + getMobileFormEntryService().deleteError(error); + } + else{ + log.error("Could not complete error resolution process"); + } + } /** diff --git a/omod/src/main/resources/webModuleApplicationContext.xml b/omod/src/main/resources/webModuleApplicationContext.xml index f7aa449..bb1e740 100644 --- a/omod/src/main/resources/webModuleApplicationContext.xml +++ b/omod/src/main/resources/webModuleApplicationContext.xml @@ -1,14 +1,23 @@ + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context-3.0.xsd + http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd"> + + + + + + + diff --git a/omod/src/main/webapp/resolveError.jsp b/omod/src/main/webapp/resolveError.jsp index 9981970..b24133b 100755 --- a/omod/src/main/webapp/resolveError.jsp +++ b/omod/src/main/webapp/resolveError.jsp @@ -1,4 +1,4 @@ -<%@ include file="/WEB-INF/template/include.jsp"%> + <%@ include file="/WEB-INF/template/include.jsp"%> diff --git a/omod/src/main/webapp/resolveErrors.jsp b/omod/src/main/webapp/resolveErrors.jsp index 73108c9..fd390de 100755 --- a/omod/src/main/webapp/resolveErrors.jsp +++ b/omod/src/main/webapp/resolveErrors.jsp @@ -1,606 +1,607 @@ <%@ include file="/WEB-INF/template/include.jsp" %> - + <%@ include file="/WEB-INF/template/header.jsp" %> -<%@ include file="localHeader.jsp"%> +<%@ include file="localHeader.jsp" %> - - - + + + - + - - - + + + @@ -612,161 +613,153 @@ function generate_ResolveError_table(data) { - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - : - - - - - - - - - - - : - - - - - - - - - - - : - - - - - - - - - : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + : + + + + + + + + + + : + + + + + + + + + + + : + + + + + + + + + : + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + Mobile Form Entry Errors - - - Select All in Search Results (including unseen) - - Reprocess 0 Selected Errors - - - - - - - - - - - Select - Action - ID - Error - Error Details - Form Name - Comment - - - - - - - - + + + + Select All in Search Results + (including unseen) + + Reprocess 0 Selected Errors + + + + + + + Select + Action + ID + Location + Provider + Error + Error Details + Form Name + Comment + + + + + + + + -<%@ include file="/WEB-INF/template/footer.jsp" %> \ No newline at end of file +<%@ include file="/WEB-INF/template/footer.jsp" %> diff --git a/pom.xml b/pom.xml index 2b6d76d..80eb0cd 100644 --- a/pom.xml +++ b/pom.xml @@ -136,6 +136,13 @@ + + + org.codehaus.groovy + groovy-all + 1.8.7 + +