14
14
15
15
package org.zowe.explorer.zowe.service
16
16
17
- import com.intellij.notification.Notification
18
17
import com.intellij.notification.NotificationGroupManager
19
18
import com.intellij.notification.NotificationType
20
- import com.intellij.notification.Notifications
21
19
import com.intellij.openapi.actionSystem.AnActionEvent
22
20
import com.intellij.openapi.application.runReadAction
23
21
import com.intellij.openapi.application.runWriteAction
@@ -38,6 +36,7 @@ import org.zowe.explorer.dataops.DataOpsManager
38
36
import org.zowe.explorer.dataops.operations.InfoOperation
39
37
import org.zowe.explorer.dataops.operations.ZOSInfoOperation
40
38
import org.zowe.explorer.explorer.EXPLORER_NOTIFICATION_GROUP_ID
39
+ import org.zowe.explorer.telemetry.NotificationsService
41
40
import org.zowe.explorer.utils.crudable.find
42
41
import org.zowe.explorer.utils.crudable.getAll
43
42
import org.zowe.explorer.utils.runTask
@@ -64,9 +63,6 @@ import java.util.regex.Pattern
64
63
import java.util.stream.Collectors
65
64
import kotlin.collections.set
66
65
67
-
68
- const val ZOWE_CONFIG_NOTIFICATION_GROUP_ID = " org.zowe.explorerzowe.service.ZoweConfigNotificationGroupId"
69
-
70
66
const val ZOWE_PROJECT_PREFIX = " zowe-"
71
67
72
68
/* *
@@ -123,22 +119,6 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
123
119
124
120
override var globalZoweConfig: ZoweConfig ? = null
125
121
126
- /* *
127
- * Displays an error notification if an error was received.
128
- * @param t thrown error.
129
- * @param title error text.
130
- */
131
- private fun notifyError (t : Throwable , title : String? = null) {
132
- Notifications .Bus .notify(
133
- Notification (
134
- ZOWE_CONFIG_NOTIFICATION_GROUP_ID ,
135
- title ? : " Error with Zowe config file" ,
136
- t.message ? : t.toString(),
137
- NotificationType .ERROR
138
- )
139
- )
140
- }
141
-
142
122
/* *
143
123
* Checks project contains zowe.config.json. If zowe config presented
144
124
* it will parse it and save to object model inside zoweConfig field.
@@ -162,7 +142,8 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
162
142
}
163
143
}
164
144
} catch (e: Exception ) {
165
- throw Exception (" Cannot parse $type Zowe config file" )
145
+ NotificationsService .errorNotification(e, project = myProject, custTitle= " Error with Zowe config file" )
146
+ return null
166
147
}
167
148
}
168
149
@@ -208,8 +189,8 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
208
189
209
190
/* *
210
191
* Added notification about connection failure with action of force connection adding.
211
- * @param title - notification title.
212
- * @param content - notification content.
192
+ * @param title notification title.
193
+ * @param content notification content.
213
194
* @return Nothing.
214
195
*/
215
196
private fun notifyUiOnConnectionFailure (title : String , content : String , type : ZoweConfigType ) {
@@ -277,27 +258,23 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
277
258
this .globalZoweConfig
278
259
zoweConfig ? : throw Exception (" Cannot get $type Zowe config" )
279
260
280
- var allPreparedConn = mutableListOf<ConnectionConfig >()
281
- if (checkConnection) {
282
- val failedURLs = mutableListOf<String >()
283
- allPreparedConn = testAndPrepareAllZosmfConnections(zoweConfig, type, failedURLs)
284
- if (failedURLs.isNotEmpty()) {
285
- val andMore = if (failedURLs.size > 3 )
286
- " ..."
287
- else
288
- " "
289
- notifyUiOnConnectionFailure(
290
- " Connection failed to:" ,
291
- " ${failedURLs.joinToString(separator = " , <p>" )} ${andMore} " ,
292
- type
293
- )
294
- return
295
- }
261
+ val (allPreparedConn, failedConnections) = testAndPrepareAllZosmfConnections(zoweConfig, type)
262
+ if (checkConnection and failedConnections.isNotEmpty()) {
263
+ val andMore = if (failedConnections.size > 3 ) " ..." else " "
264
+ notifyUiOnConnectionFailure(
265
+ " Connection failed to:" ,
266
+ " ${failedConnections.map{it.url}.joinToString(separator = " , <p>" )} $andMore " ,
267
+ type
268
+ )
296
269
}
297
- for (zosmfConnection in allPreparedConn) {
270
+ val conToAdd = if (checkConnection)
271
+ allPreparedConn.subtract(failedConnections.toSet())
272
+ else
273
+ allPreparedConn
274
+ conToAdd.forEach { zosmfConnection ->
298
275
val connectionOpt = configCrudable.addOrUpdate(zosmfConnection)
299
276
if (! connectionOpt.isEmpty) {
300
- var topic = if (type == ZoweConfigType .LOCAL )
277
+ val topic = if (type == ZoweConfigType .LOCAL )
301
278
LOCAL_ZOWE_CONFIG_CHANGED
302
279
else
303
280
GLOBAL_ZOWE_CONFIG_CHANGED
@@ -306,7 +283,7 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
306
283
}
307
284
308
285
} catch (e: Exception ) {
309
- notifyError(e )
286
+ NotificationsService .errorNotification(e, project = myProject, custTitle = " Error with Zowe config file " )
310
287
}
311
288
}
312
289
@@ -319,9 +296,9 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
319
296
private fun prepareConnection (zosmfConnection : ZOSConnection , type : ZoweConfigType ): ConnectionConfig {
320
297
val username = zosmfConnection.user
321
298
val password = zosmfConnection.password
322
- val zoweConnection = findExistingConnection(type, zosmfConnection.profileName)?. let {
323
- zosmfConnection.toConnectionConfig(it.uuid, it.zVersion, type = type)
324
- } ? : zosmfConnection.toConnectionConfig(UUID .randomUUID().toString(), type = type)
299
+ val zoweConnection = findExistingConnection(type, zosmfConnection.profileName)
300
+ ?. let { zosmfConnection.toConnectionConfig(it.uuid, it.zVersion, type = type) }
301
+ ? : zosmfConnection.toConnectionConfig(UUID .randomUUID().toString(), type = type)
325
302
CredentialService .getService().setCredentials(zoweConnection.uuid, username, password)
326
303
return zoweConnection
327
304
}
@@ -330,24 +307,25 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
330
307
* Convert all zosmf connections from zowe config file to ConnectionConfig and tests them
331
308
* @param zoweConfig
332
309
* @param type of zowe config
333
- * @return list of URLs which did not pass the test
310
+ * @return pair of lists, one is the connections list, the second is the list of URLs which did not pass the test
334
311
*/
335
312
private fun testAndPrepareAllZosmfConnections (
336
313
zoweConfig : ZoweConfig ,
337
- type : ZoweConfigType ,
338
- failedURLs : MutableList <String >
339
- ): MutableList <ConnectionConfig > {
340
- var allPreparedConn = mutableListOf<ConnectionConfig >()
341
- for (zosmfConnection in zoweConfig.getListOfZosmfConections()) {
342
- val zoweConnection = prepareConnection(zosmfConnection, type)
343
- try {
344
- testAndPrepareConnection(zoweConnection)
345
- } catch (t: Throwable ) {
346
- failedURLs.add(zoweConnection.url)
314
+ type : ZoweConfigType
315
+ ): Pair <List <ConnectionConfig >, List<ConnectionConfig>> {
316
+ return zoweConfig.getListOfZosmfConections()
317
+ .fold(
318
+ mutableListOf<ConnectionConfig >() to mutableListOf<ConnectionConfig >()
319
+ ) { (allConnectionConfigs, failedURLs), zosmfConnection ->
320
+ val zoweConnection = prepareConnection(zosmfConnection, type)
321
+ try {
322
+ testAndPrepareConnection(zoweConnection)
323
+ } catch (t: Throwable ) {
324
+ failedURLs.add(zoweConnection)
325
+ }
326
+ allConnectionConfigs.add(zoweConnection)
327
+ allConnectionConfigs to failedURLs
347
328
}
348
- allPreparedConn.add(zoweConnection)
349
- }
350
- return allPreparedConn
351
329
}
352
330
353
331
/* *
@@ -384,7 +362,7 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
384
362
}
385
363
386
364
} catch (e: Exception ) {
387
- notifyError(e )
365
+ NotificationsService .errorNotification(e, project = myProject, custTitle = " Error with Zowe config file " )
388
366
}
389
367
}
390
368
@@ -434,18 +412,20 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
434
412
val allConnections = configCrudable.getAll<ConnectionConfig >().toList()
435
413
val allConnectionsNames: MutableList <String > = allConnections.map { it.name }.toMutableList()
436
414
437
- allConnections.filter { it.zoweConfigPath == getZoweConfigLocation(myProject, type) }.forEach {
438
- var index = 1
439
- var newName = it.name
440
- while (allConnectionsNames.contains(newName)) {
441
- newName = it.name.plus(index.toString())
442
- index++
415
+ allConnections
416
+ .filter { it.zoweConfigPath == getZoweConfigLocation(myProject, type) }
417
+ .forEach {
418
+ var index = 1
419
+ var newName = it.name
420
+ while (allConnectionsNames.contains(newName)) {
421
+ newName = it.name.plus(index.toString())
422
+ index++
423
+ }
424
+ allConnectionsNames.add(newName)
425
+ it.name = newName
426
+ it.zoweConfigPath = null
427
+ configCrudable.update(it)
443
428
}
444
- allConnectionsNames.add(newName)
445
- it.name = newName
446
- it.zoweConfigPath = null
447
- configCrudable.update(it)
448
- }
449
429
}
450
430
451
431
private fun createZoweSchemaJsonIfNotExists () {
@@ -490,51 +470,45 @@ class ZoweConfigServiceImpl(override val myProject: Project) : ZoweConfigService
490
470
*/
491
471
override fun getZoweConfigState (scanProject : Boolean , type : ZoweConfigType ): ZoweConfigState {
492
472
if (scanProject) {
493
- try {
494
473
scanForZoweConfig(type)
495
- } catch (e: Exception ) {
496
- notifyError(e)
497
- }
498
474
}
499
475
val zoweConfig = if (type == ZoweConfigType .LOCAL )
500
476
localZoweConfig ? : return ZoweConfigState .NOT_EXISTS
501
477
else
502
478
globalZoweConfig ? : return ZoweConfigState .NOT_EXISTS
503
479
504
480
findAllZosmfExistingConnection(type) ? : return ZoweConfigState .NEED_TO_ADD
505
- var ret = ZoweConfigState .SYNCHRONIZED
506
-
507
- for (zosConnection in zoweConfig.getListOfZosmfConections()) {
508
- val existingConnection =
509
- findExistingConnection(type, zosConnection.profileName)
510
- if (existingConnection == null )
511
- ret = setZoweConfigState(ret, ZoweConfigState .NEED_TO_ADD )
512
- else {
513
- val newConnectionList = zoweConfig.getListOfZosmfConections()
514
- .filter { it.profileName == getProfileNameFromConnName(existingConnection.name) }
515
- val newConnection = if (newConnectionList.isNotEmpty()) {
516
- newConnectionList[0 ].toConnectionConfig(
517
- existingConnection.uuid.toString(), existingConnection.zVersion, existingConnection.owner, type = type
518
- )
519
- } else {
520
- ret = setZoweConfigState(ret, ZoweConfigState .NEED_TO_ADD )
521
- continue
522
- }
523
- val zoweUsername = zosConnection.user
524
- val zowePassword = zosConnection.password
525
-
526
- ret = if (
527
- existingConnection == newConnection
528
- && CredentialService .getUsername(newConnection) == zoweUsername
529
- && CredentialService .getPassword(newConnection) == zowePassword
530
- ) {
531
- setZoweConfigState(ret, ZoweConfigState .SYNCHRONIZED )
481
+
482
+ return zoweConfig
483
+ .getListOfZosmfConections()
484
+ .fold(ZoweConfigState .SYNCHRONIZED ) { prevZoweConfigState, zosConnection ->
485
+ val existingConnection = findExistingConnection(type, zosConnection.profileName)
486
+ val currZoweConfigState = if (existingConnection == null ) {
487
+ ZoweConfigState .NEED_TO_ADD
532
488
} else {
533
- setZoweConfigState(ret, ZoweConfigState .NEED_TO_UPDATE )
489
+ val newConnectionList = zoweConfig.getListOfZosmfConections()
490
+ .filter { it.profileName == getProfileNameFromConnName(existingConnection.name) }
491
+ if (newConnectionList.isNotEmpty()) {
492
+ val newConnection = newConnectionList[0 ].toConnectionConfig(
493
+ existingConnection.uuid, existingConnection.zVersion, existingConnection.owner, type = type
494
+ )
495
+ val zoweUsername = zosConnection.user
496
+ val zowePassword = zosConnection.password
497
+ if (
498
+ existingConnection == newConnection
499
+ && CredentialService .getUsername(newConnection) == zoweUsername
500
+ && CredentialService .getPassword(newConnection) == zowePassword
501
+ ) {
502
+ ZoweConfigState .SYNCHRONIZED
503
+ } else {
504
+ ZoweConfigState .NEED_TO_UPDATE
505
+ }
506
+ } else {
507
+ ZoweConfigState .NEED_TO_ADD
508
+ }
534
509
}
510
+ setZoweConfigState(prevZoweConfigState, currZoweConfigState)
535
511
}
536
- }
537
- return ret
538
512
}
539
513
540
514
/* *
0 commit comments