Skip to content

Commit 5c45552

Browse files
committed
Added AutoMigrateCommand and SeedCommand
1 parent 300b338 commit 5c45552

File tree

10 files changed

+89
-29
lines changed

10 files changed

+89
-29
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@
33
target
44

55
# IntelliJ Linux bug
6-
.fuse_*
6+
.fuse_*
7+
8+
9+
localtest.java

src/main/java/org/javawebstack/framework/WebApplication.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.javawebstack.abstractdata.AbstractElement;
55
import org.javawebstack.framework.bind.ModelBindParamTransformer;
66
import org.javawebstack.framework.bind.ModelBindTransformer;
7+
import org.javawebstack.framework.commands.AutoMigrateCommand;
78
import org.javawebstack.framework.commands.HelpCommand;
9+
import org.javawebstack.framework.commands.SeedCommand;
810
import org.javawebstack.framework.commands.StartWebServerCommand;
911
import org.javawebstack.framework.config.Config;
1012
import org.javawebstack.framework.module.Module;
@@ -21,10 +23,7 @@
2123
import picocli.CommandLine;
2224

2325
import java.io.IOException;
24-
import java.io.PrintWriter;
25-
import java.io.Reader;
2626
import java.util.*;
27-
import java.util.concurrent.Callable;
2827
import java.util.logging.Logger;
2928

3029
public abstract class WebApplication {
@@ -170,24 +169,30 @@ public I18N getTranslation() {
170169
return translation;
171170
}
172171

173-
protected void setupModules() { }
172+
protected void setupModules() {
173+
}
174174

175175
protected abstract void setupConfig(Config config);
176176

177-
protected void setupSeeding() { }
177+
protected void setupSeeding() {
178+
}
178179

179-
void setupCommands(CommandLine commandLine) { }
180+
void setupCommands(CommandLine commandLine) {
181+
}
180182

181183
protected abstract void setupModels(SQL sql) throws ORMConfigurationException;
182184

183185
protected abstract void setupServer(HTTPServer server);
184186

185-
public void run(String[] args){
186-
HelpCommand help = new HelpCommand();
187+
public void run(String[] args) {
188+
HelpCommand help = new HelpCommand();
187189

188190
CommandLine commandLine = new CommandLine(help);
189191
help.setCommandLine(commandLine);
190192
commandLine.addSubcommand(new StartWebServerCommand(this));
193+
commandLine.addSubcommand(new SeedCommand(this));
194+
commandLine.addSubcommand(new AutoMigrateCommand(this));
195+
191196
modules.forEach(module -> module.setupCommands(this, commandLine));
192197
setupCommands(commandLine);
193198

src/main/java/org/javawebstack/framework/bind/ModelBindParamTransformer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public ModelBindParamTransformer() {
1818
super();
1919
this.transformer = (exchange, repo, fieldName, source) -> {
2020
Map<Class<? extends Model>, Map<Object, Object>> cache = exchange.attrib("__modelbindcache__");
21-
if(cache == null) {
21+
if (cache == null) {
2222
cache = new HashMap<>();
2323
exchange.attrib("__modelbindcache__", cache);
2424
}
25-
if(!cache.containsKey(repo.getInfo().getModelClass()))
25+
if (!cache.containsKey(repo.getInfo().getModelClass()))
2626
cache.put(repo.getInfo().getModelClass(), new HashMap<>());
2727
Map<Object, Object> modelCache = cache.get(repo.getInfo().getModelClass());
2828
Object model = modelCache.get(source);
29-
if(model == null) {
29+
if (model == null) {
3030
model = repo.accessible(accessorAttribName == null ? null : exchange.attrib(accessorAttribName)).where(fieldName, source).first();
3131
modelCache.put(source, model);
3232
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.javawebstack.framework.commands;
2+
3+
import org.javawebstack.framework.WebApplication;
4+
import org.javawebstack.orm.ORM;
5+
import picocli.CommandLine;
6+
import picocli.CommandLine.Command;
7+
8+
import java.util.concurrent.Callable;
9+
10+
@Command(name = "migrate:auto", description = "Migrate your Models automatically")
11+
public class AutoMigrateCommand implements Callable<Integer> {
12+
private WebApplication webApplication;
13+
14+
@CommandLine.Option(names = {"--fresh", "-f"}, description = "Auto Migrate fresh")
15+
public boolean fresh = false;
16+
17+
public AutoMigrateCommand(WebApplication webApplication) {
18+
this.webApplication = webApplication;
19+
}
20+
21+
public Integer call() {
22+
ORM.autoMigrate(fresh);
23+
return 0;
24+
}
25+
}

src/main/java/org/javawebstack/framework/commands/HelpCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public void setCommandLine(CommandLine commandLine) {
1515
this.commandLine = commandLine;
1616
}
1717

18-
public HelpCommand(){
18+
public HelpCommand() {
1919
}
2020

21-
private void printCommands(CommandLine.Help help, int indent){
22-
System.out.println(String.join("",Collections.nCopies(indent, " ")) +help.commandList());
23-
help.subcommands().forEach((n, h) -> printCommands(h, indent+4));
21+
private void printCommands(CommandLine.Help help, int indent) {
22+
System.out.println(String.join("", Collections.nCopies(indent, " ")) + help.commandList());
23+
help.subcommands().forEach((n, h) -> printCommands(h, indent + 4));
2424
}
2525

2626
public Integer call() {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.javawebstack.framework.commands;
2+
3+
import org.javawebstack.framework.WebApplication;
4+
import picocli.CommandLine;
5+
import picocli.CommandLine.Command;
6+
7+
import java.util.concurrent.Callable;
8+
9+
@Command(name = "seed", description = "Seeding command")
10+
public class SeedCommand implements Callable<Integer> {
11+
private WebApplication webApplication;
12+
13+
@CommandLine.Parameters(description = "The seeders which are going to be seeded separated by a comma or * for all")
14+
private String seeders;
15+
16+
public SeedCommand(WebApplication webApplication) {
17+
this.webApplication = webApplication;
18+
}
19+
20+
public Integer call() {
21+
for (String s : seeders.split(",")) {
22+
if (s.equals("*")) {
23+
webApplication.getSeeders().forEach((s1, seeder) -> {
24+
seeder.seed();
25+
});
26+
} else {
27+
webApplication.getSeeder(s).seed();
28+
}
29+
}
30+
return 0;
31+
}
32+
}

src/main/java/org/javawebstack/framework/commands/StartWebServerCommand.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
package org.javawebstack.framework.commands;
22

33
import org.javawebstack.framework.WebApplication;
4-
import picocli.CommandLine.*;
4+
import picocli.CommandLine.Command;
55

66
import java.util.concurrent.Callable;
77

88
@Command(name = "start", description = "Starts the WebServer")
99
public class StartWebServerCommand implements Callable<Integer> {
1010
private WebApplication webApplication;
1111

12-
/*@Option(names = {"--port", "-p"}, description = "Port")
13-
private int port;*/
1412

15-
public StartWebServerCommand(WebApplication webApplication){
13+
public StartWebServerCommand(WebApplication webApplication) {
1614
this.webApplication = webApplication;
1715
}
1816

19-
@Command(name = "test")
20-
public void asd(){
21-
22-
}
23-
2417
public Integer call() {
2518
webApplication.start();
2619
return 0;

src/main/java/org/javawebstack/framework/module/Module.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ default void setupServer(WebApplication application, HTTPServer server) {
3737
default void setupSeeding(WebApplication application) {
3838
}
3939

40-
default void setupCommands(WebApplication application, CommandLine commandLine) { }
40+
default void setupCommands(WebApplication application, CommandLine commandLine) {
41+
}
4142

4243
}

src/main/java/org/javawebstack/framework/testing/WebFrameworkTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public abstract class WebFrameworkTest extends HTTPTest {
88

99
private WebApplication webApplication;
1010

11-
public WebFrameworkTest(WebApplication webApplication){
11+
public WebFrameworkTest(WebApplication webApplication) {
1212
super(webApplication.getServer());
1313

1414
this.webApplication = webApplication;
@@ -18,11 +18,11 @@ public WebApplication getApplication() {
1818
return webApplication;
1919
}
2020

21-
public void seed(String name){
21+
public void seed(String name) {
2222
webApplication.getSeeder(name).seed();
2323
}
2424

25-
public Config getConfig(){
25+
public Config getConfig() {
2626
return webApplication.getConfig();
2727
}
2828
}

src/main/java/org/javawebstack/framework/utils/Json.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.HashMap;
66
import java.util.Map;
7+
78
public class Json {
89

910
public static Map<String, String> toKeyValueMap(JsonElement element) {

0 commit comments

Comments
 (0)