Skip to content

Commit 6f05ca7

Browse files
authored
Merge pull request #241 from cloudimpl/global-update-child-method
global-update-child-method
2 parents 90ba0ac + 1fb9328 commit 6f05ca7

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

outstack-spring-repo/src/main/java/com/cloudimpl/outstack/repo/core/ReactiveRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ public interface ReactiveRepository extends ReadOnlyReactiveRepository{
1414
<T extends Entity> Mono<T> delete(String tenantId,String tid);
1515
<T extends Entity> Mono<T> createChild(String parentTenantId,String parentTid,String tenantId,T child);
1616
<T extends Entity> Mono<T> update(String tenantId, T entity, String id);
17+
<T extends Entity> Mono<T> updateChild(String parentTid, String tenantId, T child, String id);
1718
}

outstack-spring-repo/src/main/java/com/cloudimpl/outstack/repo/postgres/Postgres13ReactiveRepository.java

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public <T extends Entity> Mono<T> update(String tenantId, T entity, String id) {
6969
return createTenantIfNotExist(tenantId).flatMap(it -> it.executeMono(config.connectionFromPool(table.config(), tenantId), conn -> update(conn, tenantId, entity, id)));
7070
}
7171

72+
@Override
73+
public <T extends Entity> Mono<T> updateChild(String parentTid, String tenantId, T child, String id) {
74+
return createTenantIfNotExist(tenantId)
75+
.flatMap(it -> it.executeMono(config.connectionFromPool(table.config(), tenantId), conn -> updateChild(conn, parentTid, tenantId, child, id)));
76+
}
77+
7278
private void checkGeoEntity(Object child) {
7379
if (child instanceof GeoData) {
7480
if (!table.enableGeo()) {
@@ -232,6 +238,27 @@ private <T extends Entity> Mono<T> update(Connection connection, String tenantId
232238
long time = System.currentTimeMillis();
233239
boolean geoApplicable = table.enableGeo() && entity instanceof GeoData && GeoData.class.cast(entity).getGeom() != null;
234240
if (id.startsWith("id-")) {
241+
return Mono.just(connection).flatMapMany(conn -> {
242+
Statement stmt = conn.createStatement("update " + table.name() + " set uniqueid=$1, id=$2, entity = $3::JSON , updatedTime = $4 " +
243+
(geoApplicable ? " , geom = $8" : "") +
244+
"where parenttid is null and tid= $5 and tenantId= $6 and resourceType=$7 returning tenantId,createdTime,updatedTime,tid,resourceType,entity")
245+
.bind("$1", entity.id())
246+
.bind("$2", entity.id())
247+
.bind("$3", json)
248+
.bind("$4", time)
249+
.bind("$5", id)
250+
.bind("$6", tenantId == null ? "default" : tenantId)
251+
.bind("$7", entity.getClass().getName());
252+
if (geoApplicable) {
253+
GeoMetry geo = GeoData.class.cast(entity).getGeom();
254+
stmt.bind("$8", GeoUtil.convertToGeo(geo));
255+
}
256+
return stmt.execute();
257+
}).take(1)
258+
.flatMap(it -> it.map((row, meta) -> (T) (createEntity(row)))).next()
259+
.switchIfEmpty(Mono.defer(() -> Mono.error(new RepoException("entity not exist"))));
260+
261+
} else {
235262
return Mono.just(connection).flatMapMany(conn -> {
236263
Statement stmt = conn.createStatement("update " + table.name() + " set entity = $1::JSON , updatedTime = $2 " +
237264
(geoApplicable ? " , geom = $6" : "") +
@@ -249,6 +276,37 @@ private <T extends Entity> Mono<T> update(Connection connection, String tenantId
249276
}).take(1)
250277
.flatMap(it -> it.map((row, meta) -> (T) (createEntity(row)))).next()
251278
.switchIfEmpty(Mono.defer(() -> Mono.error(new RepoException("entity not exist"))));
279+
}
280+
281+
}
282+
283+
private <T extends Entity> Mono<T> updateChild(Connection connection, String parentTid, String tenantId, T child, String id) {
284+
Objects.requireNonNull(parentTid);
285+
checkGeoEntity(child);
286+
validateObject(child);
287+
String json = GsonCodec.encode(child);
288+
long time = System.currentTimeMillis();
289+
boolean geoApplicable = table.enableGeo() && child instanceof GeoData && GeoData.class.cast(child).getGeom() != null;
290+
if (id.startsWith("id-")) {
291+
return Mono.just(connection).flatMapMany(conn -> {
292+
Statement stmt = conn.createStatement("update " + table.name() + " set uniqueid=$1, id=$2, entity = $3::JSON , updatedTime = $4 " +
293+
(geoApplicable ? " , geom = $8" : "") +
294+
"where parenttid is not null and tid= $5 and tenantId= $6 and resourceType=$7 returning tenantId,createdTime,updatedTime,tid,resourceType,entity")
295+
.bind("$1",parentTid + "/" + child.id())
296+
.bind("$2", child.id())
297+
.bind("$3", json)
298+
.bind("$4", time)
299+
.bind("$5", id)
300+
.bind("$6", tenantId == null ? "default" : tenantId)
301+
.bind("$7", child.getClass().getName());
302+
if (geoApplicable) {
303+
GeoMetry geo = GeoData.class.cast(child).getGeom();
304+
stmt.bind("$8", GeoUtil.convertToGeo(geo));
305+
}
306+
return stmt.execute();
307+
}).take(1)
308+
.flatMap(it -> it.map((row, meta) -> (T) (createEntity(row)))).next()
309+
.switchIfEmpty(Mono.defer(() -> Mono.error(new RepoException("entity not exist"))));
252310

253311
} else {
254312
return Mono.just(connection).flatMapMany(conn -> {
@@ -259,17 +317,16 @@ private <T extends Entity> Mono<T> update(Connection connection, String tenantId
259317
.bind("$2", time)
260318
.bind("$3", id)
261319
.bind("$4", tenantId == null ? "default" : tenantId)
262-
.bind("$5", entity.getClass().getName());
320+
.bind("$5", child.getClass().getName());
263321
if (geoApplicable) {
264-
GeoMetry geo = GeoData.class.cast(entity).getGeom();
322+
GeoMetry geo = GeoData.class.cast(child).getGeom();
265323
stmt.bind("$6", GeoUtil.convertToGeo(geo));
266324
}
267325
return stmt.execute();
268326
}).take(1)
269327
.flatMap(it -> it.map((row, meta) -> (T) (createEntity(row)))).next()
270328
.switchIfEmpty(Mono.defer(() -> Mono.error(new RepoException("entity not exist"))));
271329
}
272-
273330
}
274331

275332
protected Mono<Postgres13ReactiveRepository> createTenantIfNotExist(String tenantId) {

0 commit comments

Comments
 (0)