1   package com.atlassian.maven.plugins.amps.codegen.prompter.common.web;
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.ConditionFactory;
9   import com.atlassian.maven.plugins.amps.codegen.ContextProviderFactory;
10  import com.atlassian.maven.plugins.amps.codegen.prompter.common.AbstractResourcePrompter;
11  import com.atlassian.plugins.codegen.modules.NameBasedModuleProperties;
12  import com.atlassian.plugins.codegen.modules.common.Condition;
13  import com.atlassian.plugins.codegen.modules.common.Conditional;
14  import com.atlassian.plugins.codegen.modules.common.Conditions;
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  public abstract class AbstractWebFragmentPrompter<T extends NameBasedModuleProperties> extends AbstractResourcePrompter<T>
23  {
24  
25      public static final String CUSTOM_CONDITION = "Custom Condition";
26      public static final String CUSTOM_PROVIDER = "Custom Context Provider";
27  
28      public AbstractWebFragmentPrompter(Prompter prompter)
29      {
30          super(prompter);
31      }
32  
33      protected String promptForContextProvider() throws PrompterException
34      {
35          String fqProvider = "";
36          if (promptForBoolean("Add Velocity Context Provider", "N"))
37          {
38              Map<String, String> productProviders = ContextProviderFactory.getAvailableContextProviders();
39              if (productProviders.isEmpty())
40              {
41                  fqProvider = promptFullyQualifiedJavaClass("Enter Fully Qualified Context Provider Class", getDefaultBasePackage() + ".web.contextproviders.MyContextProvider");
42              } else
43              {
44  
45                  StringBuilder contextQuery = new StringBuilder("Choose A Context Provider\n");
46                  List<String> indexChoices = new ArrayList<String>(productProviders.size());
47                  Map<String, String> indexedValues = new HashMap<String, String>();
48                  int index = 1;
49                  String strIndex;
50                  for (Map.Entry<String, String> entry : productProviders.entrySet())
51                  {
52                      strIndex = Integer.toString(index);
53                      contextQuery.append(strIndex + ": " + entry.getKey() + "\n");
54                      indexChoices.add(strIndex);
55                      indexedValues.put(strIndex, entry.getValue());
56                      index++;
57                  }
58  
59                  strIndex = Integer.toString(index);
60                  contextQuery.append(strIndex + ": " + CUSTOM_PROVIDER + "\n");
61                  indexChoices.add(strIndex);
62                  indexedValues.put(strIndex, CUSTOM_PROVIDER);
63  
64                  contextQuery.append("Choose a number: ");
65                  String contextAnswer = prompt(contextQuery.toString(), indexChoices, "");
66                  int answerInt = (Integer.parseInt(contextAnswer) - 1);
67  
68                  if (answerInt < (productProviders.size()))
69                  {
70                      fqProvider = indexedValues.get(contextAnswer);
71                  } else
72                  {
73                      fqProvider = promptFullyQualifiedJavaClass("Enter Fully Qualified Context Provider Class", getDefaultBasePackage() + ".web.contextproviders.MyContextProvider");
74                  }
75              }
76          }
77  
78          return fqProvider;
79      }
80  
81      protected List<Conditional> promptForConditions() throws PrompterException
82      {
83          List<Conditional> conditionList = new ArrayList<Conditional>();
84  
85          if (promptForBoolean("Add Conditions?", "N"))
86          {
87              Conditions conditions = promptForConditionsContainer();
88              conditionList.add(conditions);
89  
90              conditions.addCondition(promptForCondition());
91              promptForCondition(conditions);
92          }
93  
94          return conditionList;
95      }
96  
97      protected Conditions promptForConditionsContainer() throws PrompterException
98      {
99          String conditionType = prompt("Condition Type", ANDOR_ANSWERS, "AND");
100         return new Conditions(conditionType);
101     }
102 
103     private void promptForCondition(Conditions conditions) throws PrompterException
104     {
105         if (promptForBoolean("Add Condition?", "N"))
106         {
107             conditions.addCondition(promptForCondition());
108             promptForCondition(conditions);
109         }
110     }
111 
112     protected Condition promptForCondition() throws PrompterException
113     {
114         Map<String, String> productConditions = ConditionFactory.getAvailableConditions();
115         String fqCondition;
116         if (productConditions.isEmpty())
117         {
118             fqCondition = promptFullyQualifiedJavaClass("Enter Fully Qualified Condition Class", getDefaultBasePackage() + ".web.condition.MyCondition");
119         } else
120         {
121 
122             StringBuilder conditionQuery = new StringBuilder("Choose A Condition\n");
123             List<String> indexChoices = new ArrayList<String>(productConditions.size());
124             Map<String, String> indexedValues = new HashMap<String, String>();
125             int index = 1;
126             String strIndex;
127             for (Map.Entry<String, String> entry : productConditions.entrySet())
128             {
129                 strIndex = Integer.toString(index);
130                 conditionQuery.append(strIndex + ": " + entry.getKey() + "\n");
131                 indexChoices.add(strIndex);
132                 indexedValues.put(strIndex, entry.getValue());
133                 index++;
134             }
135 
136             strIndex = Integer.toString(index);
137             conditionQuery.append(strIndex + ": " + CUSTOM_CONDITION + "\n");
138             indexedValues.put(strIndex, CUSTOM_CONDITION);
139             indexChoices.add(strIndex);
140 
141             conditionQuery.append("Choose a number: ");
142             String conditionAnswer = prompt(conditionQuery.toString(), indexChoices, "");
143             int answerInt = (Integer.parseInt(conditionAnswer) - 1);
144 
145             if (answerInt < (productConditions.size()))
146             {
147                 fqCondition = indexedValues.get(conditionAnswer);
148             } else
149             {
150                 fqCondition = promptFullyQualifiedJavaClass("Enter Fully Qualified Condition Class", getDefaultBasePackage() + ".web.condition.MyCondition");
151             }
152         }
153 
154         Condition condition = new Condition(fqCondition);
155         Map<String, String> params = promptForParams("Add Condition Parameter?");
156 
157         condition.setParams(params);
158 
159         condition.setInvert(promptForBoolean("Invert Condition?", "N"));
160 
161         return condition;
162     }
163 
164 }