Browse Source

added dbo for template; added primary keys

tags/v1.1.0
Robin Thoni 8 years ago
parent
commit
073442bb1b

+ 45
- 8
src/com/rthoni/intellij/codefromds/business/Generator.java View File

@@ -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
     }

+ 0
- 3
src/com/rthoni/intellij/codefromds/business/Helper.java View File

@@ -4,15 +4,12 @@ import com.intellij.database.psi.DbDataSource;
4 4
 import com.intellij.database.psi.DbPsiFacade;
5 5
 import com.intellij.openapi.project.Project;
6 6
 import com.intellij.openapi.project.ProjectManager;
7
-import com.rthoni.intellij.codefromds.dbo.TableSelection;
8 7
 import org.json.JSONArray;
9 8
 import org.json.JSONObject;
10 9
 
11
-import java.io.File;
12 10
 import java.io.IOException;
13 11
 import java.nio.charset.StandardCharsets;
14 12
 import java.nio.file.Files;
15
-import java.nio.file.LinkOption;
16 13
 import java.nio.file.Path;
17 14
 import java.nio.file.Paths;
18 15
 import java.util.Arrays;

src/com/rthoni/intellij/codefromds/dbo/ColumnSelection.java → src/com/rthoni/intellij/codefromds/dbo/options/ColumnSelection.java View File

@@ -1,4 +1,4 @@
1
-package com.rthoni.intellij.codefromds.dbo;
1
+package com.rthoni.intellij.codefromds.dbo.options;
2 2
 
3 3
 import com.intellij.database.model.DasColumn;
4 4
 import org.json.JSONObject;

src/com/rthoni/intellij/codefromds/dbo/DataSourceSelection.java → src/com/rthoni/intellij/codefromds/dbo/options/DataSourceSelection.java View File

@@ -1,4 +1,4 @@
1
-package com.rthoni.intellij.codefromds.dbo;
1
+package com.rthoni.intellij.codefromds.dbo.options;
2 2
 
3 3
 import com.intellij.database.model.DasTable;
4 4
 import com.intellij.database.psi.DbDataSource;

src/com/rthoni/intellij/codefromds/dbo/GenerateOptions.java → src/com/rthoni/intellij/codefromds/dbo/options/GenerateOptions.java View File

@@ -1,15 +1,12 @@
1
-package com.rthoni.intellij.codefromds.dbo;
1
+package com.rthoni.intellij.codefromds.dbo.options;
2 2
 
3 3
 import com.intellij.database.model.DasTable;
4 4
 import com.intellij.database.psi.DbDataSource;
5 5
 import com.intellij.database.util.DasUtil;
6
-import com.intellij.openapi.project.Project;
7 6
 import com.rthoni.intellij.codefromds.business.Helper;
8 7
 import org.json.JSONObject;
9 8
 
10 9
 import java.io.File;
11
-import java.nio.file.Path;
12
-import java.nio.file.Paths;
13 10
 import java.util.HashMap;
14 11
 import java.util.List;
15 12
 import java.util.Vector;

src/com/rthoni/intellij/codefromds/dbo/TableSelection.java → src/com/rthoni/intellij/codefromds/dbo/options/TableSelection.java View File

@@ -1,4 +1,4 @@
1
-package com.rthoni.intellij.codefromds.dbo;
1
+package com.rthoni.intellij.codefromds.dbo.options;
2 2
 
3 3
 import com.intellij.database.model.DasTable;
4 4
 import com.rthoni.intellij.codefromds.business.Helper;

+ 37
- 0
src/com/rthoni/intellij/codefromds/dbo/template/ColumnDataSourceDbo.java View File

@@ -0,0 +1,37 @@
1
+package com.rthoni.intellij.codefromds.dbo.template;
2
+
3
+/**
4
+ * Created by robin on 11/18/16.
5
+ */
6
+public class ColumnDataSourceDbo {
7
+
8
+    private String _name;
9
+
10
+    private boolean _isPrimary;
11
+
12
+    private boolean _isSelected;
13
+
14
+    public String getName() {
15
+        return _name;
16
+    }
17
+
18
+    public void setName(String name) {
19
+        _name = name;
20
+    }
21
+
22
+    public boolean isPrimary() {
23
+        return _isPrimary;
24
+    }
25
+
26
+    public void setPrimary(boolean primary) {
27
+        _isPrimary = primary;
28
+    }
29
+
30
+    public boolean isSelected() {
31
+        return _isSelected;
32
+    }
33
+
34
+    public void setSelected(boolean selected) {
35
+        _isSelected = selected;
36
+    }
37
+}

+ 29
- 0
src/com/rthoni/intellij/codefromds/dbo/template/DataSourceDbo.java View File

@@ -0,0 +1,29 @@
1
+package com.rthoni.intellij.codefromds.dbo.template;
2
+
3
+import java.util.List;
4
+
5
+/**
6
+ * Created by robin on 11/18/16.
7
+ */
8
+public class DataSourceDbo {
9
+
10
+    private String _name;
11
+
12
+    private List<TableDataSourceDbo> _tables;
13
+
14
+    public String getName() {
15
+        return _name;
16
+    }
17
+
18
+    public void setName(String name) {
19
+        _name = name;
20
+    }
21
+
22
+    public List<TableDataSourceDbo> getTables() {
23
+        return _tables;
24
+    }
25
+
26
+    public void setTables(List<TableDataSourceDbo> tables) {
27
+        _tables = tables;
28
+    }
29
+}

+ 48
- 0
src/com/rthoni/intellij/codefromds/dbo/template/TableDataSourceDbo.java View File

@@ -0,0 +1,48 @@
1
+package com.rthoni.intellij.codefromds.dbo.template;
2
+
3
+import java.util.List;
4
+
5
+/**
6
+ * Created by robin on 11/18/16.
7
+ */
8
+public class TableDataSourceDbo {
9
+    private String _name;
10
+
11
+    private List<ColumnDataSourceDbo> _columns;
12
+
13
+    private List<ColumnDataSourceDbo> _primaryKeys;
14
+
15
+    private boolean _hasAny;
16
+
17
+    public String getName() {
18
+        return _name;
19
+    }
20
+
21
+    public void setName(String name) {
22
+        _name = name;
23
+    }
24
+
25
+    public List<ColumnDataSourceDbo> getColumns() {
26
+        return _columns;
27
+    }
28
+
29
+    public void setColumns(List<ColumnDataSourceDbo> columns) {
30
+        _columns = columns;
31
+    }
32
+
33
+    public List<ColumnDataSourceDbo> getPrimaryKeys() {
34
+        return _primaryKeys;
35
+    }
36
+
37
+    public void setPrimaryKeys(List<ColumnDataSourceDbo> primaryKeys) {
38
+        _primaryKeys = primaryKeys;
39
+    }
40
+
41
+    public boolean hasAny() {
42
+        return _hasAny;
43
+    }
44
+
45
+    public void setHasAny(boolean hasAny) {
46
+        _hasAny = hasAny;
47
+    }
48
+}

+ 1
- 8
src/com/rthoni/intellij/codefromds/ui/actions/GenerateAction.java View File

@@ -1,23 +1,16 @@
1 1
 package com.rthoni.intellij.codefromds.ui.actions;
2 2
 
3
-import com.intellij.notification.Notification;
4
-import com.intellij.notification.NotificationType;
5
-import com.intellij.notification.Notifications;
6 3
 import com.intellij.openapi.actionSystem.AnAction;
7 4
 import com.intellij.openapi.actionSystem.AnActionEvent;
8 5
 import com.intellij.openapi.actionSystem.CommonDataKeys;
9
-import com.intellij.openapi.actionSystem.DataContext;
10 6
 import com.intellij.openapi.project.Project;
11
-import com.intellij.openapi.project.ProjectManager;
12 7
 import com.intellij.openapi.ui.DialogWrapper;
13 8
 import com.intellij.openapi.vfs.VirtualFile;
14 9
 import com.rthoni.intellij.codefromds.business.Generator;
15
-import com.rthoni.intellij.codefromds.dbo.GenerateOptions;
10
+import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
16 11
 import com.rthoni.intellij.codefromds.ui.dialogs.GenerateDialog;
17 12
 import com.rthoni.intellij.codefromds.ui.others.NotificationsService;
18 13
 
19
-import javax.swing.*;
20
-
21 14
 /**
22 15
  * Created by robin on 11/14/16.
23 16
  */

+ 4
- 4
src/com/rthoni/intellij/codefromds/ui/dialogs/GenerateDialog.java View File

@@ -11,10 +11,10 @@ import com.intellij.ui.ColoredListCellRenderer;
11 11
 import com.intellij.ui.JBColor;
12 12
 import com.intellij.ui.components.JBList;
13 13
 import com.rthoni.intellij.codefromds.business.Helper;
14
-import com.rthoni.intellij.codefromds.dbo.ColumnSelection;
15
-import com.rthoni.intellij.codefromds.dbo.DataSourceSelection;
16
-import com.rthoni.intellij.codefromds.dbo.GenerateOptions;
17
-import com.rthoni.intellij.codefromds.dbo.TableSelection;
14
+import com.rthoni.intellij.codefromds.dbo.options.ColumnSelection;
15
+import com.rthoni.intellij.codefromds.dbo.options.DataSourceSelection;
16
+import com.rthoni.intellij.codefromds.dbo.options.GenerateOptions;
17
+import com.rthoni.intellij.codefromds.dbo.options.TableSelection;
18 18
 import org.jetbrains.annotations.NotNull;
19 19
 import org.jetbrains.annotations.Nullable;
20 20
 

Loading…
Cancel
Save