/*
 * 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.job.step;

import java.util.Collection;

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

import org.slf4j.Logger;
import org.xwiki.component.annotation.Component;
import org.xwiki.component.annotation.InstantiationStrategy;
import org.xwiki.component.descriptor.ComponentInstantiationStrategy;
import org.xwiki.extension.Extension;
import org.xwiki.extension.ExtensionId;
import org.xwiki.extension.InstalledExtension;
import org.xwiki.extension.distribution.internal.DistributionManager;
import org.xwiki.extension.repository.InstalledExtensionRepository;
import org.xwiki.extension.repository.result.IterableResult;
import org.xwiki.extension.repository.search.ExtensionQuery;
import org.xwiki.extension.repository.search.ExtensionQuery.COMPARISON;
import org.xwiki.wiki.descriptor.WikiDescriptorManager;
import org.xwiki.wiki.manager.WikiManagerException;

/**
 * Install and upgrade flavor extension.
 * 
 * @version $Id$
 * @since 7.1M1
 */
@Component
@Named(FlavorDistributionStep.ID)
@InstantiationStrategy(ComponentInstantiationStrategy.PER_LOOKUP)
public class FlavorDistributionStep extends AbstractDistributionStep
{
    public static final String ID = "extension.flavor";

    @Inject
    private transient InstalledExtensionRepository installedRepository;

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

    @Inject
    private transient Logger logger;

    public FlavorDistributionStep()
    {
        super(ID);
    }

    @Override
    public void prepare()
    {
        if (getState() == null) {
            setState(State.COMPLETED);

            ExtensionQuery extensionQuery = new ExtensionQuery();
            extensionQuery.addFilter(field, "flavor", COMPARISON.EQUAL);

            String namespace = "wiki:" + getWiki();

            IterableResult<InstalledExtension> result = this.installedRepository.searchInstalledExtensions(namespace, extensionQuery);

            if (result.getSize() > 0) {
                InstalledExtension extension = result.iterator().next();
                setState(null);
            }
            
            
            
            
            
            if (isMainWiki()) {
                WikiDescriptorManager wikiDescriptorManager = this.wikiDescriptorManagerProvider.get();

                Collection<String> wikiIds;
                try {
                    wikiIds = wikiDescriptorManager.getAllIds();
                } catch (WikiManagerException e) {
                    this.logger.error("Failed to get the list of wikis", e);
                    setState(null);
                    return;
                }

                ExtensionId wikiExtensionUI = this.distributionManager.getWikiUIExtensionId();

                for (String wikiId : wikiIds) {
                    if (!wikiDescriptorManager.getMainWikiId().equals(wikiId)) {
                        String namespace = "wiki:" + wikiId;

                        // Only if the UI is not already installed
                        if (wikiExtensionUI != null) {
                            InstalledExtension installedExtension =
                                this.installedRepository.getInstalledExtension(wikiExtensionUI.getId(), namespace);
                            if (installedExtension == null
                                || !installedExtension.getId().getVersion().equals(wikiExtensionUI.getVersion())) {
                                setState(null);
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
}

@@ -952,38 +952,8 @@
 p;%0A%0A
-import java.util.Collection;%0A%0A
 impo
@@ -1251,48 +1251,8 @@
 on;%0A
-import org.xwiki.extension.ExtensionId;%0A
 impo
@@ -1647,88 +1647,42 @@
 iki.
-wiki.descriptor.WikiDescriptorManager;%0Aimport org.xwiki.wiki.manager.WikiManager
+extension.repository.search.Search
 Exce
@@ -2484,789 +2484,305 @@
     
-setState(State.COMPLETED);%0A%0A            ExtensionQuery extensionQuery = new ExtensionQuery();%0A            extensionQuery.addFilter(field, %22flavor%22, COMPARISON.EQUAL);%0A%0A            String namespace = %22wiki:%22 + getWiki();%0A%0A            IterableResult%3CInstalledExtension%3E result = this.installedRepository.searchInstalledExtensions(namespace, extensionQuery);%0A%0A            if (result.getSize() %3E 0) %7B%0A                InstalledExtension extension = result.iterator().next();%0A                setState(null);%0A            %7D%0A            %0A            %0A            %0A            %0A            %0A            if (isMainWiki()) %7B%0A                WikiDescriptorManager wikiDescriptorManager = this.wikiDescriptorManagerProvider.get();%0A%0A                Collection%3CString%3E wikiIds;%0A                try
+InstalledExtension flavor = getFlavor();%0A%0A            // If the extension is invalid it probably means it needs to be upgraded%0A            if (flavor.isValid(getNamespace())) %7B%0A                setState(State.COMPLETED);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private InstalledExtension getFlavor()%0A   
  %7B%0A 
@@ -2792,784 +2792,243 @@
     
- 
-           wikiIds = wikiDescriptorManager.getAllIds();%0A                %7D catch (WikiManagerException e) %7B%0A                    this.logger.error(%22Failed to get the list of wikis%22, e);%0A                    setState(null);%0A                    return;%0A                %7D%0A%0A                ExtensionId wikiExtensionUI = this.distributionManager.getWikiUIExtensionId();%0A%0A                for (String wikiId : wikiIds) %7B%0A                    if (!wikiDescriptorManager.getMainWikiId().equals(wikiId)) %7B%0A                        String namespace = %22wiki:%22 + wikiId;%0A%0A                        // Only if the UI is not already installed%0A                        if (wikiExtensionUI != null) %7B%0A                            InstalledExtension installedExtension =%0A                               
+IterableResult%3CInstalledExtension%3E result;%0A        try %7B%0A            ExtensionQuery extensionQuery = new ExtensionQuery();%0A            extensionQuery.addFilter(Extension.FIELD_CATEGORY, %22flavor%22, COMPARISON.EQUAL);%0A            result =
  thi
@@ -3049,19 +3049,22 @@
 ository.
-get
+search
 Installe
@@ -3077,244 +3077,188 @@
 sion
-(wikiExtensionUI.getId(), namespace);%0A                            if (installedExtension == null%0A                                %7C%7C !installedExtension.getId().getVersion().equals(wikiExtensionUI.getVersion())) %7B%0A               
+s(getNamespace(), extensionQuery);%0A        %7D catch (SearchException e) %7B%0A            this.logger.error(%22Failed to search installed extension%22, e);%0A            return null;%0A
         
     
@@ -3249,32 +3249,35 @@
 ll;%0A        
+%7D%0A%0A
         
  setState(nu
@@ -3268,184 +3268,112 @@
     
- setState(null);%0A                                return;%0A                            %7D%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D%0A        %7D
+if (result.getSize() %3E 0) %7B%0A            return result.iterator().next();%0A        %7D%0A%0A        return null;
 %0A   

Allow selecting a flavor when installing a wiki