Ejemplo n.º 1
0
  /**
   * Mimics org.openmrs.web.Listener.getRuntimeProperties()
   *
   * @param webappName name to use when looking up the runtime properties env var or filename
   * @return Properties runtime
   */
  public static Properties getRuntimeProperties(String webappName) {

    Properties props = new Properties();

    try {
      FileInputStream propertyStream = null;

      // Look for environment variable
      // {WEBAPP.NAME}_RUNTIME_PROPERTIES_FILE
      String env = webappName.toUpperCase() + "_RUNTIME_PROPERTIES_FILE";

      String filepath = System.getenv(env);

      if (filepath != null) {
        try {
          propertyStream = new FileInputStream(filepath);
        } catch (IOException e) {
        }
      }

      // env is the name of the file to look for in the directories
      String filename = webappName + "-runtime.properties";

      if (propertyStream == null) {
        filepath = OpenmrsUtil.getApplicationDataDirectory() + filename;
        try {
          propertyStream = new FileInputStream(filepath);
        } catch (IOException e) {
        }
      }

      // look in current directory last
      if (propertyStream == null) {
        filepath = filename;
        try {
          propertyStream = new FileInputStream(filepath);
        } catch (IOException e) {
        }
      }

      if (propertyStream == null)
        throw new IOException("Could not open '" + filename + "' in user or local directory.");
      OpenmrsUtil.loadProperties(props, propertyStream);
      propertyStream.close();

    } catch (IOException e) {
    }

    return props;
  }
  public static File getEmrMonitorDirectory() {
    File appDataDirectory = new File(OpenmrsUtil.getApplicationDataDirectory());
    String emrMonitorDirectoryPath =
        appDataDirectory.getAbsolutePath() + File.separatorChar + EMR_MONITOR_DIRECTORY;
    File emrMonitorDirectory = new File(emrMonitorDirectoryPath);
    if (!emrMonitorDirectory.exists()) {
      if (emrMonitorDirectory.mkdir()) {
        log.debug("directory created: " + emrMonitorDirectoryPath);
      } else {
        log.error("failed to create directory: " + emrMonitorDirectoryPath);
      }
    }

    return emrMonitorDirectory;
  }
Ejemplo n.º 3
0
  /**
   * Gets the folder where modules are stored. ModuleExceptions are thrown on errors
   *
   * @return folder containing modules
   * @should use the runtime property as the first choice if specified
   * @should return the correct file if the runtime property is an absolute path
   */
  public static File getModuleRepository() {

    String folderName =
        Context.getRuntimeProperties()
            .getProperty(ModuleConstants.REPOSITORY_FOLDER_RUNTIME_PROPERTY);
    if (StringUtils.isBlank(folderName)) {
      AdministrationService as = Context.getAdministrationService();
      folderName =
          as.getGlobalProperty(
              ModuleConstants.REPOSITORY_FOLDER_PROPERTY,
              ModuleConstants.REPOSITORY_FOLDER_PROPERTY_DEFAULT);
    }
    // try to load the repository folder straight away.
    File folder = new File(folderName);

    // if the property wasn't a full path already, assume it was intended to be a folder in the
    // application directory
    if (!folder.exists()) {
      folder = new File(OpenmrsUtil.getApplicationDataDirectory(), folderName);
    }

    // now create the modules folder if it doesn't exist
    if (!folder.exists()) {
      log.warn(
          "Module repository "
              + folder.getAbsolutePath()
              + " doesn't exist.  Creating directories now.");
      folder.mkdirs();
    }

    if (!folder.isDirectory())
      throw new ModuleException(
          "Module repository is not a directory at: " + folder.getAbsolutePath());

    return folder;
  }
  public static File generate(HrReport report, String outputFormat, String jrxmlUrl)
      throws IOException {

    String url = Context.getRuntimeProperties().getProperty("connection.url", null);
    Connection conn;
    try {
      conn = connect(url);
    } catch (SQLException e) {
      log.error("Error connecting to DB.", e);
      return null;
    }
    Map<String, Object> map = new HashMap<String, Object>();
    for (HrReportParameter reportParameter : report.getParameters()) {
      if (reportParameter != null)
        if (reportParameter.getName() != null)
          map.put(reportParameter.getName(), reportParameter.getValue());
    }
    log.debug("Report parameter map: " + map);
    String exportPath = OpenmrsUtil.getApplicationDataDirectory();
    URL resourceUrl = new URL(jrxmlUrl);
    InputStream is = resourceUrl.openStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    FileWriter appDirJrxml = new FileWriter(new File(exportPath + report.getFileName()));
    BufferedWriter bw = new BufferedWriter(appDirJrxml);
    String line;
    while ((line = br.readLine()) != null) {
      bw.write(line);
      bw.write("\n");
    }
    bw.flush();
    bw.close();
    JasperPrint jasperPrint = null;
    try {
      JasperDesign jasperDesign = JRXmlLoader.load(exportPath + report.getFileName());
      JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
      jasperPrint = JasperFillManager.fillReport(jasperReport, map, conn);
      if (outputFormat.equals("PDF")) {
        File pdfFile =
            new File(
                exportPath
                    + report.getName()
                    + new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date())
                    + ".pdf");
        JasperExportManager.exportReportToPdfFile(jasperPrint, pdfFile.getAbsolutePath());
        return pdfFile;
      } else if (outputFormat.equals("Excel")) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        File outputfile =
            new File(
                exportPath
                    + report.getName()
                    + new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss").format(new Date())
                    + ".xls");
        OutputStream outputfileStream = new FileOutputStream(outputfile);
        JRXlsExporter exporterXLS = new JRXlsExporter();
        exporterXLS.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
        exporterXLS.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, output);
        exporterXLS.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
        exporterXLS.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
        exporterXLS.setParameter(
            JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
        exporterXLS.exportReport();
        outputfileStream.write(output.toByteArray());
        outputfileStream.flush();
        outputfileStream.close();
        return outputfile;
      }
    } catch (JRException e) {
      log.error("Error generating report", e);
    } finally {
      try {
        if (!conn.isClosed()) {
          conn.close();
        }
      } catch (SQLException e) {
        log.error("Exception closing report connection.", e);
      }
    }
    return null;
  }
 @Override
 public File getFile(String relativePath) {
   return new File(OpenmrsUtil.getApplicationDataDirectory() + relativePath);
 }