Exemplo n.º 1
0
  public static LayoutContent displayHome(spark.Request request, Configuration cfg) {
    String html = "";
    Map<String, String> root = Tools.listLayoutMap();
    root.replace("msg", DBH.getMsg(request.session().id()));
    Template temp;
    try {
      temp = cfg.getTemplate("user-home.htm");
      Writer out = new StringWriter();
      temp.process(root, out);
      html = out.toString();
    } catch (IOException | TemplateException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      html = e.getMessage();
    }
    HashMap<String, String> options = new HashMap<String, String>();
    options.put("addBtn", Tools.getAddBtn("/tramites/agregar", "Iniciar Trámite"));
    options.put(
        "toolBar",
        Tools.getToolbarItem("/tramites/agregar", "Iniciar Trámite", "", "btn red darken-3")
            + Tools.getToolbarItem("/tramites", "Trámites", "", "btn green darken-3"));

    LayoutContent lc = new LayoutContent(html, options);
    return lc;
  }
  @Override
  public void perform(
      final GraphRewrite event, final EvaluationContext context, final ReportModel payload) {
    String templatePath = payload.getTemplatePath();
    String outputFilename = payload.getReportFilename();

    ExecutionStatistics.get()
        .begin("FreeMarkerIterationOperation.render(" + templatePath + ", " + outputFilename + ")");
    try {
      ReportService reportService = new ReportService(event.getGraphContext());

      Path outputDir = Paths.get(reportService.getReportDirectory());

      if (!Files.isDirectory(outputDir)) {
        Files.createDirectories(outputDir);
      }

      Path outputPath = outputDir.resolve(outputFilename);

      LOG.info(
          "Reporting: Writing template \""
              + templatePath
              + "\" to output file \""
              + outputPath.toAbsolutePath().toString()
              + "\"");

      Configuration freemarkerConfig =
          new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
      DefaultObjectWrapperBuilder objectWrapperBuilder =
          new DefaultObjectWrapperBuilder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
      objectWrapperBuilder.setUseAdaptersForContainers(true);
      freemarkerConfig.setObjectWrapper(objectWrapperBuilder.build());
      freemarkerConfig.setAPIBuiltinEnabled(true);
      freemarkerConfig.setTemplateLoader(new FurnaceFreeMarkerTemplateLoader());
      freemarkerConfig.setTemplateUpdateDelayMilliseconds(3600);

      Template template = freemarkerConfig.getTemplate(templatePath);

      Variables variables = Variables.instance(event);

      // just the variables
      Map<String, Object> vars =
          FreeMarkerUtil.findFreeMarkerContextVariables(
              variables, variableNames.toArray(new String[variableNames.size()]));

      if (useDefaultPayloadVariableName) {
        vars.put(DEFAULT_ITERATION_PAYLOAD_NAME, payload);
      }

      // also, extension functions (these are kept separate from vars in order to prevent them
      // from being stored in the associated data with the reportmodel)
      final Map<String, Object> freeMarkerExtensions;
      freeMarkerExtensions =
          furnace
              .getLockManager()
              .performLocked(
                  LockMode.WRITE,
                  new Callable<Map<String, Object>>() {
                    @Override
                    public Map<String, Object> call() throws Exception {
                      return FreeMarkerUtil.findFreeMarkerExtensions(furnace, event);
                    }
                  });

      Map<String, Object> objects = new HashMap<>(vars);
      objects.putAll(freeMarkerExtensions);

      try (FileWriter fw = new FileWriter(outputPath.toFile())) {
        template.process(objects, fw);
      }

      FreeMarkerUtil.addAssociatedReportData(event.getGraphContext(), payload, vars);
    } catch (IOException | TemplateException e) {
      LOG.log(
          Level.WARNING,
          "Template \""
              + templatePath
              + "\" Failed to write report at \""
              + outputFilename
              + "\" due to: "
              + e.getMessage(),
          e);
    } finally {
      ExecutionStatistics.get()
          .end("FreeMarkerIterationOperation.render(" + templatePath + ", " + outputFilename + ")");
    }
  }