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