Search in sources :

Example 1 with FreemarkerScript

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);
    }
}
Also used : FreemarkerCatalog(com.developmentontheedge.be5.metadata.model.FreemarkerCatalog) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript) Module(com.developmentontheedge.be5.metadata.model.Module)

Example 2 with FreemarkerScript

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;
}
Also used : Path(java.nio.file.Path) WriteException(com.developmentontheedge.be5.metadata.exception.WriteException) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript) ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) WriteException(com.developmentontheedge.be5.metadata.exception.WriteException)

Example 3 with FreemarkerScript

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());
}
Also used : Project(com.developmentontheedge.be5.metadata.model.Project) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript) Test(org.junit.Test)

Example 4 with FreemarkerScript

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();
        }
    }
}
Also used : FreemarkerSqlException(com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException) PrintStream(java.io.PrintStream) ProjectElementException(com.developmentontheedge.be5.metadata.exception.ProjectElementException) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) FreemarkerCatalog(com.developmentontheedge.be5.metadata.model.FreemarkerCatalog) FreemarkerSqlException(com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException) ProjectElementException(com.developmentontheedge.be5.metadata.exception.ProjectElementException) IOException(java.io.IOException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) BeSqlExecutor(com.developmentontheedge.be5.metadata.sql.BeSqlExecutor) SqlExecutor(com.developmentontheedge.dbms.SqlExecutor) BeSqlExecutor(com.developmentontheedge.be5.metadata.sql.BeSqlExecutor) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript) Module(com.developmentontheedge.be5.metadata.model.Module) File(java.io.File)

Example 5 with FreemarkerScript

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));
        }
    }
}
Also used : Path(java.nio.file.Path) DataElementPath(com.developmentontheedge.be5.metadata.model.base.DataElementPath) ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript) ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MarkedYAMLException(org.yaml.snakeyaml.error.MarkedYAMLException) YAMLException(org.yaml.snakeyaml.error.YAMLException)

Aggregations

FreemarkerScript (com.developmentontheedge.be5.metadata.model.FreemarkerScript)9 FreemarkerCatalog (com.developmentontheedge.be5.metadata.model.FreemarkerCatalog)6 Module (com.developmentontheedge.be5.metadata.model.Module)3 Project (com.developmentontheedge.be5.metadata.model.Project)3 DataElementPath (com.developmentontheedge.be5.metadata.model.base.DataElementPath)3 ReadException (com.developmentontheedge.be5.metadata.exception.ReadException)2 BeSqlExecutor (com.developmentontheedge.be5.metadata.sql.BeSqlExecutor)2 SqlExecutor (com.developmentontheedge.dbms.SqlExecutor)2 IOException (java.io.IOException)2 PrintStream (java.io.PrintStream)2 Path (java.nio.file.Path)2 MojoFailureException (org.apache.maven.plugin.MojoFailureException)2 Test (org.junit.Test)2 FreemarkerSqlException (com.developmentontheedge.be5.metadata.exception.FreemarkerSqlException)1 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)1 WriteException (com.developmentontheedge.be5.metadata.exception.WriteException)1 FreemarkerSqlHandler (com.developmentontheedge.be5.metadata.freemarker.FreemarkerSqlHandler)1 BeConnectionProfile (com.developmentontheedge.be5.metadata.model.BeConnectionProfile)1 ParseResult (com.developmentontheedge.be5.metadata.model.ParseResult)1 NullLogger (com.developmentontheedge.be5.metadata.util.NullLogger)1