From 20ef982d87cc1973a91bef2a9e5ac6825c5f4c13 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 15 Jan 2021 10:31:28 +0200 Subject: [PATCH 1/4] 309: Code refactoring --- .../generation/data/DbSchemaXmlData.java | 4 +- .../DbSchemaXmlSourceDataUtil.java} | 10 ++-- .../generation/dialog/NewDbSchemaDialog.java | 22 ++++---- .../magento/files/ModuleDbSchemaXml.java | 53 ++++++++++--------- .../ui/table/ComboBoxCellEditor.java | 50 ++++++++++------- .../ui/table/TableGroupWrapper.java | 18 +++---- 6 files changed, 87 insertions(+), 70 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/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 c01e98c6e1089d675b3da8b42bf1f658af46d534 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Tue, 19 Jan 2021 15:10:24 +0200 Subject: [PATCH 2/4] Entity manager innitial --- resources/META-INF/plugin.xml | 1 + .../actions/generation/NewEntityAction.java | 56 ++ .../generation/dialog/NewDataModelDialog.java | 14 +- .../generation/dialog/NewEntityDialog.form | 491 ++++++++++ .../generation/dialog/NewEntityDialog.java | 873 ++++++++++++++++++ .../magento/packages/PropertiesTypes.java | 32 + 6 files changed, 1460 insertions(+), 7 deletions(-) create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/NewEntityAction.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.form create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java create mode 100644 src/com/magento/idea/magento2plugin/magento/packages/PropertiesTypes.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index c764c4986..329af85a3 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -58,6 +58,7 @@ + diff --git a/src/com/magento/idea/magento2plugin/actions/generation/NewEntityAction.java b/src/com/magento/idea/magento2plugin/actions/generation/NewEntityAction.java new file mode 100644 index 000000000..7f798d919 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/NewEntityAction.java @@ -0,0 +1,56 @@ +/* + * 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.NewEntityDialog; + +public class NewEntityAction extends AnAction { + public static final String ACTION_NAME = "Magento 2 Entity"; + public static final String ACTION_DESCRIPTION = "Create a new Magento 2 Entity"; + + /** + * Constructor. + */ + public NewEntityAction() { + super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE); + } + + @Override + public void actionPerformed(final 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; + } + + NewEntityDialog.open(project, directory); + } + + @Override + public boolean isDumbAware() { + return false; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDataModelDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDataModelDialog.java index 07296ae35..2d6d1ffba 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDataModelDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDataModelDialog.java @@ -27,6 +27,7 @@ import com.magento.idea.magento2plugin.bundles.ValidatorBundle; import com.magento.idea.magento2plugin.magento.files.DataModel; import com.magento.idea.magento2plugin.magento.files.DataModelInterface; +import com.magento.idea.magento2plugin.magento.packages.PropertiesTypes; import com.magento.idea.magento2plugin.ui.table.ComboBoxEditor; import com.magento.idea.magento2plugin.ui.table.DeleteRowButton; import com.magento.idea.magento2plugin.ui.table.TableButton; @@ -70,8 +71,6 @@ public class NewDataModelDialog extends AbstractDialog { private static final String PROPERTY_ACTION = "Action"; private static final String PROPERTY_DELETE = "Delete"; - private static final String[] PROPERTY_TYPES = {"int", "float", "string", "bool"}; - private JPanel contentPanel; private JButton buttonOK; private JButton buttonCancel; @@ -304,9 +303,10 @@ private void initPropertiesTable() { addProperty.addActionListener(e -> { propertiesTable.addRow(new Object[]{ - "", - PROPERTY_TYPES[0], - PROPERTY_DELETE + "", + PropertiesTypes.valueOf(PropertiesTypes.INT.toString()) + .getPropertyType(), + PROPERTY_DELETE }); }); @@ -315,8 +315,8 @@ private void initPropertiesTable() { private void initPropertyTypeColumn() { final TableColumn formElementTypeColumn = propertyTable.getColumn(PROPERTY_TYPE); - formElementTypeColumn.setCellEditor(new ComboBoxEditor(PROPERTY_TYPES)); - formElementTypeColumn.setCellRenderer(new ComboBoxTableRenderer<>(PROPERTY_TYPES)); + formElementTypeColumn.setCellEditor(new ComboBoxEditor(PropertiesTypes.getPropertyTypes())); + formElementTypeColumn.setCellRenderer(new ComboBoxTableRenderer<>(PropertiesTypes.getPropertyTypes())); } private DefaultTableModel getPropertiesTable() { diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.form new file mode 100644 index 000000000..bdb3a51df --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.form @@ -0,0 +1,491 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java new file mode 100644 index 000000000..afc3d4885 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java @@ -0,0 +1,873 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.dialog; + +import com.google.common.base.CaseFormat; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.ComboBoxTableRenderer; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.generation.NewUiComponentGridAction; +import com.magento.idea.magento2plugin.actions.generation.NewViewModelAction; +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.util.ArrayList; +import java.util.List; +import javax.swing.*; +import javax.swing.table.DefaultTableModel; +import javax.swing.table.TableColumn; + +import com.magento.idea.magento2plugin.actions.generation.data.AclXmlData; +import com.magento.idea.magento2plugin.actions.generation.data.CollectionData; +import com.magento.idea.magento2plugin.actions.generation.data.ControllerFileData; +import com.magento.idea.magento2plugin.actions.generation.data.DataModelData; +import com.magento.idea.magento2plugin.actions.generation.data.DataModelInterfaceData; +import com.magento.idea.magento2plugin.actions.generation.data.LayoutXmlData; +import com.magento.idea.magento2plugin.actions.generation.data.MenuXmlData; +import com.magento.idea.magento2plugin.actions.generation.data.ModelData; +import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData; +import com.magento.idea.magento2plugin.actions.generation.data.RoutesXmlData; +import com.magento.idea.magento2plugin.actions.generation.data.UiComponentDataProviderData; +import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormButtonData; +import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormFieldData; +import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormFieldsetData; +import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormFileData; +import com.magento.idea.magento2plugin.actions.generation.data.UiComponentGridData; +import com.magento.idea.magento2plugin.actions.generation.data.UiComponentGridToolbarData; +import com.magento.idea.magento2plugin.actions.generation.data.code.ClassPropertyData; +import com.magento.idea.magento2plugin.actions.generation.generator.AclXmlGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.DataModelGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.DataModelInterfaceGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.LayoutXmlGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.MenuXmlGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.ModuleCollectionGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.ModuleControllerClassGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.ModuleModelGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.ModuleResourceModelGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.RoutesXmlGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentDataProviderGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentFormGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentGridXmlGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder; +import com.magento.idea.magento2plugin.magento.files.ControllerBackendPhp; +import com.magento.idea.magento2plugin.magento.files.DataModel; +import com.magento.idea.magento2plugin.magento.files.DataModelInterface; +import com.magento.idea.magento2plugin.magento.files.ModelPhp; +import com.magento.idea.magento2plugin.magento.files.ModuleMenuXml; +import com.magento.idea.magento2plugin.magento.files.ResourceModelPhp; +import com.magento.idea.magento2plugin.magento.packages.Areas; +import com.magento.idea.magento2plugin.magento.packages.File; +import com.magento.idea.magento2plugin.magento.packages.HttpMethod; +import com.magento.idea.magento2plugin.magento.packages.PropertiesTypes; +import com.magento.idea.magento2plugin.ui.FilteredComboBox; +import com.magento.idea.magento2plugin.ui.table.ComboBoxEditor; +import com.magento.idea.magento2plugin.ui.table.DeleteRowButton; +import com.magento.idea.magento2plugin.ui.table.TableButton; +import com.magento.idea.magento2plugin.util.FirstLetterToLowercaseUtil; +import com.magento.idea.magento2plugin.util.magento.GetAclResourcesListUtil; +import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil; +import org.apache.commons.lang.StringUtils; +import org.jetbrains.annotations.NotNull; + +@SuppressWarnings({ + "PMD.TooManyFields", + "PMD.MissingSerialVersionUID", + "PMD.DataClass", + "PMD.UnusedPrivateField", + "PMD.ExcessiveImports", +}) +public class NewEntityDialog extends AbstractDialog { + @NotNull + private final Project project; + private final String moduleName; + private JPanel contentPane; + private JTabbedPane tabbedPane1; + private JPanel propertiesPanel; + private JTable propertyTable; + private JButton addProperty; + private JButton buttonOK; + private JButton buttonCancel; + private JPanel generalTable; + private JCheckBox createUiComponent; + private JLabel createUiComponentsLabel; + private JTextField entityName; + private JLabel entityNameLabel; + private JLabel dbTableNameLabel; + private JTextField dbTableName; + private JTextField entityId; + private JLabel entityIdColumnNameLabel; + private JTextField route; + private JLabel routeLabel; + private JTextField acl; + private JLabel aclLabel; + private JTextField aclTitle; + private FilteredComboBox parentAcl; + private JLabel formNameLabel; + private JTextField formName; + private JTextField formLabel; + private JLabel formLabelLabel; + private JTextField gridName; + private JLabel gridNameLabel; + private JLabel parentMenuItemLabel; + private JLabel sortOrderLabel; + private JLabel menuIdentifierLabel; + private JTextField sortOrder; + private JTextField menuIdentifier; + private JLabel menuTitleLabel; + private JTextField menuTitle; + private FilteredComboBox parentMenu; + private JLabel formMenuLabel; + private JCheckBox addToolBar; + private JCheckBox addBookmarksCheckBox; + private JCheckBox addColumnsControlCheckBox; + private JCheckBox addFullTextSearchCheckBox; + private JCheckBox addListingFiltersCheckBox; + private JCheckBox addListingPagingCheckBox; + private final List properties; + + private static final String ACTION_NAME = "Create Entity"; + private static final String PROPERTY_NAME = "Name"; + private static final String PROPERTY_TYPE = "Type"; + private static final String PROPERTY_ACTION = "Action"; + private static final String PROPERTY_DELETE = "Delete"; + + private JTextField observerName; + + /** + * Constructor. + * + * @param project Project + * @param directory PsiDirectory + */ + public NewEntityDialog(final Project project, final PsiDirectory directory) { + super(); + + this.project = project; + this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project); + this.properties = new ArrayList<>(); + + setContentPane(contentPane); + setModal(true); + setTitle(NewViewModelAction.ACTION_DESCRIPTION); + getRootPane().setDefaultButton(buttonOK); + + buttonOK.addActionListener((final ActionEvent event) -> onOK()); + buttonCancel.addActionListener((final ActionEvent event) -> onCancel()); + + // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(final WindowEvent event) { + onCancel(); + } + }); + + initPropertiesTable(); + + // call onCancel() on ESCAPE + contentPane.registerKeyboardAction( + (final ActionEvent event) -> onCancel(), + KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), + JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT + ); + } + + /** + * Open new controller dialog. + * + * @param project Project + * @param directory PsiDirectory + */ + public static void open(final Project project, final PsiDirectory directory) { + final NewEntityDialog dialog = new NewEntityDialog(project, directory); + dialog.pack(); + dialog.centerDialog(dialog); + dialog.setVisible(true); + } + + private void initPropertiesTable() { + final DefaultTableModel propertiesTable = getPropertiesTable(); + propertiesTable.setDataVector( + new Object[][]{}, + new Object[]{ + PROPERTY_NAME, + PROPERTY_TYPE, + PROPERTY_ACTION + } + ); + + final TableColumn column = propertyTable.getColumn(PROPERTY_ACTION); + column.setCellRenderer(new TableButton(PROPERTY_DELETE)); + column.setCellEditor(new DeleteRowButton(new JCheckBox())); + + addProperty.addActionListener(e -> { + propertiesTable.addRow(new Object[]{ + "", + PropertiesTypes.valueOf(PropertiesTypes.INT.toString()) + .getPropertyType(), + PROPERTY_DELETE + }); + }); + + initPropertyTypeColumn(); + } + + private void initPropertyTypeColumn() { + final TableColumn formElementTypeColumn = propertyTable.getColumn(PROPERTY_TYPE); + formElementTypeColumn.setCellEditor(new ComboBoxEditor(PropertiesTypes.getPropertyTypes())); + formElementTypeColumn.setCellRenderer(new ComboBoxTableRenderer<>(PropertiesTypes.getPropertyTypes())); + } + + private DefaultTableModel getPropertiesTable() { + return (DefaultTableModel) propertyTable.getModel(); + } + + /** + * Perform code generation using input data. + */ + private void onOK() { + generateModelFile(); + generateResourceModelFile(); + generateCollectionFile(); + + formatProperties(); + generateDataModelFile(); + generateDataModelInterfaceFile(); + + generateRoutesXmlFile(); + generateViewControllerFile(); + generateSubmitControllerFile(); + generateDataProviderFile(); + generateLayoutFile(); + generateFormFile(); + generateAclXmlFile(); + + generateGridViewControllerFile(); + generateGridLayoutFile(); + generateMenuFile(); + generateUiComponentGridFile(); + } + + private PsiFile generateModelFile() { + final NamespaceBuilder modelNamespace = getModelNamespace(); + final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace(); + String resourceModelName = getResourceModelName(); + + + return new ModuleModelGenerator(new ModelData( + getModuleName(), + getDbTableName(), + getModelName(), + resourceModelName, + modelNamespace.getClassFqn(), + modelNamespace.getNamespace(), + resourceModelNamespace.getClassFqn() + ), project).generate(ACTION_NAME, true); + } + + private NamespaceBuilder getModelNamespace() { + return new NamespaceBuilder(getModuleName(), getModelName(), ModelPhp.MODEL_DIRECTORY); + } + + private NamespaceBuilder getDataModelNamespace() { + return new NamespaceBuilder(getModuleName(), getDataModelName(), DataModel.DIRECTORY); + } + + private NamespaceBuilder getDataModelInterfaceNamespace() { + return new NamespaceBuilder(getModuleName(), getDataModelInterfaceName(), DataModelInterface.DIRECTORY); + } + + private NamespaceBuilder getResourceModelNamespace() { + return new NamespaceBuilder( + getModuleName(), + getResourceModelName(), + ResourceModelPhp.RESOURCE_MODEL_DIRECTORY + ); + } + + private String getModuleName() { + return moduleName; + } + + private String getEntityName() { + return entityName.getText().trim(); + } + + private String getModelName() { + return getEntityName().concat("Model"); + } + + private String getDataModelName() { + return getEntityName().concat("Data"); + } + + private String getDataProviderClassName() { + return getEntityName().concat("DataProvider"); + } + + private String getDataModelInterfaceName() { + return getEntityName().concat("Interface"); + } + + private String getResourceModelName() { + return getEntityName().concat("Resource"); + } + + private String getCollectionName() { + return getEntityName().concat("Collection"); + } + + private String getDbTableName() { + return dbTableName.getText().trim(); + } + + private PsiFile generateResourceModelFile() { + final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace(); + return new ModuleResourceModelGenerator(new ResourceModelData( + getModuleName(), + getDbTableName(), + getResourceModelName(), + getEntityIdColumn(), + resourceModelNamespace.getNamespace(), + resourceModelNamespace.getClassFqn() + ), project).generate(ACTION_NAME, true); + } + + private String getEntityIdColumn() { + return entityId.getText().trim(); + } + + private PsiFile generateCollectionFile() { + final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace(); + final NamespaceBuilder modelNamespace = getModelNamespace(); + final NamespaceBuilder collectionNamespace = getCollectionNamespace(); + final StringBuilder modelFqn = new StringBuilder(modelNamespace.getClassFqn()); + String modelName = getModelName(); + final StringBuilder resourceModelFqn + = new StringBuilder(resourceModelNamespace.getClassFqn()); + String resourceModelName = getResourceModelName(); + + + return new ModuleCollectionGenerator(new CollectionData( + getModuleName(), + getDbTableName(), + modelName, + getCollectionName(), + collectionNamespace.getClassFqn(), + getCollectionDirectory(), + collectionNamespace.getNamespace(), + resourceModelName, + resourceModelFqn.toString(), + modelFqn.toString() + ), project).generate(ACTION_NAME, true); + } + + private void generateDataModelFile() { + NamespaceBuilder nameSpaceBuilder = getDataModelNamespace(); + new DataModelGenerator(project, new DataModelData( + getDataModelNamespace().getNamespace(), + getDataModelName(), + getModuleName(), + nameSpaceBuilder.getClassFqn(), + getDataModelInterfaceNamespace().getClassFqn(), + getProperties() + )).generate(ACTION_NAME, true); + } + + private void generateDataModelInterfaceFile() { + NamespaceBuilder nameSpaceBuilder = getDataModelInterfaceNamespace(); + new DataModelInterfaceGenerator(project, new DataModelInterfaceData( + nameSpaceBuilder.getNamespace(), + getDataModelInterfaceName(), + getModuleName(), + nameSpaceBuilder.getClassFqn(), + getProperties() + )).generate(ACTION_NAME, true); + } + + /** + * Gets properties as a string, ready for templating. + * "UPPER_SNAKE;lower_snake;type;UpperCamel;lowerCamel". + */ + private String getProperties() { + return StringUtils.join(properties, ","); + } + + /** + * Formats properties into an array of ClassPropertyData objects. + */ + private void formatProperties() { + final DefaultTableModel propertiesTable = getPropertiesTable(); + final int rowCount = propertiesTable.getRowCount(); + String name; + String type; + + name = getEntityIdColumn(); + type = "int"; + properties.add(new ClassPropertyData(// NOPMD + type, + CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name), + CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name), + name, + CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, name) + ).string()); + + for (int index = 0; index < rowCount; index++) { + name = propertiesTable.getValueAt(index, 0).toString(); + type = propertiesTable.getValueAt(index, 1).toString(); + properties.add(new ClassPropertyData(// NOPMD + type, + CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name), + CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, name), + name, + CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_UNDERSCORE, name) + ).string()); + } + } + + private NamespaceBuilder getCollectionNamespace() { + return new NamespaceBuilder( + getModuleName(), + getCollectionName(), + getCollectionDirectory() + ); + } + + private String getCollectionDirectory() { + return ResourceModelPhp.RESOURCE_MODEL_DIRECTORY + File.separator + + getEntityName(); + } + + private PsiFile generateRoutesXmlFile() { + return new RoutesXmlGenerator(new RoutesXmlData( + Areas.adminhtml.toString(), + getRoute(), + getModuleName() + ), project).generate(ACTION_NAME, false); + } + + public String getRoute() { + return route.getText().trim(); + } + + private PsiFile generateViewControllerFile() { + final NamespaceBuilder namespace = new NamespaceBuilder( + getModuleName(), + getViewActionName(), + getViewControllerDirectory() + ); + return new ModuleControllerClassGenerator(new ControllerFileData( + getViewControllerDirectory(), + getViewActionName(), + getModuleName(), + Areas.adminhtml.toString(), + HttpMethod.GET.toString(), + getAcl(), + true, + namespace.getNamespace() + ), project).generate(ACTION_NAME, false); + } + + private String getViewActionName() { + return "Edit"; + } + + private String getSubmitActionName() { + return "Save"; + } + + private String getViewControllerDirectory() { + return getControllerDirectory() + getModelName(); + } + + private String getControllerDirectory() { + return ControllerBackendPhp.DEFAULT_DIR + File.separator; + } + + private PsiFile generateSubmitControllerFile() { + final NamespaceBuilder namespace = new NamespaceBuilder( + getModuleName(), + getSubmitActionName(), + getViewControllerDirectory() + ); + return new ModuleControllerClassGenerator(new ControllerFileData( + getViewControllerDirectory(), + getSubmitActionName(), + getModuleName(), + Areas.adminhtml.toString(), + HttpMethod.POST.toString(), + getAcl(), + true, + namespace.getNamespace() + ), project).generate(ACTION_NAME, false); + } + + public String getAcl() { + return acl.getText().trim(); + } + + private PsiFile generateDataProviderFile() { + final NamespaceBuilder namespace = getDataProviderNamespace(); + return new UiComponentDataProviderGenerator(new UiComponentDataProviderData( + getDataProviderClassName(), + namespace.getNamespace(), + getDataProviderDirectory() + ), getModuleName(), project).generate(ACTION_NAME, false); + } + + @NotNull + private NamespaceBuilder getDataProviderNamespace() { + return new NamespaceBuilder( + getModuleName(), + getDataProviderClassName(), + getDataProviderDirectory() + ); + } + + public String getDataProviderDirectory() { + return "UI/DataProvider"; + } + + private PsiFile generateLayoutFile() { + return new LayoutXmlGenerator(new LayoutXmlData( + Areas.adminhtml.toString(), + getRoute(), + getModuleName(), + getEntityName(), + getViewActionName(), + getFormName() + ), project).generate(ACTION_NAME, false); + } + + /** + * Get controller name. + * + * @return String + */ + public String getFormName() { + return formName.getText().trim(); + } + + private PsiFile generateAclXmlFile() { + return new AclXmlGenerator(new AclXmlData( + getParentAcl(), + getAcl(), + getAclTitle() + ), getModuleName(), project).generate(ACTION_NAME, false); + } + + public String getParentAcl() { + return parentAcl.getSelectedItem().toString().trim(); + } + + public String getAclTitle() { + return aclTitle.getText().trim(); + } + + private PsiFile generateFormFile() { + return new UiComponentFormGenerator(new UiComponentFormFileData( + getFormName(), + Areas.adminhtml.toString(), + getModuleName(), + getFormLabel(), + getButtons(), + getFieldsets(), + getFields(), + getRoute(), + getEntityName(), + getSubmitActionName(), + getDataProviderNamespace().getClassFqn() + ), project).generate(ACTION_NAME, true); + } + + public String getFormLabel() { + return formLabel.getText().trim(); + } + + /** + * Returns form fieldsets. + * + * @return List[UiComponentFormFieldsetData] + */ + public List getFieldsets() { + + final ArrayList fieldsets = + new ArrayList<>(); + final UiComponentFormFieldsetData fieldsetData = new UiComponentFormFieldsetData( + "general", + "General", + "10" + ); + + fieldsets.add( + fieldsetData + ); + + return fieldsets; + } + + /** + * Return form buttons list. + * + * @return List[UiComponentFormButtonData] + */ + protected List getButtons() { + final List buttons = new ArrayList(); + final String directory = "Block/Form"; + + NamespaceBuilder namespaceBuilderSave = new NamespaceBuilder( + getModuleName(), + "Save", + directory + ); + buttons.add(new UiComponentFormButtonData( + directory, + "SaveEntity", + getModuleName(), + "Save", + namespaceBuilderSave.getNamespace(), + "Save Entity", + "10", + getFormName(), + namespaceBuilderSave.getClassFqn() + )); + + NamespaceBuilder namespaceBuilderBack = new NamespaceBuilder( + getModuleName(), + "Back", + directory + ); + buttons.add(new UiComponentFormButtonData( + directory, + "Back", + getModuleName(), + "Back", + namespaceBuilderBack.getNamespace(), + "Back To Grid", + "20", + getFormName(), + namespaceBuilderBack.getClassFqn() + )); + + NamespaceBuilder namespaceBuilderDelete = new NamespaceBuilder( + getModuleName(), + "Delete", + directory + ); + buttons.add(new UiComponentFormButtonData( + directory, + "Delete", + getModuleName(), + "Save", + namespaceBuilderDelete.getNamespace(), + "Delete Entity", + "30", + getFormName(), + namespaceBuilderDelete.getClassFqn() + )); + return buttons; + } + + /** + * Returns form fields list. + * + * @return List[UiComponentFormFieldData] + */ + public List getFields() { + final DefaultTableModel model = getPropertiesTable(); + final ArrayList fieldsets = new ArrayList<>(); + + fieldsets.add( + new UiComponentFormFieldData( + "entity_id", + "int", + "Entity ID", + "0", + "general", + "hidden", + "entity_id" + ) + ); + + for (int count = 0; count < model.getRowCount(); count++) { + + final String name = model.getValueAt(count, 0).toString(); + final String dataType = model.getValueAt(count, 1).toString(); + + final String label = model.getValueAt(count, 0).toString(); //todo: convert + final String sortOrder = String.valueOf(count).concat("0"); + final String fieldset = "general"; + final String formElementType = model.getValueAt(count, 1).toString(); + final String source = model.getValueAt(count, 0).toString(); //todo: convert + + final UiComponentFormFieldData fieldsetData = new UiComponentFormFieldData(//NOPMD + name, + label, + sortOrder, + fieldset, + formElementType, + dataType, + source + ); + + fieldsets.add( + fieldsetData + ); + } + + return fieldsets; + } + + @SuppressWarnings({"PMD.UnusedPrivateMethod"}) + private void createUIComponents() { + this.parentAcl = new FilteredComboBox(getAclResourcesList()); + + if (getAclResourcesList().contains(ModuleMenuXml.defaultAcl)) { + parentAcl.setSelectedItem(ModuleMenuXml.defaultAcl); + } + } + + private List getAclResourcesList() { + return GetAclResourcesListUtil.execute(project); + } + + private PsiFile generateGridViewControllerFile() { + final NamespaceBuilder namespace = new NamespaceBuilder( + getModuleName(), + "Listing", + getControllerDirectory() + ); + return new ModuleControllerClassGenerator(new ControllerFileData( + getControllerDirectory(), + "Listing", + getModuleName(), + Areas.adminhtml.toString(), + HttpMethod.GET.toString(), + getAcl(), + true, + namespace.getNamespace() + ), project).generate(ACTION_NAME, false); + } + + private PsiFile generateGridLayoutFile() { + return new LayoutXmlGenerator(new LayoutXmlData( + Areas.adminhtml.toString(), + getRoute(), + getModuleName(), + getEntityName(), + "Listing", + getGridName() + ), project).generate(ACTION_NAME, false); + } + + private String getGridName() { + return gridName.getText().toString(); + } + + private PsiFile generateMenuFile() { + return new MenuXmlGenerator(new MenuXmlData( + getParentMenuItem(), + getSortOrder(), + getModuleName(), + getMenuIdentifier(), + getMenuTitle(), + getAcl(), + getMenuAction() + ), project).generate(ACTION_NAME, false); + } + + private String getParentMenuItem() { + return parentMenu.getSelectedItem().toString(); + } + + public String getSortOrder() { + return sortOrder.getText().trim(); + } + + public String getMenuIdentifier() { + return menuIdentifier.getText().trim(); + } + + private String getMenuAction() { + return getRoute() + + File.separator + + FirstLetterToLowercaseUtil.convert(getEntityName()) + + File.separator + + "listing"; + } + + public String getMenuTitle() { + return menuTitle.getText().trim(); + } + + private void generateUiComponentGridFile() { + final UiComponentGridXmlGenerator gridXmlGenerator = new UiComponentGridXmlGenerator( + getUiComponentGridData(), + project + ); + gridXmlGenerator.generate(NewUiComponentGridAction.ACTION_NAME, true); + } + + /** + * Get grid UI component data. + * + * @return UiComponentGridData + */ + public UiComponentGridData getUiComponentGridData() { + return new UiComponentGridData( + getModuleName(), + Areas.adminhtml.toString(), + getGridName(), + getDataProviderNamespace().getClassFqn(), + getEntityIdColumn(), + getAcl(), + getUiComponentGridToolbarData() + ); + } + + /** + * Get grid toolbar data. + * + * @return UiComponentGridToolbarData + */ + public UiComponentGridToolbarData getUiComponentGridToolbarData() { + return new UiComponentGridToolbarData( + getAddToolBar(), + getAddBookmarksCheckBox(), + getAddColumnsControlCheckBox(), + getAddFullTextSearchCheckBox(), + getAddListingFiltersCheckBox(), + getAddListingPagingCheckBox() + ); + } + + private Boolean getAddToolBar() { + return addToolBar.isSelected(); + } + + private Boolean getAddColumnsControlCheckBox() { + return addColumnsControlCheckBox.isSelected(); + } + + private Boolean getAddFullTextSearchCheckBox() { + return addFullTextSearchCheckBox.isSelected(); + } + + private Boolean getAddListingFiltersCheckBox() { + return addListingFiltersCheckBox.isSelected(); + } + + private Boolean getAddListingPagingCheckBox() { + return addListingPagingCheckBox.isSelected(); + } + + private Boolean getAddBookmarksCheckBox() { + return addBookmarksCheckBox.isSelected(); + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/packages/PropertiesTypes.java b/src/com/magento/idea/magento2plugin/magento/packages/PropertiesTypes.java new file mode 100644 index 000000000..197566518 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/packages/PropertiesTypes.java @@ -0,0 +1,32 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.packages; + +public enum PropertiesTypes { + INT("int"), + FLOAT("float"), + STRING("string"), + BOOL("bool"); + + private final String propertyType; + + PropertiesTypes(final String propertyType) { + this.propertyType = propertyType; + } + + public String getPropertyType() { + return propertyType; + } + + public static String[] getPropertyTypes() { + return new String[]{ + valueOf(INT.toString()).getPropertyType(), + valueOf(FLOAT.toString()).getPropertyType(), + valueOf(STRING.toString()).getPropertyType(), + valueOf(BOOL.toString()).getPropertyType() + }; + } +} From c473393d4f42f72e178cc747aaac75e64b741132 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Tue, 19 Jan 2021 16:34:23 +0200 Subject: [PATCH 3/4] New Entity DB --- .../generation/dialog/NewEntityDialog.form | 36 ++++++++++- .../generation/dialog/NewEntityDialog.java | 59 ++++++++++++++++++- 2 files changed, 91 insertions(+), 4 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.form index bdb3a51df..fe2ed32b8 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.form +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.form @@ -21,7 +21,7 @@ - + @@ -31,7 +31,7 @@ - + @@ -47,7 +47,7 @@ - + @@ -95,6 +95,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java index afc3d4885..6a2960c09 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java @@ -10,6 +10,7 @@ import com.intellij.openapi.ui.ComboBoxTableRenderer; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.generation.NewDbSchemaAction; import com.magento.idea.magento2plugin.actions.generation.NewUiComponentGridAction; import com.magento.idea.magento2plugin.actions.generation.NewViewModelAction; import java.awt.event.ActionEvent; @@ -18,6 +19,7 @@ import java.awt.event.WindowEvent; import java.util.ArrayList; import java.util.List; +import java.util.Map; import javax.swing.*; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; @@ -27,6 +29,7 @@ import com.magento.idea.magento2plugin.actions.generation.data.ControllerFileData; import com.magento.idea.magento2plugin.actions.generation.data.DataModelData; import com.magento.idea.magento2plugin.actions.generation.data.DataModelInterfaceData; +import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; import com.magento.idea.magento2plugin.actions.generation.data.LayoutXmlData; import com.magento.idea.magento2plugin.actions.generation.data.MenuXmlData; import com.magento.idea.magento2plugin.actions.generation.data.ModelData; @@ -43,6 +46,7 @@ import com.magento.idea.magento2plugin.actions.generation.generator.AclXmlGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.DataModelGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.DataModelInterfaceGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.LayoutXmlGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.MenuXmlGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.ModuleCollectionGenerator; @@ -68,6 +72,7 @@ import com.magento.idea.magento2plugin.ui.table.ComboBoxEditor; import com.magento.idea.magento2plugin.ui.table.DeleteRowButton; import com.magento.idea.magento2plugin.ui.table.TableButton; +import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper; import com.magento.idea.magento2plugin.util.FirstLetterToLowercaseUtil; import com.magento.idea.magento2plugin.util.magento.GetAclResourcesListUtil; import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil; @@ -128,7 +133,13 @@ public class NewEntityDialog extends AbstractDialog { private JCheckBox addFullTextSearchCheckBox; private JCheckBox addListingFiltersCheckBox; private JCheckBox addListingPagingCheckBox; + private JComboBox tableEngine; + private JLabel tableEngineLabel; + private JComboBox tableResource; + private JLabel tableResourceLabel; private final List properties; + // Table Columns UI components group + private TableGroupWrapper columnsTableGroupWrapper; private static final String ACTION_NAME = "Create Entity"; private static final String PROPERTY_NAME = "Name"; @@ -252,6 +263,8 @@ private void onOK() { generateGridLayoutFile(); generateMenuFile(); generateUiComponentGridFile(); + + generateDbSchemaXmlFile(); } private PsiFile generateModelFile() { @@ -811,7 +824,7 @@ private void generateUiComponentGridFile() { getUiComponentGridData(), project ); - gridXmlGenerator.generate(NewUiComponentGridAction.ACTION_NAME, true); + gridXmlGenerator.generate(ACTION_NAME, true); } /** @@ -870,4 +883,48 @@ private Boolean getAddListingPagingCheckBox() { private Boolean getAddBookmarksCheckBox() { return addBookmarksCheckBox.isSelected(); } + + /** + * Run db_schema.xml file generator. + */ + private void generateDbSchemaXmlFile() { + new DbSchemaXmlGenerator( + new DbSchemaXmlData( + getDbTableName(), + getTableResource(), + getTableEngine(), + getEntityName(), + getColumns() + ), + project, + moduleName + ).generate(ACTION_NAME, false); + } + + /** + * 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 columnsTable values. + * + * @return List + */ + private List> getColumns() { + return columnsTableGroupWrapper.getColumnsData(); + } } From e4b19ea19b07930012bd8b247c5dfe7ef06b8632 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Wed, 20 Jan 2021 17:53:15 +0200 Subject: [PATCH 4/4] Fixed null pointer --- .../generation/dialog/NewEntityDialog.java | 14 ++ .../generation/generator/QueryGenerator.java | 155 ++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/QueryGenerator.java diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java index 6a2960c09..f566b05b6 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java @@ -10,6 +10,7 @@ import com.intellij.openapi.ui.ComboBoxTableRenderer; import com.intellij.psi.PsiDirectory; import com.intellij.psi.PsiFile; +import com.intellij.util.indexing.FileBasedIndex; import com.magento.idea.magento2plugin.actions.generation.NewDbSchemaAction; import com.magento.idea.magento2plugin.actions.generation.NewUiComponentGridAction; import com.magento.idea.magento2plugin.actions.generation.NewViewModelAction; @@ -18,6 +19,8 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; import javax.swing.*; @@ -68,6 +71,7 @@ import com.magento.idea.magento2plugin.magento.packages.File; import com.magento.idea.magento2plugin.magento.packages.HttpMethod; import com.magento.idea.magento2plugin.magento.packages.PropertiesTypes; +import com.magento.idea.magento2plugin.stubs.indexes.xml.MenuIndex; import com.magento.idea.magento2plugin.ui.FilteredComboBox; import com.magento.idea.magento2plugin.ui.table.ComboBoxEditor; import com.magento.idea.magento2plugin.ui.table.DeleteRowButton; @@ -740,6 +744,7 @@ public List getFields() { @SuppressWarnings({"PMD.UnusedPrivateMethod"}) private void createUIComponents() { this.parentAcl = new FilteredComboBox(getAclResourcesList()); + this.parentMenu = new FilteredComboBox(getMenuReferences()); if (getAclResourcesList().contains(ModuleMenuXml.defaultAcl)) { parentAcl.setSelectedItem(ModuleMenuXml.defaultAcl); @@ -927,4 +932,13 @@ private String getTableEngine() { private List> getColumns() { return columnsTableGroupWrapper.getColumnsData(); } + + @NotNull + private List getMenuReferences() { + final Collection menuReferences + = FileBasedIndex.getInstance().getAllKeys(MenuIndex.KEY, project); + final ArrayList menuReferencesList = new ArrayList<>(menuReferences); + Collections.sort(menuReferencesList); + return menuReferencesList; + } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/QueryGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/QueryGenerator.java new file mode 100644 index 000000000..93ac918dc --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/QueryGenerator.java @@ -0,0 +1,155 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.jetbrains.php.lang.psi.PhpFile; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.magento.idea.magento2plugin.actions.generation.data.UiComponentDataProviderData; +import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; +import com.magento.idea.magento2plugin.bundles.CommonBundle; +import com.magento.idea.magento2plugin.bundles.ValidatorBundle; +import com.magento.idea.magento2plugin.indexes.ModuleIndex; +import com.magento.idea.magento2plugin.magento.files.UiComponentDataProviderPhp; +import com.magento.idea.magento2plugin.magento.packages.File; +import com.magento.idea.magento2plugin.magento.packages.Package; +import com.magento.idea.magento2plugin.util.GetFirstClassOfFile; +import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; + +import javax.swing.*; +import java.util.Properties; + +public class QueryGenerator extends FileGenerator { + private final UiComponentDataProviderData uiComponentGridDataProviderData; + private final Project project; + private final DirectoryGenerator directoryGenerator; + private final FileFromTemplateGenerator fileFromTemplateGenerator; + private final ValidatorBundle validatorBundle; + private final CommonBundle commonBundle; + private final String moduleName; + private final GetFirstClassOfFile getFirstClassOfFile; + + /** + * Ui component grid data provider constructor. + * + * @param uiComponentGridDataProviderData UiComponentGridDataProviderData + * @param moduleName String + * @param project Project + */ + public QueryGenerator( + final UiComponentDataProviderData uiComponentGridDataProviderData, + final String moduleName, + final Project project + ) { + super(project); + + this.uiComponentGridDataProviderData = uiComponentGridDataProviderData; + this.directoryGenerator = DirectoryGenerator.getInstance(); + this.fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project); + this.validatorBundle = new ValidatorBundle(); + this.commonBundle = new CommonBundle(); + this.getFirstClassOfFile = GetFirstClassOfFile.getInstance(); + this.project = project; + this.moduleName = moduleName; + } + + @Override + public PsiFile generate(final String actionName) { + final PsiFile[] dataProviderFiles = new PsiFile[1]; + + WriteCommandAction.runWriteCommandAction(project, () -> { + PhpClass dataProvider = GetPhpClassByFQN.getInstance(project).execute( + getDataProviderFqn() + ); + + if (dataProvider != null) { + final String errorMessage = this.validatorBundle.message( + "validator.file.alreadyExists", + "DataProvider Class" + ); + JOptionPane.showMessageDialog( + null, + errorMessage, + commonBundle.message("common.error"), + JOptionPane.ERROR_MESSAGE + ); + + return; + } + + dataProvider = createDataProviderClass(actionName); + + if (dataProvider == null) { + final String errorMessage = this.validatorBundle.message( + "validator.file.cantBeCreated", + "DataProvider Class" + ); + JOptionPane.showMessageDialog( + null, + errorMessage, + commonBundle.message("common.error"), + JOptionPane.ERROR_MESSAGE + ); + + return; + } + + dataProviderFiles[0] = dataProvider.getContainingFile(); + }); + + return dataProviderFiles[0]; + } + + @Override + protected void fillAttributes(final Properties attributes) { + attributes.setProperty("NAMESPACE", uiComponentGridDataProviderData.getNamespace()); + attributes.setProperty("CLASS_NAME", uiComponentGridDataProviderData.getName()); + } + + private PhpClass createDataProviderClass(final String actionName) { + PsiDirectory parentDirectory = ModuleIndex.getInstance(project) + .getModuleDirectoryByModuleName(this.moduleName); + final PsiFile dataProviderFile; + final String[] dataProviderDirectories = uiComponentGridDataProviderData.getPath().split( + File.separator + ); + for (final String dataProviderDirectory: dataProviderDirectories) { + parentDirectory = directoryGenerator.findOrCreateSubdirectory( + parentDirectory, dataProviderDirectory + ); + } + + final Properties attributes = getAttributes(); + + dataProviderFile = fileFromTemplateGenerator.generate( + UiComponentDataProviderPhp.getInstance( + uiComponentGridDataProviderData.getName() + ), + attributes, + parentDirectory, + actionName + ); + + if (dataProviderFile == null) { + return null; + } + + return getFirstClassOfFile.execute((PhpFile) dataProviderFile); + } + + private String getDataProviderFqn() { + return String.format( + "%s%s%s", + uiComponentGridDataProviderData.getNamespace(), + Package.fqnSeparator, + uiComponentGridDataProviderData.getName() + ); + } +}