18
18
import org .lowcoder .api .usermanagement .OrgDevChecker ;
19
19
import org .lowcoder .domain .application .model .ApplicationStatus ;
20
20
import org .lowcoder .domain .application .model .ApplicationType ;
21
- import org .lowcoder .domain .bundle .model .Bundle ;
22
- import org .lowcoder .domain .bundle .model .BundleRequestType ;
23
- import org .lowcoder .domain .bundle .model .BundleStatus ;
21
+ import org .lowcoder .domain .application .service .ApplicationServiceImpl ;
22
+ import org .lowcoder .domain .bundle .model .*;
24
23
import org .lowcoder .domain .bundle .service .BundleElementRelationService ;
25
24
import org .lowcoder .domain .bundle .service .BundleNode ;
26
25
import org .lowcoder .domain .bundle .service .BundleService ;
27
- import org .lowcoder .domain .bundle .model .BundleElement ;
28
26
import org .lowcoder .domain .bundle .service .*;
29
27
import org .lowcoder .domain .group .service .GroupService ;
30
28
import org .lowcoder .domain .organization .model .OrgMember ;
34
32
import org .lowcoder .domain .permission .service .ResourcePermissionService ;
35
33
import org .lowcoder .domain .user .model .User ;
36
34
import org .lowcoder .domain .user .service .UserService ;
35
+ import org .lowcoder .infra .birelation .BiRelation ;
36
+ import org .lowcoder .infra .birelation .BiRelationBizType ;
37
+ import org .lowcoder .infra .birelation .BiRelationServiceImpl ;
37
38
import org .lowcoder .sdk .exception .BizError ;
38
39
import org .lowcoder .sdk .exception .BizException ;
40
+ import org .springframework .beans .factory .annotation .Autowired ;
39
41
import org .springframework .context .annotation .Lazy ;
40
42
import org .springframework .stereotype .Service ;
41
43
import reactor .core .publisher .Flux ;
42
44
import reactor .core .publisher .Mono ;
45
+ import reactor .core .scheduler .Schedulers ;
43
46
44
47
import java .util .*;
45
48
import java .util .function .Function ;
55
58
@ RequiredArgsConstructor
56
59
@ Service
57
60
public class BundleApiServiceImpl implements BundleApiService {
58
-
59
- private static final Comparator <Node <ApplicationInfoView , BundleInfoView >> DEFAULT_COMPARATOR =
60
- // compare by last view time reversed.
61
- Comparator .comparingLong ((ToLongFunction <Node <ApplicationInfoView , BundleInfoView >>) node -> {
62
- if (node instanceof ElementNode <ApplicationInfoView , BundleInfoView > elementNode ) {
63
- return elementNode .getSelf ().getBundlePosition ();
64
- }
65
- return ((BundleNode <ApplicationInfoView , BundleInfoView >) node ).getSelf ().getCreateTime ();
66
- })
67
- .reversed ()
68
- // compare by name.
69
- .thenComparing (node -> {
70
- if (node instanceof ElementNode <ApplicationInfoView , BundleInfoView > elementNode ) {
71
- return elementNode .getSelf ().getName ();
72
- }
73
- return ((BundleNode <ApplicationInfoView , BundleInfoView >) node ).getSelf ().getName ();
74
- });
75
-
61
+ private final BiRelationServiceImpl biRelationService ;
76
62
private final BundleService bundleService ;
77
63
private final SessionUserService sessionUserService ;
78
64
private final OrgDevChecker orgDevChecker ;
@@ -85,6 +71,7 @@ public class BundleApiServiceImpl implements BundleApiService {
85
71
private final UserService userService ;
86
72
private final OrganizationService organizationService ;
87
73
private final FolderApiService folderApiService ;
74
+ private final ApplicationServiceImpl applicationServiceImpl ;
88
75
89
76
@ Override
90
77
public Mono <BundleInfoView > create (CreateBundleRequest createBundleRequest ) {
@@ -331,45 +318,15 @@ public Mono<Void> reorder(String bundleId, List<String> elementIds) {
331
318
*/
332
319
@ Override
333
320
public Flux <?> getElements (@ Nullable String bundleId , @ Nullable ApplicationType applicationType ) {
334
- return buildApplicationInfoViewTree (applicationType )
335
- .flatMap (tree -> {
336
- BundleNode <ApplicationInfoView , BundleInfoView > bundleNode = tree .get (bundleId );
337
- if (bundleNode == null ) {
338
- return Mono .error (new BizException (BUNDLE_NOT_EXIST , "BUNDLE_NOT_EXIST" , bundleId ));
339
- }
340
- return Mono .just (bundleNode );
341
- })
342
- .zipWith (Mono .zip (sessionUserService .getVisitorOrgMemberCache (), orgDevChecker .isCurrentOrgDev ()))
343
- .doOnNext (tuple -> {
344
- BundleNode <ApplicationInfoView , BundleInfoView > node = tuple .getT1 ();
345
- OrgMember orgMember = tuple .getT2 ().getT1 ();
346
- boolean devOrAdmin = tuple .getT2 ().getT2 ();
347
- // father bundle's visibility depends on child nodes
348
- node .postOrderIterate (n -> {
349
- if (n instanceof BundleNode <ApplicationInfoView , BundleInfoView > bundleNode ) {
350
- BundleInfoView bundleInfoView = bundleNode .getSelf ();
351
- if (bundleInfoView == null ) {
352
- return ;
353
- }
354
- bundleInfoView .setManageable (orgMember .isAdmin () || orgMember .isSuperAdmin () || orgMember .getUserId ().equals (bundleInfoView .getCreateBy ()));
355
- bundleInfoView .setSubApplications (bundleNode .getElementChildren ());
356
- bundleInfoView .setVisible (devOrAdmin || isNotEmpty (bundleInfoView .getSubApplications ()));
357
- }
358
- });
359
- })
360
- .flatMapIterable (tuple -> tuple .getT1 ().getChildren ())
361
- .map (node -> {
362
- if (node instanceof ElementNode <ApplicationInfoView , BundleInfoView > elementNode ) {
363
- return elementNode .getSelf ();
364
- }
365
- return ((BundleNode <ApplicationInfoView , BundleInfoView >) node ).getSelf ();
366
- });
367
- }
368
-
369
- private Mono <Tree <Object , Bundle >> buildBundleTree (String userId ) {
370
- return bundleService .findByUserId (userId )
371
- .collectList ()
372
- .map (bundles -> new Tree <>(bundles , Bundle ::getId , __ -> null , Collections .emptyList (), null , null ));
321
+ return biRelationService .getBySourceId (BiRelationBizType .BUNDLE_ELEMENT , bundleId )
322
+ .sort ((o1 , o2 ) -> {
323
+ var pos1 = Integer .parseInt (o1 .getExtParam1 ());
324
+ var pos2 = Integer .parseInt (o2 .getExtParam1 ());
325
+ return pos1 - pos2 ;
326
+ }).map (bi -> applicationServiceImpl .findById (bi .getTargetId ()))
327
+ .index ()
328
+ .publishOn (Schedulers .boundedElastic ())
329
+ .map (tuple -> new BundleApplication (tuple .getT2 ().block (), tuple .getT1 ()));
373
330
}
374
331
375
332
@ Override
@@ -440,62 +397,6 @@ public Mono<ResourcePermission> checkBundlePermissionWithReadableErrorMsg(String
440
397
});
441
398
}
442
399
443
- private Mono <Tree <ApplicationInfoView , BundleInfoView >> buildApplicationInfoViewTree (@ Nullable ApplicationType applicationType ) {
444
-
445
- Mono <OrgMember > orgMemberMono = sessionUserService .getVisitorOrgMemberCache ()
446
- .cache ();
447
-
448
- Flux <ApplicationInfoView > applicationInfoViewFlux =
449
- userHomeApiService .getAllAuthorisedApplications4CurrentOrgMember (applicationType , ApplicationStatus .NORMAL , false )
450
- .cache ();
451
-
452
- Mono <Map <String , String >> application2BundleMapMono = applicationInfoViewFlux
453
- .map (ApplicationInfoView ::getApplicationId )
454
- .collectList ()
455
- .flatMapMany (applicationIds -> bundleElementRelationService .getByElementIds (applicationIds ))
456
- .collectMap (BundleElement ::elementId , BundleElement ::bundleId );
457
-
458
- Flux <Bundle > bundleFlux = orgMemberMono .flatMapMany (orgMember -> bundleService .findByUserId (orgMember .getUserId ()))
459
- .cache ();
460
-
461
- Mono <Map <String , User >> userMapMono = bundleFlux
462
- .flatMap (bundle -> emptyIfNull (bundle .getCreatedBy ()))
463
- .collectList ()
464
- .flatMap (list -> userService .getByIds (list ))
465
- .cache ();
466
-
467
- Flux <BundleInfoView > bundleInfoViewFlux = bundleFlux
468
- .flatMap (bundle -> Mono .zip (orgMemberMono , userMapMono )
469
- .map (tuple -> {
470
- OrgMember orgMember = tuple .getT1 ();
471
- Map <String , User > userMap = tuple .getT2 ();
472
- User creator = userMap .get (bundle .getCreatedBy ());
473
- return BundleInfoView .builder ()
474
- .userId (orgMember .getUserId ())
475
- .bundleId (bundle .getId ())
476
- .name (bundle .getName ())
477
- .createAt (bundle .getCreatedAt ().toEpochMilli ())
478
- .createBy (creator == null ? null : creator .getName ())
479
- .createTime (bundle .getCreatedAt ())
480
- .build ();
481
- }));
482
-
483
- return Mono .zip (applicationInfoViewFlux .collectList (),
484
- application2BundleMapMono ,
485
- bundleInfoViewFlux .collectList ())
486
- .map (tuple -> {
487
- List <ApplicationInfoView > applicationInfoViews = tuple .getT1 ();
488
- Map <String , String > application2BundleMap = tuple .getT2 ();
489
- List <BundleInfoView > bundleInfoViews = tuple .getT3 ();
490
- return new Tree <>(bundleInfoViews ,
491
- BundleInfoView ::getBundleId ,
492
- __ -> null ,
493
- applicationInfoViews ,
494
- application -> application2BundleMap .get (application .getApplicationId ()),
495
- DEFAULT_COMPARATOR );
496
- });
497
- }
498
-
499
400
/**
500
401
* only bundle creator has manage permissions
501
402
*/
0 commit comments