/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.easyant.tasks;

import java.util.Vector;

import org.apache.easyant.core.EasyAntConstants;
import org.apache.ivy.Ivy;
import org.apache.ivy.ant.IvyTask;
import org.apache.ivy.core.module.id.ModuleRevisionId;
import org.apache.ivy.core.resolve.ResolvedModuleRevision;
import org.apache.ivy.core.settings.IvySettings;
import org.apache.ivy.plugins.matcher.PatternMatcher;
import org.apache.ivy.plugins.resolver.DependencyResolver;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.input.InputHandler;
import org.apache.tools.ant.input.InputRequest;
import org.apache.tools.ant.input.MultipleChoiceInputRequest;

public class SearchModule extends IvyTask {

    private String organisation;

    private String module;

    private String branch = PatternMatcher.ANY_EXPRESSION;

    private String revision;

    private String matcher = PatternMatcher.EXACT_OR_REGEXP;

    private String propertyPrefix;
    private String resolver;

    public void doExecute() throws BuildException {
        if (getOrganisation() == null) {
            throw new BuildException(
                    "no organisation provided for SearchModule task");
        }
        if (getModule() == null) {
            throw new BuildException(
                    "no module name provided for SearchModule task");
        }
        if (getRevision() == null) {
            throw new BuildException(
                    "no revision provided for SearchModule task");
        }

        if (getPropertyPrefix() == null) {
            throw new BuildException(
                    "no property prefix provided provided for SearchModule task");
        }
        Ivy ivy = getIvyInstance();
        IvySettings settings = ivy.getSettings();

        // search all modules revision matching the requested criteria
        DependencyResolver resolverToCheck;
        if (getResolver() != null) {
            resolverToCheck = settings.getResolver(getResolver());
        } else {
            resolverToCheck = settings.getDefaultResolver();
        }
        ModuleRevisionId mridToSearch = ModuleRevisionId.newInstance(
                getOrganisation(), getModule(), getBranch(), getRevision());
        ModuleRevisionId[] mrids = ivy.getSearchEngine().listModules(
                resolverToCheck, mridToSearch, settings.getMatcher(matcher));

        // diplay the list
        Vector<String> choices = new Vector<String>();
        for (int i = 0; i < mrids.length; i++) {
            choices.add(String.valueOf(i));
            StringBuilder sb = new StringBuilder();
            sb.append(i).append(": ");
            sb.append(mrids[i].getName());
            sb.append(" v").append(mrids[i].getRevision());
            // hide organization if its the default one
            if (!mrids[i].getOrganisation().equals(
                    EasyAntConstants.EASYANT_SKELETONS_ORGANISATION))
                sb.append(" by ").append(mrids[i].getOrganisation());
            // Get the description
            ResolvedModuleRevision rmr = ivy.findModule(mrids[i]);
            if (rmr.getDescriptor().getDescription() != null
                    && !rmr.getDescriptor().getDescription().equals("")) {
                sb.append(" (").append(rmr.getDescriptor().getDescription())
                        .append(")");
            }
            log(sb.toString());

        }
        
        
        // ask end user to select a module
        //TODO handle a default value
        Integer value = new Integer(getInput("Choose a number:", null, choices));
        ModuleRevisionId moduleToRetrieve = mrids[value];

        // set final properties
        getProject().setProperty(getPropertyPrefix() + ".org",
                moduleToRetrieve.getOrganisation());
        getProject().setProperty(getPropertyPrefix() + ".module",
                moduleToRetrieve.getName());
        getProject().setProperty(getPropertyPrefix() + ".rev",
                moduleToRetrieve.getRevision());

    }

    protected String getInput(String message, String defaultvalue, Vector<String> choices) {
        InputRequest request = null;
        request = new MultipleChoiceInputRequest(message, choices);
        request.setDefaultValue(defaultvalue);

        InputHandler h = getProject().getInputHandler();

        h.handleInput(request);

        String value = request.getInput();
        if ((value == null || value.trim().length() == 0)
                && defaultvalue != null) {
            value = defaultvalue;
        }
        return value;

    }

    public String getMatcher() {
        return matcher;
    }

    public void setMatcher(String matcher) {
        this.matcher = matcher;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getOrganisation() {
        return organisation;
    }

    public void setOrganisation(String organisation) {
        this.organisation = organisation;
    }

    public String getRevision() {
        return revision;
    }

    public void setRevision(String revision) {
        this.revision = revision;
    }

    public String getBranch() {
        return branch;
    }

    public void setBranch(String branch) {
        this.branch = branch;
    }

    public String getPropertyPrefix() {
        return propertyPrefix;
    }

    public void setPropertyPrefix(String propertyPrefix) {
        this.propertyPrefix = propertyPrefix;
    }

    public String getResolver() {
        return resolver;
    }

    public void setResolver(String resolver) {
        this.resolver = resolver;
    }

}

@@ -3536,28 +3536,19 @@
 ion());%0A
-            
+%09%09%09
 // hide 
@@ -3587,146 +3587,112 @@
 one%0A
-            if (!mrids%5Bi%5D.getOrganisation().equals(%0A                    EasyAntConstants.EASYANT_SKELETONS_ORGANISATION))%0A                
+%09%09%09if (!EasyAntConstants.EASYANT_SKELETONS_ORGANISATION%0A%09%09%09%09%09.equals(mrids%5Bi%5D.getOrganisation())) %7B%0A%09%09%09%09
 sb.a
@@ -3733,32 +3733,37 @@
 rganisation());%0A
+%09%09%09%7D%0A
             // G

Fix potential null pointer