1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.utgenome.shell;
26
27 import java.text.DateFormat;
28 import java.util.Date;
29 import java.util.Properties;
30
31 import org.utgenome.config.UTGBConfig;
32 import org.xerial.util.log.Logger;
33 import org.xerial.util.opt.Argument;
34 import org.xerial.util.opt.Option;
35
36
37
38
39
40
41
42 public class AddAction extends UTGBShellCommand {
43
44 private static Logger _logger = Logger.getLogger(AddAction.class);
45
46 @Option(symbol = "p", longName = "package", varName = "PACKAGE", description = "base package name to add a new action class")
47 private String packageName;
48
49 @Argument
50 private String target;
51
52 public AddAction() {
53
54 }
55
56 @Override
57 public void execute(String[] args) throws Exception {
58
59 if (packageName == null) {
60
61 UTGBConfig config = loadUTGBConfig();
62 packageName = config.javaPackage;
63 }
64 String appPackageName = packageName + "." + APP_FOLDER;
65
66 if (target == null) {
67 throw new UTGBShellException("No web action name is given. Type utgb action --help for the usage");
68 }
69
70 String newActionClassFullPath = appPackageName + "." + target.replaceAll("/", ".");
71 _logger.info("add a new action class : " + newActionClassFullPath);
72
73 int extPos = newActionClassFullPath.lastIndexOf(".");
74 String actionClassName = (extPos > 0) ? newActionClassFullPath.substring(extPos + 1) : newActionClassFullPath;
75 String actionPackageName = (extPos > 0) ? newActionClassFullPath.substring(0, extPos) : appPackageName;
76
77
78 Properties prop = new Properties();
79 prop.put("actionClass", actionClassName);
80 prop.put("actionPackage", actionPackageName);
81 Date now = new Date();
82 prop.put("date", DateFormat.getDateInstance().format(now));
83
84 String javaFileName = SRC_FOLDER + "/" + newActionClassFullPath.replaceAll("\\.", "/") + ".java";
85 createFileFromTemplate(AddAction.class, "template/java/Action.java.template", javaFileName, prop);
86
87 }
88
89 @Override
90 public String name() {
91 return "action";
92 }
93
94 public String getOneLinerDescription() {
95 return "add a new web action handler";
96 }
97
98 }