1   package com.atlassian.maven.plugins.amps.codegen.prompter.common;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import com.atlassian.maven.plugins.amps.codegen.annotations.ModuleCreatorClass;
7   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter;
8   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
9   import com.atlassian.plugins.codegen.modules.common.RESTModuleCreator;
10  import com.atlassian.plugins.codegen.modules.common.RESTProperties;
11  import com.atlassian.plugins.codegen.util.ClassnameUtil;
12  
13  import org.codehaus.plexus.components.interactivity.Prompter;
14  import org.codehaus.plexus.components.interactivity.PrompterException;
15  
16  /**
17   * @since 3.6
18   */
19  @ModuleCreatorClass(RESTModuleCreator.class)
20  public class RESTPrompter extends AbstractModulePrompter<RESTProperties>
21  {
22  
23      public RESTPrompter(Prompter prompter)
24      {
25          super(prompter);
26  
27      }
28  
29      @Override
30      public RESTProperties promptForBasicProperties(PluginModuleLocation moduleLocation) throws PrompterException
31      {
32          String className = promptJavaClassname("Enter New Classname", "MyRestResource");
33          String packageName = promptJavaPackagename("Enter Package Name", getDefaultBasePackage() + ".rest");
34  
35          String fqClass = ClassnameUtil.fullyQualifiedName(packageName, className);
36  
37          RESTProperties props = new RESTProperties(fqClass);
38  
39          props.setPath(promptNotBlank("Enter REST Path", props.getPath()));
40          props.setVersion(promptNotBlank("Enter Version", props.getVersion()));
41  
42          return props;
43      }
44  
45      @Override
46      public void promptForAdvancedProperties(RESTProperties props, PluginModuleLocation moduleLocation) throws PrompterException
47      {
48          props.setPackagesToScan(promptForList("Add Package To Scan?", "Enter Package"));
49  
50          List<String> dispatchers = promptForDispatchers(props.allowedDispatchers());
51          if (dispatchers.size() > 0)
52          {
53              props.setDispatchers(dispatchers);
54          }
55      }
56  
57      private List<String> promptForDispatchers(List<String> allowedDispatchers) throws PrompterException
58      {
59          List<String> dispatchers = new ArrayList<String>();
60          List<String> mutableValues = new ArrayList<String>(allowedDispatchers);
61  
62          promptForDispatcher(dispatchers, mutableValues);
63  
64          return dispatchers;
65      }
66  
67      private void promptForDispatcher(List<String> dispatchers, List<String> allowedDispatchers) throws PrompterException
68      {
69          boolean addDispatcher = promptForBoolean("Add Dispatcher?", "N");
70  
71          if (addDispatcher)
72          {
73              StringBuilder dispatcherQuery = new StringBuilder("Choose A Dispatcher\n");
74              List<String> indexChoices = new ArrayList<String>(allowedDispatchers.size());
75              int index = 1;
76              for (String dispatcher : allowedDispatchers)
77              {
78                  String strIndex = Integer.toString(index);
79                  dispatcherQuery.append(strIndex + ": " + dispatcher + "\n");
80                  indexChoices.add(strIndex);
81                  index++;
82              }
83  
84              dispatcherQuery.append("Choose a number: ");
85              String dispatcherAnswer = prompt(dispatcherQuery.toString(), indexChoices, "1");
86              int selectedIndex = Integer.parseInt(dispatcherAnswer) - 1;
87  
88              String selectedDispatcher = allowedDispatchers.get(selectedIndex);
89  
90              dispatchers.add(selectedDispatcher);
91              allowedDispatchers.remove(selectedIndex);
92  
93              promptForDispatcher(dispatchers, allowedDispatchers);
94          }
95      }
96  }