1   package com.atlassian.maven.plugins.amps.codegen.prompter.jira;
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.jira.ActionTypeFactory;
8   import com.atlassian.maven.plugins.amps.codegen.prompter.AbstractModulePrompter;
9   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
10  import com.atlassian.plugins.codegen.modules.jira.WorkflowPostFunctionModuleCreator;
11  import com.atlassian.plugins.codegen.modules.jira.WorkflowPostFunctionProperties;
12  import com.atlassian.plugins.codegen.util.ClassnameUtil;
13  
14  import org.codehaus.plexus.components.interactivity.Prompter;
15  import org.codehaus.plexus.components.interactivity.PrompterException;
16  
17  /**
18   * @since 3.5
19   */
20  @ModuleCreatorClass(WorkflowPostFunctionModuleCreator.class)
21  public class WorkflowPostFunctionPrompter extends AbstractModulePrompter<WorkflowPostFunctionProperties>
22  {
23  
24      public WorkflowPostFunctionPrompter(Prompter prompter)
25      {
26          super(prompter);
27  
28      }
29  
30      @Override
31      public WorkflowPostFunctionProperties promptForBasicProperties(PluginModuleLocation moduleLocation) throws PrompterException
32      {
33          String className = promptJavaClassname("Enter New Classname", "MyPostFunction");
34          String packageName = promptJavaPackagename("Enter Package Name", getDefaultBasePackage() + ".jira.workflow");
35  
36          String fqClass = ClassnameUtil.fullyQualifiedName(packageName, className);
37  
38          return new WorkflowPostFunctionProperties(fqClass);
39      }
40  
41      @Override
42      public void promptForAdvancedProperties(WorkflowPostFunctionProperties props, PluginModuleLocation moduleLocation) throws PrompterException
43      {
44          props.setOrderable(promptForBoolean("Is Function Orderable?", "Y"));
45          props.setUnique(promptForBoolean("Is Function Unique?", "Y"));
46          props.setDeletable(promptForBoolean("Is Function Deletable?", "Y"));
47  
48          String addable = "";
49          if (promptForBoolean("Define Action Types? (addable)", "N"))
50          {
51              addable = promptForAddables();
52          }
53  
54          props.setAddable(addable);
55      }
56  
57      private String promptForAddables() throws PrompterException
58      {
59          List<String> addables = new ArrayList<String>();
60          List<String> mutableValues = new ArrayList<String>(ActionTypeFactory.getAvailableActionTypes());
61  
62          promptForAddable(addables, mutableValues);
63  
64          StringBuffer addableBuffer = new StringBuffer();
65          for (String addable : addables)
66          {
67              if (addableBuffer.length() > 0)
68              {
69                  addableBuffer.append(",");
70              }
71  
72              addableBuffer.append(addable);
73          }
74  
75          return addableBuffer.toString();
76      }
77  
78      private void promptForAddable(List<String> addables, List<String> allowedAddables) throws PrompterException
79      {
80          boolean addAddable;
81  
82          if (addables.isEmpty())
83          {
84              addAddable = true;
85          } else
86          {
87              addAddable = promptForBoolean("Add Action Type?", "N");
88          }
89  
90          if (addAddable)
91          {
92              StringBuilder addableQuery = new StringBuilder("Choose A Type\n");
93              List<String> indexChoices = new ArrayList<String>(allowedAddables.size());
94              int index = 1;
95              for (String addable : allowedAddables)
96              {
97                  String strIndex = Integer.toString(index);
98                  addableQuery.append(strIndex + ": " + addable + "\n");
99                  indexChoices.add(strIndex);
100                 index++;
101             }
102 
103             addableQuery.append("Choose a number: ");
104             String addableAnswer = prompt(addableQuery.toString(), indexChoices, "1");
105             int selectedIndex = Integer.parseInt(addableAnswer) - 1;
106 
107             String selectedDispatcher = allowedAddables.get(selectedIndex);
108 
109             addables.add(selectedDispatcher);
110             allowedAddables.remove(selectedIndex);
111 
112             if (!allowedAddables.isEmpty())
113             {
114                 promptForAddable(addables, allowedAddables);
115             }
116         }
117     }
118 }