|
@@ -1,9 +1,14 @@
|
1
|
1
|
package com.rthoni.intellij.codefromds.business;
|
2
|
2
|
|
3
|
3
|
import com.intellij.database.psi.DbDataSource;
|
|
4
|
+import com.intellij.database.util.DasUtil;
|
4
|
5
|
import com.intellij.openapi.project.Project;
|
5
|
|
-import com.rthoni.intellij.codefromds.dbo.GenerateOptions;
|
6
|
|
-import com.rthoni.intellij.codefromds.dbo.TableSelection;
|
|
6
|
+import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
|
|
7
|
+import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
|
|
8
|
+import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
|
|
9
|
+import com.rthoni.intellij.codefromds.dbo.template.ColumnDataSourceDbo;
|
|
10
|
+import com.rthoni.intellij.codefromds.dbo.template.DataSourceDbo;
|
|
11
|
+import com.rthoni.intellij.codefromds.dbo.template.TableDataSourceDbo;
|
7
|
12
|
import org.json.JSONObject;
|
8
|
13
|
import org.jtwig.JtwigModel;
|
9
|
14
|
import org.jtwig.JtwigTemplate;
|
|
@@ -15,6 +20,7 @@ import java.nio.charset.StandardCharsets;
|
15
|
20
|
import java.nio.file.Files;
|
16
|
21
|
import java.nio.file.Paths;
|
17
|
22
|
import java.util.HashMap;
|
|
23
|
+import java.util.stream.Collectors;
|
18
|
24
|
|
19
|
25
|
/**
|
20
|
26
|
* Created by robin on 11/15/16.
|
|
@@ -48,14 +54,43 @@ public abstract class Generator {
|
48
|
54
|
return options;
|
49
|
55
|
}
|
50
|
56
|
|
51
|
|
- public static void generateFile(String templatePath, String outputFile, GenerateOptions options, TableSelection table) throws IOException
|
|
57
|
+ public static ColumnDataSourceDbo convertColumn(ColumnSelection columnSelection)
|
|
58
|
+ {
|
|
59
|
+ ColumnDataSourceDbo dbo = new ColumnDataSourceDbo();
|
|
60
|
+ dbo.setName(columnSelection.getColumn().getName());
|
|
61
|
+ dbo.setPrimary(DasUtil.isPrimary(columnSelection.getColumn()));
|
|
62
|
+ dbo.setSelected(columnSelection.isSelected());
|
|
63
|
+ return dbo;
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ public static TableDataSourceDbo convertTable(TableSelection tableSelection)
|
|
67
|
+ {
|
|
68
|
+ TableDataSourceDbo dbo = new TableDataSourceDbo();
|
|
69
|
+ dbo.setName(tableSelection.getTable().getName());
|
|
70
|
+ dbo.setColumns(tableSelection.getColumns().stream().map(Generator::convertColumn).collect(Collectors.toList()));
|
|
71
|
+ dbo.setPrimaryKeys(tableSelection.getColumns().stream().filter(c -> DasUtil.isPrimary(c.getColumn())).map(Generator::convertColumn).collect(Collectors.toList()));
|
|
72
|
+ dbo.setHasAny(!tableSelection.hasNone());
|
|
73
|
+ return dbo;
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ public static DataSourceDbo convertOptions(GenerateOptions options)
|
|
77
|
+ {
|
|
78
|
+ DataSourceDbo dbo = new DataSourceDbo();
|
|
79
|
+
|
|
80
|
+ dbo.setName(options.getSelection().getSource().getName());
|
|
81
|
+ dbo.setTables(options.getSelection().getTables().stream().map(Generator::convertTable).collect(Collectors.toList()));
|
|
82
|
+
|
|
83
|
+ return dbo;
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ public static void generateFile(String templatePath, String outputFile, DataSourceDbo dataSource, TableDataSourceDbo table) throws IOException
|
52
|
87
|
{
|
53
|
88
|
String data = Helper.readFile(templatePath);
|
54
|
89
|
|
55
|
90
|
FileOutputStream file = new FileOutputStream(outputFile);
|
56
|
91
|
|
57
|
92
|
JtwigTemplate template = JtwigTemplate.inlineTemplate(data);
|
58
|
|
- JtwigModel model = JtwigModel.newModel().with("options", options).with("table", table);
|
|
93
|
+ JtwigModel model = JtwigModel.newModel().with("dataSource", dataSource).with("table", table);
|
59
|
94
|
|
60
|
95
|
template.render(model, file);
|
61
|
96
|
|
|
@@ -68,12 +103,14 @@ public abstract class Generator {
|
68
|
103
|
String dataSourceTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getDataSourceTemplateRelativePath());
|
69
|
104
|
String modelsTemplateAbsolutePath = Helper.getAbsolutePath(project, options.getModelsTemplateRelativePath());
|
70
|
105
|
|
|
106
|
+ DataSourceDbo dbo = convertOptions(options);
|
|
107
|
+
|
71
|
108
|
generateFile(dataSourceTemplateAbsolutePath, modelsAbsolutePath + File.separator +
|
72
|
|
- "Database." + options.getFilesExtension(), options, null);
|
73
|
|
- for (TableSelection table : options.getSelection().getTables()) {
|
74
|
|
- if (!table.hasNone()) {
|
|
109
|
+ "Database." + options.getFilesExtension(), dbo, null);
|
|
110
|
+ for (TableDataSourceDbo table : dbo.getTables()) {
|
|
111
|
+ if (table.hasAny()) {
|
75
|
112
|
generateFile(modelsTemplateAbsolutePath, modelsAbsolutePath + File.separator +
|
76
|
|
- table.getTable().getName() + "." + options.getFilesExtension(), options, table);
|
|
113
|
+ table.getName() + "." + options.getFilesExtension(), dbo, table);
|
77
|
114
|
}
|
78
|
115
|
}
|
79
|
116
|
}
|