19
19
import org .lowcoder .api .usermanagement .OrgDevChecker ;
20
20
import org .lowcoder .domain .application .model .ApplicationStatus ;
21
21
import org .lowcoder .domain .application .model .ApplicationType ;
22
- import org .lowcoder .domain .bundle .model .Bundle ;
23
- import org .lowcoder .domain .bundle .model .BundleRequestType ;
24
- import org .lowcoder .domain .bundle .model .BundleStatus ;
22
+ import org .lowcoder .domain .application .service .ApplicationServiceImpl ;
23
+ import org .lowcoder .domain .bundle .model .*;
25
24
import org .lowcoder .domain .bundle .service .BundleElementRelationService ;
26
25
import org .lowcoder .domain .bundle .service .BundleNode ;
27
26
import org .lowcoder .domain .bundle .service .BundleService ;
28
- import org .lowcoder .domain .bundle .model .BundleElement ;
29
27
import org .lowcoder .domain .bundle .service .*;
30
28
import org .lowcoder .domain .group .service .GroupService ;
31
29
import org .lowcoder .domain .organization .model .OrgMember ;
35
33
import org .lowcoder .domain .permission .service .ResourcePermissionService ;
36
34
import org .lowcoder .domain .user .model .User ;
37
35
import org .lowcoder .domain .user .service .UserService ;
36
+ import org .lowcoder .infra .birelation .BiRelation ;
37
+ import org .lowcoder .infra .birelation .BiRelationBizType ;
38
+ import org .lowcoder .infra .birelation .BiRelationServiceImpl ;
38
39
import org .lowcoder .sdk .exception .BizError ;
39
40
import org .lowcoder .sdk .exception .BizException ;
41
+ import org .springframework .beans .factory .annotation .Autowired ;
40
42
import org .springframework .context .annotation .Lazy ;
41
43
import org .springframework .stereotype .Service ;
42
44
import reactor .core .publisher .Flux ;
43
45
import reactor .core .publisher .Mono ;
46
+ import reactor .core .scheduler .Schedulers ;
44
47
45
48
import java .util .*;
46
49
import java .util .function .Function ;
56
59
@ RequiredArgsConstructor
57
60
@ Service
58
61
public class BundleApiServiceImpl implements BundleApiService {
59
-
60
- private static final Comparator <Node <ApplicationInfoView , BundleInfoView >> DEFAULT_COMPARATOR =
61
- // compare by last view time reversed.
62
- Comparator .comparingLong ((ToLongFunction <Node <ApplicationInfoView , BundleInfoView >>) node -> {
63
- if (node instanceof ElementNode <ApplicationInfoView , BundleInfoView > elementNode ) {
64
- return elementNode .getSelf ().getBundlePosition ();
65
- }
66
- return ((BundleNode <ApplicationInfoView , BundleInfoView >) node ).getSelf ().getCreateTime ();
67
- })
68
- .reversed ()
69
- // compare by name.
70
- .thenComparing (node -> {
71
- if (node instanceof ElementNode <ApplicationInfoView , BundleInfoView > elementNode ) {
72
- return elementNode .getSelf ().getName ();
73
- }
74
- return ((BundleNode <ApplicationInfoView , BundleInfoView >) node ).getSelf ().getName ();
75
- });
76
-
62
+ private final BiRelationServiceImpl biRelationService ;
77
63
private final BundleService bundleService ;
78
64
private final SessionUserService sessionUserService ;
79
65
private final OrgDevChecker orgDevChecker ;
@@ -86,6 +72,7 @@ public class BundleApiServiceImpl implements BundleApiService {
86
72
private final UserService userService ;
87
73
private final OrganizationService organizationService ;
88
74
private final FolderApiService folderApiService ;
75
+ private final ApplicationServiceImpl applicationServiceImpl ;
89
76
90
77
@ Override
91
78
public Mono <BundleInfoView > create (CreateBundleRequest createBundleRequest ) {
@@ -308,45 +295,15 @@ public Mono<Void> reorder(String bundleId, List<String> elementIds) {
308
295
*/
309
296
@ Override
310
297
public Flux <?> getElements (@ Nullable String bundleId , @ Nullable ApplicationType applicationType ) {
311
- return buildApplicationInfoViewTree (applicationType )
312
- .flatMap (tree -> {
313
- BundleNode <ApplicationInfoView , BundleInfoView > bundleNode = tree .get (bundleId );
314
- if (bundleNode == null ) {
315
- return Mono .error (new BizException (BUNDLE_NOT_EXIST , "BUNDLE_NOT_EXIST" , bundleId ));
316
- }
317
- return Mono .just (bundleNode );
318
- })
319
- .zipWith (Mono .zip (sessionUserService .getVisitorOrgMemberCache (), orgDevChecker .isCurrentOrgDev ()))
320
- .doOnNext (tuple -> {
321
- BundleNode <ApplicationInfoView , BundleInfoView > node = tuple .getT1 ();
322
- OrgMember orgMember = tuple .getT2 ().getT1 ();
323
- boolean devOrAdmin = tuple .getT2 ().getT2 ();
324
- // father bundle's visibility depends on child nodes
325
- node .postOrderIterate (n -> {
326
- if (n instanceof BundleNode <ApplicationInfoView , BundleInfoView > bundleNode ) {
327
- BundleInfoView bundleInfoView = bundleNode .getSelf ();
328
- if (bundleInfoView == null ) {
329
- return ;
330
- }
331
- bundleInfoView .setManageable (orgMember .isAdmin () || orgMember .isSuperAdmin () || orgMember .getUserId ().equals (bundleInfoView .getCreateBy ()));
332
- bundleInfoView .setSubApplications (bundleNode .getElementChildren ());
333
- bundleInfoView .setVisible (devOrAdmin || isNotEmpty (bundleInfoView .getSubApplications ()));
334
- }
335
- });
336
- })
337
- .flatMapIterable (tuple -> tuple .getT1 ().getChildren ())
338
- .map (node -> {
339
- if (node instanceof ElementNode <ApplicationInfoView , BundleInfoView > elementNode ) {
340
- return elementNode .getSelf ();
341
- }
342
- return ((BundleNode <ApplicationInfoView , BundleInfoView >) node ).getSelf ();
343
- });
344
- }
345
-
346
- private Mono <Tree <Object , Bundle >> buildBundleTree (String userId ) {
347
- return bundleService .findByUserId (userId )
348
- .collectList ()
349
- .map (bundles -> new Tree <>(bundles , Bundle ::getId , __ -> null , Collections .emptyList (), null , null ));
298
+ return biRelationService .getBySourceId (BiRelationBizType .BUNDLE_ELEMENT , bundleId )
299
+ .sort ((o1 , o2 ) -> {
300
+ var pos1 = Integer .parseInt (o1 .getExtParam1 ());
301
+ var pos2 = Integer .parseInt (o2 .getExtParam1 ());
302
+ return pos1 - pos2 ;
303
+ }).map (bi -> applicationServiceImpl .findById (bi .getTargetId ()))
304
+ .index ()
305
+ .publishOn (Schedulers .boundedElastic ())
306
+ .map (tuple -> new BundleApplication (tuple .getT2 ().block (), tuple .getT1 ()));
350
307
}
351
308
352
309
@ Override
@@ -417,62 +374,6 @@ public Mono<ResourcePermission> checkBundlePermissionWithReadableErrorMsg(String
417
374
});
418
375
}
419
376
420
- private Mono <Tree <ApplicationInfoView , BundleInfoView >> buildApplicationInfoViewTree (@ Nullable ApplicationType applicationType ) {
421
-
422
- Mono <OrgMember > orgMemberMono = sessionUserService .getVisitorOrgMemberCache ()
423
- .cache ();
424
-
425
- Flux <ApplicationInfoView > applicationInfoViewFlux =
426
- userHomeApiService .getAllAuthorisedApplications4CurrentOrgMember (applicationType , ApplicationStatus .NORMAL , false )
427
- .cache ();
428
-
429
- Mono <Map <String , String >> application2BundleMapMono = applicationInfoViewFlux
430
- .map (ApplicationInfoView ::getApplicationId )
431
- .collectList ()
432
- .flatMapMany (applicationIds -> bundleElementRelationService .getByElementIds (applicationIds ))
433
- .collectMap (BundleElement ::elementId , BundleElement ::bundleId );
434
-
435
- Flux <Bundle > bundleFlux = orgMemberMono .flatMapMany (orgMember -> bundleService .findByUserId (orgMember .getUserId ()))
436
- .cache ();
437
-
438
- Mono <Map <String , User >> userMapMono = bundleFlux
439
- .flatMap (bundle -> emptyIfNull (bundle .getCreatedBy ()))
440
- .collectList ()
441
- .flatMap (list -> userService .getByIds (list ))
442
- .cache ();
443
-
444
- Flux <BundleInfoView > bundleInfoViewFlux = bundleFlux
445
- .flatMap (bundle -> Mono .zip (orgMemberMono , userMapMono )
446
- .map (tuple -> {
447
- OrgMember orgMember = tuple .getT1 ();
448
- Map <String , User > userMap = tuple .getT2 ();
449
- User creator = userMap .get (bundle .getCreatedBy ());
450
- return BundleInfoView .builder ()
451
- .userId (orgMember .getUserId ())
452
- .bundleId (bundle .getId ())
453
- .name (bundle .getName ())
454
- .createAt (bundle .getCreatedAt ().toEpochMilli ())
455
- .createBy (creator == null ? null : creator .getName ())
456
- .createTime (bundle .getCreatedAt ())
457
- .build ();
458
- }));
459
-
460
- return Mono .zip (applicationInfoViewFlux .collectList (),
461
- application2BundleMapMono ,
462
- bundleInfoViewFlux .collectList ())
463
- .map (tuple -> {
464
- List <ApplicationInfoView > applicationInfoViews = tuple .getT1 ();
465
- Map <String , String > application2BundleMap = tuple .getT2 ();
466
- List <BundleInfoView > bundleInfoViews = tuple .getT3 ();
467
- return new Tree <>(bundleInfoViews ,
468
- BundleInfoView ::getBundleId ,
469
- __ -> null ,
470
- applicationInfoViews ,
471
- application -> application2BundleMap .get (application .getApplicationId ()),
472
- DEFAULT_COMPARATOR );
473
- });
474
- }
475
-
476
377
/**
477
378
* only bundle creator has manage permissions
478
379
*/
0 commit comments