use of org.structr.web.entity.dom.Template in project structr by structr.
the class TemplateImportVisitor method createTemplate.
private void createTemplate(final Path file, final String fileName) throws IOException, FrameworkException {
final String templateName = StringUtils.substringBeforeLast(fileName, ".html");
// either the template was exported with name + uuid or just the uuid
final boolean byNameAndId = DeployCommand.endsWithUuid(templateName);
final boolean byId = DeployCommand.isUuid(templateName);
try (final Tx tx = app.tx(true, false, false)) {
final PropertyMap properties = getPropertiesForTemplate(templateName);
if (properties == null) {
logger.info("Ignoring {} (not in templates.json)", fileName);
} else {
final String src = new String(Files.readAllBytes(file), Charset.forName("UTF-8"));
final Template template;
if (byId) {
logger.info("Importing template {} from {}..", new Object[] { templateName, fileName });
final DOMNode existingTemplate = app.get(DOMNode.class, templateName);
if (existingTemplate != null) {
deleteTemplate(app, existingTemplate);
}
template = app.create(Template.class, new NodeAttribute(AbstractNode.id, templateName));
} else if (byNameAndId) {
// the last characters in the name string are the uuid
final String uuid = templateName.substring(templateName.length() - 32);
final String name = templateName.substring(0, templateName.length() - 33);
logger.info("Importing template {} from {}..", new Object[] { name, fileName });
final DOMNode existingTemplate = app.get(DOMNode.class, uuid);
if (existingTemplate != null) {
deleteTemplate(app, existingTemplate);
}
template = app.create(Template.class, new NodeAttribute(AbstractNode.id, uuid));
properties.put(Template.name, name);
} else {
final String name = byId ? null : templateName;
logger.info("Importing template {} from {}..", new Object[] { name, fileName });
final DOMNode existingTemplate = getExistingTemplate(name);
if (existingTemplate != null) {
deleteTemplate(app, existingTemplate);
}
template = app.create(Template.class);
properties.put(Template.name, name);
}
properties.put(StructrApp.key(Template.class, "content"), src);
// insert "shared" templates into ShadowDocument
final Object value = properties.get(internalSharedTemplateKey);
if (value != null) {
if ("true".equals(value)) {
template.setOwnerDocument(CreateComponentCommand.getOrCreateHiddenDocument());
}
properties.remove(internalSharedTemplateKey);
}
// store properties from templates.json if present
template.setProperties(securityContext, properties);
}
tx.success();
} catch (Throwable t) {
logger.debug("Error trying to create template {}", fileName);
}
}
Aggregations