-
Notifications
You must be signed in to change notification settings - Fork 2
/
SfGpsDsNavigationORA.cls
183 lines (158 loc) · 4.6 KB
/
SfGpsDsNavigationORA.cls
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
global without sharing class SfGpsDsNavigationORA extends SfGpsDsBaseORA implements Callable {
global Object call(String action, Map<String, Object> args) {
init(args);
switch on action {
when 'GetNavigation' {
String developerName = (String) getTypedFromMap(
WhereFrom.INPUT,
'developerName',
String.class,
false
);
Id communityId = (Id) getTypedFromMap(
WhereFrom.INPUT,
'communityId',
Id.class,
false
);
Boolean communityPreview = (Boolean) getTypedFromMap(
WhereFrom.INPUT,
'communityPreview',
Boolean.class,
true
);
if (communityPreview == null) {
communityPreview = false;
}
List<ConnectApi.NavigationMenuItem> lcnmi = getNavigationItems(
communityId,
developerName,
communityPreview
);
List<Object> rv = (List<Object>) JSON.deserializeUntyped(
JSON.serialize(lcnmi)
);
handleNavigationItemsEntities(rv);
output.put('items', rv);
}
when 'GetNavigationV2' {
String developerName = (String) getTypedFromMap(
WhereFrom.INPUT,
'developerName',
String.class,
false
);
Id communityId = (Id) getTypedFromMap(
WhereFrom.INPUT,
'communityId',
Id.class,
false
);
Boolean communityPreview = (Boolean) getTypedFromMap(
WhereFrom.INPUT,
'communityPreview',
Boolean.class,
true
);
if (communityPreview == null) {
communityPreview = false;
}
GetNavigationItemsResp resp = getNavigationItemsV2(
communityId,
developerName,
communityPreview
);
Map<String, Object> rv = (Map<String, Object>) JSON.deserializeUntyped(
JSON.serialize(resp)
);
handleNavigationItemsEntities((List<Object>) rv.get('items'));
output.put('response', rv);
}
when 'GetBaseUrl' {
output.put('url', getBaseUrl());
}
when else {
throw new MalformedCallException('Method not implemented');
}
}
return args;
}
public class GetNavigationItemsResp {
public @AuraEnabled
List<ConnectApi.NavigationMenuItem> items;
public @AuraEnabled
String errorMessage;
}
@AuraEnabled(cacheable=false)
public static GetNavigationItemsResp getNavigationItemsV2(
String communityId,
String developerName,
Boolean communityPreview
) {
GetNavigationItemsResp resp = new GetNavigationItemsResp();
try {
Id cId = Id.valueOf(communityId); // To cater to 15 vs 18 char
ConnectApi.NavigationMenuItemCollection coll = ConnectApi.NavigationMenu.getCommunityNavigationMenu(
cId,
null,
developerName,
communityPreview
? ConnectApi.PublishStatus.Draft
: ConnectApi.PublishStatus.Live,
false,
false,
null
);
resp.items = coll.menuItems;
} catch (Exception e) {
resp.errorMessage = e.getMessage();
}
return resp;
}
@AuraEnabled(cacheable=false)
public static List<ConnectApi.NavigationMenuItem> getNavigationItems(
String communityId,
String developerName,
Boolean communityPreview
) {
try {
Id cId = Id.valueOf(communityId); // To cater to 15 vs 18 char
ConnectApi.NavigationMenuItemCollection coll = ConnectApi.NavigationMenu.getCommunityNavigationMenu(
cId,
null,
developerName,
communityPreview
? ConnectApi.PublishStatus.Draft
: ConnectApi.PublishStatus.Live,
false,
false,
null
);
return coll.menuItems;
} catch (Exception e) {
throw new AuraHandledException(e.getMessage());
}
}
static List<Object> handleNavigationItemsEntities(List<Object> items) {
if (items != null) {
for (Object item : items) {
Map<String, Object> mapItem = (Map<String, Object>) item;
if (mapItem.containsKey('label')) {
mapItem.put('label', ((String) mapItem.get('label')).unescapeHtml4());
if (mapItem.containsKey('subMenu')) {
handleNavigationItemsEntities(
(List<Object>) mapItem.get('subMenu')
);
}
}
}
}
return items;
}
@AuraEnabled(cacheable=true)
public static String getBaseUrl() {
return URL.getOrgDomainURL().toExternalForm();
}
public class MalformedCallException extends Exception {
}
}