/*
 * 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.contrib.nestedpagesmigrator.internal.job;

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

import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.InstantiationStrategy;
import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.contrib.nestedpagesmigrator.MigrationPlanTree;
import org.xwiki.contrib.nestedpagesmigrator.internal.pages.PagesMigrationPlanCreator;
import org.xwiki.contrib.nestedpagesmigrator.internal.preferences.PreferencesMigrationPlanCreator;
import org.xwiki.contrib.nestedpagesmigrator.internal.rights.RightsMigrationPlanCreator;
import org.xwiki.job.AbstractJob;
import org.xwiki.job.Job;
import org.xwiki.job.event.status.JobProgressManager;
import org.xwiki.job.event.status.JobStatus;

/**
 * @version $Id: $
 */
@Component
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
@Named(MigrationPlanCreatorJob.JOB_TYPE)
public class MigrationPlanCreatorJob extends AbstractJob<MigrationPlanRequest, MigrationPlanCreatorJobStatus>
{
    /**
     * The job type.
     */
    public static final String JOB_TYPE = "npmig";
    
    @Inject
    private ComponentManager componentManager;

    @Inject
    private JobProgressManager progressManager;

    @Inject
    private RightsMigrationPlanCreator rightsMigrationPlanCreator;

    @Override
    protected void runInternal() throws Exception
    {
        progressManager.pushLevelProgress(3, this);

        // Step 1: convert pages
        progressManager.startStep(this);
        PagesMigrationPlanCreator pagesMigrationPlanCreator
                = componentManager.getInstance(PagesMigrationPlanCreator.class);
        MigrationPlanTree plan = pagesMigrationPlanCreator.computeMigrationPlan(request.getConfiguration());

        // Step 2: convert preferences
        progressManager.startStep(this);
        PreferencesMigrationPlanCreator preferencesMigrationPlanCreator
                = componentManager.getInstance(PreferencesMigrationPlanCreator.class);
        preferencesMigrationPlanCreator.convertPreferences(plan, request.getConfiguration());

        // Step 3: convert rights
        progressManager.startStep(this);
        rightsMigrationPlanCreator.convertRights(plan, request.getConfiguration());

        // End
        getStatus().setPlan(plan);
        progressManager.popLevelProgress(this);
    }

    @Override
    public String getType()
    {
        return JOB_TYPE;
    }

    @Override
    protected MigrationPlanCreatorJobStatus createNewStatus(MigrationPlanRequest request)
    {
        Job currentJob = this.jobContext.getCurrentJob();
        JobStatus currentJobStatus = currentJob != null ? currentJob.getStatus() : null;
        return new MigrationPlanCreatorJobStatus(request, currentJobStatus, this.observationManager,
                this.loggerManager);
    }
}

@@ -1207,40 +1207,52 @@
 i.co
-mponent.manager.ComponentManager
+ntrib.nestedpagesmigrator.MigrationException
 ;%0Aim
@@ -1652,62 +1652,8 @@
 ob;%0A
-import org.xwiki.job.event.status.JobProgressManager;%0A
 impo
@@ -2048,129 +2048,8 @@
    %0A
-    @Inject%0A    private ComponentManager componentManager;%0A%0A    @Inject%0A    private JobProgressManager progressManager;%0A%0A
     
@@ -2190,24 +2190,42 @@
 ption%0A    %7B%0A
+        try %7B%0A    
         prog
@@ -2265,32 +2265,36 @@
 this);%0A%0A        
+    
 // Step 1: conve
@@ -2302,16 +2302,20 @@
 t pages%0A
+    
         
@@ -2355,16 +2355,20 @@
         
+    
 PagesMig
@@ -2419,32 +2419,36 @@
                 
+   
+ 
 = componentManag
@@ -2488,32 +2488,36 @@
 Creator.class);%0A
+    
         Migratio
@@ -2610,32 +2610,36 @@
 on());%0A%0A        
+ 
+   
 // Step 2: conve
@@ -2653,32 +2653,36 @@
 erences%0A        
+   
+ 
 progressManager.
@@ -2690,32 +2690,36 @@
 tartStep(this);%0A
+    
         Preferen
@@ -2782,32 +2782,36 @@
                 
+ 
+   
 = componentManag
@@ -2857,32 +2857,36 @@
 Creator.class);%0A
+    
         preferen
@@ -2964,32 +2964,36 @@
 on());%0A%0A        
+    
 // Step 3: conve
@@ -3002,32 +3002,36 @@
  rights%0A        
+  
+  
 progressManager.
@@ -3039,32 +3039,36 @@
 tartStep(this);%0A
+    
         rightsMi
@@ -3144,16 +3144,20 @@
         
+    
 // End%0A 
@@ -3163,16 +3163,20 @@
         
+   
+ 
 getStatu
@@ -3194,16 +3194,20 @@
 (plan);%0A
+    
         
@@ -3242,24 +3242,145 @@
 ress(this);%0A
+        %7D catch (MigrationException e) %7B%0A            logger.error(%22Failed to compute the migration plan.%22, e);%0A        %7D%0A
     %7D%0A%0A    @

Handle page rights.