Search in sources :

Example 11 with Configuration

use of freemarker.template.Configuration in project OpenClinica by OpenClinica.

the class EnketoUrlService method populateInstance.

private String populateInstance(CrfVersion crfVersion, FormLayout formLayout, EventCrf eventCrf, String studyOid, String flavor) throws Exception {
    Map<String, Object> data = new HashMap<String, Object>();
    List<ItemGroup> igs = itemGroupDao.findByCrfVersionId(crfVersion.getCrfVersionId());
    for (ItemGroup ig : igs) {
        List<HashMap<String, Object>> hashMapList = new ArrayList<HashMap<String, Object>>();
        List<ItemGroupMetadata> igms = itemGroupMetadataDao.findByItemGroupCrfVersion(ig.getItemGroupId(), crfVersion.getCrfVersionId());
        int maxRowCount = itemDataDao.getMaxCountByEventCrfGroup(eventCrf.getEventCrfId(), ig.getItemGroupId());
        HashMap<String, Object> hashMap = null;
        if (igms.get(0).isRepeatingGroup() && maxRowCount == 0) {
            hashMap = new HashMap<>();
            hashMap.put("index", 1);
            hashMap.put("lastUsedOrdinal", 1);
            for (ItemGroupMetadata igm : igms) {
                hashMap.put(igm.getItem().getName(), "");
                if (flavor.equals(QUERY_FLAVOR))
                    hashMap.put(igm.getItem().getName() + QUERY_SUFFIX, "");
            }
            hashMapList.add(hashMap);
            data.put(ig.getName(), hashMapList);
        }
        boolean rowDeleted = false;
        if (igms.get(0).isRepeatingGroup()) {
            for (int i = 0; i < maxRowCount; i++) {
                rowDeleted = false;
                for (ItemGroupMetadata igm : igms) {
                    ItemData itemData = itemDataDao.findByItemEventCrfOrdinalDeleted(igm.getItem().getItemId(), eventCrf.getEventCrfId(), i + 1);
                    if (itemData != null) {
                        rowDeleted = true;
                        break;
                    }
                }
                if (!rowDeleted) {
                    hashMap = new HashMap<>();
                    hashMap.put("index", i + 1);
                    if (i == 0) {
                        hashMap.put("lastUsedOrdinal", maxRowCount);
                    }
                    for (ItemGroupMetadata igm : igms) {
                        ItemData itemData = itemDataDao.findByItemEventCrfOrdinal(igm.getItem().getItemId(), eventCrf.getEventCrfId(), i + 1);
                        String itemValue = getItemValue(itemData, crfVersion);
                        hashMap.put(igm.getItem().getName(), itemData != null ? itemValue : "");
                        if (flavor.equals(QUERY_FLAVOR)) {
                            if (itemData != null) {
                                ObjectMapper mapper = new ObjectMapper();
                                QueriesBean queriesBean = buildQueryElement(itemData);
                                hashMap.put(igm.getItem().getName() + QUERY_SUFFIX, queriesBean != null ? mapper.writeValueAsString(queriesBean) : "");
                            } else {
                                hashMap.put(igm.getItem().getName() + QUERY_SUFFIX, "");
                            }
                        }
                    }
                    hashMapList.add(hashMap);
                }
            }
        }
        if (igms.get(0).isRepeatingGroup() && maxRowCount != 0) {
            data.put(ig.getName(), hashMapList);
        }
        if (!igms.get(0).isRepeatingGroup()) {
            for (ItemGroupMetadata igm : igms) {
                ItemData itemData = itemDataDao.findByItemEventCrfOrdinal(igm.getItem().getItemId(), eventCrf.getEventCrfId(), 1);
                String itemValue = getItemValue(itemData, crfVersion);
                data.put(igm.getItem().getName(), itemData != null ? itemValue : "");
                if (flavor.equals(QUERY_FLAVOR)) {
                    if (itemData != null) {
                        ObjectMapper mapper = new ObjectMapper();
                        QueriesBean queriesBean = buildQueryElement(itemData);
                        data.put(igm.getItem().getName() + QUERY_SUFFIX, queriesBean != null ? mapper.writeValueAsString(queriesBean) : "");
                    } else {
                        data.put(igm.getItem().getName() + QUERY_SUFFIX, "");
                    }
                }
            }
        }
    }
    String templateStr = null;
    CrfBean crfBean = crfDao.findById(formLayout.getCrf().getCrfId());
    String directoryPath = Utils.getCrfMediaFilePath(crfBean.getOcOid(), formLayout.getOcOid());
    File dir = new File(directoryPath);
    File[] directoryListing = dir.listFiles();
    if (directoryListing != null) {
        for (File child : directoryListing) {
            if (flavor.equals(QUERY_FLAVOR) && child.getName().endsWith(INSTANCE_QUERIES_SUFFIX) || flavor.equals(NO_FLAVOR) && child.getName().endsWith(INSTANCE_SUFFIX)) {
                templateStr = new String(Files.readAllBytes(Paths.get(child.getPath())));
                break;
            }
        }
    }
    Template template = new Template("template name", new StringReader(templateStr), new Configuration());
    StringWriter wtr = new StringWriter();
    template.process(data, wtr);
    String instance = wtr.toString();
    System.out.println(instance);
    return instance;
}
Also used : CrfBean(org.akaza.openclinica.domain.datamap.CrfBean) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Template(freemarker.template.Template) ItemGroup(org.akaza.openclinica.domain.datamap.ItemGroup) StringWriter(java.io.StringWriter) ItemGroupMetadata(org.akaza.openclinica.domain.datamap.ItemGroupMetadata) QueriesBean(org.akaza.openclinica.core.form.xform.QueriesBean) StringReader(java.io.StringReader) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ItemData(org.akaza.openclinica.domain.datamap.ItemData)

Example 12 with Configuration

use of freemarker.template.Configuration in project OpenClinica by OpenClinica.

the class ODMMetadataRestResource method initFreemarker.

/**/
private Configuration initFreemarker(ServletContext context) {
    // Initialize the FreeMarker configuration;
    // - Create a configuration instance
    Configuration cfg = new freemarker.template.Configuration();
    // - Templates are stoted in the WEB-INF/templates directory of the Web app.
    cfg.setServletContextForTemplateLoading(context, "WEB-INF/template");
    // - Set update dealy to 0 for now, to ease debugging and testing.
    //   Higher value should be used in production environment.
    cfg.setTemplateUpdateDelay(0);
    // - Set an error handler that prints errors so they are readable with
    //   a HTML browser.
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
    // - Use beans wrapper (recommmended for most applications)
    cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
    // - Set the default charset of the template files
    cfg.setDefaultEncoding("ISO-8859-1");
    // - Set the charset of the output. This is actually just a hint, that
    //   templates may require for URL encoding and for generating META element
    //   that uses http-equiv="Content-type".
    cfg.setOutputEncoding("UTF-8");
    // - Set the default locale
    cfg.setLocale(Locale.US);
    return cfg;
}
Also used : Configuration(freemarker.template.Configuration)

Example 13 with Configuration

use of freemarker.template.Configuration in project opennms by OpenNMS.

the class NavBarController method afterPropertiesSet.

/**
     * <p>afterPropertiesSet</p>
     * @throws IOException
     */
@Override
public void afterPropertiesSet() throws IOException {
    Assert.state(m_navBarItems != null, "navBarItems property has not been set");
    // Initialize the Freemarker engine and fetch our template
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_21);
    cfg.setDefaultEncoding(StandardCharsets.UTF_8.name());
    cfg.setClassForTemplateLoading(NavBarController.class, "");
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
    Template template = cfg.getTemplate("navbar.ftl");
    m_view = new FreemarkerView(template);
}
Also used : Configuration(freemarker.template.Configuration) Template(freemarker.template.Template)

Example 14 with Configuration

use of freemarker.template.Configuration in project pcgen by PCGen.

the class VariableReport method outputReport.

/**
	 * Output the variable report for the supplied data in a particular format.
	 * 
	 * @param gameModeVarMap The map of variable definitions for each game mode. 
	 * @param gameModeVarCountMap The map of the number of variables for each game mode.
	 * @param reportFormat The format in which to output the report.
	 * @param outputWriter The writer to output the report to.
	 * @throws IOException If the template cannot be accessed or the writer cannot be written to.
	 * @throws TemplateException If there is an error in processing the template.
	 */
public void outputReport(Map<String, List<VarDefine>> gameModeVarMap, Map<String, Integer> gameModeVarCountMap, ReportFormat reportFormat, Writer outputWriter) throws IOException, TemplateException {
    // Configuration
    Writer file = null;
    Configuration cfg = new Configuration();
    int dataPathLen = ConfigurationSettings.getPccFilesDir().length();
    try {
        // Set Directory for templates
        File codeDir = new File("code");
        File templateDir = new File(codeDir, "templates");
        cfg.setDirectoryForTemplateLoading(templateDir);
        // load template
        Template template = cfg.getTemplate(reportFormat.getTemplate());
        // data-model
        Map<String, Object> input = new HashMap<>();
        input.put("gameModeVarMap", gameModeVarMap);
        input.put("gameModeVarCountMap", gameModeVarCountMap);
        input.put("pathIgnoreLen", dataPathLen + 1);
        // Process the template
        template.process(input, outputWriter);
        outputWriter.flush();
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (Exception e2) {
            }
        }
    }
}
Also used : Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) PCGFile(pcgen.io.PCGFile) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Template(freemarker.template.Template)

Example 15 with Configuration

use of freemarker.template.Configuration in project OpenAM by OpenRock.

the class OpenAMResourceOwnerSessionValidatorTest method mockCustomLoginUrlTemplate.

private void mockCustomLoginUrlTemplate(String customLoginUrlTemplate) throws ServerException, IOException {
    Template template = new Template("", new StringReader(customLoginUrlTemplate), new Configuration());
    given(providerSettings.getCustomLoginUrlTemplate()).willReturn(template);
}
Also used : Configuration(freemarker.template.Configuration) StringReader(java.io.StringReader) Template(freemarker.template.Template)

Aggregations

Configuration (freemarker.template.Configuration)56 Template (freemarker.template.Template)30 Writer (java.io.Writer)17 IOException (java.io.IOException)14 StringWriter (java.io.StringWriter)14 File (java.io.File)13 HashMap (java.util.HashMap)11 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)8 TemplateException (freemarker.template.TemplateException)7 OutputStreamWriter (java.io.OutputStreamWriter)7 StringTemplateLoader (freemarker.cache.StringTemplateLoader)5 ThirdEyeAnomalyConfiguration (com.linkedin.thirdeye.anomaly.ThirdEyeAnomalyConfiguration)4 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)4 FileTemplateLoader (freemarker.cache.FileTemplateLoader)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 FileWriter (java.io.FileWriter)4 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)3 TemplateLoader (freemarker.cache.TemplateLoader)3