Skip to content

[Bug] 626: Fixed Invalid column types provided exception during db_schema.xml file generation #819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/magento2/validation.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,7 +144,11 @@ private void onOK() {
getTableComment(),
getColumns()
);
generateDbSchemaXmlFile(dbSchemaXmlData);
final PsiFile dbSchemaXmlFile = generateDbSchemaXmlFile(dbSchemaXmlData);

if (dbSchemaXmlFile == null) {
return;
}
generateWhitelistJsonFile(dbSchemaXmlData);
}
exit();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<XmlTag> newTagsQueue;
private final Map<XmlTag, XmlTag> newTagsChildParentRelationMap;
Expand All @@ -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<>();
Expand All @@ -62,6 +69,7 @@ public DbSchemaXmlGenerator(
@SuppressWarnings({
"PMD.NPathComplexity",
"PMD.CyclomaticComplexity",
"PMD.CognitiveComplexity",
"PMD.ExcessiveImports",
"PMD.AvoidInstantiatingObjectsInLoops"
})
Expand Down Expand Up @@ -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<String, String> attributes = new LinkedHashMap<>();
final List<String> allowedColumns = ModuleDbSchemaXml.getAllowedAttributes(columnType);

for (final Map.Entry<String, String> 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
);
}
Expand Down