Skip to content

Commit 6cff307

Browse files
dragonpooludomikula
authored andcommitted
Added before/after detail of library query update event
1 parent eefe884 commit 6cff307

File tree

4 files changed

+70
-16
lines changed

4 files changed

+70
-16
lines changed

server/api-service/lowcoder-infra/src/main/java/org/lowcoder/infra/event/LibraryQueryEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class LibraryQueryEvent extends AbstractEvent {
1010
private String id;
1111
private String name;
1212
private EventType eventType;
13+
private String oldName;
1314

1415
@Override
1516
public EventType getEventType() {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.lowcoder.infra.event;
2+
3+
import lombok.Getter;
4+
import lombok.experimental.SuperBuilder;
5+
6+
@Getter
7+
@SuperBuilder
8+
public class LibraryQueryPublishEvent extends AbstractEvent {
9+
10+
private String id;
11+
private String oldVersion;
12+
private String newVersion;
13+
private EventType eventType;
14+
15+
@Override
16+
public EventType getEventType() {
17+
return eventType;
18+
}
19+
}

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/query/LibraryQueryController.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44

5+
import org.lowcoder.api.datasource.UpsertDatasourceRequest;
56
import org.lowcoder.api.framework.view.PageResponseView;
67
import org.lowcoder.api.framework.view.ResponseView;
78
import org.lowcoder.api.query.view.LibraryQueryAggregateView;
@@ -11,7 +12,10 @@
1112
import org.lowcoder.api.query.view.UpsertLibraryQueryRequest;
1213
import org.lowcoder.api.util.BusinessEventPublisher;
1314
import org.lowcoder.api.util.GidService;
15+
import org.lowcoder.domain.datasource.model.Datasource;
1416
import org.lowcoder.domain.query.model.LibraryQuery;
17+
import org.lowcoder.domain.query.model.LibraryQueryRecord;
18+
import org.lowcoder.domain.query.service.LibraryQueryRecordService;
1519
import org.lowcoder.domain.query.service.LibraryQueryService;
1620
import org.lowcoder.plugin.api.event.LowcoderEvent.EventType;
1721
import org.springframework.beans.factory.annotation.Autowired;
@@ -22,8 +26,10 @@
2226

2327
import reactor.core.publisher.Flux;
2428
import reactor.core.publisher.Mono;
29+
import reactor.util.function.Tuple2;
2530

2631
import static org.lowcoder.api.util.Pagination.fluxToPageResponseView;
32+
import static org.lowcoder.plugin.api.event.LowcoderEvent.EventType.DATA_SOURCE_UPDATE;
2733

2834
@RestController
2935
public class LibraryQueryController implements LibraryQueryEndpoints
@@ -37,6 +43,8 @@ public class LibraryQueryController implements LibraryQueryEndpoints
3743
private BusinessEventPublisher businessEventPublisher;
3844
@Autowired
3945
private GidService gidService;
46+
@Autowired
47+
private LibraryQueryRecordService libraryQueryRecordService;
4048

4149
@Override
4250
public Mono<ResponseView<List<LibraryQueryAggregateView>>> dropDownList(@RequestParam(required = false, defaultValue = "") String name) {
@@ -64,16 +72,20 @@ public Mono<ResponseView<LibraryQueryView>> create(@RequestBody LibraryQuery lib
6472
return libraryQueryApiService.create(libraryQuery)
6573
.delayUntil(libraryQueryView ->
6674
businessEventPublisher.publishLibraryQueryEvent(libraryQueryView.id(), libraryQueryView.name(),
67-
EventType.LIBRARY_QUERY_CREATE))
75+
EventType.LIBRARY_QUERY_CREATE, null))
6876
.map(ResponseView::success);
6977
}
7078

7179
@Override
7280
public Mono<ResponseView<Boolean>> update(@PathVariable String libraryQueryId,
73-
@RequestBody UpsertLibraryQueryRequest upsertLibraryQueryRequest) {
81+
@RequestBody UpsertLibraryQueryRequest request) {
7482
return gidService.convertLibraryQueryIdToObjectId(libraryQueryId).flatMap(objectId ->
75-
libraryQueryApiService.update(objectId, upsertLibraryQueryRequest)
76-
.map(ResponseView::success));
83+
libraryQueryService.getById(objectId).flatMap(orgLibraryQuery ->
84+
libraryQueryApiService.update(objectId, request)
85+
.zipWith( libraryQueryService.getById(objectId))
86+
.delayUntil(tuple -> businessEventPublisher.publishLibraryQueryEvent(tuple.getT2().getId(), tuple.getT2().getName(), EventType.LIBRARY_QUERY_UPDATE, orgLibraryQuery.getName()))
87+
.map(Tuple2::getT1)
88+
.map(ResponseView::success)));
7789
}
7890

7991
@Override
@@ -82,18 +94,19 @@ public Mono<ResponseView<Boolean>> delete(@PathVariable String libraryQueryId) {
8294
libraryQueryService.getById(objectId)
8395
.delayUntil(__ -> libraryQueryApiService.delete(objectId))
8496
.delayUntil(libraryQuery -> businessEventPublisher.publishLibraryQueryEvent(libraryQuery.getId(), libraryQuery.getName(),
85-
EventType.LIBRARY_QUERY_DELETE))
97+
EventType.LIBRARY_QUERY_DELETE, libraryQuery.getName()))
8698
.thenReturn(ResponseView.success(true)));
8799
}
88100

89101
@Override
90102
public Mono<ResponseView<LibraryQueryRecordMetaView>> publish(@PathVariable String libraryQueryId,
91103
@RequestBody LibraryQueryPublishRequest libraryQueryPublishRequest) {
92104
return gidService.convertLibraryQueryIdToObjectId(libraryQueryId).flatMap(objectId ->
93-
libraryQueryApiService.publish(objectId, libraryQueryPublishRequest)
94-
.delayUntil(__ -> libraryQueryService.getById(objectId)
95-
.flatMap(libraryQuery -> businessEventPublisher.publishLibraryQuery(libraryQuery, EventType.LIBRARY_QUERY_PUBLISH)))
96-
.map(ResponseView::success));
105+
libraryQueryRecordService.getLatestRecordByLibraryQueryId(objectId).map(LibraryQueryRecord::getTag).defaultIfEmpty("").flatMap(oldVersion ->
106+
libraryQueryApiService.publish(objectId, libraryQueryPublishRequest)
107+
.delayUntil(__ -> libraryQueryService.getById(objectId)
108+
.flatMap(libraryQuery -> businessEventPublisher.publishLibraryQueryPublishEvent(libraryQueryId, oldVersion.isEmpty()?null:oldVersion, libraryQueryPublishRequest.tag(), EventType.LIBRARY_QUERY_PUBLISH)))
109+
.map(ResponseView::success)));
97110
}
98111

99112
}

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/util/BusinessEventPublisher.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929
import org.lowcoder.domain.query.model.LibraryQuery;
3030
import org.lowcoder.domain.user.model.User;
3131
import org.lowcoder.domain.user.service.UserService;
32-
import org.lowcoder.infra.event.ApplicationCommonEvent;
33-
import org.lowcoder.infra.event.FolderCommonEvent;
34-
import org.lowcoder.infra.event.LibraryQueryEvent;
35-
import org.lowcoder.infra.event.QueryExecutionEvent;
32+
import org.lowcoder.infra.event.*;
3633
import org.lowcoder.infra.event.datasource.DatasourceEvent;
3734
import org.lowcoder.infra.event.datasource.DatasourcePermissionEvent;
3835
import org.lowcoder.infra.event.group.GroupCreateEvent;
@@ -766,11 +763,34 @@ public Mono<Void> publishDatasourcePermissionEvent(String datasourceId,
766763
});
767764
}
768765

769-
public Mono<Void> publishLibraryQuery(LibraryQuery libraryQuery, EventType eventType) {
770-
return publishLibraryQueryEvent(libraryQuery.getId(), libraryQuery.getName(), eventType);
766+
public Mono<Void> publishLibraryQueryPublishEvent(String id, String oldVersion, String newVersion, EventType eventType) {
767+
return sessionUserService.getVisitorOrgMemberCache()
768+
.zipWith(sessionUserService.getVisitorToken())
769+
.flatMap(tuple -> {
770+
LibraryQueryPublishEvent event = LibraryQueryPublishEvent.builder()
771+
.id(id)
772+
.oldVersion(oldVersion)
773+
.newVersion(newVersion)
774+
.eventType(eventType)
775+
.userId(tuple.getT1().getUserId())
776+
.orgId(tuple.getT1().getOrgId())
777+
.isAnonymous(Authentication.isAnonymousUser(tuple.getT1().getUserId()))
778+
.sessionHash(Hashing.sha512().hashString(tuple.getT2(), StandardCharsets.UTF_8).toString())
779+
.build();
780+
return Mono.deferContextual(contextView -> {
781+
event.populateDetails(contextView);
782+
applicationEventPublisher.publishEvent(event);
783+
return Mono.<Void>empty();
784+
});
785+
})
786+
.then()
787+
.onErrorResume(throwable -> {
788+
log.error("publishLibraryQueryPublishEvent error.", throwable);
789+
return Mono.empty();
790+
});
771791
}
772792

773-
public Mono<Void> publishLibraryQueryEvent(String id, String name, EventType eventType) {
793+
public Mono<Void> publishLibraryQueryEvent(String id, String name, EventType eventType, String oldName) {
774794
return sessionUserService.getVisitorOrgMemberCache()
775795
.zipWith(sessionUserService.getVisitorToken())
776796
.flatMap(tuple -> {
@@ -779,6 +799,7 @@ public Mono<Void> publishLibraryQueryEvent(String id, String name, EventType eve
779799
.orgId(tuple.getT1().getOrgId())
780800
.id(id)
781801
.name(name)
802+
.oldName(oldName)
782803
.eventType(eventType)
783804
.isAnonymous(Authentication.isAnonymousUser(tuple.getT1().getUserId()))
784805
.sessionHash(Hashing.sha512().hashString(tuple.getT2(), StandardCharsets.UTF_8).toString())

0 commit comments

Comments
 (0)