16
16
import com .magento .idea .magento2plugin .actions .generation .data .DbSchemaXmlData ;
17
17
import com .magento .idea .magento2plugin .actions .generation .generator .util .FindOrCreateDbSchemaXmlUtil ;
18
18
import com .magento .idea .magento2plugin .magento .files .ModuleDbSchemaXml ;
19
+ import com .magento .idea .magento2plugin .magento .packages .database .TableColumnTypes ;
19
20
import java .util .HashMap ;
21
+ import java .util .InputMismatchException ;
20
22
import java .util .LinkedHashMap ;
21
23
import java .util .LinkedList ;
22
24
import java .util .List ;
@@ -56,7 +58,12 @@ public DbSchemaXmlGenerator(
56
58
}
57
59
58
60
@ Override
59
- @ SuppressWarnings ({"PMD.NPathComplexity" , "PMD.CyclomaticComplexity" , "PMD.ExcessiveImports" })
61
+ @ SuppressWarnings ({
62
+ "PMD.NPathComplexity" ,
63
+ "PMD.CyclomaticComplexity" ,
64
+ "PMD.ExcessiveImports" ,
65
+ "PMD.AvoidInstantiatingObjectsInLoops"
66
+ })
60
67
public PsiFile generate (final String actionName ) {
61
68
final XmlFile dbSchemaXmlFile = (XmlFile ) findOrCreateDbSchemaXmlUtil .execute (
62
69
actionName ,
@@ -80,30 +87,37 @@ public PsiFile generate(final String actionName) {
80
87
);
81
88
82
89
boolean hasPrimaryKey = false ;
83
- final Map <String , String > primaryKeyData = new HashMap <>();//NOPMD
90
+ final Map <String , String > primaryKeyData = new HashMap <>();
84
91
85
92
for (final Map <String , String > columnData : dbSchemaXmlData .getColumns ()) {
86
- final String columnIdentityValue =
87
- columnData .get (ModuleDbSchemaXml .XML_ATTR_COLUMN_NAME );
88
93
final String identityAttrValue =
89
94
columnData .get (ModuleDbSchemaXml .XML_ATTR_COLUMN_IDENTITY );
90
- final Map <String , String > attributes = new LinkedHashMap <>();//NOPMD
91
95
92
96
if (!hasPrimaryKey && Boolean .parseBoolean (identityAttrValue )) {
93
97
hasPrimaryKey = true ;
94
98
primaryKeyData .putAll (columnData );
95
99
}
96
100
97
101
final String columnTypeValue = columnData .get (ModuleDbSchemaXml .XML_ATTR_COLUMN_TYPE );
98
- final List <String > allowedColumns =
99
- ModuleDbSchemaXml .getAllowedAttributes (columnTypeValue );
102
+ final TableColumnTypes columnType = TableColumnTypes .getByValue (columnTypeValue );
103
+
104
+ if (columnType == null ) {
105
+ throw new InputMismatchException (
106
+ "Invalid column types provided. Should be compatible with "
107
+ + TableColumnTypes .class
108
+ );
109
+ }
100
110
111
+ final Map <String , String > attributes = new LinkedHashMap <>();
112
+ final List <String > allowedColumns = ModuleDbSchemaXml .getAllowedAttributes (columnType );
101
113
for (final Map .Entry <String , String > columnDataEntry : columnData .entrySet ()) {
102
114
if (allowedColumns .contains (columnDataEntry .getKey ())
103
115
&& !columnDataEntry .getValue ().isEmpty ()) {
104
116
attributes .put (columnDataEntry .getKey (), columnDataEntry .getValue ());
105
117
}
106
118
}
119
+ final String columnIdentityValue =
120
+ columnData .get (ModuleDbSchemaXml .XML_ATTR_COLUMN_NAME );
107
121
108
122
findOrCreateTag (
109
123
ModuleDbSchemaXml .XML_TAG_COLUMN ,
@@ -131,10 +145,7 @@ private void generatePrimaryKey(
131
145
@ NotNull final Map <String , String > primaryKeyData ,
132
146
final XmlTag tableTag
133
147
) {
134
- final String columnIdentityValue = primaryKeyData .get (
135
- ModuleDbSchemaXml .XML_ATTR_COLUMN_NAME
136
- );
137
- final Map <String , String > attributes = new LinkedHashMap <>();//NOPMD
148
+ final Map <String , String > attributes = new LinkedHashMap <>();
138
149
attributes .put (
139
150
ModuleDbSchemaXml .XML_ATTR_COLUMN_TYPE ,
140
151
ModuleDbSchemaXml .XML_ATTR_TYPE_PK
@@ -151,7 +162,10 @@ private void generatePrimaryKey(
151
162
ModuleDbSchemaXml .XML_ATTR_REFERENCE_ID_PK ,
152
163
attributes
153
164
);
154
- final Map <String , String > pkColumnAttributes = new HashMap <>();//NOPMD
165
+ final Map <String , String > pkColumnAttributes = new HashMap <>();
166
+ final String columnIdentityValue = primaryKeyData .get (
167
+ ModuleDbSchemaXml .XML_ATTR_COLUMN_NAME
168
+ );
155
169
pkColumnAttributes .put (ModuleDbSchemaXml .XML_ATTR_COLUMN_NAME , columnIdentityValue );
156
170
157
171
findOrCreateTag (
@@ -162,7 +176,7 @@ private void generatePrimaryKey(
162
176
pkColumnAttributes
163
177
);
164
178
165
- final Map <String , String > pkIndexAttributes = new LinkedHashMap <>();//NOPMD
179
+ final Map <String , String > pkIndexAttributes = new LinkedHashMap <>();
166
180
final List <String > indexColumnsNames = new LinkedList <>();
167
181
indexColumnsNames .add (columnIdentityValue );
168
182
@@ -277,5 +291,6 @@ private boolean validateData(final DbSchemaXmlData dbSchemaXmlData) {
277
291
}
278
292
279
293
@ Override
280
- protected void fillAttributes (Properties attributes ) {}//NOPMD
294
+ @ SuppressWarnings ("PMD.UncommentedEmptyMethodBody" )
295
+ protected void fillAttributes (final Properties attributes ) {}
281
296
}
0 commit comments