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.prompter.AbstractModulePrompter;
8 import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
9 import com.atlassian.plugins.codegen.modules.jira.ActionProperties;
10 import com.atlassian.plugins.codegen.modules.jira.View;
11 import com.atlassian.plugins.codegen.modules.jira.WebworkModuleCreator;
12 import com.atlassian.plugins.codegen.modules.jira.WebworkProperties;
13 import com.atlassian.plugins.codegen.util.ClassnameUtil;
14
15 import org.apache.commons.io.FilenameUtils;
16 import org.codehaus.plexus.components.interactivity.Prompter;
17 import org.codehaus.plexus.components.interactivity.PrompterException;
18
19
20
21
22 @ModuleCreatorClass(WebworkModuleCreator.class)
23 public class WebworkPrompter extends AbstractModulePrompter<WebworkProperties>
24 {
25
26 public WebworkPrompter(Prompter prompter)
27 {
28 super(prompter);
29
30 }
31
32 @Override
33 public WebworkProperties promptForBasicProperties(PluginModuleLocation moduleLocation) throws PrompterException
34 {
35 suppressAdvancedNamePrompt();
36 String moduleName = promptNotBlank("Enter Plugin Module Name", "My Webwork Module");
37
38 WebworkProperties props = new WebworkProperties(moduleName);
39
40 props.setActions(createDefaultAction(props));
41
42 return props;
43 }
44
45 private List<ActionProperties> createDefaultAction(WebworkProperties props)
46 {
47 List<ActionProperties> actions = new ArrayList<ActionProperties>();
48 String packageName = getDefaultBasePackage() + ".jira.webwork";
49 String className = ClassnameUtil.removeSpaces(props.getModuleName()) + "Action";
50
51 String fqName = ClassnameUtil.fullyQualifiedName(packageName, className);
52 ActionProperties action = new ActionProperties(fqName);
53 action.setAlias(ClassnameUtil.removeSpaces(props.getModuleName()));
54
55 String templatePath = "/templates/" + props.getModuleKey() + "/";
56 View success = new View("success", templatePath + "success.vm");
57 View input = new View("input", templatePath + "input.vm");
58 View error = new View("error", templatePath + "error.vm");
59
60 action.addView(success);
61 action.addView(input);
62 action.addView(error);
63
64 actions.add(action);
65
66 return actions;
67 }
68
69 @Override
70 public void promptForAdvancedProperties(WebworkProperties props, PluginModuleLocation moduleLocation) throws PrompterException
71 {
72 props.setActions(promptForActions(props));
73 }
74
75 private List<ActionProperties> promptForActions(WebworkProperties props) throws PrompterException
76 {
77 List<ActionProperties> actions = new ArrayList<ActionProperties>();
78
79 String initialPackage = getDefaultBasePackage() + ".jira.webwork";
80 String templatePathPrefix = "/templates/" + props.getModuleKey() + "/";
81 promptForAction(actions, initialPackage, templatePathPrefix);
82
83 return actions;
84 }
85
86 private void promptForAction(List<ActionProperties> actions, String packageName, String templatePathPrefix) throws PrompterException
87 {
88
89 String className = promptJavaClassname("Enter Action Classname", "MyActionClass");
90 String newPackageName = promptJavaPackagename("Enter Package Name", packageName);
91
92 String fqClass = ClassnameUtil.fullyQualifiedName(newPackageName, className);
93 String alias = promptNotBlank("Enter Alias", className);
94
95 ActionProperties action = new ActionProperties(fqClass);
96 action.setAlias(alias);
97
98 action.setViews(promptForViews(action, templatePathPrefix + className.toLowerCase() + "/"));
99
100 actions.add(action);
101
102 if (promptForBoolean("Add Another Action?", "N"))
103 {
104 promptForAction(actions, newPackageName, templatePathPrefix);
105 }
106 }
107
108 private List<View> promptForViews(ActionProperties action, String templatePath) throws PrompterException
109 {
110 List<View> views = new ArrayList<View>();
111
112 promptForView(views, templatePath);
113
114 return views;
115 }
116
117 private void promptForView(List<View> views, String templatePath) throws PrompterException
118 {
119 String viewName = promptNotBlank("Enter View Name", "success");
120
121 String pathWithEndSlash = templatePath;
122 if (!pathWithEndSlash.endsWith("/"))
123 {
124 pathWithEndSlash = pathWithEndSlash + "/";
125 }
126
127 String viewPath = promptNotBlank("Enter Template Path", pathWithEndSlash + viewName + ".vm");
128
129 View view = new View(viewName, viewPath);
130 views.add(view);
131
132 if (promptForBoolean("Add Another View?", "N"))
133 {
134 promptForView(views, FilenameUtils.getFullPath(viewPath));
135 }
136 }
137 }