1
+ package extensions.wu.seal
2
+
3
+ import extensions.Extension
4
+ import wu.seal.jsontokotlin.model.classscodestruct.DataClass
5
+ import wu.seal.jsontokotlin.model.classscodestruct.KotlinClass
6
+ import wu.seal.jsontokotlin.ui.*
7
+ import javax.swing.JPanel
8
+
9
+ object BaseClassSupport : Extension() {
10
+ /* *
11
+ * Config key can't be private, as it will be accessed from `library` module
12
+ */
13
+
14
+ @Suppress(" MemberVisibilityCanBePrivate" )
15
+ val baseClassSupportEnabledKey = " azk.zero.baseclass_enabled"
16
+
17
+ @Suppress(" MemberVisibilityCanBePrivate" )
18
+ const val baseClassImportKey = " azk.zero.baseclass_import"
19
+
20
+ @Suppress(" MemberVisibilityCanBePrivate" )
21
+ const val baseClassNameKey = " azk.zero.baseclass_name"
22
+
23
+ @Suppress(" MemberVisibilityCanBePrivate" )
24
+ const val baseClassPropertiesKey = " azk.zero.baseclass_properties"
25
+
26
+ override fun createUI (): JPanel {
27
+ val classImportField = jTextInput(getConfig(baseClassImportKey), getConfig(baseClassSupportEnabledKey).toBoolean()) {
28
+ addFocusLostListener {
29
+ if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
30
+ setConfig(baseClassImportKey, text)
31
+ }
32
+ }
33
+ document = ImportConventionDocument ()
34
+ }
35
+
36
+ val classNameField = jTextInput(getConfig(baseClassNameKey), getConfig(baseClassSupportEnabledKey).toBoolean()) {
37
+ addFocusLostListener {
38
+ if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
39
+ setConfig(baseClassNameKey, text)
40
+ }
41
+ }
42
+ document = SuperClassConventionDocument (100 )
43
+ }
44
+
45
+ val classPropertiesField = jTextInput(getConfig(baseClassPropertiesKey), getConfig(baseClassSupportEnabledKey).toBoolean()) {
46
+ addFocusLostListener {
47
+ if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
48
+ setConfig(baseClassPropertiesKey, text)
49
+ }
50
+ }
51
+ document = PropertyConventionDocument ()
52
+ }
53
+
54
+ return jVerticalLinearLayout {
55
+ jHorizontalLinearLayout{
56
+ jCheckBox(" Base Class Support?" , getConfig(baseClassSupportEnabledKey).toBoolean(), { isSelected ->
57
+ setConfig(baseClassSupportEnabledKey, isSelected.toString())
58
+ classImportField.isEnabled = isSelected
59
+ classNameField.isEnabled = isSelected
60
+ classPropertiesField.isEnabled = isSelected
61
+ })
62
+ }
63
+ jHorizontalLinearLayout {
64
+ jLabel(" Base Class Import line" )
65
+ add(classImportField)
66
+ }
67
+ jHorizontalLinearLayout {
68
+ jLabel(" Base Class Name, used as-is" )
69
+ add(classNameField)
70
+ }
71
+ jHorizontalLinearLayout {
72
+ jLabel(" Excluded Properties list, comma-separated" )
73
+ add(classPropertiesField)
74
+ }
75
+ }
76
+ }
77
+ ;
78
+ override fun intercept (kotlinClass : KotlinClass ): KotlinClass {
79
+ // val exclusion = listOf("error", "message", "status_code", "status", "statusCode")
80
+ return if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
81
+ val exclusionNames = getConfig(baseClassPropertiesKey).split(" ," ).map { it.trim() }
82
+ val baseClassName = getConfig(baseClassNameKey)
83
+ if (kotlinClass is DataClass ) {
84
+ if (kotlinClass.isTop.not ()) return kotlinClass
85
+ val newProperties = kotlinClass.properties.mapNotNull { it.takeIf { it.originName !in exclusionNames } }
86
+ kotlinClass.copy(properties = newProperties, parentClassTemplate = baseClassName)
87
+ } else kotlinClass
88
+ } else {
89
+ kotlinClass
90
+ }
91
+ }
92
+
93
+ override fun intercept (originClassImportDeclaration : String ): String {
94
+
95
+ // val classAnnotationImportClassString = "import com.arena.banglalinkmela.app.data.model.response.base.BaseResponse"
96
+ val classAnnotationImportClassString = getConfig(baseClassImportKey)
97
+
98
+ return if (getConfig(baseClassSupportEnabledKey).toBoolean()) {
99
+ originClassImportDeclaration.append(" import $classAnnotationImportClassString " )
100
+ } else {
101
+ originClassImportDeclaration
102
+ }
103
+ }
104
+ }
0 commit comments