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;
}
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;
}
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);
}
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) {
}
}
}
}
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);
}
Aggregations