1
1
package com .openblocks .api .application ;
2
2
3
3
import static com .openblocks .domain .permission .model .ResourceAction .READ_APPLICATIONS ;
4
+ import static com .openblocks .sdk .constants .DslConstants .CompoundAppDslConstants .ACTION ;
5
+ import static com .openblocks .sdk .constants .DslConstants .CompoundAppDslConstants .APP ;
6
+ import static com .openblocks .sdk .constants .DslConstants .CompoundAppDslConstants .APP_ID ;
7
+ import static com .openblocks .sdk .constants .DslConstants .CompoundAppDslConstants .COMP ;
8
+ import static com .openblocks .sdk .constants .DslConstants .CompoundAppDslConstants .HIDE_WHEN_NO_PERMISSION ;
4
9
5
10
import java .util .ArrayList ;
6
11
import java .util .Collections ;
7
12
import java .util .HashMap ;
8
13
import java .util .Iterator ;
9
14
import java .util .List ;
10
15
import java .util .Map ;
16
+ import java .util .Optional ;
11
17
import java .util .Set ;
12
18
import java .util .stream .Collectors ;
13
19
19
25
20
26
import com .google .common .collect .Sets ;
21
27
import com .openblocks .api .home .SessionUserService ;
22
- import com .openblocks .sdk .util .MoreMapUtils ;
23
28
import com .openblocks .domain .permission .service .ResourcePermissionService ;
24
29
import com .openblocks .sdk .constants .DslConstants .CompoundAppDslConstants ;
30
+ import com .openblocks .sdk .util .MoreMapUtils ;
25
31
26
32
import reactor .core .publisher .Mono ;
27
33
@@ -50,7 +56,6 @@ public Mono<Void> removeSubAppsFromCompoundDsl(Map<String, Object> dsl) {
50
56
.then ();
51
57
}
52
58
53
- @ SuppressWarnings ("unchecked" )
54
59
private void removeSubAppsFromCompoundDsl (Map <String , Object > dsl , Set <String > appIdsNeedRemoved ) {
55
60
56
61
List <Map <String , Object >> items = MoreMapUtils .getList (dsl , CompoundAppDslConstants .ITEMS , new ArrayList <>());
@@ -59,13 +64,11 @@ private void removeSubAppsFromCompoundDsl(Map<String, Object> dsl, Set<String> a
59
64
Map <String , Object > item = iterator .next ();
60
65
// for leaf node which has empty items.
61
66
if (isLeaf (item )) {
62
- boolean hideWhenNoPermission = MapUtils .getBoolean (item , CompoundAppDslConstants .HIDE_WHEN_NO_PERMISSION , true );
63
- if (!hideWhenNoPermission ) {
67
+ if (!hideWhenNoPermission (item )) {
64
68
continue ;
65
69
}
66
70
67
- Map <String , Object > app = (Map <String , Object >) MapUtils .getMap (item , CompoundAppDslConstants .APP , new HashMap <>());
68
- String appId = MapUtils .getString (app , CompoundAppDslConstants .APP_ID );
71
+ String appId = getAppId (item );
69
72
if (StringUtils .isNotBlank (appId ) && appIdsNeedRemoved .contains (appId )) {
70
73
iterator .remove ();
71
74
continue ;
@@ -92,15 +95,13 @@ private boolean isLeaf(Map<String, Object> item) {
92
95
/**
93
96
* Recursively find all sub-application ids from the DSL of the compound application.
94
97
*/
95
- @ SuppressWarnings ("unchecked" )
96
98
public Set <String > getAllSubAppIdsFromCompoundAppDsl (Map <String , Object > dsl ) {
97
99
List <Map <String , Object >> items = MoreMapUtils .getList (dsl , CompoundAppDslConstants .ITEMS , new ArrayList <>());
98
100
return items .stream ()
99
101
.map (item -> {
100
102
// If the item is a leaf node, find its id and return it.
101
103
if (isLeaf (item )) {
102
- Map <String , Object > app = (Map <String , Object >) MapUtils .getMap (item , CompoundAppDslConstants .APP , new HashMap <>());
103
- String appId = MapUtils .getString (app , CompoundAppDslConstants .APP_ID );
104
+ String appId = getAppId (item );
104
105
if (StringUtils .isBlank (appId )) {
105
106
return Collections .<String > emptySet ();
106
107
}
@@ -112,4 +113,23 @@ public Set<String> getAllSubAppIdsFromCompoundAppDsl(Map<String, Object> dsl) {
112
113
.flatMap (Set ::stream )
113
114
.collect (Collectors .toSet ());
114
115
}
116
+
117
+ @ SuppressWarnings ("unchecked" )
118
+ private String getAppId (Map <String , Object > item ) {
119
+ return Optional .ofNullable ((Map <String , Object >) MapUtils .getMap (item , ACTION ))
120
+ .map (action -> (Map <String , Object >) MapUtils .getMap (action , COMP ))
121
+ .or (() -> Optional .of (item )) // compatible code
122
+ .map (i -> (Map <String , Object >) MapUtils .getMap (i , APP ))
123
+ .map (app -> MapUtils .getString (app , APP_ID ))
124
+ .orElse (null );
125
+ }
126
+
127
+ @ SuppressWarnings ("unchecked" )
128
+ private boolean hideWhenNoPermission (Map <String , Object > item ) {
129
+ return Optional .ofNullable ((Map <String , Object >) MapUtils .getMap (item , ACTION ))
130
+ .map (action -> (Map <String , Object >) MapUtils .getMap (action , COMP ))
131
+ .or (() -> Optional .of (item )) // compatible code
132
+ .map (i -> MapUtils .getBoolean (i , HIDE_WHEN_NO_PERMISSION ))
133
+ .orElse (true );
134
+ }
115
135
}
0 commit comments