Search in sources :

Example 1 with FreemarkerCatalog

use of com.developmentontheedge.be5.metadata.model.FreemarkerCatalog 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 FreemarkerCatalog

use of com.developmentontheedge.be5.metadata.model.FreemarkerCatalog 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 FreemarkerCatalog

use of com.developmentontheedge.be5.metadata.model.FreemarkerCatalog 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 4 with FreemarkerCatalog

use of com.developmentontheedge.be5.metadata.model.FreemarkerCatalog 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)

Example 5 with FreemarkerCatalog

use of com.developmentontheedge.be5.metadata.model.FreemarkerCatalog in project be5 by DevelopmentOnTheEdge.

the class FreemarkerUtils method getConfiguration.

public static Configuration getConfiguration(Project project) {
    Configuration config = (Configuration) configTemplate.clone();
    config.setCacheStorage(new SoftCacheStorage());
    try {
        config.setSharedVariable("project", project);
    } catch (TemplateModelException e) {
        throw new RuntimeException("Unexpected error: " + e, e);
    }
    FreemarkerScript macroCollection = project.getMacroCollection().optScript(FreemarkerCatalog.MAIN_MACRO_LIBRARY);
    if (macroCollection != null) {
        config.addAutoInclude(macroCollection.getCompletePath().toString());
    }
    for (Module module : project.getModules()) {
        FreemarkerCatalog collection = module.getMacroCollection();
        if (collection != null) {
            FreemarkerScript script = collection.optScript(FreemarkerCatalog.MAIN_MACRO_LIBRARY);
            if (script != null) {
                config.addAutoInclude(script.getCompletePath().toString());
            }
        }
    }
    config.setTemplateLoader(new ProjectTemplateLoader(project));
    return config;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) Configuration(freemarker.template.Configuration) SoftCacheStorage(freemarker.cache.SoftCacheStorage) FreemarkerCatalog(com.developmentontheedge.be5.metadata.model.FreemarkerCatalog) FreemarkerScript(com.developmentontheedge.be5.metadata.model.FreemarkerScript) Module(com.developmentontheedge.be5.metadata.model.Module)

Aggregations

FreemarkerCatalog (com.developmentontheedge.be5.metadata.model.FreemarkerCatalog)6 FreemarkerScript (com.developmentontheedge.be5.metadata.model.FreemarkerScript)6 Module (com.developmentontheedge.be5.metadata.model.Module)3 ReadException (com.developmentontheedge.be5.metadata.exception.ReadException)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)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 DataElementPath (com.developmentontheedge.be5.metadata.model.base.DataElementPath)1 BeSqlExecutor (com.developmentontheedge.be5.metadata.sql.BeSqlExecutor)1 SqlExecutor (com.developmentontheedge.dbms.SqlExecutor)1 SoftCacheStorage (freemarker.cache.SoftCacheStorage)1 Configuration (freemarker.template.Configuration)1 TemplateModelException (freemarker.template.TemplateModelException)1 File (java.io.File)1 PrintStream (java.io.PrintStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1