Skip to content

Commit 77d2ca2

Browse files
Thomasludomikula
Thomas
authored andcommitted
Remove bundleposition from application api
Fix bundle reordering
1 parent 95a6ba5 commit 77d2ca2

File tree

6 files changed

+31
-123
lines changed

6 files changed

+31
-123
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.lowcoder.domain.bundle.model;
2+
3+
import org.lowcoder.domain.application.model.Application;
4+
5+
public record BundleApplication(Application application, long position) {
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package org.lowcoder.domain.bundle.model;
22

3-
public record BundleElement(String bundleId, String elementId, int position) {
3+
public record BundleElement(String bundleId, String elementId, long position) {
44
}

server/api-service/lowcoder-domain/src/main/java/org/lowcoder/domain/bundle/service/BundleElementRelationServiceImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.stereotype.Service;
77
import reactor.core.publisher.Flux;
88
import reactor.core.publisher.Mono;
9+
import reactor.core.scheduler.Schedulers;
910

1011
import java.util.List;
1112
import java.util.Objects;
@@ -59,6 +60,8 @@ public Flux<BundleElement> getByElementIds(List<String> elementIds) {
5960
public Mono<Void> updateElementPos(String bundleId, String elementId, long position) {
6061
return biRelationService.getBiRelation(BUNDLE_ELEMENT, bundleId, elementId)
6162
.doOnNext(biRelation -> biRelation.setExtParam1(String.valueOf(position)))
63+
.publishOn(Schedulers.boundedElastic())
64+
.mapNotNull(biRelation -> biRelationService.upsert(biRelation).block())
6265
.then();
6366
}
6467
}

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/application/view/ApplicationInfoView.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public class ApplicationInfoView {
3636
private final boolean publicToAll;
3737
private final boolean publicToMarketplace;
3838
private final boolean agencyProfile;
39-
private final int bundlePosition;
4039

4140
public long getLastViewTime() {
4241
return lastViewTime == null ? 0 : lastViewTime.toEpochMilli();

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/bundle/BundleApiServiceImpl.java

Lines changed: 18 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
import org.lowcoder.api.usermanagement.OrgDevChecker;
1919
import org.lowcoder.domain.application.model.ApplicationStatus;
2020
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.*;
2423
import org.lowcoder.domain.bundle.service.BundleElementRelationService;
2524
import org.lowcoder.domain.bundle.service.BundleNode;
2625
import org.lowcoder.domain.bundle.service.BundleService;
27-
import org.lowcoder.domain.bundle.model.BundleElement;
2826
import org.lowcoder.domain.bundle.service.*;
2927
import org.lowcoder.domain.group.service.GroupService;
3028
import org.lowcoder.domain.organization.model.OrgMember;
@@ -34,12 +32,17 @@
3432
import org.lowcoder.domain.permission.service.ResourcePermissionService;
3533
import org.lowcoder.domain.user.model.User;
3634
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;
3738
import org.lowcoder.sdk.exception.BizError;
3839
import org.lowcoder.sdk.exception.BizException;
40+
import org.springframework.beans.factory.annotation.Autowired;
3941
import org.springframework.context.annotation.Lazy;
4042
import org.springframework.stereotype.Service;
4143
import reactor.core.publisher.Flux;
4244
import reactor.core.publisher.Mono;
45+
import reactor.core.scheduler.Schedulers;
4346

4447
import java.util.*;
4548
import java.util.function.Function;
@@ -55,24 +58,7 @@
5558
@RequiredArgsConstructor
5659
@Service
5760
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;
7662
private final BundleService bundleService;
7763
private final SessionUserService sessionUserService;
7864
private final OrgDevChecker orgDevChecker;
@@ -85,6 +71,7 @@ public class BundleApiServiceImpl implements BundleApiService {
8571
private final UserService userService;
8672
private final OrganizationService organizationService;
8773
private final FolderApiService folderApiService;
74+
private final ApplicationServiceImpl applicationServiceImpl;
8875

8976
@Override
9077
public Mono<BundleInfoView> create(CreateBundleRequest createBundleRequest) {
@@ -331,45 +318,15 @@ public Mono<Void> reorder(String bundleId, List<String> elementIds) {
331318
*/
332319
@Override
333320
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()));
373330
}
374331

375332
@Override
@@ -440,62 +397,6 @@ public Mono<ResourcePermission> checkBundlePermissionWithReadableErrorMsg(String
440397
});
441398
}
442399

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-
499400
/**
500401
* only bundle creator has manage permissions
501402
*/

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/home/UserHomeApiServiceImpl.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,10 @@ public Flux<ApplicationInfoView> getAllAuthorisedApplications4CurrentOrgMember(@
244244

245245
return bundleElementRelationServiceImpl.getByElementIds(List.of(Objects.requireNonNull(application.getId())))
246246
.mapNotNull(BundleElement::position)
247-
.defaultIfEmpty(0)
247+
.defaultIfEmpty(0L)
248248
.collectList()
249249
.flatMap(positions -> {
250-
int position = positions.isEmpty() ? 0 : positions.get(0);
250+
long position = positions.isEmpty() ? 0 : positions.get(0);
251251
ResourceRole resourceRole = resourcePermissionMap.get(application.getId()).getResourceRole();
252252
return Mono.just(buildView(application, resourceRole, userMap, applicationLastViewTimeMap.get(application.getId()),
253253
position, withContainerSize));
@@ -556,7 +556,7 @@ public Flux<MarketplaceBundleInfoView> getAllAgencyProfileBundles() {
556556
}
557557

558558
private ApplicationInfoView buildView(Application application, ResourceRole maxRole, Map<String, User> userMap, @Nullable Instant lastViewTime,
559-
Integer bundlePosition, boolean withContainerSize) {
559+
Long bundlePosition, boolean withContainerSize) {
560560
ApplicationInfoViewBuilder applicationInfoViewBuilder = ApplicationInfoView.builder()
561561
.applicationId(application.getId())
562562
.orgId(application.getOrganizationId())
@@ -570,7 +570,6 @@ private ApplicationInfoView buildView(Application application, ResourceRole maxR
570570
.applicationStatus(application.getApplicationStatus())
571571
.lastModifyTime(application.getUpdatedAt())
572572
.lastViewTime(lastViewTime)
573-
.bundlePosition(bundlePosition)
574573
.publicToAll(application.isPublicToAll())
575574
.publicToMarketplace(application.isPublicToMarketplace())
576575
.agencyProfile(application.agencyProfile());

0 commit comments

Comments
 (0)