Java tutorial
/** * The contents of this file are subject to the OpenMRS Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://license.openmrs.org * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations * under the License. * * Copyright (C) OpenMRS, LLC. All Rights Reserved. */ package org.openmrs.module.amrscustomization.web.controller; import javax.servlet.ServletException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.openmrs.api.context.Context; import org.openmrs.module.amrscustomization.AMRSCustomizationService; import org.openmrs.patient.IdentifierValidator; import org.openmrs.patient.UnallowedIdentifierException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; /** * controller for the MRN Generator form */ @Controller @RequestMapping("admin/maintenance/mrnGenerator.htm") public class MRNGeneratorFormController { private static Log log = LogFactory.getLog(MRNGeneratorFormController.class); /** * mrnGenerator.htm controller * * @param modelMap * @return */ @RequestMapping(method = RequestMethod.GET) public String mrnGeneratorForm(ModelMap modelMap) { AMRSCustomizationService service = Context.getService(AMRSCustomizationService.class); modelMap.addAttribute("entries", service.getMRNGeneratorLogEntries()); return "module/amrscustomization/mrnGeneratorForm"; } /** * handler for mrnGenerator file requests * * @param site * @param first * @param prefix * @param count * @param response * @return * @throws ServletException */ @RequestMapping(method = RequestMethod.POST) public ResponseEntity<String> generateMRNs(@RequestParam("site") String site, @RequestParam("first") int first, @RequestParam("prefix") String prefix, @RequestParam("count") int count) throws Exception { // fix prefix if (prefix == null) prefix = ""; // render filename String filename = site + "_" + first + "-" + (first + (count - 1)) + prefix + ".txt"; // set headers HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.set("Content-Type", "text/plain"); responseHeaders.set("Content-Disposition", "attachment; filename=" + filename); // render the identifiers IdentifierValidator validator = Context.getPatientService().getDefaultIdentifierValidator(); StringBuilder out = new StringBuilder(); for (int i = first; i < count + first; i++) { try { out.append(validator.getValidIdentifier(prefix + i + site) + "\n"); } catch (UnallowedIdentifierException e) { throw new Exception("invalid identifier created"); } } // log who generated this list log.info("generated " + count + " MRNs for " + site + " starting at " + first); AMRSCustomizationService as = Context.getService(AMRSCustomizationService.class); as.addMRNGeneratorLogEntry(site, first, count); // send the file return new ResponseEntity<String>(out.toString(), responseHeaders, HttpStatus.CREATED); } }