1   package com.atlassian.maven.plugins.amps.codegen.prompter.jira;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   
8   import com.atlassian.maven.plugins.amps.codegen.annotations.ModuleCreatorClass;
9   import com.atlassian.maven.plugins.amps.codegen.jira.CustomFieldSearcherFactory;
10  import com.atlassian.maven.plugins.amps.codegen.prompter.common.AbstractResourcePrompter;
11  import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
12  import com.atlassian.plugins.codegen.modules.common.Resource;
13  import com.atlassian.plugins.codegen.modules.jira.CustomFieldSearcherModuleCreator;
14  import com.atlassian.plugins.codegen.modules.jira.CustomFieldSearcherProperties;
15  
16  import org.codehaus.plexus.components.interactivity.Prompter;
17  import org.codehaus.plexus.components.interactivity.PrompterException;
18  
19  /**
20   * @since 3.6
21   */
22  @ModuleCreatorClass(CustomFieldSearcherModuleCreator.class)
23  public class CustomFieldSearcherPrompter extends AbstractResourcePrompter<CustomFieldSearcherProperties>
24  {
25  
26      public static final String CUSTOM_SEARCHER = "Custom Searcher Class";
27  
28      public CustomFieldSearcherPrompter(Prompter prompter)
29      {
30          super(prompter);
31  
32      }
33  
34      @Override
35      public CustomFieldSearcherProperties promptForBasicProperties(PluginModuleLocation moduleLocation) throws PrompterException
36      {
37  
38          CustomFieldSearcherProperties props = new CustomFieldSearcherProperties(promptForSearcherClass());
39          if (props.getClassId().getPackage()
40                  .equals("com.atlassian.jira.issue.customfields.searchers"))
41          {
42              props.setGenerateClass(false);
43          } else
44          {
45              props.setGenerateClass(true);
46          }
47  
48          props.setValidCustomFieldPackage(promptNotBlank("Enter Valid CustomField Package", getDefaultBasePackage()));
49          props.setValidCustomFieldKey(promptNotBlank("Enter Valid CustomField Key"));
50  
51          List<Resource> resources = new ArrayList<Resource>(1);
52  
53          String templatePath = "/templates/customfields/" + props.getModuleKey() + "/";
54  
55          Resource search = new Resource();
56          search.setName("search");
57          search.setType("velocity");
58          search.setLocation(templatePath + "search.vm");
59  
60          resources.add(search);
61  
62          props.setResources(resources);
63  
64          return props;
65      }
66  
67      @Override
68      public void promptForAdvancedProperties(CustomFieldSearcherProperties props, PluginModuleLocation moduleLocation) throws PrompterException
69      {
70  
71          props.setResources(promptForResources());
72      }
73  
74      @Override
75      protected Resource promptForResource() throws PrompterException
76      {
77          Resource resource = new Resource();
78          resource.setName(promptNotBlank("Enter Resource Name"));
79  
80          resource.setType("velocity");
81          resource.setLocation(promptNotBlank("Enter Location (path to resource file)"));
82  
83          return resource;
84      }
85  
86      protected String promptForSearcherClass() throws PrompterException
87      {
88          String fqProvider = "";
89          Map<String, String> searchers = CustomFieldSearcherFactory.getAvailableCustomFieldSearchers();
90          if (searchers.isEmpty())
91          {
92              fqProvider = promptFullyQualifiedJavaClass("Enter Fully Qualified Searcher Class", getDefaultBasePackage() + ".jira.customfields.MyCustomFieldSearcher");
93          } else
94          {
95  
96              StringBuilder contextQuery = new StringBuilder("Choose A Searcher Class\n");
97              List<String> indexChoices = new ArrayList<String>(searchers.size());
98              Map<String, String> indexedValues = new HashMap<String, String>();
99              int index = 1;
100             String strIndex;
101             for (Map.Entry<String, String> entry : searchers.entrySet())
102             {
103                 strIndex = Integer.toString(index);
104                 contextQuery.append(strIndex + ": " + entry.getKey() + "\n");
105                 indexChoices.add(strIndex);
106                 indexedValues.put(strIndex, entry.getValue());
107                 index++;
108             }
109 
110             strIndex = Integer.toString(index);
111             contextQuery.append(strIndex + ": " + CUSTOM_SEARCHER + "\n");
112             indexChoices.add(strIndex);
113             indexedValues.put(strIndex, CUSTOM_SEARCHER);
114 
115             contextQuery.append("Choose a number: ");
116             String contextAnswer = prompt(contextQuery.toString(), indexChoices, "");
117             int answerInt = (Integer.parseInt(contextAnswer) - 1);
118 
119             if (answerInt < (searchers.size()))
120             {
121                 fqProvider = indexedValues.get(contextAnswer);
122             } else
123             {
124                 fqProvider = promptFullyQualifiedJavaClass("Enter Fully Qualified Searcher Class", getDefaultBasePackage() + ".jira.customfields.MyCustomFieldSearcher");
125             }
126         }
127 
128         return fqProvider;
129     }
130 }