diff --git a/resources/magento2/validation.properties b/resources/magento2/validation.properties index 8fec08b55..2b1a894d8 100644 --- a/resources/magento2/validation.properties +++ b/resources/magento2/validation.properties @@ -38,3 +38,4 @@ validator.db.invalidTableNameLength=Table name must contain up to 64 characters validator.lowerSnakeCase=The {0} field must be of the lower snake case format validator.menuIdentifierInvalid=The menu identifier is invalid validator.someFieldsHaveErrors=Please, check the dialog. Some fields have errors +validator.dbSchema.invalidColumnType=Invalid ''{0}'' column type specified 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 b5314a571..f48820135 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDbSchemaDialog.java @@ -7,6 +7,7 @@ import com.intellij.openapi.project.Project; 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.data.DbSchemaXmlData; import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData; @@ -143,7 +144,11 @@ private void onOK() { getTableComment(), getColumns() ); - generateDbSchemaXmlFile(dbSchemaXmlData); + final PsiFile dbSchemaXmlFile = generateDbSchemaXmlFile(dbSchemaXmlData); + + if (dbSchemaXmlFile == null) { + return; + } generateWhitelistJsonFile(dbSchemaXmlData); } exit(); @@ -170,8 +175,8 @@ public static void open( * * @param dbSchemaXmlData DbSchemaXmlData */ - private void generateDbSchemaXmlFile(final @NotNull DbSchemaXmlData dbSchemaXmlData) { - new DbSchemaXmlGenerator( + private PsiFile generateDbSchemaXmlFile(final @NotNull DbSchemaXmlData dbSchemaXmlData) { + return new DbSchemaXmlGenerator( dbSchemaXmlData, project, moduleName 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 f394ebde5..fb91e1fbc 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java @@ -15,23 +15,28 @@ import com.intellij.psi.xml.XmlTag; import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData; import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateDbSchemaXmlUtil; +import com.magento.idea.magento2plugin.bundles.CommonBundle; +import com.magento.idea.magento2plugin.bundles.ValidatorBundle; 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; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Properties; +import javax.swing.JOptionPane; import org.jetbrains.annotations.NotNull; public class DbSchemaXmlGenerator extends FileGenerator { + private final Project project; private final String moduleName; private final DbSchemaXmlData dbSchemaXmlData; private final FindOrCreateDbSchemaXmlUtil findOrCreateDbSchemaXmlUtil; + private final ValidatorBundle validatorBundle; + private final CommonBundle commonBundle; private final List newTagsQueue; private final Map newTagsChildParentRelationMap; @@ -52,6 +57,8 @@ public DbSchemaXmlGenerator( this.project = project; this.moduleName = moduleName; this.dbSchemaXmlData = dbSchemaXmlData; + this.validatorBundle = new ValidatorBundle(); + this.commonBundle = new CommonBundle(); findOrCreateDbSchemaXmlUtil = new FindOrCreateDbSchemaXmlUtil(project); newTagsQueue = new LinkedList<>(); @@ -62,6 +69,7 @@ public DbSchemaXmlGenerator( @SuppressWarnings({ "PMD.NPathComplexity", "PMD.CyclomaticComplexity", + "PMD.CognitiveComplexity", "PMD.ExcessiveImports", "PMD.AvoidInstantiatingObjectsInLoops" }) @@ -99,32 +107,39 @@ public PsiFile generate(final String actionName) { primaryKeyData.putAll(columnData); } + final String columnName = columnData.get(ColumnAttributes.NAME.getName()); final String columnTypeValue = columnData.get(ColumnAttributes.TYPE.getName()); final TableColumnTypes columnType = TableColumnTypes.getByValue(columnTypeValue); if (columnType == null) { - throw new InputMismatchException( - "Invalid column types provided. Should be compatible with " - + TableColumnTypes.class + final String errorMessage = validatorBundle.message( + "validator.dbSchema.invalidColumnType", + columnName == null ? "" : columnName + + ); + JOptionPane.showMessageDialog( + null, + errorMessage, + commonBundle.message("common.error"), + JOptionPane.ERROR_MESSAGE ); + return null; } final Map attributes = new LinkedHashMap<>(); final List allowedColumns = ModuleDbSchemaXml.getAllowedAttributes(columnType); + for (final Map.Entry columnDataEntry : columnData.entrySet()) { if (allowedColumns.contains(columnDataEntry.getKey()) && !columnDataEntry.getValue().isEmpty()) { attributes.put(columnDataEntry.getKey(), columnDataEntry.getValue()); } } - final String columnIdentityValue = - columnData.get(ColumnAttributes.NAME.getName()); - findOrCreateTag( ModuleDbSchemaXml.XML_TAG_COLUMN, ColumnAttributes.NAME.getName(), tableTag, - columnIdentityValue, + columnName, attributes ); }