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 e69de29bb..000000000 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 d8f9d2d61..f2dda4aa8 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java @@ -18,7 +18,7 @@ import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.TableNameLength; import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaWhitelistJsonGenerator; 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.ColumnAttributes; 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; @@ -192,36 +192,36 @@ private void generateWhitelistJsonFile(final @NotNull DbSchemaXmlData dbSchemaXm */ private void initializeColumnsUiComponentGroup() { final List columns = new LinkedList<>(Arrays.asList( - 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 + ColumnAttributes.TYPE.getName(), + ColumnAttributes.NAME.getName(), + ColumnAttributes.PADDING.getName(), + ColumnAttributes.UNSIGNED.getName(), + ColumnAttributes.NULLABLE.getName(), + ColumnAttributes.IDENTITY.getName(), + ColumnAttributes.LENGTH.getName(), + ColumnAttributes.PRECISION.getName(), + ColumnAttributes.SCALE.getName(), + ColumnAttributes.ON_UPDATE.getName(), + ColumnAttributes.DEFAULT.getName(), + ColumnAttributes.COMMENT.getName() )); // Set default values for columns final Map defaultValues = new HashMap<>(); - defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, "false"); - defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, "false"); + defaultValues.put(ColumnAttributes.NULLABLE.getName(), "false"); + defaultValues.put(ColumnAttributes.IDENTITY.getName(), "false"); // Set sources for columns 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, + ColumnAttributes.TYPE.getName(), columnTypes ); - 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); + sources.put(ColumnAttributes.UNSIGNED.getName(), booleanSource); + sources.put(ColumnAttributes.NULLABLE.getName(), booleanSource); + sources.put(ColumnAttributes.IDENTITY.getName(), booleanSource); + sources.put(ColumnAttributes.ON_UPDATE.getName(), booleanSource); // Initialize new Table Group columnsTableGroupWrapper = new TableGroupWrapper( columnsTable, 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 f566b05b6..883e1d9b6 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java @@ -7,26 +7,10 @@ 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.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; -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.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Map; -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; @@ -46,9 +30,11 @@ 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.data.ui.ComboBoxItemData; 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.DbSchemaWhitelistJsonGenerator; 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; @@ -60,6 +46,7 @@ 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.DbSchemaGeneratorUtil; 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; @@ -71,15 +58,37 @@ 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.magento.packages.database.TableEngines; +import com.magento.idea.magento2plugin.magento.packages.database.TableResources; 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; -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; +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.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +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.JLabel; +import javax.swing.JPanel; +import javax.swing.JTabbedPane; +import javax.swing.JTable; +import javax.swing.JTextField; +import javax.swing.KeyStroke; +import javax.swing.table.DefaultTableModel; import org.apache.commons.lang.StringUtils; import org.jetbrains.annotations.NotNull; @@ -89,6 +98,9 @@ "PMD.DataClass", "PMD.UnusedPrivateField", "PMD.ExcessiveImports", + "PMD.GodClass", + "PMD.TooManyMethods", + "PMD.CyclomaticComplexity" }) public class NewEntityDialog extends AbstractDialog { @NotNull @@ -142,14 +154,11 @@ public class NewEntityDialog extends AbstractDialog { private JComboBox tableResource; private JLabel tableResourceLabel; private final List properties; - // Table Columns UI components group - private TableGroupWrapper columnsTableGroupWrapper; + private TableGroupWrapper entityPropertiesTableGroupWrapper; 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; @@ -183,13 +192,14 @@ public void windowClosing(final WindowEvent event) { } }); + initializeComboboxSources(); initPropertiesTable(); // call onCancel() on ESCAPE contentPane.registerKeyboardAction( - (final ActionEvent event) -> onCancel(), - KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), - JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT + (final ActionEvent event) -> onCancel(), + KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), + JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); } @@ -206,37 +216,39 @@ public static void open(final Project project, final PsiDirectory directory) { dialog.setVisible(true); } + /** + * Initialize combobox sources. + */ + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") + private void initializeComboboxSources() { + for (final String engine : TableEngines.getTableEnginesList()) { + tableEngine.addItem(new ComboBoxItemData(engine, engine)); + } + for (final String resource : TableResources.getTableResourcesList()) { + tableResource.addItem(new ComboBoxItemData(resource, resource)); + } + } + + /** + * Initialize properties table. + */ private void initPropertiesTable() { - final DefaultTableModel propertiesTable = getPropertiesTable(); - propertiesTable.setDataVector( - new Object[][]{}, - new Object[]{ + final List columns = new LinkedList<>(Arrays.asList( PROPERTY_NAME, - PROPERTY_TYPE, - PROPERTY_ACTION - } + PROPERTY_TYPE + )); + final Map> sources = new HashMap<>(); + sources.put(PROPERTY_TYPE, PropertiesTypes.getPropertyTypesList()); + + // Initialize entity properties Table Group + entityPropertiesTableGroupWrapper = new TableGroupWrapper( + propertyTable, + addProperty, + columns, + new HashMap<>(), + sources ); - - 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())); + entityPropertiesTableGroupWrapper.initTableGroup(); } private DefaultTableModel getPropertiesTable() { @@ -268,13 +280,22 @@ private void onOK() { generateMenuFile(); generateUiComponentGridFile(); - generateDbSchemaXmlFile(); + final DbSchemaXmlData dbSchemaXmlData = new DbSchemaXmlData( + getDbTableName(), + getTableResource(), + getTableEngine(), + getEntityName(), + getEntityProperties() + ); + + generateDbSchemaXmlFile(dbSchemaXmlData); + generateWhitelistJsonFile(dbSchemaXmlData); } private PsiFile generateModelFile() { final NamespaceBuilder modelNamespace = getModelNamespace(); final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace(); - String resourceModelName = getResourceModelName(); + final String resourceModelName = getResourceModelName(); return new ModuleModelGenerator(new ModelData( @@ -297,7 +318,11 @@ private NamespaceBuilder getDataModelNamespace() { } private NamespaceBuilder getDataModelInterfaceNamespace() { - return new NamespaceBuilder(getModuleName(), getDataModelInterfaceName(), DataModelInterface.DIRECTORY); + return new NamespaceBuilder( + getModuleName(), + getDataModelInterfaceName(), + DataModelInterface.DIRECTORY + ); } private NamespaceBuilder getResourceModelNamespace() { @@ -365,10 +390,10 @@ private PsiFile generateCollectionFile() { final NamespaceBuilder modelNamespace = getModelNamespace(); final NamespaceBuilder collectionNamespace = getCollectionNamespace(); final StringBuilder modelFqn = new StringBuilder(modelNamespace.getClassFqn()); - String modelName = getModelName(); + final String modelName = getModelName(); final StringBuilder resourceModelFqn = new StringBuilder(resourceModelNamespace.getClassFqn()); - String resourceModelName = getResourceModelName(); + final String resourceModelName = getResourceModelName(); return new ModuleCollectionGenerator(new CollectionData( @@ -386,7 +411,7 @@ private PsiFile generateCollectionFile() { } private void generateDataModelFile() { - NamespaceBuilder nameSpaceBuilder = getDataModelNamespace(); + final NamespaceBuilder nameSpaceBuilder = getDataModelNamespace(); new DataModelGenerator(project, new DataModelData( getDataModelNamespace().getNamespace(), getDataModelName(), @@ -398,7 +423,7 @@ private void generateDataModelFile() { } private void generateDataModelInterfaceFile() { - NamespaceBuilder nameSpaceBuilder = getDataModelInterfaceNamespace(); + final NamespaceBuilder nameSpaceBuilder = getDataModelInterfaceNamespace(); new DataModelInterfaceGenerator(project, new DataModelInterfaceData( nameSpaceBuilder.getNamespace(), getDataModelInterfaceName(), @@ -428,22 +453,22 @@ private void formatProperties() { 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) + 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) + 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()); } } @@ -475,19 +500,19 @@ public String getRoute() { private PsiFile generateViewControllerFile() { final NamespaceBuilder namespace = new NamespaceBuilder( - getModuleName(), - getViewActionName(), - getViewControllerDirectory() + getModuleName(), + getViewActionName(), + getViewControllerDirectory() ); return new ModuleControllerClassGenerator(new ControllerFileData( - getViewControllerDirectory(), - getViewActionName(), - getModuleName(), - Areas.adminhtml.toString(), - HttpMethod.GET.toString(), - getAcl(), - true, - namespace.getNamespace() + getViewControllerDirectory(), + getViewActionName(), + getModuleName(), + Areas.adminhtml.toString(), + HttpMethod.GET.toString(), + getAcl(), + true, + namespace.getNamespace() ), project).generate(ACTION_NAME, false); } @@ -496,7 +521,7 @@ private String getViewActionName() { } private String getSubmitActionName() { - return "Save"; + return "Save";//NOPMD } private String getViewControllerDirectory() { @@ -509,19 +534,19 @@ private String getControllerDirectory() { private PsiFile generateSubmitControllerFile() { final NamespaceBuilder namespace = new NamespaceBuilder( - getModuleName(), - getSubmitActionName(), - getViewControllerDirectory() + getModuleName(), + getSubmitActionName(), + getViewControllerDirectory() ); return new ModuleControllerClassGenerator(new ControllerFileData( - getViewControllerDirectory(), - getSubmitActionName(), - getModuleName(), - Areas.adminhtml.toString(), - HttpMethod.POST.toString(), - getAcl(), - true, - namespace.getNamespace() + getViewControllerDirectory(), + getSubmitActionName(), + getModuleName(), + Areas.adminhtml.toString(), + HttpMethod.POST.toString(), + getAcl(), + true, + namespace.getNamespace() ), project).generate(ACTION_NAME, false); } @@ -638,41 +663,41 @@ protected List getButtons() { final List buttons = new ArrayList(); final String directory = "Block/Form"; - NamespaceBuilder namespaceBuilderSave = new NamespaceBuilder( - getModuleName(), - "Save", - directory + final NamespaceBuilder namespaceBuilderSave = new NamespaceBuilder( + getModuleName(), + "Save", + directory ); buttons.add(new UiComponentFormButtonData( - directory, - "SaveEntity", - getModuleName(), - "Save", - namespaceBuilderSave.getNamespace(), - "Save Entity", - "10", - getFormName(), - namespaceBuilderSave.getClassFqn() + directory, + "SaveEntity", + getModuleName(), + "Save", + namespaceBuilderSave.getNamespace(), + "Save Entity", + "10", + getFormName(), + namespaceBuilderSave.getClassFqn() )); - NamespaceBuilder namespaceBuilderBack = new NamespaceBuilder( - getModuleName(), - "Back", - directory + final NamespaceBuilder namespaceBuilderBack = new NamespaceBuilder( + getModuleName(), + "Back", + directory ); buttons.add(new UiComponentFormButtonData( - directory, - "Back", - getModuleName(), - "Back", - namespaceBuilderBack.getNamespace(), - "Back To Grid", - "20", - getFormName(), - namespaceBuilderBack.getClassFqn() + directory, + "Back", + getModuleName(), + "Back", + namespaceBuilderBack.getNamespace(), + "Back To Grid", + "20", + getFormName(), + namespaceBuilderBack.getClassFqn() )); - NamespaceBuilder namespaceBuilderDelete = new NamespaceBuilder( + final NamespaceBuilder namespaceBuilderDelete = new NamespaceBuilder( getModuleName(), "Delete", directory @@ -701,15 +726,15 @@ public List getFields() { final ArrayList fieldsets = new ArrayList<>(); fieldsets.add( - new UiComponentFormFieldData( - "entity_id", - "int", - "Entity ID", - "0", - "general", - "hidden", - "entity_id" - ) + new UiComponentFormFieldData( + "entity_id", + "int", + "Entity ID", + "0", + "general", + "hidden", + "entity_id" + ) ); for (int count = 0; count < model.getRowCount(); count++) { @@ -724,17 +749,17 @@ public List getFields() { final String source = model.getValueAt(count, 0).toString(); //todo: convert final UiComponentFormFieldData fieldsetData = new UiComponentFormFieldData(//NOPMD - name, - label, - sortOrder, - fieldset, - formElementType, - dataType, - source + name, + label, + sortOrder, + fieldset, + formElementType, + dataType, + source ); fieldsets.add( - fieldsetData + fieldsetData ); } @@ -757,30 +782,30 @@ private List getAclResourcesList() { private PsiFile generateGridViewControllerFile() { final NamespaceBuilder namespace = new NamespaceBuilder( - getModuleName(), - "Listing", - getControllerDirectory() + getModuleName(), + "Listing", + getControllerDirectory() ); return new ModuleControllerClassGenerator(new ControllerFileData( - getControllerDirectory(), - "Listing", - getModuleName(), - Areas.adminhtml.toString(), - HttpMethod.GET.toString(), - getAcl(), - true, - namespace.getNamespace() + 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() + Areas.adminhtml.toString(), + getRoute(), + getModuleName(), + getEntityName(), + "Listing", + getGridName() ), project).generate(ACTION_NAME, false); } @@ -826,8 +851,8 @@ public String getMenuTitle() { private void generateUiComponentGridFile() { final UiComponentGridXmlGenerator gridXmlGenerator = new UiComponentGridXmlGenerator( - getUiComponentGridData(), - project + getUiComponentGridData(), + project ); gridXmlGenerator.generate(ACTION_NAME, true); } @@ -891,18 +916,27 @@ private Boolean getAddBookmarksCheckBox() { /** * Run db_schema.xml file generator. + * + * @param dbSchemaXmlData DbSchemaXmlData */ - private void generateDbSchemaXmlFile() { + private void generateDbSchemaXmlFile(final @NotNull DbSchemaXmlData dbSchemaXmlData) { new DbSchemaXmlGenerator( - new DbSchemaXmlData( - getDbTableName(), - getTableResource(), - getTableEngine(), - getEntityName(), - getColumns() - ), - project, - moduleName + dbSchemaXmlData, + project, + moduleName + ).generate(ACTION_NAME, false); + } + + /** + * Run db_schema_whitelist.json generator. + * + * @param dbSchemaXmlData DbSchemaXmlData + */ + private void generateWhitelistJsonFile(final @NotNull DbSchemaXmlData dbSchemaXmlData) { + new DbSchemaWhitelistJsonGenerator( + project, + dbSchemaXmlData, + moduleName ).generate(ACTION_NAME, false); } @@ -925,20 +959,28 @@ private String getTableEngine() { } /** - * Get columnsTable values. + * Get entity properties table columns data and format to suitable for generator. * - * @return List + * @return List of entity properties stored in HashMap. */ - private List> getColumns() { - return columnsTableGroupWrapper.getColumnsData(); + private List> getEntityProperties() { + final List> shortColumnsData = + entityPropertiesTableGroupWrapper.getColumnsData(); + + final List> columnsData = + DbSchemaGeneratorUtil.complementShortPropertiesByDefaults(shortColumnsData); + columnsData.add(0, DbSchemaGeneratorUtil.getTableIdentityColumnData(getEntityIdColumn())); + + return columnsData; } @NotNull private List getMenuReferences() { final Collection menuReferences - = FileBasedIndex.getInstance().getAllKeys(MenuIndex.KEY, project); + = 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/DbSchemaWhitelistJsonGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaWhitelistJsonGenerator.java index 331a4e770..8268d2ab8 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaWhitelistJsonGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaWhitelistJsonGenerator.java @@ -21,6 +21,7 @@ import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateDbSchemaWhitelistJson; import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaWhitelistJson; import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import com.magento.idea.magento2plugin.magento.packages.database.ColumnAttributes; import java.util.Arrays; import java.util.Map; import java.util.Properties; @@ -84,10 +85,10 @@ public PsiFile generate(final String actionName) { final JsonObject indexObject = jsonElementGenerator.createObject(""); for (final Map columnData : dbSchemaXmlData.getColumns()) { - final String columnName = columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME); + final String columnName = columnData.get(ColumnAttributes.NAME.getName()); final boolean isIdentity = Boolean.parseBoolean( - columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY) + columnData.get(ColumnAttributes.IDENTITY.getName()) ); if (isIdentity) { 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 5be58ad68..f394ebde5 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java @@ -16,6 +16,7 @@ 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.ColumnAttributes; import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes; import java.util.HashMap; import java.util.InputMismatchException; @@ -91,14 +92,14 @@ public PsiFile generate(final String actionName) { for (final Map columnData : dbSchemaXmlData.getColumns()) { final String identityAttrValue = - columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY); + columnData.get(ColumnAttributes.IDENTITY.getName()); if (!hasPrimaryKey && Boolean.parseBoolean(identityAttrValue)) { hasPrimaryKey = true; primaryKeyData.putAll(columnData); } - final String columnTypeValue = columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE); + final String columnTypeValue = columnData.get(ColumnAttributes.TYPE.getName()); final TableColumnTypes columnType = TableColumnTypes.getByValue(columnTypeValue); if (columnType == null) { @@ -117,11 +118,11 @@ public PsiFile generate(final String actionName) { } } final String columnIdentityValue = - columnData.get(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME); + columnData.get(ColumnAttributes.NAME.getName()); findOrCreateTag( ModuleDbSchemaXml.XML_TAG_COLUMN, - ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, + ColumnAttributes.NAME.getName(), tableTag, columnIdentityValue, attributes @@ -147,7 +148,7 @@ private void generatePrimaryKey( ) { final Map attributes = new LinkedHashMap<>(); attributes.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), ModuleDbSchemaXml.XML_ATTR_TYPE_PK ); attributes.put( @@ -164,13 +165,13 @@ private void generatePrimaryKey( ); final Map pkColumnAttributes = new HashMap<>(); final String columnIdentityValue = primaryKeyData.get( - ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME + ColumnAttributes.NAME.getName() ); - pkColumnAttributes.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, columnIdentityValue); + pkColumnAttributes.put(ColumnAttributes.NAME.getName(), columnIdentityValue); findOrCreateTag( ModuleDbSchemaXml.XML_TAG_COLUMN, - ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, + ColumnAttributes.NAME.getName(), pkTag, columnIdentityValue, pkColumnAttributes @@ -202,7 +203,7 @@ private void generatePrimaryKey( findOrCreateTag( ModuleDbSchemaXml.XML_TAG_COLUMN, - ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, + ColumnAttributes.NAME.getName(), pkIndexTag, columnIdentityValue, pkColumnAttributes diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/DbSchemaGeneratorUtil.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/DbSchemaGeneratorUtil.java new file mode 100644 index 000000000..d84f10318 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/DbSchemaGeneratorUtil.java @@ -0,0 +1,111 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator.util; + +import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import com.magento.idea.magento2plugin.magento.packages.PropertiesTypes; +import com.magento.idea.magento2plugin.magento.packages.database.ColumnAttributes; +import com.magento.idea.magento2plugin.magento.packages.database.PropertyToDefaultTypeMapperUtil; +import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import org.jetbrains.annotations.NotNull; + +public final class DbSchemaGeneratorUtil { + private static final String PROPERTY_NAME = "Name"; + private static final String PROPERTY_TYPE = "Type"; + + private DbSchemaGeneratorUtil() {} + + /** + * Complement short entity properties to expected in the db_schema.xml generator. + * + * @param shortProperties List + * + * @return List of expected in the DbSchemaXmlGenerator properties. + */ + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") + public static List> complementShortPropertiesByDefaults( + final List> shortProperties + ) { + final List> complemented = new LinkedList<>(); + + for (final Map property : shortProperties) { + final String name = property.get(PROPERTY_NAME); + final String type = property.get(PROPERTY_TYPE); + + final PropertiesTypes propType = PropertiesTypes.getByValue(type); + final TableColumnTypes tableColumnType = PropertyToDefaultTypeMapperUtil.map(propType); + + final List allowedAttributes = ModuleDbSchemaXml.getAllowedAttributes( + tableColumnType + ); + + final Map columnData = new LinkedHashMap<>(); + columnData.put(ColumnAttributes.TYPE.getName(), tableColumnType.getColumnType()); + columnData.put(ColumnAttributes.NAME.getName(), name); + + for (final String columnAttributeName : allowedAttributes) { + final ColumnAttributes attribute = ColumnAttributes.getByName(columnAttributeName); + + if (columnData.containsKey(columnAttributeName) || !attribute.hasDefault()) { + continue; + } + columnData.put(columnAttributeName, attribute.getDefault()); + } + columnData.put(ColumnAttributes.COMMENT.getName(), getColumnCommentByName(name)); + + complemented.add(columnData); + } + + return complemented; + } + + /** + * Get primary key default data for specified name. + * + * @param name String + * + * @return String + */ + public static Map getTableIdentityColumnData(final @NotNull String name) { + final Map columnData = new LinkedHashMap<>(); + + columnData.put(ColumnAttributes.TYPE.getName(), TableColumnTypes.INT.getColumnType()); + columnData.put(ColumnAttributes.NAME.getName(), name); + columnData.put(ColumnAttributes.PADDING.getName(), ColumnAttributes.PADDING.getDefault()); + columnData.put(ColumnAttributes.UNSIGNED.getName(), ColumnAttributes.UNSIGNED.getDefault()); + columnData.put(ColumnAttributes.NULLABLE.getName(), ColumnAttributes.NULLABLE.getDefault()); + columnData.put(ColumnAttributes.IDENTITY.getName(), "true"); + columnData.put(ColumnAttributes.COMMENT.getName(), getColumnCommentByName(name)); + + return columnData; + } + + /** + * Generate column comment by its name. + * + * @param name String + * + * @return String + */ + @SuppressWarnings("PMD.UseLocaleWithCaseConversions") + private static String getColumnCommentByName(final @NotNull String name) { + final StringBuilder commentStringBuilder = new StringBuilder(); + final String[] nameParts = name.split("_"); + + for (final String namePart : nameParts) { + commentStringBuilder + .append(namePart.substring(0, 1).toUpperCase()) + .append(namePart.substring(1)) + .append(' '); + } + + return commentStringBuilder.append("Column").toString(); + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java b/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java index 9b41ffcb8..7594e6f3a 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java @@ -7,9 +7,11 @@ import com.intellij.lang.Language; import com.intellij.lang.xml.XMLLanguage; +import com.magento.idea.magento2plugin.magento.packages.database.ColumnAttributes; import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes; import java.util.ArrayList; import java.util.List; +import org.jetbrains.annotations.NotNull; public class ModuleDbSchemaXml implements ModuleFileInterface { private static final ModuleDbSchemaXml INSTANCE = new ModuleDbSchemaXml(); @@ -27,18 +29,6 @@ public class ModuleDbSchemaXml implements ModuleFileInterface { 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"; - 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"; //constant attributes values public static final String XML_ATTR_TYPE_PK = "primary"; @@ -62,7 +52,7 @@ public class ModuleDbSchemaXml implements ModuleFileInterface { * @return List */ @SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NcssCount"}) - public static List getAllowedAttributes(final TableColumnTypes columnType) { + public static List getAllowedAttributes(final @NotNull TableColumnTypes columnType) { final List allowedAttributes = new ArrayList<>(); switch (columnType) { @@ -70,68 +60,62 @@ public static List getAllowedAttributes(final TableColumnTypes columnTyp 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 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); + allowedAttributes.add(ColumnAttributes.NAME.getName()); + allowedAttributes.add(ColumnAttributes.NULLABLE.getName()); + allowedAttributes.add(ColumnAttributes.COMMENT.getName()); break; 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); - allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE); - allowedAttributes.add(XML_ATTR_COLUMN_IDENTITY); - allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT); - allowedAttributes.add(XML_ATTR_COLUMN_COMMENT); + allowedAttributes.add(ColumnAttributes.NAME.getName()); + allowedAttributes.add(ColumnAttributes.PADDING.getName()); + allowedAttributes.add(ColumnAttributes.UNSIGNED.getName()); + allowedAttributes.add(ColumnAttributes.NULLABLE.getName()); + allowedAttributes.add(ColumnAttributes.IDENTITY.getName()); + allowedAttributes.add(ColumnAttributes.DEFAULT.getName()); + allowedAttributes.add(ColumnAttributes.COMMENT.getName()); break; case DECIMAL: case DOUBLE: case 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); + allowedAttributes.add(ColumnAttributes.NAME.getName()); + allowedAttributes.add(ColumnAttributes.PRECISION.getName()); + allowedAttributes.add(ColumnAttributes.SCALE.getName()); + allowedAttributes.add(ColumnAttributes.UNSIGNED.getName()); + allowedAttributes.add(ColumnAttributes.NULLABLE.getName()); + allowedAttributes.add(ColumnAttributes.DEFAULT.getName()); + allowedAttributes.add(ColumnAttributes.COMMENT.getName()); break; + case VARBINARY: 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); + allowedAttributes.add(ColumnAttributes.NAME.getName()); + allowedAttributes.add(ColumnAttributes.NULLABLE.getName()); + allowedAttributes.add(ColumnAttributes.LENGTH.getName()); + allowedAttributes.add(ColumnAttributes.DEFAULT.getName()); + allowedAttributes.add(ColumnAttributes.COMMENT.getName()); break; 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); + allowedAttributes.add(ColumnAttributes.NAME.getName()); + allowedAttributes.add(ColumnAttributes.NULLABLE.getName()); + allowedAttributes.add(ColumnAttributes.DEFAULT.getName()); + allowedAttributes.add(ColumnAttributes.COMMENT.getName()); break; case DATETIME: case 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); + allowedAttributes.add(ColumnAttributes.NAME.getName()); + allowedAttributes.add(ColumnAttributes.ON_UPDATE.getName()); + allowedAttributes.add(ColumnAttributes.NULLABLE.getName()); + allowedAttributes.add(ColumnAttributes.DEFAULT.getName()); + allowedAttributes.add(ColumnAttributes.COMMENT.getName()); break; default: break; } - allowedAttributes.add(XML_ATTR_COLUMN_TYPE); + allowedAttributes.add(ColumnAttributes.TYPE.getName()); return allowedAttributes; } diff --git a/src/com/magento/idea/magento2plugin/magento/packages/PropertiesTypes.java b/src/com/magento/idea/magento2plugin/magento/packages/PropertiesTypes.java index 197566518..81c05aa7a 100644 --- a/src/com/magento/idea/magento2plugin/magento/packages/PropertiesTypes.java +++ b/src/com/magento/idea/magento2plugin/magento/packages/PropertiesTypes.java @@ -5,6 +5,11 @@ package com.magento.idea.magento2plugin.magento.packages; +import java.util.InputMismatchException; +import java.util.LinkedList; +import java.util.List; +import org.jetbrains.annotations.NotNull; + public enum PropertiesTypes { INT("int"), FLOAT("float"), @@ -13,14 +18,49 @@ public enum PropertiesTypes { private final String propertyType; + /** + * Entity property types ENUM constructor. + * + * @param propertyType String + */ PropertiesTypes(final String propertyType) { this.propertyType = propertyType; } + /** + * Get property type. + * + * @return String + */ public String getPropertyType() { return propertyType; } + /** + * Get ENUM by its string representation. + * + * @param value String + * + * @return PropertiesTypes + */ + public static PropertiesTypes getByValue(final @NotNull String value) { + for (final PropertiesTypes type : PropertiesTypes.values()) { + if (type.getPropertyType().equals(value)) { + return type; + } + } + + throw new InputMismatchException( + "Invalid property type value provided. Should be compatible with " + + PropertiesTypes.class + ); + } + + /** + * Get property types. + * + * @return String[] array of property types. + */ public static String[] getPropertyTypes() { return new String[]{ valueOf(INT.toString()).getPropertyType(), @@ -29,4 +69,19 @@ public static String[] getPropertyTypes() { valueOf(BOOL.toString()).getPropertyType() }; } + + /** + * Get property types list. + * + * @return List of entity property types. + */ + public static List getPropertyTypesList() { + final List propertyList = new LinkedList<>(); + + for (final PropertiesTypes property : PropertiesTypes.values()) { + propertyList.add(property.getPropertyType()); + } + + return propertyList; + } } diff --git a/src/com/magento/idea/magento2plugin/magento/packages/database/ColumnAttributes.java b/src/com/magento/idea/magento2plugin/magento/packages/database/ColumnAttributes.java new file mode 100644 index 000000000..21c787377 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/packages/database/ColumnAttributes.java @@ -0,0 +1,85 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.packages.database; + +import java.util.InputMismatchException; +import org.jetbrains.annotations.NotNull; + +public enum ColumnAttributes { + NAME("name", null), + TYPE("xsi:type", null), + PADDING("padding", "10"), + UNSIGNED("unsigned", "true"), + NULLABLE("nullable", "false"), + IDENTITY("identity", "false"), + LENGTH("length", "255"), + PRECISION("precision", "20"), + SCALE("scale", "2"), + ON_UPDATE("on_update", "false"), + DEFAULT("default", null), + COMMENT("comment", null); + + private final String name; + private final String defaultValue; + + /** + * Column Attribute ENUM Constructor. + * + * @param name String + * @param defaultValue String + */ + ColumnAttributes(final @NotNull String name, final String defaultValue) { + this.name = name; + this.defaultValue = defaultValue; + } + + /** + * Get column attribute name. + * + * @return String + */ + public String getName() { + return name; + } + + /** + * Get column attribute default value. + * + * @return String + */ + public String getDefault() { + return defaultValue; + } + + /** + * Check if column attribute has default value. + * + * @return boolean + */ + public boolean hasDefault() { + return defaultValue != null; + } + + /** + * Get column attribute ENUM by its name string representation. + * + * @param name String + * + * @return ColumnAttributes + */ + public static @NotNull ColumnAttributes getByName(final @NotNull String name) { + for (final ColumnAttributes attribute : ColumnAttributes.values()) { + if (attribute.getName().equals(name)) { + return attribute; + } + } + + throw new InputMismatchException( + "Invalid column attribute name provided. Should be compatible with " + + ColumnAttributes.class + ); + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/packages/database/PropertyToDefaultTypeMapperUtil.java b/src/com/magento/idea/magento2plugin/magento/packages/database/PropertyToDefaultTypeMapperUtil.java new file mode 100644 index 000000000..29eba08ac --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/packages/database/PropertyToDefaultTypeMapperUtil.java @@ -0,0 +1,39 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.packages.database; + +import com.intellij.openapi.externalSystem.service.execution.NotSupportedException; +import com.magento.idea.magento2plugin.magento.packages.PropertiesTypes; +import org.jetbrains.annotations.NotNull; + +public final class PropertyToDefaultTypeMapperUtil { + + private PropertyToDefaultTypeMapperUtil() {} + + /** + * Get default database type for specified entity property. + * + * @param propertyType PropertiesTypes + * + * @return TableColumnTypes + */ + public static TableColumnTypes map(final @NotNull PropertiesTypes propertyType) { + switch (propertyType) { + case INT: + return TableColumnTypes.INT; + case BOOL: + return TableColumnTypes.BOOLEAN; + case FLOAT: + return TableColumnTypes.DECIMAL; + case STRING: + return TableColumnTypes.VARCHAR; + default: + throw new NotSupportedException( + "ENUMs " + PropertiesTypes.class + " property is not supported." + ); + } + } +} diff --git a/src/com/magento/idea/magento2plugin/reference/provider/TableColumnNamesReferenceProvider.java b/src/com/magento/idea/magento2plugin/reference/provider/TableColumnNamesReferenceProvider.java index 47e1c1218..f28c6f7df 100644 --- a/src/com/magento/idea/magento2plugin/reference/provider/TableColumnNamesReferenceProvider.java +++ b/src/com/magento/idea/magento2plugin/reference/provider/TableColumnNamesReferenceProvider.java @@ -22,6 +22,7 @@ import com.intellij.util.ProcessingContext; import com.intellij.util.indexing.FileBasedIndex; import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import com.magento.idea.magento2plugin.magento.packages.database.ColumnAttributes; import com.magento.idea.magento2plugin.reference.xml.PolyVariantReferenceBase; import com.magento.idea.magento2plugin.stubs.indexes.xml.DeclarativeSchemaElementsIndex; import java.util.ArrayList; @@ -120,7 +121,7 @@ public PsiReference[] getReferencesByElement( for (final XmlTag columnTag : columnTags) { final String columnName = columnTag.getAttributeValue( - ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME + ColumnAttributes.NAME.getName() ); if (columnName != null && columnName.equals(columnIdentifier)) { diff --git a/testData/actions/generation/generator/DbSchemaWhitelistGenerator/generateDbSchemaWhitelistJsonFileForShortProperties/db_schema_whitelist.json b/testData/actions/generation/generator/DbSchemaWhitelistGenerator/generateDbSchemaWhitelistJsonFileForShortProperties/db_schema_whitelist.json new file mode 100644 index 000000000..172bbebfc --- /dev/null +++ b/testData/actions/generation/generator/DbSchemaWhitelistGenerator/generateDbSchemaWhitelistJsonFileForShortProperties/db_schema_whitelist.json @@ -0,0 +1,17 @@ +{ + "test_table": { + "column": { + "entity_id": true, + "name": true, + "age": true, + "salary": true, + "is_single": true + }, + "index": { + "TEST_TABLE_ENTITY_ID": true + }, + "constraint": { + "PRIMARY": true + } + } +} diff --git a/testData/actions/generation/generator/DbSchemaXmlGenerator/generateDbSchemaXmlFileForShortProperties/db_schema.xml b/testData/actions/generation/generator/DbSchemaXmlGenerator/generateDbSchemaXmlFileForShortProperties/db_schema.xml new file mode 100644 index 000000000..c3429b510 --- /dev/null +++ b/testData/actions/generation/generator/DbSchemaXmlGenerator/generateDbSchemaXmlFileForShortProperties/db_schema.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + +
+
diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaWhitelistGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaWhitelistGeneratorTest.java index b7aed8c09..a8d747fa5 100644 --- a/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaWhitelistGeneratorTest.java +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaWhitelistGeneratorTest.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.generator.util.DbSchemaGeneratorDataProviderUtil; +import com.magento.idea.magento2plugin.actions.generation.generator.util.DbSchemaGeneratorUtil; import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaWhitelistJson; -import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import com.magento.idea.magento2plugin.magento.packages.database.ColumnAttributes; import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes; import java.util.LinkedHashMap; import java.util.LinkedList; @@ -46,6 +48,41 @@ public void testGenerateDbSchemaWhitelistJsonFile() { ); } + /** + * Test whether db_schema_whitelist.json file generated correctly + * when columns provided as short entity properties. + */ + public void testGenerateDbSchemaWhitelistJsonFileForShortProperties() { + final List> properties = + DbSchemaGeneratorDataProviderUtil.generateEntityPropertiesForTest(); + + final List> columnsData = + DbSchemaGeneratorUtil.complementShortPropertiesByDefaults(properties); + columnsData.add(0, DbSchemaGeneratorUtil.getTableIdentityColumnData("entity_id")); + + final DbSchemaXmlData dbSchemaXmlData = new DbSchemaXmlData( + TABLE_NAME, + "", + "", + "", + columnsData + ); + final DbSchemaWhitelistJsonGenerator dbSchemaWhitelistJsonGenerator = + new DbSchemaWhitelistJsonGenerator( + myFixture.getProject(), + dbSchemaXmlData, + MODULE_NAME + ); + + final String filePath = this.getFixturePath(ModuleDbSchemaWhitelistJson.FILE_NAME); + + assertGeneratedFileIsCorrect( + myFixture.configureByFile(filePath), + EXPECTED_DIRECTORY, + dbSchemaWhitelistJsonGenerator.generate("test") + ); + } + /** * Generate columns for testcase. * @@ -55,53 +92,53 @@ private List> createColumnsForTest() { final List> columns = new LinkedList<>(); final Map entityIdColumnData = new LinkedHashMap<>(); entityIdColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.INT.getColumnType() ); - entityIdColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "entity_id"); - entityIdColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, "true"); + entityIdColumnData.put(ColumnAttributes.NAME.getName(), "entity_id"); + entityIdColumnData.put(ColumnAttributes.IDENTITY.getName(), "true"); columns.add(entityIdColumnData); final Map nameColumnData = new LinkedHashMap<>(); nameColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.VARCHAR.getColumnType() ); - nameColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "name"); + nameColumnData.put(ColumnAttributes.NAME.getName(), "name"); columns.add(nameColumnData); final Map ageColumnData = new LinkedHashMap<>(); ageColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.INT.getColumnType() ); - ageColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "age"); + ageColumnData.put(ColumnAttributes.NAME.getName(), "age"); columns.add(ageColumnData); final Map salaryColumnData = new LinkedHashMap<>(); salaryColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.DECIMAL.getColumnType() ); - salaryColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "salary"); + salaryColumnData.put(ColumnAttributes.NAME.getName(), "salary"); columns.add(salaryColumnData); final Map dobColumnData = new LinkedHashMap<>(); dobColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.DATE.getColumnType() ); - dobColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "dob"); + dobColumnData.put(ColumnAttributes.NAME.getName(), "dob"); columns.add(dobColumnData); final Map createdAtColumnData = new LinkedHashMap<>(); createdAtColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.TIMESTAMP.getColumnType() ); - createdAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "created_at"); + createdAtColumnData.put(ColumnAttributes.NAME.getName(), "created_at"); columns.add(createdAtColumnData); final Map updatedAtColumnData = new LinkedHashMap<>(); updatedAtColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.TIMESTAMP.getColumnType() ); - updatedAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NAME, "updated_at"); + updatedAtColumnData.put(ColumnAttributes.NAME.getName(), "updated_at"); columns.add(updatedAtColumnData); return columns; 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 b19d69f52..5e31071f1 100644 --- a/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGeneratorTest.java +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGeneratorTest.java @@ -6,7 +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.generator.util.DbSchemaGeneratorDataProviderUtil; +import com.magento.idea.magento2plugin.actions.generation.generator.util.DbSchemaGeneratorUtil; import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml; +import com.magento.idea.magento2plugin.magento.packages.database.ColumnAttributes; 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; @@ -52,6 +55,39 @@ public void testGenerateDbSchemaXmlFile() { ); } + /** + * Test db_schema.xml file generation when columns provided as short entity properties. + */ + public void testGenerateDbSchemaXmlFileForShortProperties() { + final List> properties = + DbSchemaGeneratorDataProviderUtil.generateEntityPropertiesForTest(); + + final List> columnsData = + DbSchemaGeneratorUtil.complementShortPropertiesByDefaults(properties); + columnsData.add(0, DbSchemaGeneratorUtil.getTableIdentityColumnData("entity_id")); + + final DbSchemaXmlData dbSchemaXmlData = new DbSchemaXmlData( + TABLE_NAME, + TABLE_RESOURCE, + TABLE_ENGINE, + TABLE_COMMENT, + columnsData + ); + 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. * @@ -62,89 +98,89 @@ private List> createColumnsForTest() { final List> columns = new LinkedList<>(); final Map entityIdColumnData = new LinkedHashMap<>(); entityIdColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.INT.getColumnType() ); - 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"); + entityIdColumnData.put(ColumnAttributes.NAME.getName(), "entity_id"); + entityIdColumnData.put(ColumnAttributes.PADDING.getName(), "11"); + entityIdColumnData.put(ColumnAttributes.UNSIGNED.getName(), BOOLEAN_VALUE_TRUE); + entityIdColumnData.put(ColumnAttributes.NULLABLE.getName(), BOOLEAN_VALUE_FALSE); + entityIdColumnData.put(ColumnAttributes.IDENTITY.getName(), BOOLEAN_VALUE_TRUE); + entityIdColumnData.put(ColumnAttributes.COMMENT.getName(), "Entity Id Column"); columns.add(entityIdColumnData); final Map nameColumnData = new LinkedHashMap<>(); nameColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.VARCHAR.getColumnType() ); - 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"); + nameColumnData.put(ColumnAttributes.NAME.getName(), "name"); + nameColumnData.put(ColumnAttributes.NULLABLE.getName(), BOOLEAN_VALUE_FALSE); + nameColumnData.put(ColumnAttributes.LENGTH.getName(), "255"); + nameColumnData.put(ColumnAttributes.DEFAULT.getName(), "John Smith"); + nameColumnData.put(ColumnAttributes.COMMENT.getName(), "Name Column"); columns.add(nameColumnData); final Map ageColumnData = new LinkedHashMap<>(); ageColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.INT.getColumnType() ); - 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"); + ageColumnData.put(ColumnAttributes.NAME.getName(), "age"); + ageColumnData.put(ColumnAttributes.PADDING.getName(), "5"); + ageColumnData.put(ColumnAttributes.UNSIGNED.getName(), BOOLEAN_VALUE_TRUE); + ageColumnData.put(ColumnAttributes.NULLABLE.getName(), BOOLEAN_VALUE_TRUE); + ageColumnData.put(ColumnAttributes.IDENTITY.getName(), BOOLEAN_VALUE_FALSE); + ageColumnData.put(ColumnAttributes.COMMENT.getName(), "Age Column"); columns.add(ageColumnData); final Map salaryColumnData = new LinkedHashMap<>(); salaryColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.DECIMAL.getColumnType() ); - 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"); + salaryColumnData.put(ColumnAttributes.NAME.getName(), "salary"); + salaryColumnData.put(ColumnAttributes.PADDING.getName(), "5"); + salaryColumnData.put(ColumnAttributes.UNSIGNED.getName(), BOOLEAN_VALUE_TRUE); + salaryColumnData.put(ColumnAttributes.NULLABLE.getName(), BOOLEAN_VALUE_FALSE); + salaryColumnData.put(ColumnAttributes.PRECISION.getName(), "10"); + salaryColumnData.put(ColumnAttributes.SCALE.getName(), "2"); + salaryColumnData.put(ColumnAttributes.DEFAULT.getName(), "0.0"); + salaryColumnData.put(ColumnAttributes.COMMENT.getName(), "Salary Column"); columns.add(salaryColumnData); final Map dobColumnData = new LinkedHashMap<>(); dobColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.DATE.getColumnType() ); - 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"); + dobColumnData.put(ColumnAttributes.NAME.getName(), "dob"); + dobColumnData.put(ColumnAttributes.NULLABLE.getName(), BOOLEAN_VALUE_TRUE); + dobColumnData.put(ColumnAttributes.COMMENT.getName(), "Date Of The Birth Column"); columns.add(dobColumnData); final Map createdAtColumnData = new LinkedHashMap<>(); createdAtColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.TIMESTAMP.getColumnType() ); - 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(ColumnAttributes.NAME.getName(), "created_at"); + createdAtColumnData.put(ColumnAttributes.NULLABLE.getName(), BOOLEAN_VALUE_FALSE); + createdAtColumnData.put(ColumnAttributes.ON_UPDATE.getName(), BOOLEAN_VALUE_FALSE); createdAtColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_DEFAULT, + ColumnAttributes.DEFAULT.getName(), CURRENT_TIMESTAMP_DEFAULT_VALUE ); - createdAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT, "Created At Column"); + createdAtColumnData.put(ColumnAttributes.COMMENT.getName(), "Created At Column"); columns.add(createdAtColumnData); final Map updatedAtColumnData = new LinkedHashMap<>(); updatedAtColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE, + ColumnAttributes.TYPE.getName(), TableColumnTypes.TIMESTAMP.getColumnType() ); - 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(ColumnAttributes.NAME.getName(), "updated_at"); + updatedAtColumnData.put(ColumnAttributes.NULLABLE.getName(), BOOLEAN_VALUE_FALSE); + updatedAtColumnData.put(ColumnAttributes.ON_UPDATE.getName(), BOOLEAN_VALUE_TRUE); updatedAtColumnData.put( - ModuleDbSchemaXml.XML_ATTR_COLUMN_DEFAULT, + ColumnAttributes.DEFAULT.getName(), CURRENT_TIMESTAMP_DEFAULT_VALUE ); - updatedAtColumnData.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT, "Updated At Column"); + updatedAtColumnData.put(ColumnAttributes.COMMENT.getName(), "Updated At Column"); columns.add(updatedAtColumnData); return columns; diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/util/DbSchemaGeneratorDataProviderUtil.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/util/DbSchemaGeneratorDataProviderUtil.java new file mode 100644 index 000000000..99a058ea2 --- /dev/null +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/util/DbSchemaGeneratorDataProviderUtil.java @@ -0,0 +1,51 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator.util; + +import com.magento.idea.magento2plugin.magento.packages.PropertiesTypes; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +public final class DbSchemaGeneratorDataProviderUtil { + + private static final String PROPERTY_NAME = "Name"; + private static final String PROPERTY_TYPE = "Type"; + + private DbSchemaGeneratorDataProviderUtil() {} + + /** + * Generate properties for testcases. + * + * @return List of prepared properties. + */ + public static List> generateEntityPropertiesForTest() { + final List> propertyList = new LinkedList<>(); + + final Map nameProperty = new HashMap<>(); + nameProperty.put(PROPERTY_NAME, "name"); + nameProperty.put(PROPERTY_TYPE, PropertiesTypes.STRING.getPropertyType()); + propertyList.add(nameProperty); + + final Map ageProperty = new HashMap<>(); + ageProperty.put(PROPERTY_NAME, "age"); + ageProperty.put(PROPERTY_TYPE, PropertiesTypes.INT.getPropertyType()); + propertyList.add(ageProperty); + + final Map salaryProperty = new HashMap<>(); + salaryProperty.put(PROPERTY_NAME, "salary"); + salaryProperty.put(PROPERTY_TYPE, PropertiesTypes.FLOAT.getPropertyType()); + propertyList.add(salaryProperty); + + final Map singleProperty = new HashMap<>(); + singleProperty.put(PROPERTY_NAME, "is_single"); + singleProperty.put(PROPERTY_TYPE, PropertiesTypes.BOOL.getPropertyType()); + propertyList.add(singleProperty); + + return propertyList; + } +}