package com.adaptrex.tools.command;
import java.io.File;
import java.io.IOException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;
import com.adaptrex.tools.environment.IEnvironmentService;
import com.adaptrex.tools.framework.javascript.JavaScriptFramework;
import com.adaptrex.tools.log.ILogService;
import com.adaptrex.tools.log.Log;
import com.adaptrex.tools.webapp.IWebappService;
import com.adaptrex.tools.webapp.Page;
import com.adaptrex.tools.webapp.Webapp;
import com.google.inject.Inject;
public class Import extends Command {
private static final String WEBAPP_USAGE = "adaptrex import webapp [-p <path>] -n <name>" +
"[-g <global-folder>] [-G <global-namespace>] " +
"[-b <base-package>] [-a <adaptrex-js>] [-e <extjs> -s <sencha-touch>] " +
"[-v <presentation>] [-o <orm>] [-d <dependency-injection>]" +
"[-t <ext-theme>] [-T <sencha-touch-theme>]";
private static final String PAGE_USAGE = "adaptrex import page [-p <webapp-path>]" +
"-N <namespace> -P <page-path> -f <framework> ";
@Inject
public Import(ICommandService commandService, ILogService logService,
IEnvironmentService environmentService, IWebappService webappService) throws ParseException,
IOException {
super(commandService, logService, environmentService, webappService);
}
public void run() throws ParseException, IOException {
String target = commandService.getTarget();
if (target.equals("webapp")) importWebapp();
else if (target.equals("page")) importPage();
}
private void importPage() throws ParseException, IOException {
setImportPageOptions();
cl = commandService.parseCommands(PAGE_USAGE);
commandService.requireOptions(PAGE_USAGE, "N", "P", "f");
Webapp webapp = commandService.getWebapp();
if (webapp == null) return;
String namespace = cl.getOptionValue("N");
String pagePath = cl.getOptionValue("P");
String framework = cl.getOptionValue("f", "ext-js");
Page page = webappService.importPage(webapp, pagePath, namespace, framework);
commandService.printLog();
if (page == null) return;
webappService.saveWebapp(webapp);
}
/*
* adaptrex import webapp
*/
private void importWebapp() throws ParseException, IOException {
setImportWebappOptions();
cl = commandService.parseCommands(WEBAPP_USAGE);
String path = cl.getOptionValue("p");
if (path == null) {
File directory = new File (".");
path = directory.getAbsolutePath().replace(".", "");
}
String name = cl.getOptionValue("n");
String adaptrex = null;
if (cl.hasOption("a")) {
adaptrex = cl.getOptionValue("a");
if (adaptrex == null) adaptrex = JavaScriptFramework.DEFAULT;
}
String ext = null;
if (cl.hasOption("e")) {
ext = cl.getOptionValue("e");
if (ext == null) ext = JavaScriptFramework.DEFAULT;
}
String sencha = null;
if (cl.hasOption("s")) {
sencha = cl.getOptionValue("s");
if (sencha == null) sencha = JavaScriptFramework.DEFAULT;
}
String globalFolder = cl.getOptionValue("g");
String globalNamespace = cl.getOptionValue("G");
String basePackage = cl.getOptionValue("b");
String view = cl.getOptionValue("v");
String orm = cl.getOptionValue("o");
String di = cl.getOptionValue("d");
String extTheme = cl.getOptionValue("t");
String senchaTouchTheme = cl.getOptionValue("T");
Webapp webapp = environmentService.importWebapp(path, name,
globalFolder, globalNamespace, basePackage,
adaptrex, ext, sencha,
view, orm, di, extTheme, senchaTouchTheme);
commandService.printLog();
if (webapp == null) return;
webappService.saveWebapp(webapp);
environmentService.saveEnvironment();
/*
* Add themes
*/
if (cl.hasOption("t")) {
try {
boolean success = webapp.getExt().importTheme(webapp, cl.getOptionValue("t"));
if (success) {
commandService.log(Log.SUCCESS, "Theme Successfully Imported", null);
} else {
commandService.log(Log.ERROR, "Unspecified Error Importing Theme", null);
}
} catch (Exception e) {
commandService.log(Log.ERROR, "Error Importing Theme", e.getMessage());
}
commandService.printLog();
}
if (cl.hasOption("T")) {
try {
boolean success = webapp.getSenchaTouch().importTheme(webapp, cl.getOptionValue("T"));
if (success) {
commandService.log(Log.SUCCESS, "Theme Successfully Imported", null);
} else {
commandService.log(Log.ERROR, "Unspecified Error Importing Theme", null);
}
} catch (Exception e) {
commandService.log(Log.ERROR, "Error Importing Theme", e.getMessage());
}
commandService.printLog();
}
}
/*
* "adaptrex import page" options
*/
private void setImportPageOptions() {
commandService.addWebappOptions();
Option ns = new Option("N", "namespace", true,
"The path to your weblib folder containing AdaptrexJS, ExtJS and Sencha Touch");
ns.setArgName("namespace");
options.addOption(ns);
Option pg = new Option("P", "page-path", true,
"The path to the Sencha Cmd folder to use with Adaptrex Tools");
pg.setArgName("page-path");
options.addOption(pg);
Option f = new Option("f", "framework", true,
"The path to the Sencha Cmd folder to use with Adaptrex Tools");
f.setArgName("framework");
options.addOption(f);
}
/*
* "adaptrex import webapp" options
*/
private void setImportWebappOptions() {
commandService.addFrameworkOptions();
Option p = new Option("p", "path", true,
"The path to your weblib folder containing AdaptrexJS, ExtJS and Sencha Touch");
p.setArgName("path");
options.addOption(p);
Option n = new Option("n", "name", true,
"The name of the webapp to be imported");
n.setArgName("name");
options.addOption(n);
Option t = new Option("t", "ext-theme", true, "Copy an ExtJS theme to the webapp");
t.setArgName("ext-theme");
options.addOption(t);
Option st = new Option("T", "sencha-touch-theme", true, "Copy a Sencha Touch theme to the webapp");
st.setArgName("sencha-touch-theme");
options.addOption(st);
Option b = new Option("b", "base-package", true, "The base java package for the webapp");
b.setArgName("base-package");
options.addOption(b);
Option a = new Option("a", "adaptrex-js", true, "The adaptrex-js folder for the webapp");
a.setArgName("adaptrex-js");
options.addOption(a);
Option e = new Option("e", "extjs", true, "The extjs folder for the webapp");
e.setArgName("extjs");
e.setOptionalArg(true);
options.addOption(e);
Option s = new Option("s", "sencha-touch", true, "The sencha-touch folder for the webapp");
s.setArgName("sencha-touch");
s.setOptionalArg(true);
options.addOption(s);
Option o = new Option("o", "orm", true, "The ORM framework to use for the webapp");
o.setArgName("orm");
o.setOptionalArg(true);
options.addOption(o);
Option v = new Option("v", "presentation", true, "The presentation framework to use for the webapp");
v.setArgName("presentation");
v.setOptionalArg(true);
options.addOption(v);
Option d = new Option("d", "di", true, "The dependency injection framework to use for the webapp");
d.setArgName("di");
options.addOption(d);
Option g = new Option("g", "common-folder", true, "The path to the folder containing global classes");
g.setArgName("common-folder");
options.addOption(g);
Option gn = new Option("G", "common-namespace", true, "The namespace to use for global classes");
gn.setArgName("common-namespace");
options.addOption(gn);
}
}