/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.extension.distribution.internal;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.xwiki.bridge.event.WikiReadyEvent;
import org.xwiki.component.annotation.Component;
import org.xwiki.extension.distribution.internal.DistributionManager.DistributionState;
import org.xwiki.observation.EventListener;
import org.xwiki.observation.event.ApplicationStartedEvent;
import org.xwiki.observation.event.Event;

/**
 * @version $Id$
 * @since 4.2M3
 */
@Component
@Named("DistributionInitializerListener")
public class DistributionInitializerListener implements EventListener
{
    /**
     * The list of events to listen to.
     */
    private static final List<Event> EVENTS = Arrays
        .<Event> asList(new ApplicationStartedEvent(), new WikiReadyEvent());

    /**
     * The component used to get information about the current distribution.
     */
    @Inject
    private DistributionManager distributionManager;

    /**
     * The object used to log messages.
     */
    @Inject
    private Logger logger;

    @Override
    public List<Event> getEvents()
    {
        return EVENTS;
    }

    @Override
    public String getName()
    {
        return "DistributionInitializerListener";
    }

    @Override
    public void onEvent(Event event, Object arg1, Object arg2)
    {
        DistributionState distributionState = this.distributionManager.getFarmDistributionState();

        if (distributionState != DistributionState.NONE) {
            if (event instanceof ApplicationStartedEvent) {
                this.distributionManager.startFarmJob();
            } else {
                String wiki = ((WikiReadyEvent) event).getWikiId();
                this.distributionManager.startWikiJob(wiki);
            }
        }
    }
}

@@ -1048,33 +1048,8 @@
 d;%0A%0A
-import org.slf4j.Logger;%0A
 impo
@@ -1074,25 +1074,31 @@
 e.event.
-WikiReady
+ActionExecuting
 Event;%0Ai
@@ -1380,19 +1380,96 @@
 Event;%0A%0A
-/**
+import com.xpn.xwiki.XWikiContext;%0A%0A/**%0A * Initialize farm distribution job.%0A * 
 %0A * @ver
@@ -1731,25 +1731,16 @@
 = Arrays
-%0A        
 .%3CEvent%3E
@@ -1781,28 +1781,48 @@
 t(),
- new WikiReadyEvent(
+%0A        new ActionExecutingEvent(%22view%22
 ));%0A
@@ -1985,104 +1985,8 @@
 r;%0A%0A
-    /**%0A     * The object used to log messages.%0A     */%0A    @Inject%0A    private Logger logger;%0A%0A
     
@@ -2587,44 +2587,120 @@
 = ((
+X
 Wiki
-ReadyEvent) event).getWikiId();%0A
+Context) arg2).getDatabase();%0A                if (this.distributionManager.getWikiJob(wiki) == null) %7B%0A    
     
@@ -2756,16 +2756,34 @@
 (wiki);%0A
+                %7D%0A
         

Introduce per wiki distribution status * initialize wiki distribution job only when a user access the wiki in view mode for the first time (intead of when the wiki database is initialized)