package org.axway.grapes.jenkins;

import hudson.FilePath;
import hudson.Plugin;
import hudson.PluginWrapper;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import hudson.tasks.Publisher;
import org.axway.grapes.commons.datamodel.Module;
import org.axway.grapes.commons.utils.FileUtils;
import org.axway.grapes.commons.utils.JsonUtils;
import org.axway.grapes.jenkins.notifications.GrapesNotification;
import org.axway.grapes.jenkins.notifications.GrapesNotificationDescriptor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.LogManager;
import java.util.logging.Logger;

/**
 * Placeholder for plugin entry point.
 * This class also acts as a container for methods used throughout the plugin.
 *
 * @author jdcoffre
 *
 */
public class GrapesPlugin extends Plugin {

    public static final String REPORT_FILE = "grapesReports/module.json";

    public static final String GRAPES_WORKING_FOLDER = "grapes";

    public static final String GRAPES_MODULE_FILE = "module.json";

    /**
     * Returns the path or URL to access web resources from this plugin.
     *
     * @return resource path
     */
    public static String getPluginResourcePath() {
        final PluginWrapper wrapper = Hudson.getInstance().getPluginManager()
                .getPlugin(GrapesPlugin.class);

        return "/plugin/" + wrapper.getShortName() + "/";
    }

    /**
     * Provides build report folder for Grapes archives
     *
     * @param build AbstractBuild
     * @return FilePath
     */
    public static FilePath getReportFolder(final AbstractBuild<?, ?> build) {
        assert build != null;
        final File reportFolder = new File(build.getRootDir(), REPORT_FILE);
        return  new FilePath(reportFolder);
    }

    /**
     * Returns Grapes module of a build
     *
     * @param moduleFile
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    public static Module getModule(final File moduleFile) throws IOException, InterruptedException {
        if (moduleFile.exists()) {
            final String serializedModule= FileUtils.read(moduleFile);
            return JsonUtils.unserializeModule(serializedModule);
        }

        return null;
    }

    /**
     * Returns Grapes Jenkins plugin logger
     *
     * @return Logger
     */
    public static Logger getLogger(){
        return LogManager.getLogManager().getLogger("hudson.WebAppMain");
    }


    /**
     * Returns the Grapes Notifier instance of the build (null if there is none)
     *
     * @param build AbstractBuild<?, ?>
     * @return GrapesNotifier
     */
    public static GrapesNotifier getGrapesNotifier(final AbstractBuild<?, ?> build){
        AbstractProject<?, ?> project = build.getProject();

        for(Publisher publisher : project.getPublishersList()){
            if(publisher instanceof GrapesNotifier){
               return (GrapesNotifier) publisher;
            }
        }

        return null;
    }

    /**
     * Returns all the Grapes notifications of the build
     *
     * @param build
     * @return
     */
    public static List<GrapesNotification> getAllNotifications(final AbstractBuild<?, ?> build) {
        final List<GrapesNotification> notifications = new ArrayList<GrapesNotification>();

        for(GrapesNotificationDescriptor notifDescriptor: GrapesNotificationDescriptor.all()){
            final GrapesNotification notification = notifDescriptor.newAutoInstance(build);

            if(notification != null){
                notifications.add(notification);
            }
        }

        return notifications;
    }
}

@@ -1097,24 +1097,232 @@
 ule.json%22;%0A%0A
+    /**%0A     * Returns Grapes Jenkins plugin logger%0A     *%0A     * @return Logger%0A     */%0A    public static Logger getLogger()%7B%0A        return LogManager.getLogManager().getLogger(%22hudson.WebAppMain%22);%0A    %7D%0A%0A
     /**%0A    
@@ -2069,40 +2069,44 @@
   * 
-Returns Grapes module of a build
+Un-serialize a Module from Json file
 %0A   
@@ -2536,216 +2536,8 @@
  %7D%0A%0A
-    /**%0A     * Returns Grapes Jenkins plugin logger%0A     *%0A     * @return Logger%0A     */%0A    public static Logger getLogger()%7B%0A        return LogManager.getLogManager().getLogger(%22hudson.WebAppMain%22);%0A    %7D%0A%0A
 %0A   

Enhance javadoc