use of com.developmentontheedge.be5.metadata.model.FreemarkerScript in project be5 by DevelopmentOnTheEdge.
the class ProjectGenerator method addIncludes.
public static void addIncludes(final Project project) {
final StringBuilder sb = new StringBuilder();
for (final Module module : project.getModules()) {
final FreemarkerCatalog collection = module.getMacroCollection();
if (collection == null)
continue;
final FreemarkerScript script = collection.optScript(FreemarkerCatalog.MAIN_MACRO_LIBRARY);
if (script == null)
continue;
sb.append("<#include \"../../Modules/" + module.getName() + "/Macros/common\"/>\n");
}
final String includes = sb.toString();
if (!includes.isEmpty()) {
final FreemarkerScript script = new FreemarkerScript(FreemarkerCatalog.MAIN_MACRO_LIBRARY, project.getMacroCollection());
script.setSource(includes);
DataElementUtils.saveQuiet(script);
}
}
use of com.developmentontheedge.be5.metadata.model.FreemarkerScript in project be5 by DevelopmentOnTheEdge.
the class YamlSerializer method serialize.
private List<Object> serialize(final FreemarkerCatalog scripts, Function<String, Path> getPath, final boolean serializeReferencedFiles) throws WriteException {
List<Object> paths = list();
for (final FreemarkerScript script : scripts.getScripts()) {
try {
String relativePath = script.getRelativePath(scripts);
if (serializeReferencedFiles) {
final Path scriptFile = getPath.apply(relativePath);
if (!Files.exists(scriptFile.getParent()))
Files.createDirectories(scriptFile.getParent());
if (!script.isLoaded())
continue;
write(scriptFile, script);
script.setLinkedFile(scriptFile);
}
paths.add(relativePath);
} catch (final Exception e) {
throw new WriteException(script, e);
}
}
return paths;
}
use of com.developmentontheedge.be5.metadata.model.FreemarkerScript in project be5 by DevelopmentOnTheEdge.
the class SqlMacroTest method testMacro.
@Test
public void testMacro() throws ProjectElementException {
Project project = new Project("test");
project.setDatabaseSystem(Rdbms.POSTGRESQL);
FreemarkerScript script = new FreemarkerScript("script", project.getApplication().getFreemarkerScripts());
DataElementUtils.save(script);
script.setSource("SELECT ${concat('a'?asDate, 'b', 'c'?str)} FROM test");
assertEquals("SELECT ( TO_DATE(a,'YYYY-MM-DD') || b || 'c' ) FROM test", project.mergeTemplate(script).validate());
script.setSource("<#macro _sql>${project.enterSQL()}<#assign nested><#nested></#assign>${project.translateSQL(nested)}</#macro>" + "<@_sql>SELECT TO_DATE(a) || b || 'c' FROM test</@>");
assertEquals("SELECT TO_DATE(a, 'YYYY-MM-DD') || b || 'c' FROM test", project.mergeTemplate(script).validate());
script.setSource("<#macro _sql>${project.enterSQL()}<#assign nested><#nested></#assign>${project.translateSQL(nested)}</#macro>" + "<@_sql>SELECT ${'a'?asDate} || b || 'c' FROM test</@>");
assertEquals("SELECT TO_DATE(a, 'YYYY-MM-DD') || b || 'c' FROM test", project.mergeTemplate(script).validate());
}
use of com.developmentontheedge.be5.metadata.model.FreemarkerScript in project be5 by DevelopmentOnTheEdge.
the class AppData method execute.
@Override
public void execute() throws MojoFailureException {
init();
PrintStream ps = null;
try {
if (logPath != null) {
logPath.mkdirs();
ps = new PrintStream(new File(logPath, be5Project.getName() + "_scripts_" + script.replace(';', '_').replace(':', '.') + ".sql"), "UTF-8");
}
ModuleLoader2.addModuleScripts(be5Project);
List<FreemarkerScript> scripts = new ArrayList<>();
for (String scriptName : script.split(";")) {
int pos = scriptName.indexOf(':');
FreemarkerCatalog scriptsCatalog = be5Project.getApplication().getFreemarkerScripts();
if (pos > 0) {
String moduleName = scriptName.substring(0, pos);
scriptName = scriptName.substring(pos + 1);
if (moduleName.equals("all")) {
for (Module module : be5Project.getModules()) {
scriptsCatalog = module.getFreemarkerScripts();
if (scriptsCatalog == null)
continue;
FreemarkerScript script = scriptsCatalog.optScript(scriptName);
if (script == null)
continue;
scripts.add(script);
}
FreemarkerScript script = be5Project.getApplication().getFreemarkerScripts().optScript(scriptName);
if (script != null) {
scripts.add(script);
}
continue;
} else {
Module module = be5Project.getModule(moduleName);
if (module == null) {
if (ignoreMissing) {
System.err.println("Warning: module '" + moduleName + "' not found");
continue;
} else
throw new MojoFailureException("Module '" + moduleName + "' not found");
}
scriptsCatalog = module.getFreemarkerScripts();
}
}
FreemarkerScript freemarkerScript = scriptsCatalog == null ? null : scriptsCatalog.optScript(scriptName);
if (freemarkerScript == null) {
if (ignoreMissing) {
System.err.println("Warning: FTL script " + scriptName + " not found");
continue;
} else
throw new MojoFailureException("FTL script " + scriptName + " not found");
}
scripts.add(freemarkerScript);
}
SqlExecutor sqlExecutor = new BeSqlExecutor(connector, ps);
for (FreemarkerScript freemarkerScript : scripts) {
executeScript(sqlExecutor, freemarkerScript);
}
DatabaseUtils.clearAllCache(sqlExecutor);
} catch (ProjectElementException | FreemarkerSqlException e) {
throw new MojoFailureException(e.getMessage(), e);
} catch (Exception e) {
throw new MojoFailureException(e.getMessage(), e);
} finally {
if (ps != null) {
ps.close();
}
}
}
use of com.developmentontheedge.be5.metadata.model.FreemarkerScript in project be5 by DevelopmentOnTheEdge.
the class YamlDeserializer method readMacroFiles.
private void readMacroFiles(BaseDeserializer deserializer, Map<String, Object> serializedModuleBody, FreemarkerCatalog macroFiles) throws ReadException {
final Object includes = serializedModuleBody.get(TAG_MACRO_FILES);
if (includes == null)
return;
for (final String scriptName : deserializer.asStrList(includes)) {
final Path macroFile = getFileSystem().getMacroFile(scriptName);
try {
final FreemarkerScript script = new FreemarkerScript(scriptName, macroFiles);
if (Files.exists(macroFile)) {
script.setLinkedFile(macroFile);
script.getTemplateCode();
}
DataElementUtils.saveQuiet(script);
} catch (final Exception e) {
loadContext.addWarning(new ReadException(e, macroFiles.getCompletePath().getChildPath(scriptName), macroFile));
}
}
}
Aggregations