1616import com .magento .idea .magento2plugin .actions .generation .data .DbSchemaXmlData ;
1717import com .magento .idea .magento2plugin .actions .generation .generator .util .FindOrCreateDbSchemaXmlUtil ;
1818import com .magento .idea .magento2plugin .magento .files .ModuleDbSchemaXml ;
19+ import com .magento .idea .magento2plugin .magento .packages .database .TableColumnTypes ;
1920import java .util .HashMap ;
21+ import java .util .InputMismatchException ;
2022import java .util .LinkedHashMap ;
2123import java .util .LinkedList ;
2224import java .util .List ;
@@ -56,7 +58,12 @@ public DbSchemaXmlGenerator(
5658 }
5759
5860 @ Override
59- @ SuppressWarnings ({"PMD.NPathComplexity" , "PMD.CyclomaticComplexity" , "PMD.ExcessiveImports" })
61+ @ SuppressWarnings ({
62+ "PMD.NPathComplexity" ,
63+ "PMD.CyclomaticComplexity" ,
64+ "PMD.ExcessiveImports" ,
65+ "PMD.AvoidInstantiatingObjectsInLoops"
66+ })
6067 public PsiFile generate (final String actionName ) {
6168 final XmlFile dbSchemaXmlFile = (XmlFile ) findOrCreateDbSchemaXmlUtil .execute (
6269 actionName ,
@@ -80,30 +87,37 @@ public PsiFile generate(final String actionName) {
8087 );
8188
8289 boolean hasPrimaryKey = false ;
83- final Map <String , String > primaryKeyData = new HashMap <>();//NOPMD
90+ final Map <String , String > primaryKeyData = new HashMap <>();
8491
8592 for (final Map <String , String > columnData : dbSchemaXmlData .getColumns ()) {
86- final String columnIdentityValue =
87- columnData .get (ModuleDbSchemaXml .XML_ATTR_COLUMN_NAME );
8893 final String identityAttrValue =
8994 columnData .get (ModuleDbSchemaXml .XML_ATTR_COLUMN_IDENTITY );
90- final Map <String , String > attributes = new LinkedHashMap <>();//NOPMD
9195
9296 if (!hasPrimaryKey && Boolean .parseBoolean (identityAttrValue )) {
9397 hasPrimaryKey = true ;
9498 primaryKeyData .putAll (columnData );
9599 }
96100
97101 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+ }
100110
111+ final Map <String , String > attributes = new LinkedHashMap <>();
112+ final List <String > allowedColumns = ModuleDbSchemaXml .getAllowedAttributes (columnType );
101113 for (final Map .Entry <String , String > columnDataEntry : columnData .entrySet ()) {
102114 if (allowedColumns .contains (columnDataEntry .getKey ())
103115 && !columnDataEntry .getValue ().isEmpty ()) {
104116 attributes .put (columnDataEntry .getKey (), columnDataEntry .getValue ());
105117 }
106118 }
119+ final String columnIdentityValue =
120+ columnData .get (ModuleDbSchemaXml .XML_ATTR_COLUMN_NAME );
107121
108122 findOrCreateTag (
109123 ModuleDbSchemaXml .XML_TAG_COLUMN ,
@@ -131,10 +145,7 @@ private void generatePrimaryKey(
131145 @ NotNull final Map <String , String > primaryKeyData ,
132146 final XmlTag tableTag
133147 ) {
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 <>();
138149 attributes .put (
139150 ModuleDbSchemaXml .XML_ATTR_COLUMN_TYPE ,
140151 ModuleDbSchemaXml .XML_ATTR_TYPE_PK
@@ -151,7 +162,10 @@ private void generatePrimaryKey(
151162 ModuleDbSchemaXml .XML_ATTR_REFERENCE_ID_PK ,
152163 attributes
153164 );
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+ );
155169 pkColumnAttributes .put (ModuleDbSchemaXml .XML_ATTR_COLUMN_NAME , columnIdentityValue );
156170
157171 findOrCreateTag (
@@ -162,7 +176,7 @@ private void generatePrimaryKey(
162176 pkColumnAttributes
163177 );
164178
165- final Map <String , String > pkIndexAttributes = new LinkedHashMap <>();//NOPMD
179+ final Map <String , String > pkIndexAttributes = new LinkedHashMap <>();
166180 final List <String > indexColumnsNames = new LinkedList <>();
167181 indexColumnsNames .add (columnIdentityValue );
168182
@@ -277,5 +291,6 @@ private boolean validateData(final DbSchemaXmlData dbSchemaXmlData) {
277291 }
278292
279293 @ Override
280- protected void fillAttributes (Properties attributes ) {}//NOPMD
294+ @ SuppressWarnings ("PMD.UncommentedEmptyMethodBody" )
295+ protected void fillAttributes (final Properties attributes ) {}
281296}
0 commit comments