1   package com.atlassian.maven.plugins.amps.codegen.prompter.common.component;
2   
3   import java.io.File;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import com.atlassian.maven.plugins.amps.codegen.annotations.ModuleCreatorClass;
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.common.component.ComponentModuleCreator;
11  import com.atlassian.plugins.codegen.modules.common.component.ComponentProperties;
12  import com.atlassian.plugins.codegen.util.ClassnameUtil;
13  
14  import org.apache.commons.lang.StringUtils;
15  import org.codehaus.plexus.components.interactivity.Prompter;
16  import org.codehaus.plexus.components.interactivity.PrompterException;
17  
18  /**
19   * @since 3.5
20   */
21  @ModuleCreatorClass(ComponentModuleCreator.class)
22  public class ComponentModulePrompter extends AbstractModulePrompter<ComponentProperties>
23  {
24  
25      public ComponentModulePrompter(Prompter prompter)
26      {
27          super(prompter);
28  
29      }
30  
31      @Override
32      public ComponentProperties promptForBasicProperties(PluginModuleLocation moduleLocation) throws PrompterException
33      {
34          String interfaceName = promptJavaClassname("Enter Interface name", "MYComponent");
35          String interfacePackage = promptJavaPackagename("Enter Interface package", getDefaultBasePackage() + ".components");
36  
37          String className = promptJavaClassname("Enter Class name", interfaceName + "Impl");
38          String packageName = promptJavaPackagename("Enter Package Name", interfacePackage);
39  
40          String fqClass = ClassnameUtil.fullyQualifiedName(packageName, className);
41          String fqInterface = ClassnameUtil.fullyQualifiedName(interfacePackage, interfaceName);
42  
43          ComponentProperties props = new ComponentProperties(fqClass);
44          props.setFullyQualifiedInterface(fqInterface);
45  
46          props.setGenerateClass(!javaFileExists(props.getFullyQualifiedClassname(), moduleLocation));
47          props.setGenerateInterface(!javaFileExists(props.getFullyQualifiedInterface(), moduleLocation));
48  
49          suppressExamplesPrompt();
50  
51          return props;
52      }
53  
54      @Override
55      public void promptForAdvancedProperties(ComponentProperties props, PluginModuleLocation moduleLocation) throws PrompterException
56      {
57          boolean createClass = props.generateClass();
58          boolean createInterface = props.generateInterface();
59  
60          if (createInterface)
61          {
62              props.setGenerateInterface(promptForBoolean("Generate Module Interface?", "Y"));
63          }
64  
65          if (createClass)
66          {
67              props.setGenerateClass(promptForBoolean("Generate Module Class?", "Y"));
68          }
69  
70          String alias = prompter.prompt("Alias (not required)");
71          if (StringUtils.isNotBlank(alias))
72          {
73              props.setAlias(alias);
74          }
75  
76          props.setPublic(promptForBoolean("Public access?", "N"));
77  
78          Map<String, String> serviceProps = promptForServiceProps();
79          if (serviceProps.size() > 0)
80          {
81              props.setServiceProps(serviceProps);
82          }
83  
84          props.setIncludeExamples(false);
85  
86      }
87  
88      private Map<String, String> promptForServiceProps() throws PrompterException
89      {
90          Map<String, String> props = new HashMap<String, String>();
91          promptForServiceProp(props);
92  
93          return props;
94      }
95  
96      private void promptForServiceProp(Map<String, String> props) throws PrompterException
97      {
98          StringBuffer addBuffer = new StringBuffer();
99          if (props.size() > 0)
100         {
101             addBuffer.append("service-properties:\n");
102             for (Map.Entry<String, String> entry : props.entrySet())
103             {
104                 addBuffer.append(entry.getKey())
105                         .append("->")
106                         .append(entry.getValue())
107                         .append("\n");
108             }
109         }
110         addBuffer.append("Add Service Property?");
111         boolean addProp = promptForBoolean(addBuffer.toString(), "N");
112 
113         if (addProp)
114         {
115             String key = promptNotBlank("property key");
116             String value = promptNotBlank("property value");
117             props.put(key, value);
118             promptForServiceProp(props);
119         }
120     }
121 
122     private boolean javaFileExists(String fqInterface, PluginModuleLocation moduleLocation)
123     {
124         File javaFile = new File(moduleLocation.getSourceDirectory(), fqInterface.replaceAll("\\.", File.separator) + ".java");
125         return javaFile.exists();
126     }
127 }