1   package com.atlassian.maven.plugins.amps.codegen.prompter.common.web;
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.plugins.codegen.modules.PluginModuleLocation;
8   import com.atlassian.plugins.codegen.modules.common.Resource;
9   import com.atlassian.plugins.codegen.modules.common.web.WebResourceModuleCreator;
10  import com.atlassian.plugins.codegen.modules.common.web.WebResourceProperties;
11  import com.atlassian.plugins.codegen.modules.common.web.WebResourceTransformation;
12  
13  import org.codehaus.plexus.components.interactivity.Prompter;
14  import org.codehaus.plexus.components.interactivity.PrompterException;
15  
16  /**
17   * @since 3.5
18   */
19  @ModuleCreatorClass(WebResourceModuleCreator.class)
20  public class WebResourcePrompter extends AbstractWebFragmentPrompter<WebResourceProperties>
21  {
22  
23      public static final String CUSTOM_CONTEXT = "Custom Context";
24  
25      public WebResourcePrompter(Prompter prompter)
26      {
27          super(prompter);
28  
29      }
30  
31      @Override
32      public WebResourceProperties promptForBasicProperties(PluginModuleLocation moduleLocation) throws PrompterException
33      {
34          String moduleName = promptNotBlank("Enter Plugin Module Name", "My Web Resource");
35  
36          WebResourceProperties props = new WebResourceProperties(moduleName);
37          List<Resource> resourceList = new ArrayList<Resource>();
38          resourceList.add(promptForResource());
39  
40          promptForResources(resourceList);
41  
42          props.setResources(resourceList);
43  
44          suppressAdvancedNamePrompt();
45  
46          return props;
47  
48      }
49  
50      @Override
51      public void promptForAdvancedProperties(WebResourceProperties props, PluginModuleLocation moduleLocation) throws PrompterException
52      {
53          props.setDependencies(promptForList("Add Dependency?", "Enter Dependency"));
54          props.setContexts(promptForContexts(props.knownContexts()));
55          props.setTransformations(promptForTransformations());
56          props.setConditions(promptForConditions());
57      }
58  
59      private List<String> promptForContexts(List<String> knownContexts) throws PrompterException
60      {
61          List<String> contexts = new ArrayList<String>();
62          List<String> mutableValues = new ArrayList<String>(knownContexts);
63  
64          promptForContext(contexts, mutableValues);
65  
66          return contexts;
67      }
68  
69      private void promptForContext(List<String> contexts, List<String> knownContexts) throws PrompterException
70      {
71  
72          if (promptForBoolean("Add Web Resource Context?", "N"))
73          {
74              StringBuilder contextQuery = new StringBuilder("Choose A Context\n");
75              List<String> indexChoices = new ArrayList<String>(knownContexts.size() + 1);
76              int index = 1;
77              String strIndex;
78              for (String context : knownContexts)
79              {
80                  strIndex = Integer.toString(index);
81                  contextQuery.append(strIndex + ": " + context + "\n");
82                  indexChoices.add(strIndex);
83                  index++;
84              }
85  
86              strIndex = Integer.toString(index);
87              contextQuery.append(strIndex + ": " + CUSTOM_CONTEXT + "\n");
88              indexChoices.add(strIndex);
89  
90              contextQuery.append("Choose a number: ");
91              String contextAnswer = prompt(contextQuery.toString(), indexChoices, "1");
92              int selectedIndex = Integer.parseInt(contextAnswer) - 1;
93  
94              String selectedContext;
95              if (selectedIndex < (indexChoices.size() - 1))
96              {
97                  selectedContext = knownContexts.get(selectedIndex);
98                  knownContexts.remove(selectedIndex);
99              } else
100             {
101                 selectedContext = promptNotBlank("Enter Context");
102             }
103 
104             contexts.add(selectedContext);
105 
106 
107             promptForContext(contexts, knownContexts);
108         }
109     }
110 
111     private List<WebResourceTransformation> promptForTransformations() throws PrompterException
112     {
113         List<WebResourceTransformation> transformations = new ArrayList<WebResourceTransformation>();
114         promptForTransformation(transformations);
115 
116         return transformations;
117     }
118 
119     private void promptForTransformation(List<WebResourceTransformation> transformations) throws PrompterException
120     {
121         if (promptForBoolean("Add Web Resource Transformation?", "N"))
122         {
123             String extension = promptNotBlank("File Extension");
124             WebResourceTransformation transformation = new WebResourceTransformation(extension);
125 
126             List<String> transformers = new ArrayList<String>();
127             transformers.add(promptForTransformerKey());
128 
129             promptForTransformers(transformers);
130 
131             transformation.setTransformerKeys(transformers);
132 
133             transformations.add(transformation);
134 
135             promptForTransformation(transformations);
136         }
137     }
138 
139     private void promptForTransformers(List<String> transformers) throws PrompterException
140     {
141         if (promptForBoolean("Add Transformer Key?", "N"))
142         {
143             transformers.add(promptForTransformerKey());
144         }
145     }
146 
147     private String promptForTransformerKey() throws PrompterException
148     {
149         return promptNotBlank("Transformer Key");
150     }
151 }