package hudson.plugins.emailext;

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Hudson;
import hudson.model.TaskListener;
import hudson.plugins.emailext.plugins.content.JellyScriptContent;
import hudson.plugins.emailext.plugins.content.ScriptContent;
import hudson.util.FormValidation;
import java.io.File;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.bind.JavaScriptMethod;

/**
 *
 * @author acearl
 */
public class EmailExtTemplateAction implements Action {
    
    private final AbstractProject<?,?> project;

    public EmailExtTemplateAction(AbstractProject<?,?> project) {
        this.project = project;        
    }
    
    public String getIconFileName() {
        return "/plugin/email-ext/images/template-debugger.png";
    }

    public String getDisplayName() {
        return Messages.EmailExtTemplateAction_DisplayName();
    }

    public String getUrlName() {
        return "templateTest";
    }   
    
    private String renderError(Exception ex) {
        StringBuilder builder = new StringBuilder();
        builder.append("<h3>An error occured trying to render the template:</h3><br/>");
        builder.append("<span style=\"color:red; font-weight:bold\">");
        builder.append(ex.toString().replace("\n", "<br/>"));
        builder.append("</span>");
        return builder.toString();
    }
    
    public FormValidation doTemplateFileCheck(@QueryParameter final String value) {
        if(!StringUtils.isEmpty(value)) {
            final File scriptsFolder = new File(Hudson.getInstance().getRootDir(), "email-templates");
            final File scriptFile = new File(scriptsFolder, value);
            if(!scriptFile.exists()) {
                return FormValidation.error("The file '" + value + "' does not exist");
            }
        }
        return FormValidation.ok();
    }
    
    @JavaScriptMethod
    public String renderTemplate(String templateFile, String buildId) {
        String result;
        
        try {
            AbstractBuild<?,?> build = project.getBuild(buildId);
            if(templateFile.endsWith(".jelly")) {
                JellyScriptContent jellyContent = new JellyScriptContent();
                jellyContent.template = templateFile;
                result = jellyContent.evaluate(build, TaskListener.NULL, "JELLY_SCRIPT");
            } else {
                ScriptContent scriptContent = new ScriptContent();
                scriptContent.template = templateFile;                
                result = scriptContent.evaluate(build, TaskListener.NULL, "SCRIPT");
            }
        } catch (Exception ex) {
            result = renderError(ex);
        } 
        
        return result;
    }
    
    public AbstractProject<?, ?> getProject() {
        return project;
    }
}

@@ -27,16 +27,67 @@
 ilext;%0A%0A
+import hudson.ExtensionList;%0Aimport hudson.Plugin;%0A
 import h
@@ -436,43 +436,242 @@
 ort 
-org.apache.commons.lang.StringUtils
+java.util.Collection;%0Aimport java.util.Collections;%0Aimport jenkins.model.Jenkins;%0Aimport org.apache.commons.lang.StringUtils;%0Aimport org.jenkinsci.lib.configprovider.ConfigProvider;%0Aimport org.jenkinsci.lib.configprovider.model.Config
 ;%0Aim
@@ -1838,24 +1838,147 @@
 y(value)) %7B%0A
+            if(value.startsWith(%22managed:%22)) %7B%0A                return checkForManagedFile(value);%0A            %7D else %7B%0A    
             
@@ -2072,32 +2072,36 @@
 %22);%0A            
+  
+  
 final File scrip
@@ -2148,24 +2148,28 @@
             
+    
 if(!scriptFi
@@ -2191,32 +2191,36 @@
                 
+ 
+   
 return FormValid
@@ -2279,34 +2279,42 @@
 %22);%0A            
+   
+ 
 %7D%0A
+    
         %7D%0A      
@@ -2306,29 +2306,39 @@
        %7D%0A   
+ 
+    %7D%0A
      
+   
 return FormV
@@ -2360,24 +2360,1461 @@
 %0A    %7D%0A    %0A
+    private FormValidation checkForManagedFile(final String value) %7B%0A        Plugin plugin = Jenkins.getInstance().getPlugin(%22config-file-provider%22);%0A        if(plugin != null) %7B%0A            Config config = null;%0A            Collection%3CConfigProvider%3E providers = getTemplateConfigProviders();%0A            for(ConfigProvider provider : providers) %7B%0A                for(Config c : provider.getAllConfigs()) %7B%0A                    if(c.name.equalsIgnoreCase(value) && provider.isResponsibleFor(c.id)) %7B%0A                        return FormValidation.ok();%0A                    %7D                    %0A                %7D            %0A            %7D%0A        %7D else %7B%0A            return FormValidation.error(Messages.EmailExtTemplateAction_ConfigFileProviderNotAvailable());%0A        %7D%0A        return FormValidation.error(Messages.EmailExtTemplateAction_ManagedTemplateNotFound());%0A    %7D%0A    %0A    private static Collection%3CConfigProvider%3E getTemplateConfigProviders() %7B%0A        Collection%3CConfigProvider%3E providers = Collections.emptyList();%0A        ExtensionList%3CConfigProvider%3E all = ConfigProvider.all();%0A        ConfigProvider p = all.get(GroovyTemplateConfig.GroovyTemplateConfigProvider.class);%0A        if(p != null) %7B%0A            providers.add(p);%0A        %7D%0A        %0A        p = all.get(JellyTemplateConfig.JellyTemplateConfigProvider.class);%0A        if(p != null) %7B%0A            providers.add(p);%0A        %7D%0A        return providers;%0A    %7D%0A    %0A
     @JavaScr

Support managed files.