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.CustomFieldTypeFactory;
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.CustomFieldModuleCreator;
14  import com.atlassian.plugins.codegen.modules.jira.CustomFieldProperties;
15  import com.atlassian.plugins.codegen.util.ClassnameUtil;
16  
17  import org.apache.commons.lang.StringUtils;
18  import org.codehaus.plexus.components.interactivity.Prompter;
19  import org.codehaus.plexus.components.interactivity.PrompterException;
20  
21  /**
22   * @since 3.5
23   */
24  @ModuleCreatorClass(CustomFieldModuleCreator.class)
25  public class CustomFieldPrompter extends AbstractResourcePrompter<CustomFieldProperties>
26  {
27  
28      public CustomFieldPrompter(Prompter prompter)
29      {
30          super(prompter);
31  
32      }
33  
34      @Override
35      public CustomFieldProperties promptForBasicProperties(PluginModuleLocation moduleLocation) throws PrompterException
36      {
37          String className = promptJavaClassname("Enter New Classname", "MyCustomField");
38          String packageName = promptJavaPackagename("Enter Package Name", getDefaultBasePackage() + ".jira.customfields");
39  
40          String fqClass = ClassnameUtil.fullyQualifiedName(packageName, className);
41  
42          CustomFieldProperties props = new CustomFieldProperties(fqClass);
43          List<Resource> resources = new ArrayList<Resource>(3);
44  
45          String templatePath = "/templates/customfields/" + props.getModuleKey() + "/";
46  
47          Resource view = new Resource();
48          view.setName("view");
49          view.setType("velocity");
50          view.setLocation(templatePath + "view.vm");
51  
52          Resource edit = new Resource();
53          edit.setName("edit");
54          edit.setType("velocity");
55          edit.setLocation(templatePath + "edit.vm");
56  
57          resources.add(view);
58          resources.add(edit);
59  
60          props.setResources(resources);
61  
62          return props;
63      }
64  
65      @Override
66      public void promptForAdvancedProperties(CustomFieldProperties props, PluginModuleLocation moduleLocation) throws PrompterException
67      {
68          String fqSuperClass = promptForSuperClass();
69          if (StringUtils.isNotBlank(fqSuperClass))
70          {
71              props.setFullyQualifiedClassToExtend(fqSuperClass);
72          }
73  
74          props.setResources(promptForResources());
75      }
76  
77      protected String promptForSuperClass() throws PrompterException
78      {
79          String fqSuperClass = "";
80  
81          Map<String, String> customFieldTypes = CustomFieldTypeFactory.getAvailableCustomFieldTypes();
82          if (!customFieldTypes.isEmpty())
83          {
84  
85              StringBuilder superQuery = new StringBuilder("Choose A Custom Field Type To Extend\n");
86              List<String> indexChoices = new ArrayList<String>(customFieldTypes.size());
87              Map<String, String> indexedValues = new HashMap<String, String>();
88              int index = 1;
89              String strIndex;
90              for (Map.Entry<String, String> entry : customFieldTypes.entrySet())
91              {
92                  strIndex = Integer.toString(index);
93                  superQuery.append(strIndex + ": " + entry.getKey() + "\n");
94                  indexChoices.add(strIndex);
95                  indexedValues.put(strIndex, entry.getValue());
96                  index++;
97              }
98  
99              strIndex = Integer.toString(index);
100 
101             superQuery.append("Choose a number: ");
102             String superAnswer = prompt(superQuery.toString(), indexChoices, "");
103             int answerInt = (Integer.parseInt(superAnswer) - 1);
104 
105             if (answerInt < (customFieldTypes.size()))
106             {
107                 fqSuperClass = indexedValues.get(superAnswer);
108             }
109         }
110 
111         return fqSuperClass;
112     }
113 
114     @Override
115     protected Resource promptForResource() throws PrompterException
116     {
117         Resource resource = new Resource();
118         resource.setName(promptNotBlank("Enter Resource Name"));
119 
120         resource.setType("velocity");
121         resource.setLocation(promptNotBlank("Enter Location (path to resource file)"));
122 
123         return resource;
124     }
125 
126 }