From 3a439298f8aa8e88629d6060bb4c7522ba587b36 Mon Sep 17 00:00:00 2001 From: "b.harniuk" Date: Mon, 12 Oct 2020 12:33:15 +0300 Subject: [PATCH 1/6] 309: Add declarative schema base dialog with table related fields --- resources/META-INF/plugin.xml | 1 + .../actions/generation/NewDbSchemaAction.java | 55 +++++++ .../generation/dialog/NewDbSchemaDialog.form | 140 ++++++++++++++++++ .../generation/dialog/NewDbSchemaDialog.java | 109 ++++++++++++++ .../util/HighlightDialogFieldOnErrorUtil.java | 5 + 5 files changed, 310 insertions(+) create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/NewDbSchemaAction.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index efee2c7b5..87be3ec8d 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -70,6 +70,7 @@ + diff --git a/src/com/magento/idea/magento2plugin/actions/generation/NewDbSchemaAction.java b/src/com/magento/idea/magento2plugin/actions/generation/NewDbSchemaAction.java new file mode 100644 index 000000000..aeb6e8ff8 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/NewDbSchemaAction.java @@ -0,0 +1,55 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation; + +import com.intellij.ide.IdeView; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.actionSystem.LangDataKeys; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.magento.idea.magento2plugin.MagentoIcons; +import com.magento.idea.magento2plugin.actions.generation.dialog.NewDbSchemaDialog; +import org.jetbrains.annotations.NotNull; + +public class NewDbSchemaAction extends AnAction { + public static final String ACTION_NAME = "Declarative Schema XML"; + public static final String ACTION_DESCRIPTION = "Create a new declarative schema XML"; + + /** + * Constructor. + */ + public NewDbSchemaAction() { + super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE); + } + + @Override + public void actionPerformed(final @NotNull AnActionEvent event) { + final DataContext dataContext = event.getDataContext(); + final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext); + if (view == null) { + return; + } + + final Project project = CommonDataKeys.PROJECT.getData(dataContext); + if (project == null) { + return; + } + + final PsiDirectory directory = view.getOrChooseDirectory(); + if (directory == null) { + return; + } + NewDbSchemaDialog.open(project, directory); + } + + @Override + public boolean isDumbAware() { + return false; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form new file mode 100644 index 000000000..266f77216 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form @@ -0,0 +1,140 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java new file mode 100644 index 000000000..082189f75 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java @@ -0,0 +1,109 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.dialog; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.magento.idea.magento2plugin.actions.generation.NewDbSchemaAction; +import com.magento.idea.magento2plugin.ui.FilteredComboBox; +import org.jetbrains.annotations.NotNull; +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.KeyStroke; +import java.awt.event.KeyEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.util.Arrays; + +public class NewDbSchemaDialog extends AbstractDialog { + private final Project project; + private final PsiDirectory directory; + private JPanel contentPanel; + private JButton buttonOK; + private JButton buttonCancel; + private JTextField tableName; + private JTextField tableComment; + private JLabel tableNameLabel; + private JLabel tableEngineLabel; + private FilteredComboBox tableEngine; + private JLabel tableResourceLabel; + private FilteredComboBox tableResource; + private JLabel tableCommentLabel; + + /** + * Constructor. + * + * @param project Project + * @param directory PsiDirectory + */ + public NewDbSchemaDialog( + final @NotNull Project project, + final @NotNull PsiDirectory directory + ) { + super(); + this.project = project; + this.directory = directory; + + setTitle(NewDbSchemaAction.ACTION_DESCRIPTION); + setContentPane(contentPanel); + setModal(true); + getRootPane().setDefaultButton(buttonOK); + buttonOK.addActionListener(e -> onOK()); + buttonCancel.addActionListener(e -> onCancel()); + + // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(final WindowEvent event) { + onCancel(); + } + }); + + // call onCancel() on ESCAPE + contentPanel.registerKeyboardAction( + event -> onCancel(), + KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), + JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT + ); + } + + /** + * On buttonOK action listener. + */ + private void onOK() { + if (!validateFormFields()) { + return; + } + + this.setVisible(false); + } + + /** + * Open new declarative schema dialog. + * + * @param project Project + * @param directory PsiDirectory + */ + public static void open( + final @NotNull Project project, + final @NotNull PsiDirectory directory + ) { + final NewDbSchemaDialog dialog = new NewDbSchemaDialog(project, directory); + dialog.pack(); + dialog.centerDialog(dialog); + dialog.setVisible(true); + } + + @SuppressWarnings({"PMD.UnusedPrivateMethod"}) + private void createUIComponents() { + tableEngine = new FilteredComboBox(Arrays.asList()); + tableResource = new FilteredComboBox(Arrays.asList()); + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/util/HighlightDialogFieldOnErrorUtil.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/util/HighlightDialogFieldOnErrorUtil.java index 009a5817b..d894d51f6 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/util/HighlightDialogFieldOnErrorUtil.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/util/HighlightDialogFieldOnErrorUtil.java @@ -1,3 +1,8 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + package com.magento.idea.magento2plugin.actions.generation.dialog.util; import java.awt.Color; From 35d0e0d76d70f1c18dd3a5775f521c817428e345 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Wed, 14 Oct 2020 13:13:30 +0300 Subject: [PATCH 2/6] 309: Generation of base table xml --- resources/META-INF/plugin.xml | 1 + ...gento Module Declarative Schema XML.xml.ft | 4 + ...nto Module Declarative Schema XML.xml.html | 18 ++ resources/magento2/validation.properties | 1 + .../generation/data/DbSchemaXmlData.java | 84 ++++++ .../data/DbSchemaXmlSourceData.java | 99 +++++++ .../generation/data/ui/ComboBoxItemData.java | 48 ++++ .../generation/dialog/NewDbSchemaDialog.form | 90 ++++++- .../generation/dialog/NewDbSchemaDialog.java | 245 ++++++++++++++++-- .../validator/annotation/RuleRegistry.java | 4 +- .../validator/rule/TableNameLength.java | 22 ++ .../generator/DbSchemaXmlGenerator.java | 165 ++++++++++++ .../util/FindOrCreateDbSchemaXmlUtil.java | 60 +++++ .../magento/files/ModuleDbSchemaXml.java | 84 +++++- .../ui/table/ComboBoxCellEditor.java | 80 ++++++ .../ui/table/TableGroupWrapper.java | 159 ++++++++++++ 16 files changed, 1134 insertions(+), 30 deletions(-) create mode 100644 resources/fileTemplates/internal/Magento Module Declarative Schema XML.xml.ft create mode 100644 resources/fileTemplates/internal/Magento Module Declarative Schema XML.xml.html create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/data/ui/ComboBoxItemData.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/rule/TableNameLength.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateDbSchemaXmlUtil.java create mode 100644 src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java create mode 100644 src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 87be3ec8d..c764c4986 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -225,6 +225,7 @@ + diff --git a/resources/fileTemplates/internal/Magento Module Declarative Schema XML.xml.ft b/resources/fileTemplates/internal/Magento Module Declarative Schema XML.xml.ft new file mode 100644 index 000000000..936509e1e --- /dev/null +++ b/resources/fileTemplates/internal/Magento Module Declarative Schema XML.xml.ft @@ -0,0 +1,4 @@ + + + diff --git a/resources/fileTemplates/internal/Magento Module Declarative Schema XML.xml.html b/resources/fileTemplates/internal/Magento Module Declarative Schema XML.xml.html new file mode 100644 index 000000000..25e0f3d0b --- /dev/null +++ b/resources/fileTemplates/internal/Magento Module Declarative Schema XML.xml.html @@ -0,0 +1,18 @@ + + + + +

+ Declarative Schema aims to simplify the Magento installation and upgrade processes. The new declarative schema approach allows developers to declare the final desired state of the database and has the system adjust to it automatically, without performing redundant operations. Developers are no longer forced to write scripts for each new version. In addition, this approach allows data be deleted when a module is uninstalled. +

+

+ Read more about Declarative Schema Overview. +

+
+ + diff --git a/resources/magento2/validation.properties b/resources/magento2/validation.properties index 6d857d9cd..a413fc416 100644 --- a/resources/magento2/validation.properties +++ b/resources/magento2/validation.properties @@ -33,4 +33,5 @@ validator.mustNotBeEmptyShouldContainLettersOrNumbers=Must not be empty, should validator.magentoRouteIdInvalid=The route id is invalid validator.magentoAclResourceIdInvalid=The ACL resource id is invalid validator.lowercaseCharacters={0} must contain lowercase characters only +validator.db.invalidTableNameLength=Table name must contain up to 64 characters only (inclusive) validator.lowerSnakeCase=The {0} field must be of the lower snake case format diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java new file mode 100644 index 000000000..2d36dd2e8 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java @@ -0,0 +1,84 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.data; + +import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import java.util.LinkedHashMap; +import java.util.Map; + +public class DbSchemaXmlData { + private String tableName; + private String tableResource; + private String tableEngine; + private String tableComment; + + /** + * Constructor. + * + * @param tableName String + * @param tableResource String + * @param tableEngine String + * @param tableComment String + */ + public DbSchemaXmlData( + final String tableName, + final String tableResource, + final String tableEngine, + final String tableComment + ) { + this.tableName = tableName; + this.tableResource = tableResource; + this.tableEngine = tableEngine; + this.tableComment = tableComment; + } + + public String getTableName() { + return tableName; + } + + public void setTableName(final String tableName) { + this.tableName = tableName; + } + + public String getTableResource() { + return tableResource; + } + + public void setTableResource(final String tableResource) { + this.tableResource = tableResource; + } + + public String getTableEngine() { + return tableEngine; + } + + public void setTableEngine(final String tableEngine) { + this.tableEngine = tableEngine; + } + + public String getTableComment() { + return tableComment; + } + + public void setTableComment(final String tableComment) { + this.tableComment = tableComment; + } + + /** + * Get table attributes values map. + * + * @return Map + */ + public Map getTableAttributesMap() { + final Map tableAttributesData = new LinkedHashMap<>(); + tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_NAME, getTableName()); + tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_RESOURCE, getTableResource()); + tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_ENGINE, getTableEngine()); + tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_COMMENT, getTableComment()); + + return tableAttributesData; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java new file mode 100644 index 000000000..b7079a054 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java @@ -0,0 +1,99 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.data; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +public final class DbSchemaXmlSourceData { + // currently available engines + public static final String TABLE_ENGINE_INNODB = "innodb"; + public static final String TABLE_ENGINE_MEMORY = "memory"; + // currently available resources + public static final String TABLE_RESOURCE_DEFAULT = "default"; + public static final String TABLE_RESOURCE_CHECKOUT = "checkout"; + public static final String TABLE_RESOURCE_SALES = "sales"; + // available column types + // binaries + public static final String COLUMN_TYPE_BLOB = "blob"; + public static final String COLUMN_TYPE_MEDIUMBLOB = "mediumblob"; + public static final String COLUMN_TYPE_LONGBLOB = "longblob"; + public static final String COLUMN_TYPE_VARBINARY = "varbinary"; + // integers + public static final String COLUMN_TYPE_TINYINT = "tinyint"; + public static final String COLUMN_TYPE_SMALLINT = "smallint"; + public static final String COLUMN_TYPE_INT = "int"; + public static final String COLUMN_TYPE_BIGINT = "bigint"; + // reals + public static final String COLUMN_TYPE_DECIMAL = "decimal"; + public static final String COLUMN_TYPE_DOUBLE = "double"; + public static final String COLUMN_TYPE_FLOAT = "float"; + // text + public static final String COLUMN_TYPE_VARCHAR = "varchar"; + public static final String COLUMN_TYPE_TEXT = "text"; + public static final String COLUMN_TYPE_MEDIUMTEXT = "mediumtext"; + public static final String COLUMN_TYPE_LONGTEXT = "longtext"; + // boolean + public static final String COLUMN_TYPE_BOOLEAN = "boolean"; + + /** + * Denying the possibility to initialize this class. + */ + private DbSchemaXmlSourceData() {} + + /** + * Get source list for available table engines. + * + * @return List + */ + public static List getTableEngineSource() { + return new LinkedList<>(Arrays.asList( + DbSchemaXmlSourceData.TABLE_ENGINE_INNODB, + DbSchemaXmlSourceData.TABLE_ENGINE_MEMORY) + ); + } + + /** + * Get source list for available table resources. + * + * @return List + */ + public static List getTableResourceSource() { + return new LinkedList<>(Arrays.asList( + TABLE_RESOURCE_DEFAULT, + TABLE_RESOURCE_CHECKOUT, + TABLE_RESOURCE_SALES + )); + } + + /** + * Get source list for available column types. + * + * @return List + */ + public static List getColumnTypes() { + return new LinkedList<>(Arrays.asList( + "", + COLUMN_TYPE_BLOB, + COLUMN_TYPE_MEDIUMBLOB, + COLUMN_TYPE_LONGBLOB, + COLUMN_TYPE_VARBINARY, + COLUMN_TYPE_TINYINT, + COLUMN_TYPE_SMALLINT, + COLUMN_TYPE_INT, + COLUMN_TYPE_BIGINT, + COLUMN_TYPE_DECIMAL, + COLUMN_TYPE_DOUBLE, + COLUMN_TYPE_FLOAT, + COLUMN_TYPE_VARCHAR, + COLUMN_TYPE_TEXT, + COLUMN_TYPE_MEDIUMTEXT, + COLUMN_TYPE_LONGTEXT, + COLUMN_TYPE_BOOLEAN + )); + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/ui/ComboBoxItemData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/ui/ComboBoxItemData.java new file mode 100644 index 000000000..cfbdd5b5f --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/ui/ComboBoxItemData.java @@ -0,0 +1,48 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.data.ui; + +/** + * Data Models for storing ComboBox UI component item data. + */ +public class ComboBoxItemData { + private final String key; + private final String text; + + /** + * Constructor. + * + * @param key String + * @param text String + */ + public ComboBoxItemData(final String key, final String text) { + this.key = key; + this.text = text; + } + + /** + * Get key. + * + * @return String + */ + public String getKey() { + return key; + } + + /** + * Get Text. + * + * @return String + */ + public String getText() { + return text; + } + + @Override + public String toString() { + return this.getText(); + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form index 266f77216..bf5363aa6 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form @@ -1,20 +1,20 @@
- + - + - + - + @@ -48,7 +48,7 @@ - + @@ -86,6 +86,7 @@ + @@ -94,6 +95,7 @@ + @@ -114,27 +116,91 @@ - + - + - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java index 082189f75..c151058f2 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java @@ -8,33 +8,108 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.PsiDirectory; import com.magento.idea.magento2plugin.actions.generation.NewDbSchemaAction; -import com.magento.idea.magento2plugin.ui.FilteredComboBox; -import org.jetbrains.annotations.NotNull; +import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; +import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlSourceData; +import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import javax.swing.AbstractCellEditor; import javax.swing.JButton; +import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.KeyStroke; -import java.awt.event.KeyEvent; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.util.Arrays; +import javax.swing.DefaultCellEditor; +import javax.swing.table.TableCellEditor; +import javax.swing.table.TableColumn; + +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.Lowercase; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.TableNameLength; +import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator; +import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import com.magento.idea.magento2plugin.ui.table.ComboBoxCellEditor; +import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper; +import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil; +import org.jetbrains.annotations.NotNull; public class NewDbSchemaDialog extends AbstractDialog { + private static final String TABLE_NAME = "Table Name"; + + // Table Columns + private static final String COLUMN_TYPE = "Type"; + private static final String COLUMN_NAME = "Name"; + private static final String COLUMN_PADDING = "Padding"; + private static final String COLUMN_UNSIGNED = "Unsigned"; + private static final String COLUMN_NULLABLE = "Nullable"; + private static final String COLUMN_IDENTITY = "Identity"; + private static final String COLUMN_COMMENT = "Comment"; + private static final String COLUMN_LENGTH = "Length"; + private static final String COLUMN_PRECISION = "Precision"; + private static final String COLUMN_SCALE = "Scale"; + private static final String COLUMN_DEFAULT = "Default"; + private static final String COLUMN_ON_CREATE = "On Create"; + private static final String COLUMN_ON_UPDATE = "On Update"; + private final Project project; + private final String moduleName; private final PsiDirectory directory; private JPanel contentPanel; + + // Buttons private JButton buttonOK; private JButton buttonCancel; + + // Fields + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, TABLE_NAME}) + @FieldValidation(rule = RuleRegistry.LOWERCASE, message = {Lowercase.MESSAGE, TABLE_NAME}) + @FieldValidation( + rule = RuleRegistry.ALPHANUMERIC_WITH_UNDERSCORE, + message = {AlphanumericWithUnderscoreRule.MESSAGE, TABLE_NAME} + ) + @FieldValidation( + rule = RuleRegistry.TABLE_NAME_LENGTH, + message = {TableNameLength.MESSAGE} + ) private JTextField tableName; + private JTextField tableComment; - private JLabel tableNameLabel; - private JLabel tableEngineLabel; - private FilteredComboBox tableEngine; - private JLabel tableResourceLabel; - private FilteredComboBox tableResource; - private JLabel tableCommentLabel; + private JComboBox tableEngine; + private JComboBox tableResource; + + // Table Columns UI components group + private JTable columnsTable; + private JButton addColumnButton; + private JScrollPane columnsScrollPanel;//NOPMD + + // Table Constraints UI components group + private JTable constraintsTable; + private JButton addConstraintButton; + private JScrollPane constraintsScrollPanel;//NOPMD + + // Labels + private JLabel tableNameLabel;//NOPMD + private JLabel tableEngineLabel;//NOPMD + private JLabel tableResourceLabel;//NOPMD + private JLabel tableCommentLabel;//NOPMD + private JLabel tableColumnsLabel;//NOPMD + private JLabel tableConstraintsLabel;//NOPMD /** * Constructor. @@ -49,6 +124,7 @@ public NewDbSchemaDialog( super(); this.project = project; this.directory = directory; + moduleName = GetModuleNameByDirectoryUtil.execute(directory, project); setTitle(NewDbSchemaAction.ACTION_DESCRIPTION); setContentPane(contentPanel); @@ -72,6 +148,8 @@ public void windowClosing(final WindowEvent event) { KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); + fillComboBoxes(); + initializeColumnsUiComponentGroup(); } /** @@ -81,6 +159,7 @@ private void onOK() { if (!validateFormFields()) { return; } + generateDbSchemaXmlFile(); this.setVisible(false); } @@ -101,9 +180,143 @@ public static void open( dialog.setVisible(true); } - @SuppressWarnings({"PMD.UnusedPrivateMethod"}) - private void createUIComponents() { - tableEngine = new FilteredComboBox(Arrays.asList()); - tableResource = new FilteredComboBox(Arrays.asList()); + private void generateDbSchemaXmlFile() { + new DbSchemaXmlGenerator( + new DbSchemaXmlData( + getTableName(), + getTableResource(), + getTableEngine(), + getTableComment() + ), + project, + moduleName + ).generate(NewDbSchemaAction.ACTION_NAME, false); + } + + private void initializeColumnsUiComponentGroup() { + final List columns = new LinkedList<>(Arrays.asList( + COLUMN_TYPE, + COLUMN_NAME, + COLUMN_PADDING, + COLUMN_UNSIGNED, + COLUMN_NULLABLE, + COLUMN_IDENTITY, + COLUMN_LENGTH, + COLUMN_PRECISION, + COLUMN_SCALE, + COLUMN_DEFAULT, + COLUMN_ON_CREATE, + COLUMN_ON_UPDATE, + COLUMN_COMMENT + )); + // Set default values for columns + final Map defaultValues = new HashMap<>(); + defaultValues.put(COLUMN_NULLABLE, "false"); + // Set sources for columns + final Map> sources = new HashMap<>(); + final List booleanSource = Arrays.asList("true", "false"); + sources.put(COLUMN_TYPE, DbSchemaXmlSourceData.getColumnTypes()); + sources.put(COLUMN_UNSIGNED, booleanSource); + sources.put(COLUMN_NULLABLE, booleanSource); + sources.put(COLUMN_IDENTITY, booleanSource); + sources.put(COLUMN_ON_CREATE, booleanSource); + sources.put(COLUMN_ON_UPDATE, booleanSource); + // Set action listeners for columns + final Map actionListeners = new HashMap<>(); + actionListeners.put(COLUMN_TYPE, event -> { + if (event.getActionCommand().equals("comboBoxChanged")) { + if (event.getSource() instanceof JComboBox) { + final JComboBox source = (JComboBox) event.getSource(); + final JTable table = (JTable) source.getParent(); + final String value = source.getSelectedItem().toString(); + + if (!value.isEmpty()) { + final int editingRow = ((JTable) source.getParent()).getEditingRow(); + // Index should starts from the `1` to not consider xsi:type attribute + for (int index = 1; index < columns.size(); index++) { + TableCellEditor cellEditor = table.getCellEditor(editingRow, index); + JComponent component = null; + + if (cellEditor instanceof DefaultCellEditor) { + component = (JComponent) ((DefaultCellEditor) cellEditor) + .getComponent(); + } else if (cellEditor instanceof ComboBoxCellEditor) { + component = (JComponent) ((ComboBoxCellEditor) cellEditor) + .getComponent(); + } + + if (component instanceof JComboBox) { + component.setEnabled(ModuleDbSchemaXml.getAllowedAttributes(value) + .contains(columns.get(index).toLowerCase())); + } else if (component instanceof JTextField) { + ((JTextField) component).setEditable( + ModuleDbSchemaXml.getAllowedAttributes(value) + .contains(columns.get(index).toLowerCase()) + ); + } + } + } + } + } + }); + // Initialize new Table Group + TableGroupWrapper tableGroupWrapper = new TableGroupWrapper( + columnsTable, + addColumnButton, + columns, + defaultValues, + sources, + actionListeners + ); + } + + /** + * Fill ComboBoxes ui components with predefined constant values. + */ + private void fillComboBoxes() { + // Table Engine ComboBox defaults. + for (final String engine : DbSchemaXmlSourceData.getTableEngineSource()) { + tableEngine.addItem(new ComboBoxItemData(engine, engine)); + } + // Table Resource ComboBox defaults. + for (final String resource : DbSchemaXmlSourceData.getTableResourceSource()) { + tableResource.addItem(new ComboBoxItemData(resource, resource)); + } + } + + /** + * Get tableName field value. + * + * @return String + */ + private String getTableName() { + return tableName.getText().trim(); + } + + /** + * Get tableResource field value. + * + * @return String + */ + private String getTableResource() { + return tableResource.getSelectedItem().toString().trim(); + } + + /** + * Get tableEngine field value. + * + * @return String + */ + private String getTableEngine() { + return tableEngine.getSelectedItem().toString().trim(); + } + + /** + * Get tableComment field value. + * + * @return String + */ + private String getTableComment() { + return tableComment.getText().trim(); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/annotation/RuleRegistry.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/annotation/RuleRegistry.java index 9bd913fff..4f1110374 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/annotation/RuleRegistry.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/annotation/RuleRegistry.java @@ -26,6 +26,7 @@ import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpNamespaceNameRule; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.RouteIdRule; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.StartWithNumberOrCapitalLetterRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.TableNameLength; public enum RuleRegistry { NOT_EMPTY(NotEmptyRule.class), @@ -48,7 +49,8 @@ public enum RuleRegistry { CRON_SCHEDULE(CronScheduleRule.class), CONFIG_PATH(ConfigPathRule.class), CLI_COMMAND(CliCommandRule.class), - NUMERIC(NumericRule.class); + NUMERIC(NumericRule.class), + TABLE_NAME_LENGTH(TableNameLength.class); private Class rule; diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/rule/TableNameLength.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/rule/TableNameLength.java new file mode 100644 index 000000000..0103a64d2 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/validator/rule/TableNameLength.java @@ -0,0 +1,22 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule; + +public class TableNameLength implements ValidationRule { + public static final String MESSAGE = "validator.db.invalidTableNameLength"; + private static final int MAX_TABLE_NAME_LENGTH = 64; + + private static final ValidationRule INSTANCE = new TableNameLength(); + + @Override + public boolean check(final String value) { + return value.length() <= MAX_TABLE_NAME_LENGTH; + } + + public static ValidationRule getInstance() { + return INSTANCE; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java new file mode 100644 index 000000000..c48cd2705 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java @@ -0,0 +1,165 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.google.common.collect.Lists; +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDocumentManager; +import com.intellij.psi.PsiFile; +import com.intellij.psi.xml.XmlFile; +import com.intellij.psi.xml.XmlTag; +import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; +import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateDbSchemaXmlUtil; +import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +public class DbSchemaXmlGenerator extends FileGenerator { + private final Project project; + private final String moduleName; + private final DbSchemaXmlData dbSchemaXmlData; + private final FindOrCreateDbSchemaXmlUtil findOrCreateDbSchemaXmlUtil; + + private final List newTagsQueue; + private final Map newTagsChildParentRelationMap; + + /** + * Constructor. + * + * @param dbSchemaXmlData DbSchemaXmlData + * @param project Project + * @param moduleName String + */ + public DbSchemaXmlGenerator( + final @NotNull DbSchemaXmlData dbSchemaXmlData, + final @NotNull Project project, + final @NotNull String moduleName + ) { + super(project); + this.project = project; + this.moduleName = moduleName; + this.dbSchemaXmlData = dbSchemaXmlData; + findOrCreateDbSchemaXmlUtil = new FindOrCreateDbSchemaXmlUtil(project); + + newTagsQueue = new LinkedList<>(); + newTagsChildParentRelationMap = new HashMap<>(); + } + + @Override + public PsiFile generate(final String actionName) { + final XmlFile dbSchemaXmlFile = (XmlFile) findOrCreateDbSchemaXmlUtil.execute( + actionName, + moduleName + ); + if (dbSchemaXmlFile == null || !validateData(dbSchemaXmlData)) { + return null; + } + + final XmlTag rootTag = dbSchemaXmlFile.getRootTag(); + if (rootTag == null) { + return null; + } + XmlTag tableTag = findOrCreateTag( + ModuleDbSchemaXml.XML_TAG_TABLE, + ModuleDbSchemaXml.XML_ATTR_TABLE_NAME, + rootTag, + dbSchemaXmlData.getTableName(), + dbSchemaXmlData.getTableAttributesMap() + ); + return commitDbSchemaXmlFile(dbSchemaXmlFile); + } + + /** + * Save db_schema.xml file. + * + * @param dbSchemaXmlFile XmlFile + * + * @return XmlFile + */ + private XmlFile commitDbSchemaXmlFile(final XmlFile dbSchemaXmlFile) { + WriteCommandAction.runWriteCommandAction(project, () -> { + for (final XmlTag tag : Lists.reverse(newTagsQueue)) { + if (newTagsChildParentRelationMap.containsKey(tag)) { + final XmlTag parent = newTagsChildParentRelationMap.get(tag); + parent.addSubTag(tag, false); + } + } + }); + final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project); + final Document document = psiDocumentManager.getDocument(dbSchemaXmlFile); + + if (document != null) { + psiDocumentManager.commitDocument(document); + } + return dbSchemaXmlFile; + } + + /** + * Find or create subtag by attribute. + * + * @param targetTagName String + * @param identityAttrName String + * @param parent XmlTag + * @param targetIdentityAttrValue String + * @param attributes Map + * + * @return XmlTag + */ + private XmlTag findOrCreateTag( + final String targetTagName, + final String identityAttrName, + final XmlTag parent, + final String targetIdentityAttrValue, + final Map attributes + ) { + for (final XmlTag childTag : parent.getSubTags()) { + if (!childTag.getName().equals(targetTagName)) { + continue; + } + final String childIdentityValue + = childTag.getAttributeValue(identityAttrName); + if (childIdentityValue != null && childIdentityValue.equals(targetIdentityAttrValue)) { + return childTag; + } + } + final XmlTag newTag = parent.createChildTag(targetTagName, null, "", false); + + if (attributes != null) { + for (Map.Entry attrEntry : attributes.entrySet()) { + if (attrEntry.getValue() != null) { + newTag.setAttribute(attrEntry.getKey(), attrEntry.getValue()); + } + } + } else { + newTag.setAttribute(identityAttrName, targetIdentityAttrValue); + } + newTagsQueue.add(newTag); + newTagsChildParentRelationMap.put(newTag, parent); + + return newTag; + } + + /** + * Check if all required data were provided. + * + * @param dbSchemaXmlData DbSchemaXmlData + * + * @return boolean + */ + private boolean validateData(final DbSchemaXmlData dbSchemaXmlData) { + return dbSchemaXmlData.getTableName() != null; + } + + @Override + protected void fillAttributes(Properties attributes) {}//NOPMD +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateDbSchemaXmlUtil.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateDbSchemaXmlUtil.java new file mode 100644 index 000000000..189f2eaf5 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/FindOrCreateDbSchemaXmlUtil.java @@ -0,0 +1,60 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator.util; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.indexes.ModuleIndex; +import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import com.magento.idea.magento2plugin.magento.packages.Areas; +import com.magento.idea.magento2plugin.magento.packages.Package; +import com.magento.idea.magento2plugin.util.magento.FileBasedIndexUtil; +import java.util.Properties; + +public final class FindOrCreateDbSchemaXmlUtil { + private final Project project; + + public FindOrCreateDbSchemaXmlUtil(final Project project) { + this.project = project; + } + + /** + * Finds or creates module db_schema.xml file. + * + * @param actionName String + * @param moduleName String + * + * @return PsiFile + */ + public PsiFile execute(final String actionName, final String moduleName) { + final DirectoryGenerator directoryGenerator = DirectoryGenerator.getInstance(); + final FileFromTemplateGenerator fileFromTemplateGenerator = + FileFromTemplateGenerator.getInstance(project); + + PsiDirectory parentDirectory = ModuleIndex.getInstance(project) + .getModuleDirectoryByModuleName(moduleName); + parentDirectory = directoryGenerator + .findOrCreateSubdirectory(parentDirectory, Package.moduleBaseAreaDir); + + final ModuleDbSchemaXml moduleDbSchemaXml = ModuleDbSchemaXml.getInstance(); + PsiFile dbSchemaXml = FileBasedIndexUtil.findModuleConfigFile( + moduleDbSchemaXml.getFileName(), + Areas.getAreaByString(Areas.base.name()), + moduleName, + project + ); + if (dbSchemaXml == null) { + dbSchemaXml = fileFromTemplateGenerator.generate( + moduleDbSchemaXml, + new Properties(), + parentDirectory, + actionName + ); + } + return dbSchemaXml; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java b/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java index 8c2a68ff2..2f47ad690 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java @@ -7,6 +7,10 @@ import com.intellij.lang.Language; import com.intellij.lang.xml.XMLLanguage; +import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlSourceData; + +import java.util.ArrayList; +import java.util.List; public class ModuleDbSchemaXml implements ModuleFileInterface { private static final ModuleDbSchemaXml INSTANCE = new ModuleDbSchemaXml(); @@ -15,17 +19,95 @@ public class ModuleDbSchemaXml implements ModuleFileInterface { //attributes public static final String XML_ATTR_TABLE_NAME = "name"; - public static final String XML_ATTR_COLUMN_NAME = "name"; + public static final String XML_ATTR_TABLE_RESOURCE = "resource"; + public static final String XML_ATTR_TABLE_ENGINE = "engine"; + public static final String XML_ATTR_TABLE_COMMENT = "comment"; public static final String XML_ATTR_CONSTRAINT_TABLE_NAME = "table"; public static final String XML_ATTR_CONSTRAINT_REFERENCE_TABLE_NAME = "referenceTable"; public static final String XML_ATTR_CONSTRAINT_COLUMN_NAME = "column"; public static final String XML_ATTR_CONSTRAINT_REFERENCE_COLUMN_NAME = "referenceColumn"; + public static final String XML_ATTR_COLUMN_NAME = "name"; + public static final String XML_ATTR_COLUMN_TYPE = "xsi:type"; + public static final String XML_ATTR_COLUMN_PADDING = "padding"; + public static final String XML_ATTR_COLUMN_UNSIGNED = "unsigned"; + public static final String XML_ATTR_COLUMN_NULLABLE = "nullable"; + public static final String XML_ATTR_COLUMN_IDENTITY = "identity"; + public static final String XML_ATTR_COLUMN_COMMENT = "comment"; + public static final String XML_ATTR_COLUMN_DEFAULT = "default"; + public static final String XML_ATTR_COLUMN_LENGTH = "length"; + public static final String XML_ATTR_COLUMN_SCALE = "scale"; + public static final String XML_ATTR_COLUMN_PRECISION = "precision"; + public static final String XML_ATTR_COLUMN_ON_UPDATE = "on_update"; + //tags public static final String XML_TAG_SCHEMA = "schema"; public static final String XML_TAG_TABLE = "table"; public static final String XML_TAG_COLUMN = "column"; public static final String XML_TAG_CONSTRAINT = "constraint"; + public static List getAllowedAttributes(final String columnType) { + List allowedAttributes = new ArrayList<>(); + + switch (columnType) { + case DbSchemaXmlSourceData.COLUMN_TYPE_BLOB: + case DbSchemaXmlSourceData.COLUMN_TYPE_MEDIUMBLOB: + case DbSchemaXmlSourceData.COLUMN_TYPE_LONGBLOB: + allowedAttributes.add(XML_ATTR_COLUMN_NAME); + allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); + allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); + break; + case DbSchemaXmlSourceData.COLUMN_TYPE_VARBINARY: + allowedAttributes.add(XML_ATTR_COLUMN_NAME); + allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); + allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); + allowedAttributes.add(XML_ATTR_COLUMN_LENGTH); + allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); + break; + case DbSchemaXmlSourceData.COLUMN_TYPE_TINYINT: + case DbSchemaXmlSourceData.COLUMN_TYPE_SMALLINT: + case DbSchemaXmlSourceData.COLUMN_TYPE_INT: + case DbSchemaXmlSourceData.COLUMN_TYPE_BIGINT: + allowedAttributes.add(XML_ATTR_COLUMN_NAME); + allowedAttributes.add(XML_ATTR_COLUMN_PADDING); + allowedAttributes.add(XML_ATTR_COLUMN_UNSIGNED); + allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); + allowedAttributes.add(XML_ATTR_COLUMN_IDENTITY); + allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); + allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); + break; + case DbSchemaXmlSourceData.COLUMN_TYPE_DECIMAL: + case DbSchemaXmlSourceData.COLUMN_TYPE_DOUBLE: + case DbSchemaXmlSourceData.COLUMN_TYPE_FLOAT: + allowedAttributes.add(XML_ATTR_COLUMN_NAME); + allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); + allowedAttributes.add(XML_ATTR_COLUMN_SCALE); + allowedAttributes.add(XML_ATTR_COLUMN_PRECISION); + allowedAttributes.add(XML_ATTR_COLUMN_UNSIGNED); + allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); + allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); + break; + case DbSchemaXmlSourceData.COLUMN_TYPE_VARCHAR: + case DbSchemaXmlSourceData.COLUMN_TYPE_TEXT: + case DbSchemaXmlSourceData.COLUMN_TYPE_MEDIUMTEXT: + case DbSchemaXmlSourceData.COLUMN_TYPE_LONGTEXT: + allowedAttributes.add(XML_ATTR_COLUMN_NAME); + allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); + allowedAttributes.add(XML_ATTR_COLUMN_LENGTH); + allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); + allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); + break; + case DbSchemaXmlSourceData.COLUMN_TYPE_BOOLEAN: + allowedAttributes.add(XML_ATTR_COLUMN_NAME); + allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); + allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); + allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); + break; + default: + break; + } + return allowedAttributes; + } + public static ModuleDbSchemaXml getInstance() { return INSTANCE; } diff --git a/src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java b/src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java new file mode 100644 index 000000000..3fad5f75e --- /dev/null +++ b/src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java @@ -0,0 +1,80 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.ui.table; + +import javax.swing.AbstractCellEditor; +import javax.swing.JComboBox; +import javax.swing.JTable; +import javax.swing.table.TableCellEditor; +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Arrays; +import java.util.List; +import java.util.Vector; + +public class ComboBoxCellEditor extends AbstractCellEditor + implements TableCellEditor, ActionListener { + private T value; + private List options; + private JComboBox editorComponent; + private ActionListener actionListener; + + public ComboBoxCellEditor(List options) { + this.options = options; + editorComponent = new JComboBox(); + + for (T option : options) { + editorComponent.addItem(option); + } + } + + @Override + public Component getTableCellEditorComponent( + JTable table, + Object value, + boolean isSelected, + int row, + int column + ) { + this.value = (T) value; + + editorComponent.setSelectedItem(value); + editorComponent.addActionListener(this); + + if (isSelected) { + editorComponent.setBackground(table.getSelectionBackground()); + } else { + editorComponent.setBackground(table.getSelectionForeground()); + } + + return editorComponent; + } + + @Override + public Object getCellEditorValue() { + return this.value; + } + + @Override + public void actionPerformed(ActionEvent event) { + JComboBox comboBox = (JComboBox) event.getSource(); + this.value = (T) comboBox.getSelectedItem(); + + if (actionListener != null) { + actionListener.actionPerformed(event); + } + editorComponent.setPopupVisible(false); + } + + public Component getComponent() { + return editorComponent; + } + + public void addAdditionalActionListener(ActionListener actionListener) { + this.actionListener = actionListener; + } +} diff --git a/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java b/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java new file mode 100644 index 000000000..781d3d026 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java @@ -0,0 +1,159 @@ +package com.magento.idea.magento2plugin.ui.table; + +import javax.swing.CellEditor; +import javax.swing.DefaultCellEditor; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JComponent; +import javax.swing.JTable; +import javax.swing.JTextField; +import javax.swing.event.CellEditorListener; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ListDataEvent; +import javax.swing.table.DefaultTableModel; +import javax.swing.table.TableCellEditor; +import javax.swing.table.TableColumn; +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class TableGroupWrapper { + private static final String COLUMN_ACTION = "Action"; + private static final String DELETE_ACTION = "Delete"; + + private final JTable table; + private final JButton addNewRowButton; + + private final Map columnsActionListener; + + /** + * Constructor. + * + * @param tableComponent JTable + * @param addNewRowButtonComponent JButton + * @param columns List + * @param defaultValues Map + * @param sources Map + * @param actionListeners Map + */ + public TableGroupWrapper( + final JTable tableComponent, + final JButton addNewRowButtonComponent, + final List columns, + final Map defaultValues, + final Map> sources, + final Map actionListeners + ) { + columnsActionListener = new HashMap<>(actionListeners); + table = tableComponent; + addNewRowButton = addNewRowButtonComponent; + addTableColumns(columns); + addDeleteButton(); + addNewRowRoutine(columns, defaultValues, sources); + } + + /** + * Get table model. + * + * @return DefaultTableModel + */ + public DefaultTableModel getTableModel() { + return (DefaultTableModel) table.getModel(); + } + + /** + * Add table columns. + * + * @param columns List + */ + private void addTableColumns(final List columns) { + final DefaultTableModel model = getTableModel(); + final Object[] columnsObjectArray = new Object[columns.size() + 1]; + + for (int index = 0; index < columns.size(); index++) { + columnsObjectArray[index] = columns.get(index); + } + columnsObjectArray[columnsObjectArray.length - 1] = COLUMN_ACTION; + model.setDataVector(new Object[][] {}, columnsObjectArray); + } + + /** + * Add delete button to action column. + */ + private void addDeleteButton() { + final TableColumn actionColumn = table.getColumn(COLUMN_ACTION); + actionColumn.setCellRenderer(new TableButton(DELETE_ACTION)); + actionColumn.setCellEditor(new DeleteRowButton(new JCheckBox())); + } + + /** + * Adding new row routine. + * + * @param columns List + * @param defaultValues Map + * @param sources Map + */ + private void addNewRowRoutine( + final List columns, + final Map defaultValues, + final Map> sources + ) { + final DefaultTableModel model = getTableModel(); + final Object[] columnValuesObjectArray = new Object[columns.size() + 1]; + + for (int index = 0; index < columns.size(); index++) { + if (defaultValues != null && defaultValues.containsKey(columns.get(index))) { + columnValuesObjectArray[index] = defaultValues.get(columns.get(index)); + } else { + columnValuesObjectArray[index] = ""; + } + if (sources != null && sources.containsKey(columns.get(index))) { + final String column = columns.get(index); + final List source = new LinkedList<>(sources.get(column)); + final String[] sourceArray = new String[source.size()]; + + for (int sIndex = 0; sIndex < source.size(); sIndex++) { + sourceArray[sIndex] = source.get(sIndex); + } + final TableColumn currentColumn = table.getColumn(column); + currentColumn.setCellRenderer(new ComboBoxRenderer(sourceArray)); + currentColumn.setCellEditor( + new ComboBoxCellEditor(Arrays.asList(sourceArray)) + ); + + if (columnValuesObjectArray[index].toString().isEmpty() + && !columnValuesObjectArray[index].toString().equals(sourceArray[0])) { + columnValuesObjectArray[index] = sourceArray[0]; + } + } + } + columnValuesObjectArray[columnValuesObjectArray.length - 1] = DELETE_ACTION; + + addNewRowButton.addActionListener(event -> { + model.addRow(columnValuesObjectArray); + + for (int index = 0; index < columns.size(); index++) { + final String column = columns.get(index); + + if (columnsActionListener.containsKey(column)) { + TableCellEditor editor = table.getColumnModel() + .getColumn(index) + .getCellEditor(); + + if (editor instanceof ComboBoxCellEditor) { + ((ComboBoxCellEditor) editor) + .addAdditionalActionListener(columnsActionListener.get(column)); + } + } + } + }); + } +} From 00c91714a29578f52e9bb2af8894d04726721168 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Thu, 14 Jan 2021 11:40:36 +0200 Subject: [PATCH 3/6] 309: Added columns, pk and pk index generation --- .../generation/data/DbSchemaXmlData.java | 22 ++- .../data/DbSchemaXmlSourceData.java | 9 +- .../generation/dialog/NewDbSchemaDialog.form | 36 +---- .../generation/dialog/NewDbSchemaDialog.java | 146 ++++++------------ .../generator/DbSchemaXmlGenerator.java | 134 ++++++++++++++-- .../magento/files/ModuleDbSchemaXml.java | 50 +++++- .../ui/table/TableGroupWrapper.java | 125 +++++++++------ 7 files changed, 323 insertions(+), 199 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java index 2d36dd2e8..1bb1ea0f5 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java @@ -7,6 +7,8 @@ import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; public class DbSchemaXmlData { @@ -14,6 +16,7 @@ public class DbSchemaXmlData { private String tableResource; private String tableEngine; private String tableComment; + private List> columns; /** * Constructor. @@ -22,17 +25,20 @@ public class DbSchemaXmlData { * @param tableResource String * @param tableEngine String * @param tableComment String + * @param columns List */ public DbSchemaXmlData( - final String tableName, - final String tableResource, - final String tableEngine, - final String tableComment + final String tableName, + final String tableResource, + final String tableEngine, + final String tableComment, + final List> columns ) { this.tableName = tableName; this.tableResource = tableResource; this.tableEngine = tableEngine; this.tableComment = tableComment; + this.columns = columns; } public String getTableName() { @@ -67,6 +73,14 @@ public void setTableComment(final String tableComment) { this.tableComment = tableComment; } + public List> getColumns() { + return new LinkedList<>(columns); + } + + public void setColumns(List> columns) { + this.columns = columns; + } + /** * Get table attributes values map. * diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java index b7079a054..d321a5f22 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java @@ -39,6 +39,10 @@ public final class DbSchemaXmlSourceData { public static final String COLUMN_TYPE_LONGTEXT = "longtext"; // boolean public static final String COLUMN_TYPE_BOOLEAN = "boolean"; + // date + public static final String COLUMN_TYPE_DATETIME = "datetime"; + public static final String COLUMN_TYPE_DATE = "date"; + public static final String COLUMN_TYPE_TIMESTAMP = "timestamp"; /** * Denying the possibility to initialize this class. @@ -93,7 +97,10 @@ public static List getColumnTypes() { COLUMN_TYPE_TEXT, COLUMN_TYPE_MEDIUMTEXT, COLUMN_TYPE_LONGTEXT, - COLUMN_TYPE_BOOLEAN + COLUMN_TYPE_BOOLEAN, + COLUMN_TYPE_DATETIME, + COLUMN_TYPE_DATE, + COLUMN_TYPE_TIMESTAMP )); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form index bf5363aa6..7c48e7d9b 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.form @@ -1,6 +1,6 @@
- + @@ -14,7 +14,7 @@ - + @@ -48,7 +48,7 @@ - + @@ -139,15 +139,6 @@ - - - - - - - - - @@ -180,27 +171,6 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java index c151058f2..27bb11f47 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java @@ -11,9 +11,16 @@ import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlSourceData; import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.Lowercase; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.TableNameLength; +import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator; +import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper; +import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; @@ -22,7 +29,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import javax.swing.AbstractCellEditor; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; @@ -32,41 +38,11 @@ import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.KeyStroke; -import javax.swing.DefaultCellEditor; -import javax.swing.table.TableCellEditor; -import javax.swing.table.TableColumn; - -import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation; -import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry; -import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule; -import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.Lowercase; -import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule; -import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.TableNameLength; -import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator; -import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; -import com.magento.idea.magento2plugin.ui.table.ComboBoxCellEditor; -import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper; -import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil; import org.jetbrains.annotations.NotNull; public class NewDbSchemaDialog extends AbstractDialog { private static final String TABLE_NAME = "Table Name"; - // Table Columns - private static final String COLUMN_TYPE = "Type"; - private static final String COLUMN_NAME = "Name"; - private static final String COLUMN_PADDING = "Padding"; - private static final String COLUMN_UNSIGNED = "Unsigned"; - private static final String COLUMN_NULLABLE = "Nullable"; - private static final String COLUMN_IDENTITY = "Identity"; - private static final String COLUMN_COMMENT = "Comment"; - private static final String COLUMN_LENGTH = "Length"; - private static final String COLUMN_PRECISION = "Precision"; - private static final String COLUMN_SCALE = "Scale"; - private static final String COLUMN_DEFAULT = "Default"; - private static final String COLUMN_ON_CREATE = "On Create"; - private static final String COLUMN_ON_UPDATE = "On Update"; - private final Project project; private final String moduleName; private final PsiDirectory directory; @@ -94,22 +70,17 @@ public class NewDbSchemaDialog extends AbstractDialog { private JComboBox tableResource; // Table Columns UI components group + private TableGroupWrapper columnsTableGroupWrapper; private JTable columnsTable; private JButton addColumnButton; private JScrollPane columnsScrollPanel;//NOPMD - // Table Constraints UI components group - private JTable constraintsTable; - private JButton addConstraintButton; - private JScrollPane constraintsScrollPanel;//NOPMD - // Labels private JLabel tableNameLabel;//NOPMD private JLabel tableEngineLabel;//NOPMD private JLabel tableResourceLabel;//NOPMD private JLabel tableCommentLabel;//NOPMD private JLabel tableColumnsLabel;//NOPMD - private JLabel tableConstraintsLabel;//NOPMD /** * Constructor. @@ -186,7 +157,8 @@ private void generateDbSchemaXmlFile() { getTableName(), getTableResource(), getTableEngine(), - getTableComment() + getTableComment(), + getColumns() ), project, moduleName @@ -195,79 +167,40 @@ private void generateDbSchemaXmlFile() { private void initializeColumnsUiComponentGroup() { final List columns = new LinkedList<>(Arrays.asList( - COLUMN_TYPE, - COLUMN_NAME, - COLUMN_PADDING, - COLUMN_UNSIGNED, - COLUMN_NULLABLE, - COLUMN_IDENTITY, - COLUMN_LENGTH, - COLUMN_PRECISION, - COLUMN_SCALE, - COLUMN_DEFAULT, - COLUMN_ON_CREATE, - COLUMN_ON_UPDATE, - COLUMN_COMMENT + ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, + ModuleDbSchemaXml.XML_ATTR_COLUMN_PADDING, + ModuleDbSchemaXml.XML_ATTR_COLUMN_UNSIGNED, + ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, + ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, + ModuleDbSchemaXml.XML_ATTR_COLUMN_LENGTH, + ModuleDbSchemaXml.XML_ATTR_COLUMN_PRECISION, + ModuleDbSchemaXml.XML_ATTR_COLUMN_SCALE, + ModuleDbSchemaXml.XML_ATTR_COLUMN_ON_UPDATE, + ModuleDbSchemaXml.XML_ATTR_COLUMN_DEFAULT, + ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT )); // Set default values for columns final Map defaultValues = new HashMap<>(); - defaultValues.put(COLUMN_NULLABLE, "false"); + defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, "false"); + defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, "false"); // Set sources for columns final Map> sources = new HashMap<>(); final List booleanSource = Arrays.asList("true", "false"); - sources.put(COLUMN_TYPE, DbSchemaXmlSourceData.getColumnTypes()); - sources.put(COLUMN_UNSIGNED, booleanSource); - sources.put(COLUMN_NULLABLE, booleanSource); - sources.put(COLUMN_IDENTITY, booleanSource); - sources.put(COLUMN_ON_CREATE, booleanSource); - sources.put(COLUMN_ON_UPDATE, booleanSource); - // Set action listeners for columns - final Map actionListeners = new HashMap<>(); - actionListeners.put(COLUMN_TYPE, event -> { - if (event.getActionCommand().equals("comboBoxChanged")) { - if (event.getSource() instanceof JComboBox) { - final JComboBox source = (JComboBox) event.getSource(); - final JTable table = (JTable) source.getParent(); - final String value = source.getSelectedItem().toString(); - - if (!value.isEmpty()) { - final int editingRow = ((JTable) source.getParent()).getEditingRow(); - // Index should starts from the `1` to not consider xsi:type attribute - for (int index = 1; index < columns.size(); index++) { - TableCellEditor cellEditor = table.getCellEditor(editingRow, index); - JComponent component = null; - - if (cellEditor instanceof DefaultCellEditor) { - component = (JComponent) ((DefaultCellEditor) cellEditor) - .getComponent(); - } else if (cellEditor instanceof ComboBoxCellEditor) { - component = (JComponent) ((ComboBoxCellEditor) cellEditor) - .getComponent(); - } - - if (component instanceof JComboBox) { - component.setEnabled(ModuleDbSchemaXml.getAllowedAttributes(value) - .contains(columns.get(index).toLowerCase())); - } else if (component instanceof JTextField) { - ((JTextField) component).setEditable( - ModuleDbSchemaXml.getAllowedAttributes(value) - .contains(columns.get(index).toLowerCase()) - ); - } - } - } - } - } - }); + sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, DbSchemaXmlSourceData.getColumnTypes()); + sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_UNSIGNED, booleanSource); + sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, booleanSource); + sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, booleanSource); + sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_ON_UPDATE, booleanSource); // Initialize new Table Group - TableGroupWrapper tableGroupWrapper = new TableGroupWrapper( + columnsTableGroupWrapper = new TableGroupWrapper( columnsTable, addColumnButton, columns, defaultValues, - sources, - actionListeners + sources ); + columnsTableGroupWrapper.initTableGroup(); } /** @@ -319,4 +252,13 @@ private String getTableEngine() { private String getTableComment() { return tableComment.getText().trim(); } + + /** + * Get columnsTable values. + * + * @return List + */ + private List> getColumns() { + return columnsTableGroupWrapper.getColumnsData(); + } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java index c48cd2705..88a590a69 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java @@ -16,13 +16,13 @@ import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateDbSchemaXmlUtil; import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; -import org.jetbrains.annotations.NotNull; - import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Properties; +import org.jetbrains.annotations.NotNull; public class DbSchemaXmlGenerator extends FileGenerator { private final Project project; @@ -56,6 +56,7 @@ public DbSchemaXmlGenerator( } @Override + @SuppressWarnings({"PMD.NPathComplexity", "PMD.CyclomaticComplexity"}) public PsiFile generate(final String actionName) { final XmlFile dbSchemaXmlFile = (XmlFile) findOrCreateDbSchemaXmlUtil.execute( actionName, @@ -66,19 +67,134 @@ public PsiFile generate(final String actionName) { } final XmlTag rootTag = dbSchemaXmlFile.getRootTag(); + if (rootTag == null) { return null; } - XmlTag tableTag = findOrCreateTag( + final XmlTag tableTag = findOrCreateTag( ModuleDbSchemaXml.XML_TAG_TABLE, ModuleDbSchemaXml.XML_ATTR_TABLE_NAME, rootTag, dbSchemaXmlData.getTableName(), dbSchemaXmlData.getTableAttributesMap() ); + + boolean hasPrimaryKey = false; + final Map primaryKeyData = new HashMap<>();//NOPMD + + for (final Map columnData : dbSchemaXmlData.getColumns()) { + final String columnIdentityValue = + columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME); + final String identityAttrValue = + columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY); + final Map attributes = new LinkedHashMap<>();//NOPMD + + if (!hasPrimaryKey && Boolean.parseBoolean(identityAttrValue)) { + hasPrimaryKey = true; + primaryKeyData.putAll(columnData); + } + + final String columnTypeValue = columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE); + final List allowedColumns = + ModuleDbSchemaXml.getAllowedAttributes(columnTypeValue); + + for (final Map.Entry columnDataEntry : columnData.entrySet()) { + if (allowedColumns.contains(columnDataEntry.getKey()) + && !columnDataEntry.getValue().isEmpty()) { + attributes.put(columnDataEntry.getKey(), columnDataEntry.getValue()); + } + } + + findOrCreateTag( + ModuleDbSchemaXml.XML_TAG_COLUMN, + ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, + tableTag, + columnIdentityValue, + attributes + ); + } + + if (hasPrimaryKey && !primaryKeyData.isEmpty()) { + generatePrimaryKey(primaryKeyData, tableTag); + } + return commitDbSchemaXmlFile(dbSchemaXmlFile); } + /** + * Generate PK constraint and its index. + * + * @param primaryKeyData Map + * @param tableTag XmlTag + */ + private void generatePrimaryKey( + @NotNull final Map primaryKeyData, + final XmlTag tableTag + ) { + final String columnIdentityValue = primaryKeyData.get( + ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME + ); + final Map attributes = new LinkedHashMap<>();//NOPMD + attributes.put( + ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ModuleDbSchemaXml.XML_ATTR_TYPE_PK + ); + attributes.put( + ModuleDbSchemaXml.XML_ATTR_CONSTRAINT_REFERENCE_ID_NAME, + ModuleDbSchemaXml.XML_ATTR_REFERENCE_ID_PK + ); + + final XmlTag pkTag = findOrCreateTag( + ModuleDbSchemaXml.XML_TAG_CONSTRAINT, + ModuleDbSchemaXml.XML_ATTR_CONSTRAINT_REFERENCE_ID_NAME, + tableTag, + ModuleDbSchemaXml.XML_ATTR_REFERENCE_ID_PK, + attributes + ); + final Map pkColumnAttributes = new HashMap<>();//NOPMD + pkColumnAttributes.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, columnIdentityValue); + + findOrCreateTag( + ModuleDbSchemaXml.XML_TAG_COLUMN, + ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, + pkTag, + columnIdentityValue, + pkColumnAttributes + ); + + final Map pkIndexAttributes = new LinkedHashMap<>();//NOPMD + final List indexColumnsNames = new LinkedList<>(); + indexColumnsNames.add(columnIdentityValue); + + pkIndexAttributes.put( + ModuleDbSchemaXml.XML_ATTR_CONSTRAINT_REFERENCE_ID_NAME, + ModuleDbSchemaXml.generateIndexReferenceId( + dbSchemaXmlData.getTableName(), + indexColumnsNames + ) + ); + pkIndexAttributes.put( + ModuleDbSchemaXml.XML_ATTR_INDEX_TYPE_NAME, + ModuleDbSchemaXml.XML_ATTR_INDEX_TYPE_BTREE + ); + + final XmlTag pkIndexTag = findOrCreateTag( + ModuleDbSchemaXml.XML_TAG_INDEX, + ModuleDbSchemaXml.XML_ATTR_CONSTRAINT_REFERENCE_ID_NAME, + tableTag, + ModuleDbSchemaXml.XML_ATTR_REFERENCE_ID_PK, + pkIndexAttributes + ); + + findOrCreateTag( + ModuleDbSchemaXml.XML_TAG_COLUMN, + ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, + pkIndexTag, + columnIdentityValue, + pkColumnAttributes + ); + } + /** * Save db_schema.xml file. * @@ -91,7 +207,7 @@ private XmlFile commitDbSchemaXmlFile(final XmlFile dbSchemaXmlFile) { for (final XmlTag tag : Lists.reverse(newTagsQueue)) { if (newTagsChildParentRelationMap.containsKey(tag)) { final XmlTag parent = newTagsChildParentRelationMap.get(tag); - parent.addSubTag(tag, false); + parent.addSubTag(tag, true); } } }); @@ -132,16 +248,16 @@ private XmlTag findOrCreateTag( return childTag; } } - final XmlTag newTag = parent.createChildTag(targetTagName, null, "", false); + final XmlTag newTag = parent.createChildTag(targetTagName, null, null, false); - if (attributes != null) { - for (Map.Entry attrEntry : attributes.entrySet()) { + if (attributes == null) { + newTag.setAttribute(identityAttrName, targetIdentityAttrValue); + } else { + for (final Map.Entry attrEntry : attributes.entrySet()) { if (attrEntry.getValue() != null) { newTag.setAttribute(attrEntry.getKey(), attrEntry.getValue()); } } - } else { - newTag.setAttribute(identityAttrName, targetIdentityAttrValue); } newTagsQueue.add(newTag); newTagsChildParentRelationMap.put(newTag, parent); diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java b/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java index 2f47ad690..368a8201d 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java @@ -8,7 +8,6 @@ import com.intellij.lang.Language; import com.intellij.lang.xml.XMLLanguage; import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlSourceData; - import java.util.ArrayList; import java.util.List; @@ -26,6 +25,8 @@ public class ModuleDbSchemaXml implements ModuleFileInterface { public static final String XML_ATTR_CONSTRAINT_REFERENCE_TABLE_NAME = "referenceTable"; public static final String XML_ATTR_CONSTRAINT_COLUMN_NAME = "column"; public static final String XML_ATTR_CONSTRAINT_REFERENCE_COLUMN_NAME = "referenceColumn"; + public static final String XML_ATTR_CONSTRAINT_REFERENCE_ID_NAME = "referenceId"; + public static final String XML_ATTR_INDEX_TYPE_NAME = "indexType"; public static final String XML_ATTR_COLUMN_NAME = "name"; public static final String XML_ATTR_COLUMN_TYPE = "xsi:type"; public static final String XML_ATTR_COLUMN_PADDING = "padding"; @@ -39,12 +40,27 @@ public class ModuleDbSchemaXml implements ModuleFileInterface { public static final String XML_ATTR_COLUMN_PRECISION = "precision"; public static final String XML_ATTR_COLUMN_ON_UPDATE = "on_update"; + //constant attributes values + public static final String XML_ATTR_TYPE_PK = "primary"; + public static final String XML_ATTR_REFERENCE_ID_PK = "PRIMARY"; + public static final String XML_ATTR_INDEX_TYPE_BTREE = "btree"; + public static final String XML_ATTR_INDEX_TYPE_FULLTEXT = "fulltext"; + public static final String XML_ATTR_INDEX_TYPE_HASH = "hash"; + //tags public static final String XML_TAG_SCHEMA = "schema"; public static final String XML_TAG_TABLE = "table"; public static final String XML_TAG_COLUMN = "column"; public static final String XML_TAG_CONSTRAINT = "constraint"; + public static final String XML_TAG_INDEX = "index"; + /** + * Get allowed attributes for column by its type. + * + * @param columnType String + * + * @return List + */ public static List getAllowedAttributes(final String columnType) { List allowedAttributes = new ArrayList<>(); @@ -52,6 +68,7 @@ public static List getAllowedAttributes(final String columnType) { case DbSchemaXmlSourceData.COLUMN_TYPE_BLOB: case DbSchemaXmlSourceData.COLUMN_TYPE_MEDIUMBLOB: case DbSchemaXmlSourceData.COLUMN_TYPE_LONGBLOB: + case DbSchemaXmlSourceData.COLUMN_TYPE_DATE: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); @@ -102,12 +119,43 @@ public static List getAllowedAttributes(final String columnType) { allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; + case DbSchemaXmlSourceData.COLUMN_TYPE_DATETIME: + case DbSchemaXmlSourceData.COLUMN_TYPE_TIMESTAMP: + allowedAttributes.add(XML_ATTR_COLUMN_NAME); + allowedAttributes.add(XML_ATTR_COLUMN_ON_UPDATE); + allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); + allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); + allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); + break; default: break; } + allowedAttributes.add(XML_ATTR_COLUMN_TYPE); return allowedAttributes; } + /** + * Generate index key reference id. + * + * @param tableName String + * @param indexColumnsNames List + * + * @return String + */ + public static String generateIndexReferenceId( + final String tableName, + final List indexColumnsNames + ) { + StringBuilder stringBuilder = new StringBuilder(tableName.toUpperCase()); + + for (String indexName : indexColumnsNames) { + stringBuilder.append("_").append(indexName.toUpperCase()); + } + + return stringBuilder.toString(); + } + + public static ModuleDbSchemaXml getInstance() { return INSTANCE; } diff --git a/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java b/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java index 781d3d026..38a5a3832 100644 --- a/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java +++ b/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java @@ -1,29 +1,20 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + package com.magento.idea.magento2plugin.ui.table; -import javax.swing.CellEditor; -import javax.swing.DefaultCellEditor; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; import javax.swing.JButton; import javax.swing.JCheckBox; -import javax.swing.JComboBox; -import javax.swing.JComponent; import javax.swing.JTable; -import javax.swing.JTextField; -import javax.swing.event.CellEditorListener; -import javax.swing.event.ChangeEvent; -import javax.swing.event.ListDataEvent; import javax.swing.table.DefaultTableModel; -import javax.swing.table.TableCellEditor; import javax.swing.table.TableColumn; -import java.awt.Component; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.beans.PropertyChangeEvent; -import java.beans.PropertyChangeListener; -import java.util.Arrays; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; public class TableGroupWrapper { private static final String COLUMN_ACTION = "Action"; @@ -31,30 +22,37 @@ public class TableGroupWrapper { private final JTable table; private final JButton addNewRowButton; - - private final Map columnsActionListener; + private final List columns; + private final Map defaultValues; + private final Map> sources; /** * Constructor. * * @param tableComponent JTable * @param addNewRowButtonComponent JButton - * @param columns List - * @param defaultValues Map - * @param sources Map - * @param actionListeners Map + * @param columnsList List + * @param defaultColumnsValues Map + * @param columnsSources Map */ public TableGroupWrapper( final JTable tableComponent, final JButton addNewRowButtonComponent, - final List columns, - final Map defaultValues, - final Map> sources, - final Map actionListeners + final List columnsList, + final Map defaultColumnsValues, + final Map> columnsSources ) { - columnsActionListener = new HashMap<>(actionListeners); table = tableComponent; addNewRowButton = addNewRowButtonComponent; + columns = columnsList; + defaultValues = defaultColumnsValues; + sources = columnsSources; + } + + /** + * Init table group. + */ + public void initTableGroup() { addTableColumns(columns); addDeleteButton(); addNewRowRoutine(columns, defaultValues, sources); @@ -69,6 +67,30 @@ public DefaultTableModel getTableModel() { return (DefaultTableModel) table.getModel(); } + /** + * Get table columns data. + * + * @return List + */ + public List> getColumnsData() { + List> data = new LinkedList<>(); + DefaultTableModel tableModel = getTableModel(); + + for (int row = 0; row < tableModel.getRowCount(); row++) { + Map columnValues = new LinkedHashMap<>(); + + for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { + columnValues.put( + columns.get(columnIndex), + ((String) tableModel.getValueAt(row, columnIndex)).trim() + ); + } + data.add(columnValues); + } + + return data; + } + /** * Add table columns. * @@ -79,12 +101,32 @@ private void addTableColumns(final List columns) { final Object[] columnsObjectArray = new Object[columns.size() + 1]; for (int index = 0; index < columns.size(); index++) { - columnsObjectArray[index] = columns.get(index); + columnsObjectArray[index] = getTitleForColumn(columns.get(index)); } columnsObjectArray[columnsObjectArray.length - 1] = COLUMN_ACTION; model.setDataVector(new Object[][] {}, columnsObjectArray); } + /** + * Get title for column. + * + * @param column String + * + * @return String + */ + private String getTitleForColumn(final String column) { + if (Character.isUpperCase(column.charAt(0))) { + return column; + } + String title = column.replace("_", " "); + + if (title.contains(":")) { + title = title.substring(title.indexOf(":") + 1); + } + + return title.substring(0, 1).toUpperCase() + title.substring(1); + } + /** * Add delete button to action column. */ @@ -120,10 +162,10 @@ private void addNewRowRoutine( final List source = new LinkedList<>(sources.get(column)); final String[] sourceArray = new String[source.size()]; - for (int sIndex = 0; sIndex < source.size(); sIndex++) { - sourceArray[sIndex] = source.get(sIndex); + for (int sourceIndex = 0; sourceIndex < source.size(); sourceIndex++) { + sourceArray[sourceIndex] = source.get(sourceIndex); } - final TableColumn currentColumn = table.getColumn(column); + final TableColumn currentColumn = table.getColumn(getTitleForColumn(column)); currentColumn.setCellRenderer(new ComboBoxRenderer(sourceArray)); currentColumn.setCellEditor( new ComboBoxCellEditor(Arrays.asList(sourceArray)) @@ -139,21 +181,6 @@ private void addNewRowRoutine( addNewRowButton.addActionListener(event -> { model.addRow(columnValuesObjectArray); - - for (int index = 0; index < columns.size(); index++) { - final String column = columns.get(index); - - if (columnsActionListener.containsKey(column)) { - TableCellEditor editor = table.getColumnModel() - .getColumn(index) - .getCellEditor(); - - if (editor instanceof ComboBoxCellEditor) { - ((ComboBoxCellEditor) editor) - .addAdditionalActionListener(columnsActionListener.get(column)); - } - } - } }); } } From 57ce9f8408d5e9d42a254b4b27d8c3185e3cf3c2 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 15 Jan 2021 10:31:28 +0200 Subject: [PATCH 4/6] 309: Code refactoring --- .../generation/data/DbSchemaXmlData.java | 4 +- .../DbSchemaXmlSourceDataUtil.java} | 10 ++-- .../generation/dialog/NewDbSchemaDialog.java | 22 ++++---- .../generator/DbSchemaXmlGenerator.java | 2 +- .../magento/files/ModuleDbSchemaXml.java | 53 ++++++++++--------- .../ui/table/ComboBoxCellEditor.java | 50 ++++++++++------- .../ui/table/TableGroupWrapper.java | 18 +++---- 7 files changed, 88 insertions(+), 71 deletions(-) rename src/com/magento/idea/magento2plugin/actions/generation/data/{DbSchemaXmlSourceData.java => util/DbSchemaXmlSourceDataUtil.java} (92%) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java index 1bb1ea0f5..271d0d209 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java @@ -77,7 +77,7 @@ public List> getColumns() { return new LinkedList<>(columns); } - public void setColumns(List> columns) { + public void setColumns(final List> columns) { this.columns = columns; } @@ -87,7 +87,7 @@ public void setColumns(List> columns) { * @return Map */ public Map getTableAttributesMap() { - final Map tableAttributesData = new LinkedHashMap<>(); + final Map tableAttributesData = new LinkedHashMap<>();//NOPMD tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_NAME, getTableName()); tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_RESOURCE, getTableResource()); tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_ENGINE, getTableEngine()); diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/util/DbSchemaXmlSourceDataUtil.java similarity index 92% rename from src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java rename to src/com/magento/idea/magento2plugin/actions/generation/data/util/DbSchemaXmlSourceDataUtil.java index d321a5f22..85f3112aa 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlSourceData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/util/DbSchemaXmlSourceDataUtil.java @@ -3,13 +3,13 @@ * See COPYING.txt for license details. */ -package com.magento.idea.magento2plugin.actions.generation.data; +package com.magento.idea.magento2plugin.actions.generation.data.util; import java.util.Arrays; import java.util.LinkedList; import java.util.List; -public final class DbSchemaXmlSourceData { +public final class DbSchemaXmlSourceDataUtil { // currently available engines public static final String TABLE_ENGINE_INNODB = "innodb"; public static final String TABLE_ENGINE_MEMORY = "memory"; @@ -47,7 +47,7 @@ public final class DbSchemaXmlSourceData { /** * Denying the possibility to initialize this class. */ - private DbSchemaXmlSourceData() {} + private DbSchemaXmlSourceDataUtil() {} /** * Get source list for available table engines. @@ -56,8 +56,8 @@ private DbSchemaXmlSourceData() {} */ public static List getTableEngineSource() { return new LinkedList<>(Arrays.asList( - DbSchemaXmlSourceData.TABLE_ENGINE_INNODB, - DbSchemaXmlSourceData.TABLE_ENGINE_MEMORY) + DbSchemaXmlSourceDataUtil.TABLE_ENGINE_INNODB, + DbSchemaXmlSourceDataUtil.TABLE_ENGINE_MEMORY) ); } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java index 27bb11f47..04e9127aa 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java @@ -9,8 +9,8 @@ import com.intellij.psi.PsiDirectory; import com.magento.idea.magento2plugin.actions.generation.NewDbSchemaAction; import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; -import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlSourceData; import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData; +import com.magento.idea.magento2plugin.actions.generation.data.util.DbSchemaXmlSourceDataUtil; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule; @@ -40,12 +40,12 @@ import javax.swing.KeyStroke; import org.jetbrains.annotations.NotNull; +@SuppressWarnings({"PMD.TooManyFields", "PMD.LoosePackageCoupling"}) public class NewDbSchemaDialog extends AbstractDialog { private static final String TABLE_NAME = "Table Name"; private final Project project; private final String moduleName; - private final PsiDirectory directory; private JPanel contentPanel; // Buttons @@ -94,7 +94,6 @@ public NewDbSchemaDialog( ) { super(); this.project = project; - this.directory = directory; moduleName = GetModuleNameByDirectoryUtil.execute(directory, project); setTitle(NewDbSchemaAction.ACTION_DESCRIPTION); @@ -181,13 +180,16 @@ private void initializeColumnsUiComponentGroup() { ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT )); // Set default values for columns - final Map defaultValues = new HashMap<>(); + final Map defaultValues = new HashMap<>();//NOPMD defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, "false"); defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, "false"); // Set sources for columns - final Map> sources = new HashMap<>(); + final Map> sources = new HashMap<>();//NOPMD final List booleanSource = Arrays.asList("true", "false"); - sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, DbSchemaXmlSourceData.getColumnTypes()); + sources.put( + ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + DbSchemaXmlSourceDataUtil.getColumnTypes() + ); sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_UNSIGNED, booleanSource); sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, booleanSource); sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, booleanSource); @@ -208,12 +210,12 @@ private void initializeColumnsUiComponentGroup() { */ private void fillComboBoxes() { // Table Engine ComboBox defaults. - for (final String engine : DbSchemaXmlSourceData.getTableEngineSource()) { - tableEngine.addItem(new ComboBoxItemData(engine, engine)); + for (final String engine : DbSchemaXmlSourceDataUtil.getTableEngineSource()) { + tableEngine.addItem(new ComboBoxItemData(engine, engine));//NOPMD } // Table Resource ComboBox defaults. - for (final String resource : DbSchemaXmlSourceData.getTableResourceSource()) { - tableResource.addItem(new ComboBoxItemData(resource, resource)); + for (final String resource : DbSchemaXmlSourceDataUtil.getTableResourceSource()) { + tableResource.addItem(new ComboBoxItemData(resource, resource));//NOPMD } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java index 88a590a69..eadc80fb7 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java @@ -56,7 +56,7 @@ public DbSchemaXmlGenerator( } @Override - @SuppressWarnings({"PMD.NPathComplexity", "PMD.CyclomaticComplexity"}) + @SuppressWarnings({"PMD.NPathComplexity", "PMD.CyclomaticComplexity", "PMD.ExcessiveImports"}) public PsiFile generate(final String actionName) { final XmlFile dbSchemaXmlFile = (XmlFile) findOrCreateDbSchemaXmlUtil.execute( actionName, diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java b/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java index 368a8201d..1746c7585 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java @@ -7,7 +7,7 @@ import com.intellij.lang.Language; import com.intellij.lang.xml.XMLLanguage; -import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlSourceData; +import com.magento.idea.magento2plugin.actions.generation.data.util.DbSchemaXmlSourceDataUtil; import java.util.ArrayList; import java.util.List; @@ -44,8 +44,8 @@ public class ModuleDbSchemaXml implements ModuleFileInterface { public static final String XML_ATTR_TYPE_PK = "primary"; public static final String XML_ATTR_REFERENCE_ID_PK = "PRIMARY"; public static final String XML_ATTR_INDEX_TYPE_BTREE = "btree"; - public static final String XML_ATTR_INDEX_TYPE_FULLTEXT = "fulltext"; - public static final String XML_ATTR_INDEX_TYPE_HASH = "hash"; + public static final String XML_ATTR_INDEX_TYPE_FULLTEXT = "fulltext";//NOPMD + public static final String XML_ATTR_INDEX_TYPE_HASH = "hash";//NOPMD //tags public static final String XML_TAG_SCHEMA = "schema"; @@ -61,29 +61,30 @@ public class ModuleDbSchemaXml implements ModuleFileInterface { * * @return List */ + @SuppressWarnings("PMD") public static List getAllowedAttributes(final String columnType) { - List allowedAttributes = new ArrayList<>(); + final List allowedAttributes = new ArrayList<>(); switch (columnType) { - case DbSchemaXmlSourceData.COLUMN_TYPE_BLOB: - case DbSchemaXmlSourceData.COLUMN_TYPE_MEDIUMBLOB: - case DbSchemaXmlSourceData.COLUMN_TYPE_LONGBLOB: - case DbSchemaXmlSourceData.COLUMN_TYPE_DATE: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_BLOB: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_MEDIUMBLOB: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_LONGBLOB: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DATE: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceData.COLUMN_TYPE_VARBINARY: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_VARBINARY: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_LENGTH); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceData.COLUMN_TYPE_TINYINT: - case DbSchemaXmlSourceData.COLUMN_TYPE_SMALLINT: - case DbSchemaXmlSourceData.COLUMN_TYPE_INT: - case DbSchemaXmlSourceData.COLUMN_TYPE_BIGINT: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_TINYINT: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_SMALLINT: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_INT: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_BIGINT: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_PADDING); allowedAttributes.add(XML_ATTR_COLUMN_UNSIGNED); @@ -92,9 +93,9 @@ public static List getAllowedAttributes(final String columnType) { allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceData.COLUMN_TYPE_DECIMAL: - case DbSchemaXmlSourceData.COLUMN_TYPE_DOUBLE: - case DbSchemaXmlSourceData.COLUMN_TYPE_FLOAT: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DECIMAL: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DOUBLE: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_FLOAT: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); allowedAttributes.add(XML_ATTR_COLUMN_SCALE); @@ -103,24 +104,24 @@ public static List getAllowedAttributes(final String columnType) { allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceData.COLUMN_TYPE_VARCHAR: - case DbSchemaXmlSourceData.COLUMN_TYPE_TEXT: - case DbSchemaXmlSourceData.COLUMN_TYPE_MEDIUMTEXT: - case DbSchemaXmlSourceData.COLUMN_TYPE_LONGTEXT: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_VARCHAR: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_TEXT: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_MEDIUMTEXT: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_LONGTEXT: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_LENGTH); allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceData.COLUMN_TYPE_BOOLEAN: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_BOOLEAN: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceData.COLUMN_TYPE_DATETIME: - case DbSchemaXmlSourceData.COLUMN_TYPE_TIMESTAMP: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DATETIME: + case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_TIMESTAMP: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_ON_UPDATE); allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); @@ -146,10 +147,10 @@ public static String generateIndexReferenceId( final String tableName, final List indexColumnsNames ) { - StringBuilder stringBuilder = new StringBuilder(tableName.toUpperCase()); + final StringBuilder stringBuilder = new StringBuilder(tableName.toUpperCase());//NOPMD - for (String indexName : indexColumnsNames) { - stringBuilder.append("_").append(indexName.toUpperCase()); + for (final String indexName : indexColumnsNames) { + stringBuilder.append("_").append(indexName.toUpperCase());//NOPMD } return stringBuilder.toString(); diff --git a/src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java b/src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java index 3fad5f75e..b69deebe7 100644 --- a/src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java +++ b/src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java @@ -5,40 +5,44 @@ package com.magento.idea.magento2plugin.ui.table; -import javax.swing.AbstractCellEditor; -import javax.swing.JComboBox; -import javax.swing.JTable; -import javax.swing.table.TableCellEditor; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -import java.util.Arrays; import java.util.List; -import java.util.Vector; +import javax.swing.AbstractCellEditor; +import javax.swing.JComboBox; +import javax.swing.JTable; +import javax.swing.table.TableCellEditor; public class ComboBoxCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { private T value; - private List options; - private JComboBox editorComponent; + private final List options;//NOPMD + private final JComboBox editorComponent; private ActionListener actionListener; - public ComboBoxCellEditor(List options) { + /** + * Constructor. + * + * @param options List + */ + public ComboBoxCellEditor(final List options) { + super(); this.options = options; editorComponent = new JComboBox(); - for (T option : options) { + for (final T option : this.options) { editorComponent.addItem(option); } } @Override public Component getTableCellEditorComponent( - JTable table, - Object value, - boolean isSelected, - int row, - int column + final JTable table, + final Object value, + final boolean isSelected, + final int row, + final int column ) { this.value = (T) value; @@ -60,8 +64,8 @@ public Object getCellEditorValue() { } @Override - public void actionPerformed(ActionEvent event) { - JComboBox comboBox = (JComboBox) event.getSource(); + public void actionPerformed(final ActionEvent event) { + final JComboBox comboBox = (JComboBox) event.getSource(); this.value = (T) comboBox.getSelectedItem(); if (actionListener != null) { @@ -70,11 +74,21 @@ public void actionPerformed(ActionEvent event) { editorComponent.setPopupVisible(false); } + /** + * Get component. + * + * @return Component + */ public Component getComponent() { return editorComponent; } - public void addAdditionalActionListener(ActionListener actionListener) { + /** + * Add additional action listener. + * + * @param actionListener ActionListener + */ + public void addAdditionalActionListener(final ActionListener actionListener) { this.actionListener = actionListener; } } diff --git a/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java b/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java index 38a5a3832..dd651497b 100644 --- a/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java +++ b/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java @@ -73,11 +73,11 @@ public DefaultTableModel getTableModel() { * @return List */ public List> getColumnsData() { - List> data = new LinkedList<>(); - DefaultTableModel tableModel = getTableModel(); + final List> data = new LinkedList<>(); + final DefaultTableModel tableModel = getTableModel(); for (int row = 0; row < tableModel.getRowCount(); row++) { - Map columnValues = new LinkedHashMap<>(); + final Map columnValues = new LinkedHashMap<>();//NOPMD for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { columnValues.put( @@ -121,10 +121,10 @@ private String getTitleForColumn(final String column) { String title = column.replace("_", " "); if (title.contains(":")) { - title = title.substring(title.indexOf(":") + 1); + title = title.substring(title.indexOf(':') + 1); } - return title.substring(0, 1).toUpperCase() + title.substring(1); + return title.substring(0, 1).toUpperCase() + title.substring(1);//NOPMD } /** @@ -159,16 +159,16 @@ private void addNewRowRoutine( } if (sources != null && sources.containsKey(columns.get(index))) { final String column = columns.get(index); - final List source = new LinkedList<>(sources.get(column)); - final String[] sourceArray = new String[source.size()]; + final List source = new LinkedList<>(sources.get(column));//NOPMD + final String[] sourceArray = new String[source.size()];//NOPMD for (int sourceIndex = 0; sourceIndex < source.size(); sourceIndex++) { sourceArray[sourceIndex] = source.get(sourceIndex); } final TableColumn currentColumn = table.getColumn(getTitleForColumn(column)); - currentColumn.setCellRenderer(new ComboBoxRenderer(sourceArray)); + currentColumn.setCellRenderer(new ComboBoxRenderer(sourceArray));//NOPMD currentColumn.setCellEditor( - new ComboBoxCellEditor(Arrays.asList(sourceArray)) + new ComboBoxCellEditor(Arrays.asList(sourceArray))//NOPMD ); if (columnValuesObjectArray[index].toString().isEmpty() From 7d9ac34906341a863de010f8456b669a9b32205f Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Mon, 18 Jan 2021 12:46:05 +0200 Subject: [PATCH 5/6] 309: Added test for DbSchemaXmlGenerator --- .../generation/dialog/NewDbSchemaDialog.java | 2 +- .../generateDbSchemaXmlFile/db_schema.xml | 25 +++ .../generator/DbSchemaXmlGeneratorTest.java | 150 ++++++++++++++++++ 3 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 testData/actions/generation/generator/DbSchemaXmlGenerator/generateDbSchemaXmlFile/db_schema.xml create mode 100644 tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGeneratorTest.java diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java index 04e9127aa..434ccef49 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java @@ -40,7 +40,7 @@ import javax.swing.KeyStroke; import org.jetbrains.annotations.NotNull; -@SuppressWarnings({"PMD.TooManyFields", "PMD.LoosePackageCoupling"}) +@SuppressWarnings({"PMD.TooManyFields", "PMD.ExcessiveImports"}) public class NewDbSchemaDialog extends AbstractDialog { private static final String TABLE_NAME = "Table Name"; diff --git a/testData/actions/generation/generator/DbSchemaXmlGenerator/generateDbSchemaXmlFile/db_schema.xml b/testData/actions/generation/generator/DbSchemaXmlGenerator/generateDbSchemaXmlFile/db_schema.xml new file mode 100644 index 000000000..7df2489f0 --- /dev/null +++ b/testData/actions/generation/generator/DbSchemaXmlGenerator/generateDbSchemaXmlFile/db_schema.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + +
+
diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGeneratorTest.java new file mode 100644 index 000000000..e9dd2255b --- /dev/null +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGeneratorTest.java @@ -0,0 +1,150 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; +import com.magento.idea.magento2plugin.actions.generation.data.util.DbSchemaXmlSourceDataUtil; +import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public class DbSchemaXmlGeneratorTest extends BaseGeneratorTestCase { + private static final String MODULE_NAME = "Foo_Bar"; + private static final String EXPECTED_DIRECTORY = "src/app/code/Foo/Bar/etc"; + private static final String TABLE_NAME = "test_table"; + private static final String TABLE_COMMENT = "Test Table"; + private static final String BOOLEAN_VALUE_TRUE = "true"; + private static final String BOOLEAN_VALUE_FALSE = "false"; + private static final String CURRENT_TIMESTAMP_DEFAULT_VALUE = "CURRENT_TIMESTAMP"; + private static final String TABLE_RESOURCE = DbSchemaXmlSourceDataUtil.TABLE_RESOURCE_DEFAULT; + private static final String TABLE_ENGINE = DbSchemaXmlSourceDataUtil.TABLE_ENGINE_INNODB; + + /** + * Test whether db_schema.xml file generated correctly. + */ + public void testGenerateDbSchemaXmlFile() { + final DbSchemaXmlData dbSchemaXmlData = new DbSchemaXmlData( + TABLE_NAME, + TABLE_RESOURCE, + TABLE_ENGINE, + TABLE_COMMENT, + createColumnsForTest() + ); + final DbSchemaXmlGenerator dbSchemaXmlGenerator = new DbSchemaXmlGenerator( + dbSchemaXmlData, + myFixture.getProject(), + MODULE_NAME + ); + + final String filePath = this.getFixturePath(ModuleDbSchemaXml.FILE_NAME); + + assertGeneratedFileIsCorrect( + myFixture.configureByFile(filePath), + EXPECTED_DIRECTORY, + dbSchemaXmlGenerator.generate("test") + ); + } + + /** + * Generate columns for testcase. + * + * @return List + */ + @SuppressWarnings("PMD") + private List> createColumnsForTest() { + final List> columns = new LinkedList<>(); + final Map entityIdColumnData = new LinkedHashMap<>(); + entityIdColumnData.put( + ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + DbSchemaXmlSourceDataUtil.COLUMN_TYPE_INT + ); + entityIdColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "entity_id"); + entityIdColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_PADDING, "11"); + entityIdColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_UNSIGNED, BOOLEAN_VALUE_TRUE); + entityIdColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, BOOLEAN_VALUE_FALSE); + entityIdColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, BOOLEAN_VALUE_TRUE); + entityIdColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT, "Entity Id Column"); + columns.add(entityIdColumnData); + final Map nameColumnData = new LinkedHashMap<>(); + nameColumnData.put( + ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + DbSchemaXmlSourceDataUtil.COLUMN_TYPE_VARCHAR + ); + nameColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "name"); + nameColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, BOOLEAN_VALUE_FALSE); + nameColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_LENGTH, "255"); + nameColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_DEFAULT, "John Smith"); + nameColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT, "Name Column"); + columns.add(nameColumnData); + final Map ageColumnData = new LinkedHashMap<>(); + ageColumnData.put( + ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + DbSchemaXmlSourceDataUtil.COLUMN_TYPE_INT + ); + ageColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "age"); + ageColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_PADDING, "5"); + ageColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_UNSIGNED, BOOLEAN_VALUE_TRUE); + ageColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, BOOLEAN_VALUE_TRUE); + ageColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, BOOLEAN_VALUE_FALSE); + ageColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT, "Age Column"); + columns.add(ageColumnData); + final Map salaryColumnData = new LinkedHashMap<>(); + salaryColumnData.put( + ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DECIMAL + ); + salaryColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "salary"); + salaryColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_PADDING, "5"); + salaryColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_UNSIGNED, BOOLEAN_VALUE_TRUE); + salaryColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, BOOLEAN_VALUE_FALSE); + salaryColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_PRECISION, "10"); + salaryColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_SCALE, "2"); + salaryColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_DEFAULT, "0.0"); + salaryColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT, "Salary Column"); + columns.add(salaryColumnData); + final Map dobColumnData = new LinkedHashMap<>(); + dobColumnData.put( + ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DATE + ); + dobColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "dob"); + dobColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, BOOLEAN_VALUE_TRUE); + dobColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT, "Date Of The Birth Column"); + columns.add(dobColumnData); + final Map createdAtColumnData = new LinkedHashMap<>(); + createdAtColumnData.put( + ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + DbSchemaXmlSourceDataUtil.COLUMN_TYPE_TIMESTAMP + ); + createdAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "created_at"); + createdAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, BOOLEAN_VALUE_FALSE); + createdAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_ON_UPDATE, BOOLEAN_VALUE_FALSE); + createdAtColumnData.put( + ModuleDbSchemaXml.XML_ATTR_COLUMN_DEFAULT, + CURRENT_TIMESTAMP_DEFAULT_VALUE + ); + createdAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT, "Created At Column"); + columns.add(createdAtColumnData); + final Map updatedAtColumnData = new LinkedHashMap<>(); + updatedAtColumnData.put( + ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + DbSchemaXmlSourceDataUtil.COLUMN_TYPE_TIMESTAMP + ); + updatedAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "updated_at"); + updatedAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, BOOLEAN_VALUE_FALSE); + updatedAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_ON_UPDATE, BOOLEAN_VALUE_TRUE); + updatedAtColumnData.put( + ModuleDbSchemaXml.XML_ATTR_COLUMN_DEFAULT, + CURRENT_TIMESTAMP_DEFAULT_VALUE + ); + updatedAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT, "Updated At Column"); + columns.add(updatedAtColumnData); + + return columns; + } +} From 20a5a2d46e1fe7df3a32182306e7b586e805f0d2 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Tue, 19 Jan 2021 12:28:10 +0200 Subject: [PATCH 6/6] 309: Code refactoring after code review --- gradle-tasks/pmd/ruleset.xml | 4 +- .../generation/data/DbSchemaXmlData.java | 2 +- .../data/util/DbSchemaXmlSourceDataUtil.java | 106 ------------------ .../generation/dialog/NewDbSchemaDialog.java | 27 +++-- .../generator/DbSchemaXmlGenerator.java | 43 ++++--- .../magento/files/ModuleDbSchemaXml.java | 51 ++++----- .../packages/database/TableColumnTypes.java | 88 +++++++++++++++ .../packages/database/TableEngines.java | 49 ++++++++ .../packages/database/TableResources.java | 50 +++++++++ .../ui/table/ComboBoxCellEditor.java | 12 +- .../ui/table/TableGroupWrapper.java | 15 ++- .../generator/DbSchemaXmlGeneratorTest.java | 22 ++-- 12 files changed, 297 insertions(+), 172 deletions(-) delete mode 100644 src/com/magento/idea/magento2plugin/actions/generation/data/util/DbSchemaXmlSourceDataUtil.java create mode 100644 src/com/magento/idea/magento2plugin/magento/packages/database/TableColumnTypes.java create mode 100644 src/com/magento/idea/magento2plugin/magento/packages/database/TableEngines.java create mode 100644 src/com/magento/idea/magento2plugin/magento/packages/database/TableResources.java diff --git a/gradle-tasks/pmd/ruleset.xml b/gradle-tasks/pmd/ruleset.xml index c6ed1e5a7..d72411440 100644 --- a/gradle-tasks/pmd/ruleset.xml +++ b/gradle-tasks/pmd/ruleset.xml @@ -37,7 +37,9 @@ .*/resources/.* .*/testData/.* - + + + diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java index 271d0d209..19ad86334 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java @@ -87,7 +87,7 @@ public void setColumns(final List> columns) { * @return Map */ public Map getTableAttributesMap() { - final Map tableAttributesData = new LinkedHashMap<>();//NOPMD + final Map tableAttributesData = new LinkedHashMap<>(); tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_NAME, getTableName()); tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_RESOURCE, getTableResource()); tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_ENGINE, getTableEngine()); diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/util/DbSchemaXmlSourceDataUtil.java b/src/com/magento/idea/magento2plugin/actions/generation/data/util/DbSchemaXmlSourceDataUtil.java deleted file mode 100644 index 85f3112aa..000000000 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/util/DbSchemaXmlSourceDataUtil.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright © Magento, Inc. All rights reserved. - * See COPYING.txt for license details. - */ - -package com.magento.idea.magento2plugin.actions.generation.data.util; - -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; - -public final class DbSchemaXmlSourceDataUtil { - // currently available engines - public static final String TABLE_ENGINE_INNODB = "innodb"; - public static final String TABLE_ENGINE_MEMORY = "memory"; - // currently available resources - public static final String TABLE_RESOURCE_DEFAULT = "default"; - public static final String TABLE_RESOURCE_CHECKOUT = "checkout"; - public static final String TABLE_RESOURCE_SALES = "sales"; - // available column types - // binaries - public static final String COLUMN_TYPE_BLOB = "blob"; - public static final String COLUMN_TYPE_MEDIUMBLOB = "mediumblob"; - public static final String COLUMN_TYPE_LONGBLOB = "longblob"; - public static final String COLUMN_TYPE_VARBINARY = "varbinary"; - // integers - public static final String COLUMN_TYPE_TINYINT = "tinyint"; - public static final String COLUMN_TYPE_SMALLINT = "smallint"; - public static final String COLUMN_TYPE_INT = "int"; - public static final String COLUMN_TYPE_BIGINT = "bigint"; - // reals - public static final String COLUMN_TYPE_DECIMAL = "decimal"; - public static final String COLUMN_TYPE_DOUBLE = "double"; - public static final String COLUMN_TYPE_FLOAT = "float"; - // text - public static final String COLUMN_TYPE_VARCHAR = "varchar"; - public static final String COLUMN_TYPE_TEXT = "text"; - public static final String COLUMN_TYPE_MEDIUMTEXT = "mediumtext"; - public static final String COLUMN_TYPE_LONGTEXT = "longtext"; - // boolean - public static final String COLUMN_TYPE_BOOLEAN = "boolean"; - // date - public static final String COLUMN_TYPE_DATETIME = "datetime"; - public static final String COLUMN_TYPE_DATE = "date"; - public static final String COLUMN_TYPE_TIMESTAMP = "timestamp"; - - /** - * Denying the possibility to initialize this class. - */ - private DbSchemaXmlSourceDataUtil() {} - - /** - * Get source list for available table engines. - * - * @return List - */ - public static List getTableEngineSource() { - return new LinkedList<>(Arrays.asList( - DbSchemaXmlSourceDataUtil.TABLE_ENGINE_INNODB, - DbSchemaXmlSourceDataUtil.TABLE_ENGINE_MEMORY) - ); - } - - /** - * Get source list for available table resources. - * - * @return List - */ - public static List getTableResourceSource() { - return new LinkedList<>(Arrays.asList( - TABLE_RESOURCE_DEFAULT, - TABLE_RESOURCE_CHECKOUT, - TABLE_RESOURCE_SALES - )); - } - - /** - * Get source list for available column types. - * - * @return List - */ - public static List getColumnTypes() { - return new LinkedList<>(Arrays.asList( - "", - COLUMN_TYPE_BLOB, - COLUMN_TYPE_MEDIUMBLOB, - COLUMN_TYPE_LONGBLOB, - COLUMN_TYPE_VARBINARY, - COLUMN_TYPE_TINYINT, - COLUMN_TYPE_SMALLINT, - COLUMN_TYPE_INT, - COLUMN_TYPE_BIGINT, - COLUMN_TYPE_DECIMAL, - COLUMN_TYPE_DOUBLE, - COLUMN_TYPE_FLOAT, - COLUMN_TYPE_VARCHAR, - COLUMN_TYPE_TEXT, - COLUMN_TYPE_MEDIUMTEXT, - COLUMN_TYPE_LONGTEXT, - COLUMN_TYPE_BOOLEAN, - COLUMN_TYPE_DATETIME, - COLUMN_TYPE_DATE, - COLUMN_TYPE_TIMESTAMP - )); - } -} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java index 434ccef49..628f504d1 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java @@ -10,7 +10,6 @@ import com.magento.idea.magento2plugin.actions.generation.NewDbSchemaAction; import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData; -import com.magento.idea.magento2plugin.actions.generation.data.util.DbSchemaXmlSourceDataUtil; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry; import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule; @@ -19,6 +18,9 @@ import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.TableNameLength; import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator; import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes; +import com.magento.idea.magento2plugin.magento.packages.database.TableEngines; +import com.magento.idea.magento2plugin.magento.packages.database.TableResources; import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper; import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil; import java.awt.event.KeyEvent; @@ -150,6 +152,9 @@ public static void open( dialog.setVisible(true); } + /** + * Run db_schema.xml file generator. + */ private void generateDbSchemaXmlFile() { new DbSchemaXmlGenerator( new DbSchemaXmlData( @@ -164,6 +169,9 @@ private void generateDbSchemaXmlFile() { ).generate(NewDbSchemaAction.ACTION_NAME, false); } + /** + * Initialize columns ui component. + */ private void initializeColumnsUiComponentGroup() { final List columns = new LinkedList<>(Arrays.asList( ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, @@ -180,15 +188,17 @@ private void initializeColumnsUiComponentGroup() { ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT )); // Set default values for columns - final Map defaultValues = new HashMap<>();//NOPMD + final Map defaultValues = new HashMap<>(); defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, "false"); defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, "false"); // Set sources for columns - final Map> sources = new HashMap<>();//NOPMD + final Map> sources = new HashMap<>(); final List booleanSource = Arrays.asList("true", "false"); + final List columnTypes = TableColumnTypes.getTableColumnTypesList(); + columnTypes.add(0, ""); sources.put( ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, - DbSchemaXmlSourceDataUtil.getColumnTypes() + columnTypes ); sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_UNSIGNED, booleanSource); sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, booleanSource); @@ -208,14 +218,15 @@ private void initializeColumnsUiComponentGroup() { /** * Fill ComboBoxes ui components with predefined constant values. */ + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") private void fillComboBoxes() { // Table Engine ComboBox defaults. - for (final String engine : DbSchemaXmlSourceDataUtil.getTableEngineSource()) { - tableEngine.addItem(new ComboBoxItemData(engine, engine));//NOPMD + for (final String engine : TableEngines.getTableEnginesList()) { + tableEngine.addItem(new ComboBoxItemData(engine, engine)); } // Table Resource ComboBox defaults. - for (final String resource : DbSchemaXmlSourceDataUtil.getTableResourceSource()) { - tableResource.addItem(new ComboBoxItemData(resource, resource));//NOPMD + for (final String resource : TableResources.getTableResourcesList()) { + tableResource.addItem(new ComboBoxItemData(resource, resource)); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java index eadc80fb7..5be58ad68 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java @@ -16,7 +16,9 @@ import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateDbSchemaXmlUtil; import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes; import java.util.HashMap; +import java.util.InputMismatchException; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; @@ -56,7 +58,12 @@ public DbSchemaXmlGenerator( } @Override - @SuppressWarnings({"PMD.NPathComplexity", "PMD.CyclomaticComplexity", "PMD.ExcessiveImports"}) + @SuppressWarnings({ + "PMD.NPathComplexity", + "PMD.CyclomaticComplexity", + "PMD.ExcessiveImports", + "PMD.AvoidInstantiatingObjectsInLoops" + }) public PsiFile generate(final String actionName) { final XmlFile dbSchemaXmlFile = (XmlFile) findOrCreateDbSchemaXmlUtil.execute( actionName, @@ -80,14 +87,11 @@ public PsiFile generate(final String actionName) { ); boolean hasPrimaryKey = false; - final Map primaryKeyData = new HashMap<>();//NOPMD + final Map primaryKeyData = new HashMap<>(); for (final Map columnData : dbSchemaXmlData.getColumns()) { - final String columnIdentityValue = - columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME); final String identityAttrValue = columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY); - final Map attributes = new LinkedHashMap<>();//NOPMD if (!hasPrimaryKey && Boolean.parseBoolean(identityAttrValue)) { hasPrimaryKey = true; @@ -95,15 +99,25 @@ public PsiFile generate(final String actionName) { } final String columnTypeValue = columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE); - final List allowedColumns = - ModuleDbSchemaXml.getAllowedAttributes(columnTypeValue); + final TableColumnTypes columnType = TableColumnTypes.getByValue(columnTypeValue); + + if (columnType == null) { + throw new InputMismatchException( + "Invalid column types provided. Should be compatible with " + + TableColumnTypes.class + ); + } + final Map attributes = new LinkedHashMap<>(); + final List allowedColumns = ModuleDbSchemaXml.getAllowedAttributes(columnType); for (final Map.Entry columnDataEntry : columnData.entrySet()) { if (allowedColumns.contains(columnDataEntry.getKey()) && !columnDataEntry.getValue().isEmpty()) { attributes.put(columnDataEntry.getKey(), columnDataEntry.getValue()); } } + final String columnIdentityValue = + columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME); findOrCreateTag( ModuleDbSchemaXml.XML_TAG_COLUMN, @@ -131,10 +145,7 @@ private void generatePrimaryKey( @NotNull final Map primaryKeyData, final XmlTag tableTag ) { - final String columnIdentityValue = primaryKeyData.get( - ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME - ); - final Map attributes = new LinkedHashMap<>();//NOPMD + final Map attributes = new LinkedHashMap<>(); attributes.put( ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, ModuleDbSchemaXml.XML_ATTR_TYPE_PK @@ -151,7 +162,10 @@ private void generatePrimaryKey( ModuleDbSchemaXml.XML_ATTR_REFERENCE_ID_PK, attributes ); - final Map pkColumnAttributes = new HashMap<>();//NOPMD + final Map pkColumnAttributes = new HashMap<>(); + final String columnIdentityValue = primaryKeyData.get( + ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME + ); pkColumnAttributes.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, columnIdentityValue); findOrCreateTag( @@ -162,7 +176,7 @@ private void generatePrimaryKey( pkColumnAttributes ); - final Map pkIndexAttributes = new LinkedHashMap<>();//NOPMD + final Map pkIndexAttributes = new LinkedHashMap<>(); final List indexColumnsNames = new LinkedList<>(); indexColumnsNames.add(columnIdentityValue); @@ -277,5 +291,6 @@ private boolean validateData(final DbSchemaXmlData dbSchemaXmlData) { } @Override - protected void fillAttributes(Properties attributes) {}//NOPMD + @SuppressWarnings("PMD.UncommentedEmptyMethodBody") + protected void fillAttributes(final Properties attributes) {} } diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java b/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java index 1746c7585..9b41ffcb8 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java @@ -7,7 +7,7 @@ import com.intellij.lang.Language; import com.intellij.lang.xml.XMLLanguage; -import com.magento.idea.magento2plugin.actions.generation.data.util.DbSchemaXmlSourceDataUtil; +import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes; import java.util.ArrayList; import java.util.List; @@ -61,30 +61,30 @@ public class ModuleDbSchemaXml implements ModuleFileInterface { * * @return List */ - @SuppressWarnings("PMD") - public static List getAllowedAttributes(final String columnType) { + @SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NcssCount"}) + public static List getAllowedAttributes(final TableColumnTypes columnType) { final List allowedAttributes = new ArrayList<>(); switch (columnType) { - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_BLOB: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_MEDIUMBLOB: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_LONGBLOB: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DATE: + case BLOB: + case MEDIUMBLOB: + case LONGBLOB: + case DATE: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_VARBINARY: + case VARBINARY: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_LENGTH); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_TINYINT: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_SMALLINT: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_INT: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_BIGINT: + case TINYINT: + case SMALLINT: + case INT: + case BIGINT: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_PADDING); allowedAttributes.add(XML_ATTR_COLUMN_UNSIGNED); @@ -93,9 +93,9 @@ public static List getAllowedAttributes(final String columnType) { allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DECIMAL: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DOUBLE: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_FLOAT: + case DECIMAL: + case DOUBLE: + case FLOAT: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); allowedAttributes.add(XML_ATTR_COLUMN_SCALE); @@ -104,24 +104,24 @@ public static List getAllowedAttributes(final String columnType) { allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_VARCHAR: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_TEXT: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_MEDIUMTEXT: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_LONGTEXT: + case VARCHAR: + case TEXT: + case MEDIUMTEXT: + case LONGTEXT: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_LENGTH); allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_BOOLEAN: + case BOOLEAN: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); break; - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DATETIME: - case DbSchemaXmlSourceDataUtil.COLUMN_TYPE_TIMESTAMP: + case DATETIME: + case TIMESTAMP: allowedAttributes.add(XML_ATTR_COLUMN_NAME); allowedAttributes.add(XML_ATTR_COLUMN_ON_UPDATE); allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); @@ -132,6 +132,7 @@ public static List getAllowedAttributes(final String columnType) { break; } allowedAttributes.add(XML_ATTR_COLUMN_TYPE); + return allowedAttributes; } @@ -143,20 +144,20 @@ public static List getAllowedAttributes(final String columnType) { * * @return String */ + @SuppressWarnings("PMD.UseLocaleWithCaseConversions") public static String generateIndexReferenceId( final String tableName, final List indexColumnsNames ) { - final StringBuilder stringBuilder = new StringBuilder(tableName.toUpperCase());//NOPMD + final StringBuilder stringBuilder = new StringBuilder(tableName.toUpperCase()); for (final String indexName : indexColumnsNames) { - stringBuilder.append("_").append(indexName.toUpperCase());//NOPMD + stringBuilder.append('_').append(indexName.toUpperCase()); } return stringBuilder.toString(); } - public static ModuleDbSchemaXml getInstance() { return INSTANCE; } diff --git a/src/com/magento/idea/magento2plugin/magento/packages/database/TableColumnTypes.java b/src/com/magento/idea/magento2plugin/magento/packages/database/TableColumnTypes.java new file mode 100644 index 000000000..ce68bcf7f --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/packages/database/TableColumnTypes.java @@ -0,0 +1,88 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.packages.database; + +import java.util.LinkedList; +import java.util.List; + +public enum TableColumnTypes { + // binaries + BLOB("blob"), + MEDIUMBLOB("mediumblob"), + LONGBLOB("longblob"), + VARBINARY("varbinary"), + // integers + TINYINT("tinyint"), + SMALLINT("smallint"), + INT("int"), + BIGINT("bigint"), + // reals + DECIMAL("decimal"), + DOUBLE("double"), + FLOAT("float"), + // text + VARCHAR("varchar"), + TEXT("text"), + MEDIUMTEXT("mediumtext"), + LONGTEXT("longtext"), + // boolean + BOOLEAN("boolean"), + // date + DATETIME("datetime"), + DATE("date"), + TIMESTAMP("timestamp"); + + private final String columnType; + + /** + * Table Column Types ENUM constructor. + * + * @param tableColumnType String + */ + TableColumnTypes(final String tableColumnType) { + columnType = tableColumnType; + } + + /** + * Get table column type name. + * + * @return String + */ + public String getColumnType() { + return columnType; + } + + /** + * Get ENUM by its string representation. + * + * @param value String + * + * @return TableColumnTypes + */ + public static TableColumnTypes getByValue(final String value) { + for (final TableColumnTypes columnType : TableColumnTypes.values()) { + if (columnType.getColumnType().equals(value)) { + return columnType; + } + } + return null; + } + + /** + * Get table available/supported column types list. + * + * @return List of available column types. + */ + public static List getTableColumnTypesList() { + final List availableColumnTypes = new LinkedList<>(); + + for (final TableColumnTypes columnType : TableColumnTypes.values()) { + availableColumnTypes.add(columnType.getColumnType()); + } + + return availableColumnTypes; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/packages/database/TableEngines.java b/src/com/magento/idea/magento2plugin/magento/packages/database/TableEngines.java new file mode 100644 index 000000000..cb54cad7e --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/packages/database/TableEngines.java @@ -0,0 +1,49 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.packages.database; + +import java.util.LinkedList; +import java.util.List; + +public enum TableEngines { + INNODB("innodb"), + MEMORY("memory"); + + private final String engine; + + /** + * Table Engines ENUM constructor. + * + * @param tableEngine String + */ + TableEngines(final String tableEngine) { + engine = tableEngine; + } + + /** + * Get table engine name. + * + * @return String + */ + public String getEngine() { + return engine; + } + + /** + * Get table available/supported engines list. + * + * @return List of available engines. + */ + public static List getTableEnginesList() { + final List availableEngines = new LinkedList<>(); + + for (final TableEngines engine : TableEngines.values()) { + availableEngines.add(engine.getEngine()); + } + + return availableEngines; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/packages/database/TableResources.java b/src/com/magento/idea/magento2plugin/magento/packages/database/TableResources.java new file mode 100644 index 000000000..414c058db --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/packages/database/TableResources.java @@ -0,0 +1,50 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.packages.database; + +import java.util.LinkedList; +import java.util.List; + +public enum TableResources { + DEFAULT("default"), + CHECKOUT("checkout"), + SALES("sales"); + + private final String resource; + + /** + * Table Resources ENUM constructor. + * + * @param tableResource String + */ + TableResources(final String tableResource) { + resource = tableResource; + } + + /** + * Get table resource name. + * + * @return String + */ + public String getResource() { + return resource; + } + + /** + * Get table available/supported resources list. + * + * @return List of available resources. + */ + public static List getTableResourcesList() { + final List availableResources = new LinkedList<>(); + + for (final TableResources resource : TableResources.values()) { + availableResources.add(resource.getResource()); + } + + return availableResources; + } +} diff --git a/src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java b/src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java index b69deebe7..8f2c94b95 100644 --- a/src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java +++ b/src/com/magento/idea/magento2plugin/ui/table/ComboBoxCellEditor.java @@ -8,6 +8,7 @@ import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.LinkedList; import java.util.List; import javax.swing.AbstractCellEditor; import javax.swing.JComboBox; @@ -17,7 +18,7 @@ public class ComboBoxCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { private T value; - private final List options;//NOPMD + private final List options; private final JComboBox editorComponent; private ActionListener actionListener; @@ -74,6 +75,15 @@ public void actionPerformed(final ActionEvent event) { editorComponent.setPopupVisible(false); } + /** + * Get combobox options. + * + * @return List of combobox options. + */ + public List getOptions() { + return new LinkedList<>(options); + } + /** * Get component. * diff --git a/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java b/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java index dd651497b..bbe6f3ffc 100644 --- a/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java +++ b/src/com/magento/idea/magento2plugin/ui/table/TableGroupWrapper.java @@ -72,12 +72,13 @@ public DefaultTableModel getTableModel() { * * @return List */ + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public List> getColumnsData() { final List> data = new LinkedList<>(); final DefaultTableModel tableModel = getTableModel(); for (int row = 0; row < tableModel.getRowCount(); row++) { - final Map columnValues = new LinkedHashMap<>();//NOPMD + final Map columnValues = new LinkedHashMap<>(); for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { columnValues.put( @@ -114,6 +115,7 @@ private void addTableColumns(final List columns) { * * @return String */ + @SuppressWarnings("PMD.UseLocaleWithCaseConversions") private String getTitleForColumn(final String column) { if (Character.isUpperCase(column.charAt(0))) { return column; @@ -124,7 +126,7 @@ private String getTitleForColumn(final String column) { title = title.substring(title.indexOf(':') + 1); } - return title.substring(0, 1).toUpperCase() + title.substring(1);//NOPMD + return title.substring(0, 1).toUpperCase() + title.substring(1); } /** @@ -143,6 +145,7 @@ private void addDeleteButton() { * @param defaultValues Map * @param sources Map */ + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") private void addNewRowRoutine( final List columns, final Map defaultValues, @@ -159,16 +162,16 @@ private void addNewRowRoutine( } if (sources != null && sources.containsKey(columns.get(index))) { final String column = columns.get(index); - final List source = new LinkedList<>(sources.get(column));//NOPMD - final String[] sourceArray = new String[source.size()];//NOPMD + final List source = new LinkedList<>(sources.get(column)); + final String[] sourceArray = new String[source.size()]; for (int sourceIndex = 0; sourceIndex < source.size(); sourceIndex++) { sourceArray[sourceIndex] = source.get(sourceIndex); } final TableColumn currentColumn = table.getColumn(getTitleForColumn(column)); - currentColumn.setCellRenderer(new ComboBoxRenderer(sourceArray));//NOPMD + currentColumn.setCellRenderer(new ComboBoxRenderer(sourceArray)); currentColumn.setCellEditor( - new ComboBoxCellEditor(Arrays.asList(sourceArray))//NOPMD + new ComboBoxCellEditor(Arrays.asList(sourceArray)) ); if (columnValuesObjectArray[index].toString().isEmpty() diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGeneratorTest.java index e9dd2255b..b19d69f52 100644 --- a/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGeneratorTest.java +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGeneratorTest.java @@ -6,8 +6,10 @@ package com.magento.idea.magento2plugin.actions.generation.generator; import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; -import com.magento.idea.magento2plugin.actions.generation.data.util.DbSchemaXmlSourceDataUtil; import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes; +import com.magento.idea.magento2plugin.magento.packages.database.TableEngines; +import com.magento.idea.magento2plugin.magento.packages.database.TableResources; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; @@ -21,8 +23,8 @@ public class DbSchemaXmlGeneratorTest extends BaseGeneratorTestCase { private static final String BOOLEAN_VALUE_TRUE = "true"; private static final String BOOLEAN_VALUE_FALSE = "false"; private static final String CURRENT_TIMESTAMP_DEFAULT_VALUE = "CURRENT_TIMESTAMP"; - private static final String TABLE_RESOURCE = DbSchemaXmlSourceDataUtil.TABLE_RESOURCE_DEFAULT; - private static final String TABLE_ENGINE = DbSchemaXmlSourceDataUtil.TABLE_ENGINE_INNODB; + private static final String TABLE_RESOURCE = TableResources.DEFAULT.getResource(); + private static final String TABLE_ENGINE = TableEngines.INNODB.getEngine(); /** * Test whether db_schema.xml file generated correctly. @@ -61,7 +63,7 @@ private List> createColumnsForTest() { final Map entityIdColumnData = new LinkedHashMap<>(); entityIdColumnData.put( ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, - DbSchemaXmlSourceDataUtil.COLUMN_TYPE_INT + TableColumnTypes.INT.getColumnType() ); entityIdColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "entity_id"); entityIdColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_PADDING, "11"); @@ -73,7 +75,7 @@ private List> createColumnsForTest() { final Map nameColumnData = new LinkedHashMap<>(); nameColumnData.put( ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, - DbSchemaXmlSourceDataUtil.COLUMN_TYPE_VARCHAR + TableColumnTypes.VARCHAR.getColumnType() ); nameColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "name"); nameColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, BOOLEAN_VALUE_FALSE); @@ -84,7 +86,7 @@ private List> createColumnsForTest() { final Map ageColumnData = new LinkedHashMap<>(); ageColumnData.put( ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, - DbSchemaXmlSourceDataUtil.COLUMN_TYPE_INT + TableColumnTypes.INT.getColumnType() ); ageColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "age"); ageColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_PADDING, "5"); @@ -96,7 +98,7 @@ private List> createColumnsForTest() { final Map salaryColumnData = new LinkedHashMap<>(); salaryColumnData.put( ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, - DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DECIMAL + TableColumnTypes.DECIMAL.getColumnType() ); salaryColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "salary"); salaryColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_PADDING, "5"); @@ -110,7 +112,7 @@ private List> createColumnsForTest() { final Map dobColumnData = new LinkedHashMap<>(); dobColumnData.put( ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, - DbSchemaXmlSourceDataUtil.COLUMN_TYPE_DATE + TableColumnTypes.DATE.getColumnType() ); dobColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "dob"); dobColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, BOOLEAN_VALUE_TRUE); @@ -119,7 +121,7 @@ private List> createColumnsForTest() { final Map createdAtColumnData = new LinkedHashMap<>(); createdAtColumnData.put( ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, - DbSchemaXmlSourceDataUtil.COLUMN_TYPE_TIMESTAMP + TableColumnTypes.TIMESTAMP.getColumnType() ); createdAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "created_at"); createdAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, BOOLEAN_VALUE_FALSE); @@ -133,7 +135,7 @@ private List> createColumnsForTest() { final Map updatedAtColumnData = new LinkedHashMap<>(); updatedAtColumnData.put( ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, - DbSchemaXmlSourceDataUtil.COLUMN_TYPE_TIMESTAMP + TableColumnTypes.TIMESTAMP.getColumnType() ); updatedAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "updated_at"); updatedAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, BOOLEAN_VALUE_FALSE);