Coverage Report - org.apache.maven.archetype.ui.generation.DefaultArchetypeSelectionQueryer
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultArchetypeSelectionQueryer
65%
49/75
71%
27/38
5
 
 1  
 package org.apache.maven.archetype.ui.generation;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import org.apache.commons.lang.math.NumberUtils;
 23  
 import org.apache.maven.archetype.catalog.Archetype;
 24  
 import org.apache.maven.archetype.ui.ArchetypeDefinition;
 25  
 import org.apache.maven.artifact.versioning.ArtifactVersion;
 26  
 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
 27  
 import org.codehaus.plexus.component.annotations.Component;
 28  
 import org.codehaus.plexus.component.annotations.Requirement;
 29  
 import org.codehaus.plexus.components.interactivity.Prompter;
 30  
 import org.codehaus.plexus.components.interactivity.PrompterException;
 31  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 32  
 
 33  
 import java.util.ArrayList;
 34  
 import java.util.Arrays;
 35  
 import java.util.HashMap;
 36  
 import java.util.HashSet;
 37  
 import java.util.List;
 38  
 import java.util.Map;
 39  
 import java.util.Set;
 40  
 import java.util.SortedMap;
 41  
 import java.util.TreeMap;
 42  
 
 43  
 @Component( role = ArchetypeSelectionQueryer.class )
 44  20
 public class DefaultArchetypeSelectionQueryer
 45  
     extends AbstractLogEnabled
 46  
     implements ArchetypeSelectionQueryer
 47  
 {
 48  
     @Requirement( hint = "archetype" )
 49  
     private Prompter prompter;
 50  
 
 51  
     public boolean confirmSelection( ArchetypeDefinition archetypeDefinition )
 52  
         throws PrompterException
 53  
     {
 54  0
         String query =
 55  
             "Confirm archetype selection: \n" + archetypeDefinition.getGroupId() + "/" + archetypeDefinition.getName()
 56  
                 + "\n";
 57  
 
 58  0
         String answer = prompter.prompt( query, Arrays.asList( new String[]{ "Y", "N" } ), "Y" );
 59  
 
 60  0
         return "Y".equalsIgnoreCase( answer );
 61  
     }
 62  
 
 63  
     public Archetype selectArchetype( Map<String, List<Archetype>> catalogs )
 64  
         throws PrompterException
 65  
     {
 66  4
         return selectArchetype( catalogs, null );
 67  
     }
 68  
 
 69  
     public Archetype selectArchetype( Map<String, List<Archetype>> catalogs, ArchetypeDefinition defaultDefinition )
 70  
         throws PrompterException
 71  
     {
 72  10
         Archetype selection = null;
 73  10
         Map<String, List<Archetype>> filteredCatalogs = catalogs;
 74  
 
 75  
         do
 76  
         {
 77  12
             StringBuilder query = new StringBuilder( "Choose archetype:\n" );
 78  
 
 79  12
             Set<String> archetypeKeys = new HashSet<String>();
 80  12
             List<String> answers = new ArrayList<String>();
 81  12
             Map<String, Archetype> archetypeAnswerMap = new HashMap<String, Archetype>();
 82  
 
 83  12
             int counter = 0;
 84  12
             int defaultSelection = 0;
 85  
 
 86  12
             for ( Map.Entry<String, List<Archetype>> entry : filteredCatalogs.entrySet() )
 87  
             {
 88  12
                 String catalog = entry.getKey();
 89  
 
 90  12
                 for ( Archetype archetype : entry.getValue() )
 91  
                 {
 92  22
                     String archetypeKey = archetype.getGroupId() + ":" + archetype.getArtifactId();
 93  
 
 94  22
                     if ( !archetypeKeys.add( archetypeKey ) )
 95  
                     {
 96  0
                         continue;
 97  
                     }
 98  
 
 99  22
                     counter++;
 100  
 
 101  22
                     String description = archetype.getDescription();
 102  22
                     if ( description == null )
 103  
                     {
 104  22
                         description = "-";
 105  
                     }
 106  
 
 107  22
                     String answer = String.valueOf( counter );
 108  
 
 109  22
                     query.append( answer + ": " + catalog + " -> " + archetype.getGroupId() + ":"
 110  
                         + archetype.getArtifactId() + " (" + description + ")\n" );
 111  
 
 112  22
                     answers.add( answer );
 113  
 
 114  22
                     archetypeAnswerMap.put( answer, archetype );
 115  
 
 116  
                     // the version is not tested. This is intentional.
 117  22
                     if ( defaultDefinition != null && archetype.getGroupId().equals( defaultDefinition.getGroupId() )
 118  
                         && archetype.getArtifactId().equals( defaultDefinition.getArtifactId() ) )
 119  
                     {
 120  4
                         defaultSelection = counter;
 121  
                     }
 122  22
                 }
 123  12
             }
 124  
 
 125  12
             if ( counter == 0 )
 126  
             {
 127  0
                 query.append( "   Your filter doesn't match any archetype (hint: enter to return to initial list)\n" );
 128  
             }
 129  
 
 130  12
             query.append( "Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): " );
 131  
 
 132  
             String answer;
 133  12
             if ( defaultSelection == 0 )
 134  
             {
 135  8
                 answer = prompter.prompt( query.toString() );
 136  
             }
 137  
             else
 138  
             {
 139  4
                 answer = prompter.prompt( query.toString(), Integer.toString( defaultSelection ) );
 140  
             }
 141  
 
 142  12
             if ( NumberUtils.isNumber( answer ) )
 143  
             {
 144  10
                 selection = archetypeAnswerMap.get( answer );
 145  
             }
 146  
             else
 147  
             {
 148  
                 // not a number so apply filter and ask again
 149  2
                 filteredCatalogs = ArchetypeSelectorUtils.getFilteredArchetypesByCatalog( catalogs, answer );
 150  
             }
 151  
         }
 152  12
         while ( selection == null );
 153  
 
 154  10
         return selectVersion( catalogs, selection.getGroupId(), selection.getArtifactId() );
 155  
     }
 156  
 
 157  
     private Archetype selectVersion( Map<String, List<Archetype>> catalogs, String groupId, String artifactId )
 158  
         throws PrompterException
 159  
     {
 160  10
         SortedMap<ArtifactVersion, Archetype> archetypeVersionsMap = new TreeMap<ArtifactVersion, Archetype>();
 161  
 
 162  10
         for ( Map.Entry<String, List<Archetype>> entry : catalogs.entrySet() )
 163  
         {
 164  10
             for ( Archetype archetype : entry.getValue() )
 165  
             {
 166  20
                 if ( !groupId.equals( archetype.getGroupId() ) || !artifactId.equals( archetype.getArtifactId() ) )
 167  
                 {
 168  0
                     continue;
 169  
                 }
 170  
 
 171  10
                 ArtifactVersion version = new DefaultArtifactVersion( archetype.getVersion() );
 172  
 
 173  
                 // don't override the first catalog containing a defined version of the artifact
 174  10
                 if ( !archetypeVersionsMap.containsKey( version ) )
 175  
                 {
 176  10
                     archetypeVersionsMap.put( version, archetype );
 177  
                 }
 178  10
             }
 179  
         }
 180  
 
 181  10
         if ( archetypeVersionsMap.size() == 1 )
 182  
         {
 183  10
             return archetypeVersionsMap.values().iterator().next();
 184  
         }
 185  
 
 186  
         // let the user choose between available versions
 187  0
         StringBuilder query = new StringBuilder( "Choose " + groupId + ":" + artifactId + " version: \n" );
 188  
 
 189  0
         List<String> answers = new ArrayList<String>();
 190  0
         Map<String, Archetype> answerMap = new HashMap<String, Archetype>();
 191  
 
 192  0
         int counter = 1;
 193  0
         String mapKey = null;
 194  
 
 195  0
         for ( Map.Entry<ArtifactVersion, Archetype> entry : archetypeVersionsMap.entrySet() )
 196  
         {
 197  0
             ArtifactVersion version = entry.getKey();
 198  0
             Archetype archetype = entry.getValue();
 199  
 
 200  0
             mapKey = String.valueOf( counter );
 201  
 
 202  0
             query.append( mapKey + ": " + version + "\n" );
 203  
 
 204  0
             answers.add( mapKey );
 205  
 
 206  0
             answerMap.put( mapKey, archetype );
 207  
 
 208  0
             counter++;
 209  0
         }
 210  
 
 211  0
         query.append( "Choose a number: " );
 212  
 
 213  0
         Archetype archetype = null;
 214  
 
 215  
         do
 216  
         {
 217  0
             String answer = prompter.prompt( query.toString(), answers, mapKey );
 218  
 
 219  0
             archetype = answerMap.get( answer );
 220  
         }
 221  0
         while ( archetype == null );
 222  
 
 223  0
         return archetype;
 224  
     }
 225  
 
 226  
     public void setPrompter( Prompter prompter )
 227  
     {
 228  10
         this.prompter = prompter;
 229  10
     }
 230  
 }