Skip to content

Commit 20a5a2d

Browse files
309: Code refactoring after code review
1 parent 7d9ac34 commit 20a5a2d

File tree

12 files changed

+297
-172
lines changed

12 files changed

+297
-172
lines changed

gradle-tasks/pmd/ruleset.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
</rule>
3838
<exclude-pattern>.*/resources/.*</exclude-pattern>
3939
<exclude-pattern>.*/testData/.*</exclude-pattern>
40-
<rule ref="category/java/multithreading.xml"/>
40+
<rule ref="category/java/multithreading.xml">
41+
<exclude name="UseConcurrentHashMap" />
42+
</rule>
4143
<rule ref="category/java/performance.xml"/>
4244
<rule ref="category/java/security.xml" />
4345
</ruleset>

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

+1-1
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

-106
This file was deleted.

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

+19-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import com.magento.idea.magento2plugin.actions.generation.NewDbSchemaAction;
1111
import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData;
1212
import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData;
13-
import com.magento.idea.magento2plugin.actions.generation.data.util.DbSchemaXmlSourceDataUtil;
1413
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
1514
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
1615
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule;
@@ -19,6 +18,9 @@
1918
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.TableNameLength;
2019
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator;
2120
import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml;
21+
import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes;
22+
import com.magento.idea.magento2plugin.magento.packages.database.TableEngines;
23+
import com.magento.idea.magento2plugin.magento.packages.database.TableResources;
2224
import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper;
2325
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
2426
import java.awt.event.KeyEvent;
@@ -150,6 +152,9 @@ public static void open(
150152
dialog.setVisible(true);
151153
}
152154

155+
/**
156+
* Run db_schema.xml file generator.
157+
*/
153158
private void generateDbSchemaXmlFile() {
154159
new DbSchemaXmlGenerator(
155160
new DbSchemaXmlData(
@@ -164,6 +169,9 @@ private void generateDbSchemaXmlFile() {
164169
).generate(NewDbSchemaAction.ACTION_NAME, false);
165170
}
166171

172+
/**
173+
* Initialize columns ui component.
174+
*/
167175
private void initializeColumnsUiComponentGroup() {
168176
final List<String> columns = new LinkedList<>(Arrays.asList(
169177
ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE,
@@ -180,15 +188,17 @@ private void initializeColumnsUiComponentGroup() {
180188
ModuleDbSchemaXml.XML_ATTR_COLUMN_COMMENT
181189
));
182190
// Set default values for columns
183-
final Map<String, String> defaultValues = new HashMap<>();//NOPMD
191+
final Map<String, String> defaultValues = new HashMap<>();
184192
defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, "false");
185193
defaultValues.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_IDENTITY, "false");
186194
// Set sources for columns
187-
final Map<String, List<String>> sources = new HashMap<>();//NOPMD
195+
final Map<String, List<String>> sources = new HashMap<>();
188196
final List<String> booleanSource = Arrays.asList("true", "false");
197+
final List<String> columnTypes = TableColumnTypes.getTableColumnTypesList();
198+
columnTypes.add(0, "");
189199
sources.put(
190200
ModuleDbSchemaXml.XML_ATTR_COLUMN_TYPE,
191-
DbSchemaXmlSourceDataUtil.getColumnTypes()
201+
columnTypes
192202
);
193203
sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_UNSIGNED, booleanSource);
194204
sources.put(ModuleDbSchemaXml.XML_ATTR_COLUMN_NULLABLE, booleanSource);
@@ -208,14 +218,15 @@ private void initializeColumnsUiComponentGroup() {
208218
/**
209219
* Fill ComboBoxes ui components with predefined constant values.
210220
*/
221+
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
211222
private void fillComboBoxes() {
212223
// Table Engine ComboBox defaults.
213-
for (final String engine : DbSchemaXmlSourceDataUtil.getTableEngineSource()) {
214-
tableEngine.addItem(new ComboBoxItemData(engine, engine));//NOPMD
224+
for (final String engine : TableEngines.getTableEnginesList()) {
225+
tableEngine.addItem(new ComboBoxItemData(engine, engine));
215226
}
216227
// Table Resource ComboBox defaults.
217-
for (final String resource : DbSchemaXmlSourceDataUtil.getTableResourceSource()) {
218-
tableResource.addItem(new ComboBoxItemData(resource, resource));//NOPMD
228+
for (final String resource : TableResources.getTableResourcesList()) {
229+
tableResource.addItem(new ComboBoxItemData(resource, resource));
219230
}
220231
}
221232

src/com/magento/idea/magento2plugin/actions/generation/generator/DbSchemaXmlGenerator.java

+29-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData;
1717
import com.magento.idea.magento2plugin.actions.generation.generator.util.FindOrCreateDbSchemaXmlUtil;
1818
import com.magento.idea.magento2plugin.magento.files.ModuleDbSchemaXml;
19+
import com.magento.idea.magento2plugin.magento.packages.database.TableColumnTypes;
1920
import java.util.HashMap;
21+
import java.util.InputMismatchException;
2022
import java.util.LinkedHashMap;
2123
import java.util.LinkedList;
2224
import 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

Comments
 (0)