forked from magento/magento2-phpstorm-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewEntityDialog.java
812 lines (724 loc) · 30.7 KB
/
NewEntityDialog.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
package com.magento.idea.magento2plugin.actions.generation.dialog;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.ui.DocumentAdapter;
import com.intellij.util.indexing.FileBasedIndex;
import com.magento.idea.magento2plugin.actions.generation.NewEntityAction;
import com.magento.idea.magento2plugin.actions.generation.context.EntityCreatorContext;
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormButtonData;
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormFieldData;
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormFieldsetData;
import com.magento.idea.magento2plugin.actions.generation.data.dialog.EntityCreatorContextData;
import com.magento.idea.magento2plugin.actions.generation.data.dialog.NewEntityDialogData;
import com.magento.idea.magento2plugin.actions.generation.data.ui.ComboBoxItemData;
import com.magento.idea.magento2plugin.actions.generation.dialog.reflection.GetReflectionFieldUtil;
import com.magento.idea.magento2plugin.actions.generation.dialog.util.ClassPropertyFormatterUtil;
import com.magento.idea.magento2plugin.actions.generation.dialog.util.ProcessWorker;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.data.FieldValidationData;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AclResourceIdRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.AlphanumericWithUnderscoreRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.IdentifierRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.Lowercase;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.MenuIdentifierRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NumericRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.RouteIdRule;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.TableNameLength;
import com.magento.idea.magento2plugin.actions.generation.generator.pool.GeneratorPoolHandler;
import com.magento.idea.magento2plugin.actions.generation.generator.pool.provider.NewEntityGeneratorsProviderUtil;
import com.magento.idea.magento2plugin.actions.generation.generator.util.DbSchemaGeneratorUtil;
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
import com.magento.idea.magento2plugin.actions.generation.util.GenerationContextRegistry;
import com.magento.idea.magento2plugin.magento.files.ControllerBackendPhp;
import com.magento.idea.magento2plugin.magento.files.DataModelFile;
import com.magento.idea.magento2plugin.magento.files.DataModelInterfaceFile;
import com.magento.idea.magento2plugin.magento.files.ModuleMenuXml;
import com.magento.idea.magento2plugin.magento.files.actions.NewActionFile;
import com.magento.idea.magento2plugin.magento.packages.File;
import com.magento.idea.magento2plugin.magento.packages.PropertiesTypes;
import com.magento.idea.magento2plugin.magento.packages.database.TableEngines;
import com.magento.idea.magento2plugin.magento.packages.database.TableResources;
import com.magento.idea.magento2plugin.magento.packages.uicomponent.FormElementType;
import com.magento.idea.magento2plugin.stubs.indexes.xml.MenuIndex;
import com.magento.idea.magento2plugin.ui.FilteredComboBox;
import com.magento.idea.magento2plugin.ui.table.TableGroupWrapper;
import com.magento.idea.magento2plugin.util.CamelCaseToSnakeCase;
import com.magento.idea.magento2plugin.util.magento.GetAclResourcesListUtil;
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.DocumentEvent;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings({
"PMD.TooManyFields",
"PMD.UnusedPrivateField",
"PMD.ExcessiveImports"
})
public class NewEntityDialog extends AbstractDialog {
private final @NotNull Project project;
private final String moduleName;
private JPanel contentPane;
private JTabbedPane tabbedPane1;
private JPanel propertiesPanel;
private JTable propertyTable;
private JButton addProperty;
private JButton buttonOK;
private JButton buttonCancel;
private JPanel generalTable;
private JCheckBox createUiComponent;
private JLabel entityNameLabel;
private JLabel dbTableNameLabel;
private JLabel entityIdColumnNameLabel;
private JLabel routeLabel;
private JLabel aclLabel;
private JTextField aclTitle;
private FilteredComboBox parentAcl;
private JTextField formLabel;
private JLabel formLabelLabel;
private JLabel gridNameLabel;
private JLabel parentMenuItemLabel;
private JLabel sortOrderLabel;
private JLabel menuIdentifierLabel;
private JLabel menuTitleLabel;
private JTextField menuTitle;
private FilteredComboBox parentMenu;
private JCheckBox addToolBar;
private JCheckBox addBookmarksCheckBox;
private JCheckBox addColumnsControlCheckBox;
private JCheckBox addFullTextSearchCheckBox;
private JCheckBox addListingFiltersCheckBox;
private JCheckBox addListingPagingCheckBox;
private JComboBox<ComboBoxItemData> tableEngine;
private JLabel tableEngineLabel;
private JComboBox<ComboBoxItemData> tableResource;
private JLabel tableResourceLabel;
private JCheckBox createInterface;
private final List<String> properties;
private TableGroupWrapper entityPropertiesTableGroupWrapper;
private static final String ACTION_NAME = "Create Entity";
private static final String PROPERTY_NAME = "Name";
private static final String PROPERTY_TYPE = "Type";
private static final String ACL_ID = "ACL ID";
private static final String FORM_NAME = "Form Name";
private static final String GRID_NAME = "Grid Name";
private static final String IDENTIFIER = "Identifier";
private static final String SORT_ORDER = "Sort Order";
private static final String UI_COMPONENTS_TAB_NAME = "Admin UI Components";
private static final String TABLE_NAME = "DB Table Name";
private static final String ENTITY_NAME = "Entity Name";
private static final String ROUTER = "Route";
private static final String ENTITY_ID = "Entity ID Field Name";
private static final String MODEL_SUFFIX = "Model";
private static final String RESOURCE_MODEL_SUFFIX = "Resource";
private static final String COLLECTION_MODEL_SUFFIX = "Collection";
private static final String DTO_MODEL_SUFFIX = "Data";
private static final String DTO_INTERFACE_SUFFIX = "Interface";
private static final String DATA_PROVIDER_SUFFIX = "DataProvider";
private static final String DEFAULT_MENU_SORT_ORDER = "100";
private static final boolean OPEN_FILES_FLAG = false;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, ENTITY_ID})
@FieldValidation(rule = RuleRegistry.IDENTIFIER, message = {IdentifierRule.MESSAGE, ENTITY_ID})
private JTextField entityId;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, ENTITY_NAME})
@FieldValidation(
rule = RuleRegistry.ALPHANUMERIC,
message = {AlphanumericRule.MESSAGE, ENTITY_NAME}
)
private JTextField entityName;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, TABLE_NAME})
@FieldValidation(rule = RuleRegistry.LOWERCASE, message = {Lowercase.MESSAGE, TABLE_NAME})
@FieldValidation(
rule = RuleRegistry.ALPHANUMERIC_WITH_UNDERSCORE,
message = {AlphanumericWithUnderscoreRule.MESSAGE, TABLE_NAME}
)
@FieldValidation(rule = RuleRegistry.TABLE_NAME_LENGTH, message = {TableNameLength.MESSAGE})
private JTextField dbTableName;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, ROUTER})
@FieldValidation(rule = RuleRegistry.ROUTE_ID, message = {RouteIdRule.MESSAGE})
private JTextField route;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, FORM_NAME})
@FieldValidation(rule = RuleRegistry.IDENTIFIER, message = {IdentifierRule.MESSAGE, FORM_NAME})
private JTextField formName;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, GRID_NAME})
@FieldValidation(rule = RuleRegistry.IDENTIFIER, message = {IdentifierRule.MESSAGE, FORM_NAME})
private JTextField gridName;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, ACL_ID})
@FieldValidation(rule = RuleRegistry.ACL_RESOURCE_ID, message = {AclResourceIdRule.MESSAGE})
private JTextField acl;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, SORT_ORDER})
@FieldValidation(rule = RuleRegistry.NUMERIC, message = {NumericRule.MESSAGE, SORT_ORDER})
private JTextField sortOrder;
@FieldValidation(rule = RuleRegistry.NOT_EMPTY, message = {NotEmptyRule.MESSAGE, IDENTIFIER})
@FieldValidation(rule = RuleRegistry.MENU_IDENTIFIER, message = {MenuIdentifierRule.MESSAGE})
private JTextField menuIdentifier;
private JLabel formNameLabel;
private JPanel uiComponentsPanel;
private JPanel formNamePanel;
private JPanel gridNamePanel;
private JPanel aclIdPanel;
private JPanel menuIdPanel;
private JLabel formNameErrorMessage;
private JLabel gridNameErrorMessage;
private JLabel aclErrorMessage;
private JLabel menuIdentifierErrorMessage;
private JLabel sortOrderErrorMessage;
private JLabel entityNameErrorMessage;
private JLabel dbTableNameErrorMessage;
private JLabel entityIdErrorMessage;
private JLabel routeErrorMessage;
private JCheckBox createWebApi;
private JTextField observerName;
private final ProcessWorker.InProgressFlag onOkActionFired;
/**
* Constructor.
*
* @param project Project
* @param directory PsiDirectory
*/
public NewEntityDialog(final @NotNull Project project, final PsiDirectory directory) {
super();
this.project = project;
this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
this.properties = new ArrayList<>();
setContentPane(contentPane);
setModal(false);
setTitle(NewEntityAction.ACTION_DESCRIPTION);
getRootPane().setDefaultButton(buttonOK);
onOkActionFired = new ProcessWorker.InProgressFlag(false);
buttonOK.addActionListener(this::generateNewEntityFiles);
buttonCancel.addActionListener((final ActionEvent event) -> onCancel());
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent event) {
onCancel();
}
@SuppressWarnings("PMD.AccessorMethodGeneration")
@Override
public void windowOpened(final WindowEvent event) {
entityName.requestFocus();
}
});
initializeComboboxSources();
initPropertiesTable();
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(
(final ActionEvent event) -> onCancel(),
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
);
entityName.addKeyListener(new KeyAdapter() {
@SuppressWarnings("PMD.AccessorMethodGeneration")
@Override
public void keyReleased(final KeyEvent event) {
entityName.setText(StringUtils.capitalize(entityName.getText()));
}
});
entityName.getDocument().addDocumentListener(new DocumentAdapter() {
@SuppressWarnings("PMD.AccessorMethodGeneration")
@Override
protected void textChanged(final @NotNull DocumentEvent event) {
autoCompleteIdentifiers();
}
});
toggleUiComponentsPanel();
createUiComponent.addItemListener(event -> toggleUiComponentsPanel());
registerTabbedPane(tabbedPane1);
sortOrder.setText(DEFAULT_MENU_SORT_ORDER);
}
/**
* Open new controller dialog.
*
* @param project Project
* @param directory PsiDirectory
*/
public static void open(final Project project, final PsiDirectory directory) {
final NewEntityDialog dialog = new NewEntityDialog(project, directory);
dialog.pack();
dialog.centerDialog(dialog);
dialog.setVisible(true);
}
/**
* Filter fields to validate if createUiComponent checkbox isn't selected.
*
* @return List[FieldValidationData]
*/
@Override
protected List<FieldValidationData> getFieldsToValidate() {
final List<FieldValidationData> filteredFields = new LinkedList<>();
if (createUiComponent.isSelected()) {
filteredFields.addAll(super.getFieldsToValidate());
} else {
final List<Field> fieldsToIgnore = new ArrayList<>();
fieldsToIgnore.add(GetReflectionFieldUtil.getByName("route", this.getClass()));
fieldsToIgnore.add(GetReflectionFieldUtil.getByName("formName", this.getClass()));
fieldsToIgnore.add(GetReflectionFieldUtil.getByName("gridName", this.getClass()));
for (final FieldValidationData fieldData : super.getFieldsToValidate()) {
if (fieldsToIgnore.contains(fieldData.getField())) {
continue;
}
filteredFields.add(fieldData);
}
}
return filteredFields;
}
/**
* Initialize combobox sources.
*/
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private void initializeComboboxSources() {
for (final String engine : TableEngines.getTableEnginesList()) {
tableEngine.addItem(new ComboBoxItemData(engine, engine));
}
for (final String resource : TableResources.getTableResourcesList()) {
tableResource.addItem(new ComboBoxItemData(resource, resource));
}
}
/**
* Initialize properties table.
*/
private void initPropertiesTable() {
final List<String> columns = new LinkedList<>(Arrays.asList(
PROPERTY_NAME,
PROPERTY_TYPE
));
final Map<String, List<String>> sources = new HashMap<>();
sources.put(PROPERTY_TYPE, PropertiesTypes.getPropertyTypesList());
// Initialize entity properties Table Group
entityPropertiesTableGroupWrapper = new TableGroupWrapper(
propertyTable,
addProperty,
columns,
new HashMap<>(),
sources
);
entityPropertiesTableGroupWrapper.initTableGroup();
}
/**
* Generate new entity files.
*
* @param event ActionEvent
*/
@SuppressWarnings("PMD.UnusedFormalParameter")
private void generateNewEntityFiles(final @NotNull ActionEvent event) {
if (!onOkActionFired.isInProgress()) {
buttonOK.setEnabled(false);
buttonCancel.setEnabled(false);
if (propertyTable.isEditing()) {
propertyTable.getCellEditor().stopCellEditing();
}
new ProcessWorker(
this::onOK,
this::releaseDialogAfterGeneration,
onOkActionFired
).execute();
}
}
/**
* Perform code generation using input data.
*/
private void onOK() {
if (!validateFormFields()) {
onOkActionFired.setInProgress(false);
return;
}
setCursor(new Cursor(Cursor.WAIT_CURSOR));
formatProperties();
final NewEntityDialogData dialogData = getNewEntityDialogData();
final EntityCreatorContextData context = getEntityCreatorContextData(dialogData);
final EntityCreatorContext generationContext = new EntityCreatorContext();
generationContext.putUserData(
EntityCreatorContext.DTO_TYPE,
dialogData.hasDtoInterface()
? context.getDtoInterfaceNamespaceBuilder().getClassFqn()
: context.getDtoModelNamespaceBuilder().getClassFqn()
);
generationContext.putUserData(EntityCreatorContext.ENTITY_ID, dialogData.getIdFieldName());
GenerationContextRegistry.getInstance().setContext(generationContext);
final GeneratorPoolHandler generatorPoolHandler = new GeneratorPoolHandler(context);
NewEntityGeneratorsProviderUtil.initializeGenerators(
generatorPoolHandler,
context,
dialogData
);
generatorPoolHandler.run();
onOkActionFired.setFinished(true);
}
/**
* Release dialog buttons and hide.
*/
private void releaseDialogAfterGeneration() {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
buttonCancel.setEnabled(true);
buttonOK.setEnabled(true);
if (onOkActionFired.isFinished()) {
exit();
}
}
/**
* Get entity creator context data.
*
* @param dialogData NewEntityDialogData
*
* @return EntityCreatorContextData
*/
private EntityCreatorContextData getEntityCreatorContextData(
final @NotNull NewEntityDialogData dialogData
) {
final String entityName = dialogData.getEntityName();
final String dtoClassName = entityName.concat(DTO_MODEL_SUFFIX);
final String dtoInterfaceClassName = entityName.concat(DTO_INTERFACE_SUFFIX);
final String actionsPathPrefix = dialogData.getRoute() + File.separator
+ entityName.toLowerCase(Locale.getDefault()) + File.separator;
final NamespaceBuilder dtoModelNamespace =
new DataModelFile(moduleName, dtoClassName).getNamespaceBuilder();
final NamespaceBuilder dtoInterfaceNamespace =
new DataModelInterfaceFile(moduleName, dtoInterfaceClassName).getNamespaceBuilder();
final NamespaceBuilder formViewNamespaceBuilder =
new NamespaceBuilder(
moduleName,
"Edit",
ControllerBackendPhp.DEFAULT_DIR + File.separator + entityName
);
return new EntityCreatorContextData(
project,
moduleName,
ACTION_NAME,
OPEN_FILES_FLAG,
dialogData.hasWebApi(),
actionsPathPrefix.concat("index"),
actionsPathPrefix.concat("edit"),
actionsPathPrefix.concat("new"),
actionsPathPrefix.concat("delete"),
dtoModelNamespace,
dtoInterfaceNamespace,
formViewNamespaceBuilder,
new NewActionFile(moduleName, entityName).getNamespaceBuilder(),
getEntityProperties(),
getButtons(),
getFieldSets(),
getFields()
);
}
/**
* Formats properties into an array of ClassPropertyData objects.
*/
private void formatProperties() {
final String name = getEntityIdColumn();
final String type = "int";
properties.add(ClassPropertyFormatterUtil.formatSingleProperty(name, type));
properties.addAll(ClassPropertyFormatterUtil.formatProperties(getPropertiesTable()));
}
/**
* Get controller name.
*
* @return String
*/
private String getFormName() {
return formName.getText().trim();
}
/**
* Returns form fieldSets.
*
* @return List[UiComponentFormFieldsetData]
*/
private List<UiComponentFormFieldsetData> getFieldSets() {
final ArrayList<UiComponentFormFieldsetData> fieldSets = new ArrayList<>();
final UiComponentFormFieldsetData fieldsetData = new UiComponentFormFieldsetData(
"general",
"General",
"10"
);
fieldSets.add(fieldsetData);
return fieldSets;
}
/**
* Return form buttons list.
*
* @return List[UiComponentFormButtonData]
*/
private List<UiComponentFormButtonData> getButtons() {
final List<UiComponentFormButtonData> buttons = new ArrayList<>();
final String directory = "Block/Form/" + entityName.getText().trim();
final NamespaceBuilder namespaceBuilderSave = new NamespaceBuilder(
moduleName,
"Save",
directory
);
buttons.add(new UiComponentFormButtonData(
directory,
"Save",
moduleName,
"Save",
namespaceBuilderSave.getNamespace(),
"Save Entity",
"10",
getFormName(),
namespaceBuilderSave.getClassFqn()
));
final NamespaceBuilder namespaceBuilderBack = new NamespaceBuilder(
moduleName,
"Back",
directory
);
buttons.add(new UiComponentFormButtonData(
directory,
"Back",
moduleName,
"Back",
namespaceBuilderBack.getNamespace(),
"Back To Grid",
"20",
getFormName(),
namespaceBuilderBack.getClassFqn()
));
final NamespaceBuilder namespaceBuilderDelete = new NamespaceBuilder(
moduleName,
"Delete",
directory
);
buttons.add(new UiComponentFormButtonData(
directory,
"Delete",
moduleName,
"Delete",
namespaceBuilderDelete.getNamespace(),
"Delete Entity",
"30",
getFormName(),
namespaceBuilderDelete.getClassFqn()
));
return buttons;
}
/**
* Returns form fields list.
*
* @return List[UiComponentFormFieldData]
*/
private List<UiComponentFormFieldData> getFields() {
final DefaultTableModel model = getPropertiesTable();
final ArrayList<UiComponentFormFieldData> fieldsets = new ArrayList<>();
fieldsets.add(
new UiComponentFormFieldData(
getEntityIdColumn(),
"Entity ID",
"0",
"general",
FormElementType.HIDDEN.getType(),
"text",
getEntityIdColumn()
)
);
for (int count = 0; count < model.getRowCount(); count++) {
final String name = model.getValueAt(count, 0).toString();
final String dataType = model.getValueAt(count, 1).toString();
final String label = Arrays.stream(name.split("_")).map(
string -> string.substring(0, 1).toUpperCase(Locale.getDefault())
+ string.substring(1)).collect(Collectors.joining(" ")
);
final String sortOrder = String.valueOf(count).concat("0");
final String fieldset = "general";
final PropertiesTypes property =
PropertiesTypes.getByValue(model.getValueAt(count, 1).toString());
final String formElementType =
FormElementType.getDefaultForProperty(property).getType();
final String source = model.getValueAt(count, 0).toString(); //todo: convert
final UiComponentFormFieldData fieldsetData = new UiComponentFormFieldData(//NOPMD
name,
label,
sortOrder,
fieldset,
formElementType,
dataType,
source
);
fieldsets.add(
fieldsetData
);
}
return fieldsets;
}
@SuppressWarnings({"PMD.UnusedPrivateMethod"})
private void createUIComponents() {
final List<String> aclResourcesList = GetAclResourcesListUtil.execute(project);
final Collection<String> menuReferences = FileBasedIndex
.getInstance().getAllKeys(MenuIndex.KEY, project);
final ArrayList<String> menuReferencesList = new ArrayList<>(menuReferences);
Collections.sort(menuReferencesList);
this.parentAcl = new FilteredComboBox(aclResourcesList);
this.parentMenu = new FilteredComboBox(menuReferencesList);
if (aclResourcesList.contains(ModuleMenuXml.defaultAcl)) {
parentAcl.setSelectedItem(ModuleMenuXml.defaultAcl);
}
}
/**
* Get entity properties table columns data and format to suitable for generator.
*
* @return List of entity properties stored in HashMap.
*/
private List<Map<String, String>> getEntityProperties() {
final List<Map<String, String>> shortColumnsData =
entityPropertiesTableGroupWrapper.getColumnsData();
final List<Map<String, String>> columnsData =
DbSchemaGeneratorUtil.complementShortPropertiesByDefaults(shortColumnsData);
columnsData.add(0, DbSchemaGeneratorUtil.getTableIdentityColumnData(getEntityIdColumn()));
return columnsData;
}
/**
* Autocomplete entity name dependent fields.
*/
private void autoCompleteIdentifiers() {
final String entityNameValue = entityName.getText().trim();
if (entityNameValue.isEmpty()) {
return;
}
final String entityName = CamelCaseToSnakeCase.getInstance().convert(entityNameValue);
final String entityNameLabel = Arrays.stream(entityName.split("_")).map(
string -> string.substring(0, 1).toUpperCase(Locale.getDefault())
+ string.substring(1)
).collect(Collectors.joining(" "));
dbTableName.setText(entityName);
entityId.setText(entityName.concat("_id"));
route.setText(entityName);
formLabel.setText(entityNameLabel.concat(" Form"));
formName.setText(entityName.concat("_form"));
gridName.setText(entityName.concat("_listing"));
acl.setText(moduleName.concat("::management"));
aclTitle.setText(entityNameLabel.concat(" Management"));
menuIdentifier.setText(moduleName.concat("::management"));
menuTitle.setText(entityNameLabel.concat(" Management"));
}
/**
* Resolve ui components panel state.
*/
private void toggleUiComponentsPanel() {
if (createUiComponent.isSelected()) {
tabbedPane1.add(uiComponentsPanel, 1);
tabbedPane1.setTitleAt(1, UI_COMPONENTS_TAB_NAME);
} else {
tabbedPane1.remove(1);
}
}
/**
* Get new entity dialog data object.
*
* @return NewEntityDialogData
*/
private NewEntityDialogData getNewEntityDialogData() {
return new NewEntityDialogData(
entityName.getText().trim(),
dbTableName.getText().trim(),
entityId.getText().trim(),
getTableEngine(),
getTableResource(),
createUiComponent.isSelected(),
createInterface.isSelected(),
createWebApi.isSelected(),
route.getText().trim(),
formLabel.getText().trim(),
formName.getText().trim(),
gridName.getText().trim(),
addToolBar.isSelected(),
addBookmarksCheckBox.isSelected(),
addColumnsControlCheckBox.isSelected(),
addListingFiltersCheckBox.isSelected(),
addListingPagingCheckBox.isSelected(),
addFullTextSearchCheckBox.isSelected(),
getParentAcl(),
acl.getText().trim(),
aclTitle.getText().trim(),
getParentMenu(),
Integer.parseInt(sortOrder.getText().trim()),
menuIdentifier.getText().trim(),
menuTitle.getText().trim(),
ClassPropertyFormatterUtil.joinProperties(properties)
);
}
/**
* Get properties table.
*
* @return DefaultTableModel
*/
private DefaultTableModel getPropertiesTable() {
return (DefaultTableModel) propertyTable.getModel();
}
/**
* Get entity id column name.
*
* @return String
*/
private String getEntityIdColumn() {
return entityId.getText().trim();
}
/**
* Get table engine.
*
* @return String
*/
private String getTableEngine() {
return tableEngine.getSelectedItem() == null ? ""
: tableEngine.getSelectedItem().toString().trim();
}
/**
* Get table resource.
*
* @return String
*/
private String getTableResource() {
return tableResource.getSelectedItem() == null ? ""
: tableResource.getSelectedItem().toString().trim();
}
/**
* Get parent acl resource id.
*
* @return String
*/
private String getParentAcl() {
return parentAcl.getSelectedItem() == null ? ""
: parentAcl.getSelectedItem().toString().trim();
}
/**
* Get parent menu id.
*
* @return String
*/
private String getParentMenu() {
return parentMenu.getSelectedItem() == null ? ""
: parentMenu.getSelectedItem().toString().trim();
}
}