Skip to content

Commit 70f703c

Browse files
Added posibility to generate db_schema.xml and db_schema_whitelist.json from entity manager dialog
1 parent 48789b9 commit 70f703c

File tree

8 files changed

+541
-212
lines changed

8 files changed

+541
-212
lines changed

src/com/magento/idea/magento2plugin/actions/generation/data/DbSchemaXmlData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void setColumns(final List<Map<String, String>> columns) {
8787
* @return Map
8888
*/
8989
public Map<String, String> getTableAttributesMap() {
90-
final Map<String, String> tableAttributesData = new LinkedHashMap<>();//NOPMD
90+
final Map<String, String> tableAttributesData = new LinkedHashMap<>();
9191
tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_NAME, getTableName());
9292
tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_RESOURCE, getTableResource());
9393
tableAttributesData.put(ModuleDbSchemaXml.XML_ATTR_TABLE_ENGINE, getTableEngine());

src/com/magento/idea/magento2plugin/actions/generation/data/util/DbSchemaXmlSourceDataUtil.java

Whitespace-only changes.

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java

Lines changed: 214 additions & 172 deletions
Large diffs are not rendered by default.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.generator.util;
7+
8+
import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml;
9+
import com.magento.idea.magento2plugin.magento.packages.PropertiesTypes;
10+
import com.magento.idea.magento2plugin.magento.packages.database.ColumnAttributes;
11+
import com.magento.idea.magento2plugin.magento.packages.database.PropertyToDefaultTypeMapperUtil;
12+
import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes;
13+
import java.util.LinkedHashMap;
14+
import java.util.LinkedList;
15+
import java.util.List;
16+
import java.util.Map;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
public final class DbSchemaGeneratorUtil {
20+
private static final String PROPERTY_NAME = "Name";
21+
private static final String PROPERTY_TYPE = "Type";
22+
23+
private DbSchemaGeneratorUtil() {}
24+
25+
/**
26+
* Complement short entity properties to expected in the db_schema.xml generator.
27+
*
28+
* @param shortProperties List
29+
*
30+
* @return List of expected in the DbSchemaXmlGenerator properties.
31+
*/
32+
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
33+
public static List<Map<String, String>> complementShortPropertiesByDefaults(
34+
final List<Map<String, String>> shortProperties
35+
) {
36+
final List<Map<String, String>> complemented = new LinkedList<>();
37+
38+
for (final Map<String, String> property : shortProperties) {
39+
final String name = property.get(PROPERTY_NAME);
40+
final String type = property.get(PROPERTY_TYPE);
41+
42+
final PropertiesTypes propType = PropertiesTypes.getByValue(type);
43+
final TableColumnTypes tableColumnType = PropertyToDefaultTypeMapperUtil.map(propType);
44+
45+
final List<String> allowedAttributes = ModuleDbSchemaXml.getAllowedAttributes(
46+
tableColumnType
47+
);
48+
49+
final Map<String, String> columnData = new LinkedHashMap<>();
50+
columnData.put(ColumnAttributes.TYPE.getName(), tableColumnType.getColumnType());
51+
columnData.put(ColumnAttributes.NAME.getName(), name);
52+
53+
for (final String columnAttributeName : allowedAttributes) {
54+
final ColumnAttributes attribute = ColumnAttributes.getByName(columnAttributeName);
55+
56+
if (columnData.containsKey(columnAttributeName) || !attribute.hasDefault()) {
57+
continue;
58+
}
59+
columnData.put(columnAttributeName, attribute.getDefault());
60+
}
61+
columnData.put(ColumnAttributes.COMMENT.getName(), getColumnCommentByName(name));
62+
63+
complemented.add(columnData);
64+
}
65+
66+
return complemented;
67+
}
68+
69+
/**
70+
* Get primary key default data for specified name.
71+
*
72+
* @param name String
73+
*
74+
* @return String
75+
*/
76+
public static Map<String, String> getTableIdentityColumnData(final @NotNull String name) {
77+
final Map<String, String> columnData = new LinkedHashMap<>();
78+
79+
columnData.put(ColumnAttributes.TYPE.getName(), TableColumnTypes.INT.getColumnType());
80+
columnData.put(ColumnAttributes.NAME.getName(), name);
81+
columnData.put(ColumnAttributes.PADDING.getName(), ColumnAttributes.PADDING.getDefault());
82+
columnData.put(ColumnAttributes.UNSIGNED.getName(), ColumnAttributes.UNSIGNED.getDefault());
83+
columnData.put(ColumnAttributes.NULLABLE.getName(), ColumnAttributes.NULLABLE.getDefault());
84+
columnData.put(ColumnAttributes.IDENTITY.getName(), "true");
85+
columnData.put(ColumnAttributes.COMMENT.getName(), getColumnCommentByName(name));
86+
87+
return columnData;
88+
}
89+
90+
/**
91+
* Generate column comment by its name.
92+
*
93+
* @param name String
94+
*
95+
* @return String
96+
*/
97+
@SuppressWarnings("PMD.UseLocaleWithCaseConversions")
98+
private static String getColumnCommentByName(final @NotNull String name) {
99+
final StringBuilder commentStringBuilder = new StringBuilder();
100+
final String[] nameParts = name.split("_");
101+
102+
for (final String namePart : nameParts) {
103+
commentStringBuilder
104+
.append(namePart.substring(0, 1).toUpperCase())
105+
.append(namePart.substring(1))
106+
.append(' ');
107+
}
108+
109+
return commentStringBuilder.append("Column").toString();
110+
}
111+
}

src/com/magento/idea/magento2plugin/magento/files/ModuleDbSchemaXml.java

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
import com.intellij.lang.Language;
99
import com.intellij.lang.xml.XMLLanguage;
10+
import com.magento.idea.magento2plugin.magento.packages.database.ColumnAttributes;
1011
import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes;
1112
import java.util.ArrayList;
1213
import java.util.List;
14+
import org.jetbrains.annotations.NotNull;
1315

1416
public class ModuleDbSchemaXml implements ModuleFileInterface {
1517
private static final ModuleDbSchemaXml INSTANCE = new ModuleDbSchemaXml();
@@ -27,6 +29,7 @@ public class ModuleDbSchemaXml implements ModuleFileInterface {
2729
public static final String XML_ATTR_CONSTRAINT_REFERENCE_COLUMN_NAME = "referenceColumn";
2830
public static final String XML_ATTR_CONSTRAINT_REFERENCE_ID_NAME = "referenceId";
2931
public static final String XML_ATTR_INDEX_TYPE_NAME = "indexType";
32+
3033
public static final String XML_ATTR_COLUMN_NAME = "name";
3134
public static final String XML_ATTR_COLUMN_TYPE = "xsi:type";
3235
public static final String XML_ATTR_COLUMN_PADDING = "padding";
@@ -62,71 +65,65 @@ public class ModuleDbSchemaXml implements ModuleFileInterface {
6265
* @return List
6366
*/
6467
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NcssCount"})
65-
public static List<String> getAllowedAttributes(final TableColumnTypes columnType) {
68+
public static List<String> getAllowedAttributes(final @NotNull TableColumnTypes columnType) {
6669
final List<String> allowedAttributes = new ArrayList<>();
6770

6871
switch (columnType) {
6972
case BLOB:
7073
case MEDIUMBLOB:
7174
case LONGBLOB:
7275
case DATE:
73-
allowedAttributes.add(XML_ATTR_COLUMN_NAME);
74-
allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE);
75-
allowedAttributes.add(XML_ATTR_COLUMN_COMMENT);
76-
break;
77-
case VARBINARY:
78-
allowedAttributes.add(XML_ATTR_COLUMN_NAME);
79-
allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT);
80-
allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE);
81-
allowedAttributes.add(XML_ATTR_COLUMN_LENGTH);
82-
allowedAttributes.add(XML_ATTR_COLUMN_COMMENT);
76+
allowedAttributes.add(ColumnAttributes.NAME.getName());
77+
allowedAttributes.add(ColumnAttributes.NULLABLE.getName());
78+
allowedAttributes.add(ColumnAttributes.COMMENT.getName());
8379
break;
8480
case TINYINT:
8581
case SMALLINT:
8682
case INT:
8783
case BIGINT:
88-
allowedAttributes.add(XML_ATTR_COLUMN_NAME);
89-
allowedAttributes.add(XML_ATTR_COLUMN_PADDING);
90-
allowedAttributes.add(XML_ATTR_COLUMN_UNSIGNED);
91-
allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE);
92-
allowedAttributes.add(XML_ATTR_COLUMN_IDENTITY);
93-
allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT);
94-
allowedAttributes.add(XML_ATTR_COLUMN_COMMENT);
84+
allowedAttributes.add(ColumnAttributes.NAME.getName());
85+
allowedAttributes.add(ColumnAttributes.PADDING.getName());
86+
allowedAttributes.add(ColumnAttributes.UNSIGNED.getName());
87+
allowedAttributes.add(ColumnAttributes.NULLABLE.getName());
88+
allowedAttributes.add(ColumnAttributes.IDENTITY.getName());
89+
allowedAttributes.add(ColumnAttributes.DEFAULT.getName());
90+
allowedAttributes.add(ColumnAttributes.COMMENT.getName());
9591
break;
9692
case DECIMAL:
9793
case DOUBLE:
9894
case FLOAT:
99-
allowedAttributes.add(XML_ATTR_COLUMN_NAME);
100-
allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT);
101-
allowedAttributes.add(XML_ATTR_COLUMN_SCALE);
102-
allowedAttributes.add(XML_ATTR_COLUMN_PRECISION);
103-
allowedAttributes.add(XML_ATTR_COLUMN_UNSIGNED);
104-
allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE);
105-
allowedAttributes.add(XML_ATTR_COLUMN_COMMENT);
95+
allowedAttributes.add(ColumnAttributes.NAME.getName());
96+
allowedAttributes.add(ColumnAttributes.PRECISION.getName());
97+
allowedAttributes.add(ColumnAttributes.SCALE.getName());
98+
allowedAttributes.add(ColumnAttributes.UNSIGNED.getName());
99+
allowedAttributes.add(ColumnAttributes.NULLABLE.getName());
100+
allowedAttributes.add(ColumnAttributes.DEFAULT.getName());
101+
allowedAttributes.add(ColumnAttributes.COMMENT.getName());
106102
break;
103+
case VARBINARY:
107104
case VARCHAR:
108105
case TEXT:
109106
case MEDIUMTEXT:
110107
case LONGTEXT:
111-
allowedAttributes.add(XML_ATTR_COLUMN_NAME);
112-
allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE);
113-
allowedAttributes.add(XML_ATTR_COLUMN_LENGTH);
114-
allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT);
115-
allowedAttributes.add(XML_ATTR_COLUMN_COMMENT);
108+
allowedAttributes.add(ColumnAttributes.NAME.getName());
109+
allowedAttributes.add(ColumnAttributes.NULLABLE.getName());
110+
allowedAttributes.add(ColumnAttributes.LENGTH.getName());
111+
allowedAttributes.add(ColumnAttributes.DEFAULT.getName());
112+
allowedAttributes.add(ColumnAttributes.COMMENT.getName());
116113
break;
117114
case BOOLEAN:
118-
allowedAttributes.add(XML_ATTR_COLUMN_NAME);
119-
allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT);
120-
allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE);
121-
allowedAttributes.add(XML_ATTR_COLUMN_COMMENT);
115+
allowedAttributes.add(ColumnAttributes.NAME.getName());
116+
allowedAttributes.add(ColumnAttributes.NULLABLE.getName());
117+
allowedAttributes.add(ColumnAttributes.DEFAULT.getName());
118+
allowedAttributes.add(ColumnAttributes.COMMENT.getName());
122119
break;
123120
case DATETIME:
124121
case TIMESTAMP:
125-
allowedAttributes.add(XML_ATTR_COLUMN_NAME);
126-
allowedAttributes.add(XML_ATTR_COLUMN_ON_UPDATE);
127-
allowedAttributes.add(XML_ATTR_COLUMN_NULLABLE);
128-
allowedAttributes.add(XML_ATTR_COLUMN_DEFAULT);
129-
allowedAttributes.add(XML_ATTR_COLUMN_COMMENT);
122+
allowedAttributes.add(ColumnAttributes.NAME.getName());
123+
allowedAttributes.add(ColumnAttributes.ON_UPDATE.getName());
124+
allowedAttributes.add(ColumnAttributes.NULLABLE.getName());
125+
allowedAttributes.add(ColumnAttributes.DEFAULT.getName());
126+
allowedAttributes.add(ColumnAttributes.COMMENT.getName());
130127
break;
131128
default:
132129
break;

src/com/magento/idea/magento2plugin/magento/packages/PropertiesTypes.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
package com.magento.idea.magento2plugin.magento.packages;
77

8+
import java.util.InputMismatchException;
9+
import java.util.LinkedList;
10+
import java.util.List;
11+
import org.jetbrains.annotations.NotNull;
12+
813
public enum PropertiesTypes {
914
INT("int"),
1015
FLOAT("float"),
@@ -13,14 +18,49 @@ public enum PropertiesTypes {
1318

1419
private final String propertyType;
1520

21+
/**
22+
* Entity property types ENUM constructor.
23+
*
24+
* @param propertyType String
25+
*/
1626
PropertiesTypes(final String propertyType) {
1727
this.propertyType = propertyType;
1828
}
1929

30+
/**
31+
* Get property type.
32+
*
33+
* @return String
34+
*/
2035
public String getPropertyType() {
2136
return propertyType;
2237
}
2338

39+
/**
40+
* Get ENUM by its string representation.
41+
*
42+
* @param value String
43+
*
44+
* @return PropertiesTypes
45+
*/
46+
public static PropertiesTypes getByValue(final @NotNull String value) {
47+
for (final PropertiesTypes type : PropertiesTypes.values()) {
48+
if (type.getPropertyType().equals(value)) {
49+
return type;
50+
}
51+
}
52+
53+
throw new InputMismatchException(
54+
"Invalid property type value provided. Should be compatible with "
55+
+ PropertiesTypes.class
56+
);
57+
}
58+
59+
/**
60+
* Get property types.
61+
*
62+
* @return String[] array of property types.
63+
*/
2464
public static String[] getPropertyTypes() {
2565
return new String[]{
2666
valueOf(INT.toString()).getPropertyType(),
@@ -29,4 +69,19 @@ public static String[] getPropertyTypes() {
2969
valueOf(BOOL.toString()).getPropertyType()
3070
};
3171
}
72+
73+
/**
74+
* Get property types list.
75+
*
76+
* @return List of entity property types.
77+
*/
78+
public static List<String> getPropertyTypesList() {
79+
final List<String> propertyList = new LinkedList<>();
80+
81+
for (final PropertiesTypes property : PropertiesTypes.values()) {
82+
propertyList.add(property.getPropertyType());
83+
}
84+
85+
return propertyList;
86+
}
3287
}

0 commit comments

Comments
 (0)