Skip to content

Commit 0b7cbf1

Browse files
committed
google sheet support delete row action
1 parent 40a3a9c commit 0b7cbf1

File tree

16 files changed

+289
-174
lines changed

16 files changed

+289
-174
lines changed

server/api-service/openblocks-domain/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,25 @@
271271
</plugins>
272272
</build>
273273

274+
<profiles>
275+
<profile>
276+
<id>selfhost-openblocks</id>
277+
<build>
278+
<plugins>
279+
<plugin>
280+
<groupId>org.apache.maven.plugins</groupId>
281+
<artifactId>maven-compiler-plugin</artifactId>
282+
<configuration>
283+
<excludes>
284+
<exclude>**/*MajiangVersion.java</exclude>
285+
</excludes>
286+
</configuration>
287+
</plugin>
288+
</plugins>
289+
</build>
290+
</profile>
291+
</profiles>
292+
274293
<properties>
275294
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
276295
<java.version>17</java.version>

server/api-service/openblocks-infra/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,24 @@
137137

138138
</dependencies>
139139

140+
<profiles>
141+
<profile>
142+
<id>selfhost-openblocks</id>
143+
<build>
144+
<plugins>
145+
<plugin>
146+
<groupId>org.apache.maven.plugins</groupId>
147+
<artifactId>maven-compiler-plugin</artifactId>
148+
<configuration>
149+
<excludes>
150+
<exclude>**/*MajiangVersion.java</exclude>
151+
</excludes>
152+
</configuration>
153+
</plugin>
154+
</plugins>
155+
</build>
156+
</profile>
157+
</profiles>
140158

141159
<properties>
142160
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

server/api-service/openblocks-plugins/googleSheetsPlugin/src/main/java/com/openblocks/plugin/googlesheets/GoogleSheetsPlugin.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static com.openblocks.plugin.googlesheets.GoogleSheetError.GOOGLESHEETS_REQUEST_ERROR;
66
import static com.openblocks.plugin.googlesheets.queryhandler.GoogleSheetsActionHandler.APPEND_DATA;
77
import static com.openblocks.plugin.googlesheets.queryhandler.GoogleSheetsActionHandler.CLEAR_DATA;
8+
import static com.openblocks.plugin.googlesheets.queryhandler.GoogleSheetsActionHandler.DELETE_DATA;
89
import static com.openblocks.plugin.googlesheets.queryhandler.GoogleSheetsActionHandler.READ_DATA;
910
import static com.openblocks.plugin.googlesheets.queryhandler.GoogleSheetsActionHandler.UPDATE_DATA;
1011
import static com.openblocks.sdk.util.JsonUtils.fromJson;
@@ -29,6 +30,7 @@
2930
import com.openblocks.plugin.googlesheets.model.GoogleSheetsAppendDataRequest;
3031
import com.openblocks.plugin.googlesheets.model.GoogleSheetsClearDataRequst;
3132
import com.openblocks.plugin.googlesheets.model.GoogleSheetsDatasourceConfig;
33+
import com.openblocks.plugin.googlesheets.model.GoogleSheetsDeleteDataRequest;
3234
import com.openblocks.plugin.googlesheets.model.GoogleSheetsQueryExecutionContext;
3335
import com.openblocks.plugin.googlesheets.model.GoogleSheetsReadDataRequest;
3436
import com.openblocks.plugin.googlesheets.model.GoogleSheetsUpdateDataRequest;
@@ -99,6 +101,7 @@ private GoogleSheetsActionRequest parseGoogleSheetsActionRequest(String actionTy
99101
case READ_DATA -> GoogleSheetsReadDataRequest.class;
100102
case UPDATE_DATA -> GoogleSheetsUpdateDataRequest.class;
101103
case CLEAR_DATA -> GoogleSheetsClearDataRequst.class;
104+
case DELETE_DATA -> GoogleSheetsDeleteDataRequest.class;
102105
default -> throw new PluginException(GOOGLESHEETS_EMPTY_QUERY_PARAM, "GOOGLESHEETS_QUERY_PARAM_EMPTY");
103106
};
104107
GoogleSheetsActionRequest result = fromJson(toJson(comp), requestClass);
@@ -146,9 +149,11 @@ public Mono<QueryExecutionResult> executeQuery(Object o, GoogleSheetsQueryExecut
146149
String actionType = context.getActionType();
147150
GoogleSheetsActionHandler googleSheetsActionHandler = GoogleSheetsActionHandlerFactory.getGoogleSheetsActionHandler(actionType);
148151
return googleSheetsActionHandler.execute(o, context)
149-
.onErrorResume(
150-
e -> Mono.just(QueryExecutionResult.error(GOOGLESHEETS_REQUEST_ERROR, "GOOGLESHEETS_REQUEST_ERROR", e.getMessage())))
151-
.subscribeOn(scheduler);
152+
.onErrorResume(e -> {
153+
log.error("google sheet execute error", e);
154+
return Mono.just(QueryExecutionResult.error(GOOGLESHEETS_REQUEST_ERROR, "GOOGLESHEETS_REQUEST_ERROR",
155+
e.getMessage()));
156+
});
152157
}
153158

154159
}

server/api-service/openblocks-plugins/googleSheetsPlugin/src/main/java/com/openblocks/plugin/googlesheets/queryhandler/GoogleSheetsActionHandlerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public class GoogleSheetsActionHandlerFactory {
1717
HANDLER_MAP = Lists.newArrayList(new GoogleSheetsReadDataHandler(),
1818
new GoogleSheetsAppendDataHandler(),
1919
new GoogleSheetsUpdateDataHandler(),
20-
new GoogleSheetsClearDataHandler())
20+
new GoogleSheetsClearDataHandler(),
21+
new GoogleSheetsDeleteDataHandler())
2122
.stream()
2223
.collect(Collectors.toMap(GoogleSheetsActionHandler::getActionType, Function.identity()));
2324
}

server/api-service/openblocks-plugins/googleSheetsPlugin/src/main/java/com/openblocks/plugin/googlesheets/queryhandler/GoogleSheetsAppendDataHandler.java

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33

44
import static com.openblocks.plugin.googlesheets.GoogleSheetError.GOOGLESHEETS_EMPTY_QUERY_PARAM;
5-
import static com.openblocks.plugin.googlesheets.GoogleSheetError.GOOGLESHEETS_EXECUTION_ERROR;
6-
import static com.openblocks.sdk.models.QueryExecutionResult.error;
75

8-
import java.io.IOException;
96
import java.util.LinkedHashMap;
107
import java.util.LinkedList;
118
import java.util.List;
@@ -37,50 +34,46 @@ public String getActionType() {
3734

3835
@Override
3936
public Mono<QueryExecutionResult> execute(Object o, GoogleSheetsQueryExecutionContext context) {
37+
return Mono.fromCallable(() -> {
38+
GoogleSheetsAppendDataRequest googleSheetsActionRequest = (GoogleSheetsAppendDataRequest) context.getGoogleSheetsActionRequest();
39+
Sheets sheetService = GoogleSheetsGetPreParameters.GetSheetsService(context);
40+
SheetChangeSetRow changeSetItems = GoogleSheetsGetPreParameters.getChangeSet(context);
41+
List<List<Object>> values = sheetService.spreadsheets()
42+
.values()
43+
.get(googleSheetsActionRequest.getSpreadsheetId(), googleSheetsActionRequest.getSheetName())
44+
.execute()
45+
.getValues();
46+
List<List<Object>> collect = null;
47+
List<Object> firstRow = values.get(0);
4048

41-
GoogleSheetsAppendDataRequest googleSheetsActionRequest = (GoogleSheetsAppendDataRequest) context.getGoogleSheetsActionRequest();
42-
Sheets sheetService = GoogleSheetsGetPreParameters.GetSheetsService(context);
43-
SheetChangeSetRow changeSetItems = GoogleSheetsGetPreParameters.getChangeSet(context);
44-
List<List<Object>> values;
45-
try {
46-
values = sheetService.spreadsheets()
47-
.values()
48-
.get(googleSheetsActionRequest.getSpreadsheetId(), googleSheetsActionRequest.getSheetName())
49-
.execute()
50-
.getValues();
51-
} catch (IOException e) {
52-
throw new RuntimeException(e);
53-
}
54-
List<List<Object>> collect = null;
55-
List<Object> firstRow = values.get(0);
49+
String range = googleSheetsActionRequest.getSheetName() + "!" + "A1";
50+
if (firstRow != null && !firstRow.isEmpty()) {
51+
Map<String, String> tempMap = new LinkedHashMap<>();
52+
Map<String, String> newMap = new LinkedHashMap<>();
53+
boolean validValues = false;
54+
for (Object object : firstRow) {
55+
Streams.stream(changeSetItems.iterator()).forEach((entry) -> tempMap.put(entry.column(),
56+
String.valueOf(entry.renderedValue())));
57+
final String value = tempMap.getOrDefault(object, null);
58+
if (value != null) {
59+
validValues = true;
60+
}
61+
newMap.put((String) object, value);
62+
}
63+
if (Boolean.TRUE.equals(validValues)) {
64+
List<Object> row = Streams.stream(newMap.keySet().iterator())
65+
.map(entry -> tempMap.getOrDefault(entry, null) == null ? "" : tempMap.getOrDefault(entry, null))
66+
.collect(Collectors.toCollection(LinkedList::new));
67+
collect = List.of(row);
68+
} else {
69+
throw new PluginException(GOOGLESHEETS_EMPTY_QUERY_PARAM, "GOOGLESHEETS_QUERY_PARAM_EMPTY");
70+
}
71+
}
72+
ValueRange requestBody = new ValueRange();
73+
requestBody.setMajorDimension("ROWS");
74+
requestBody.setRange(range);
75+
requestBody.setValues(collect);
5676

57-
String range = googleSheetsActionRequest.getSheetName() + "!" + "A1";
58-
if (firstRow != null && !firstRow.isEmpty()) {
59-
Map<String, String> tempMap = new LinkedHashMap<>();
60-
Map<String, String> newMap = new LinkedHashMap<>();
61-
boolean validValues = false;
62-
for (Object object : firstRow) {
63-
Streams.stream(changeSetItems.iterator()).forEach((entry) -> tempMap.put(entry.column(), (String) entry.renderedValue()));
64-
final String value = tempMap.getOrDefault(object, null);
65-
if (value != null) {
66-
validValues = true;
67-
}
68-
newMap.put((String) object, value);
69-
}
70-
if (Boolean.TRUE.equals(validValues)) {
71-
List<Object> row = Streams.stream(newMap.keySet().iterator())
72-
.map(entry -> tempMap.getOrDefault(entry, null) == null ? "" : tempMap.getOrDefault(entry, null))
73-
.collect(Collectors.toCollection(LinkedList::new));
74-
collect = List.of(row);
75-
} else {
76-
throw new PluginException(GOOGLESHEETS_EMPTY_QUERY_PARAM, "GOOGLESHEETS_QUERY_PARAM_EMPTY");
77-
}
78-
}
79-
ValueRange requestBody = new ValueRange();
80-
requestBody.setMajorDimension("ROWS");
81-
requestBody.setRange(range);
82-
requestBody.setValues(collect);
83-
return Mono.fromCallable(() -> {
8477
Sheets.Spreadsheets.Values.Append request = sheetService.spreadsheets()
8578
.values()
8679
.append(googleSheetsActionRequest.getSpreadsheetId(), range, requestBody);
@@ -89,7 +82,6 @@ public Mono<QueryExecutionResult> execute(Object o, GoogleSheetsQueryExecutionCo
8982
AppendValuesResponse response = request.execute();
9083
return QueryExecutionResult.success(response.values());
9184
})
92-
.onErrorResume(e -> Mono.just(error(GOOGLESHEETS_EXECUTION_ERROR, "GOOGLESHEETS_EXECUTION_ERROR", e.getMessage())))
9385
.subscribeOn(QueryExecutionUtils.querySharedScheduler());
9486
}
9587
}

server/api-service/openblocks-plugins/googleSheetsPlugin/src/main/java/com/openblocks/plugin/googlesheets/queryhandler/GoogleSheetsClearDataHandler.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.openblocks.plugin.googlesheets.queryhandler;
22

3-
import static com.openblocks.plugin.googlesheets.GoogleSheetError.GOOGLESHEETS_EXECUTION_ERROR;
4-
import static com.openblocks.sdk.models.QueryExecutionResult.error;
5-
63
import com.google.api.services.sheets.v4.Sheets;
74
import com.google.api.services.sheets.v4.model.ClearValuesRequest;
85
import com.google.api.services.sheets.v4.model.ClearValuesResponse;
@@ -33,7 +30,6 @@ public Mono<QueryExecutionResult> execute(Object o, GoogleSheetsQueryExecutionCo
3330
ClearValuesResponse response = request.execute();
3431
return QueryExecutionResult.success(response.values());
3532
})
36-
.onErrorResume(e -> Mono.just(error(GOOGLESHEETS_EXECUTION_ERROR, "GOOGLESHEETS_EXECUTION_ERROR", e.getMessage())))
3733
.subscribeOn(QueryExecutionUtils.querySharedScheduler());
3834
}
3935
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.openblocks.plugin.googlesheets.queryhandler;
22

33

4-
import static com.openblocks.plugin.googlesheets.GoogleSheetError.GOOGLESHEETS_EXECUTION_ERROR;
5-
import static com.openblocks.sdk.models.QueryExecutionResult.error;
4+
import java.util.ArrayList;
5+
import java.util.List;
66

77
import com.google.api.services.sheets.v4.Sheets;
8-
import com.google.api.services.sheets.v4.model.ClearValuesRequest;
9-
import com.google.api.services.sheets.v4.model.ClearValuesResponse;
8+
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest;
9+
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetResponse;
10+
import com.google.api.services.sheets.v4.model.DeleteDimensionRequest;
11+
import com.google.api.services.sheets.v4.model.DimensionRange;
12+
import com.google.api.services.sheets.v4.model.Request;
13+
import com.google.api.services.sheets.v4.model.Sheet;
14+
import com.google.api.services.sheets.v4.model.SheetProperties;
1015
import com.openblocks.plugin.googlesheets.model.GoogleSheetsDeleteDataRequest;
1116
import com.openblocks.plugin.googlesheets.model.GoogleSheetsQueryExecutionContext;
1217
import com.openblocks.sdk.models.QueryExecutionResult;
@@ -24,17 +29,34 @@ public String getActionType() {
2429
@Override
2530
public Mono<QueryExecutionResult> execute(Object o, GoogleSheetsQueryExecutionContext context) {
2631
GoogleSheetsDeleteDataRequest googleSheetsActionRequest = (GoogleSheetsDeleteDataRequest) context.getGoogleSheetsActionRequest();
27-
final int rowDelete = googleSheetsActionRequest.getRowIndex() + 1;
32+
final int rowDeleteIndex = googleSheetsActionRequest.getRowIndex() + 1;
2833
Sheets sheetService = GoogleSheetsGetPreParameters.GetSheetsService(context);
29-
String range = googleSheetsActionRequest.getSheetName() + "!" + rowDelete + ":" + rowDelete;
30-
ClearValuesRequest requestBody = new ClearValuesRequest();
3134
return Mono.fromCallable(() -> {
32-
Sheets.Spreadsheets.Values.Clear request =
33-
sheetService.spreadsheets().values().clear(googleSheetsActionRequest.getSpreadsheetId(), range, requestBody);
34-
ClearValuesResponse response = request.execute();
35+
int sheetId = sheetService.spreadsheets().get(googleSheetsActionRequest.getSpreadsheetId()).execute()
36+
.getSheets()
37+
.stream()
38+
.map(Sheet::getProperties)
39+
.filter(sheetProperties -> sheetProperties.getTitle().equals(googleSheetsActionRequest.getSheetName()))
40+
.map(SheetProperties::getSheetId)
41+
.findFirst()
42+
.orElse(0);
43+
BatchUpdateSpreadsheetRequest content = new BatchUpdateSpreadsheetRequest();
44+
Request request = new Request();
45+
DeleteDimensionRequest deleteDimensionRequest = new DeleteDimensionRequest();
46+
DimensionRange dimensionRange = new DimensionRange();
47+
dimensionRange.setDimension("ROWS");
48+
dimensionRange.setStartIndex(rowDeleteIndex - 1);
49+
dimensionRange.setEndIndex(rowDeleteIndex);
50+
dimensionRange.setSheetId(sheetId);
51+
deleteDimensionRequest.setRange(dimensionRange);
52+
request.setDeleteDimension(deleteDimensionRequest);
53+
List<Request> requests = new ArrayList<>();
54+
requests.add(request);
55+
content.setRequests(requests);
56+
BatchUpdateSpreadsheetResponse response =
57+
sheetService.spreadsheets().batchUpdate(googleSheetsActionRequest.getSpreadsheetId(), content).execute();
3558
return QueryExecutionResult.success(response.values());
3659
})
37-
.onErrorResume(e -> Mono.just(error(GOOGLESHEETS_EXECUTION_ERROR, "GOOGLESHEETS_EXECUTION_ERROR", e.getMessage())))
3860
.subscribeOn(QueryExecutionUtils.querySharedScheduler());
3961
}
4062
}

server/api-service/openblocks-plugins/googleSheetsPlugin/src/main/java/com/openblocks/plugin/googlesheets/queryhandler/GoogleSheetsReadDataHandler.java

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package com.openblocks.plugin.googlesheets.queryhandler;
22

33

4-
import static com.openblocks.plugin.googlesheets.GoogleSheetError.GOOGLESHEETS_EXECUTION_ERROR;
5-
import static com.openblocks.sdk.models.QueryExecutionResult.error;
6-
7-
import java.io.IOException;
84
import java.util.LinkedHashMap;
95
import java.util.LinkedHashSet;
106
import java.util.LinkedList;
@@ -14,13 +10,10 @@
1410
import java.util.regex.Matcher;
1511
import java.util.regex.Pattern;
1612

17-
import com.google.api.client.http.HttpRequestInitializer;
1813
import com.google.api.client.json.JsonFactory;
1914
import com.google.api.client.json.gson.GsonFactory;
2015
import com.google.api.services.sheets.v4.Sheets;
2116
import com.google.api.services.sheets.v4.model.ValueRange;
22-
import com.google.auth.http.HttpCredentialsAdapter;
23-
import com.google.auth.oauth2.GoogleCredentials;
2417
import com.openblocks.plugin.googlesheets.constants.FieldName;
2518
import com.openblocks.plugin.googlesheets.model.GoogleSheetsQueryExecutionContext;
2619
import com.openblocks.plugin.googlesheets.model.GoogleSheetsReadDataRequest;
@@ -41,24 +34,17 @@ public String getActionType() {
4134

4235
@Override
4336
public Mono<QueryExecutionResult> execute(Object o, GoogleSheetsQueryExecutionContext context) {
44-
GoogleSheetsReadDataRequest googleSheetsActionRequest = (GoogleSheetsReadDataRequest) context.getGoogleSheetsActionRequest();
45-
final GoogleCredentials googleCredentials = context.getServiceAccountCredentials();
46-
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(googleCredentials);
47-
Sheets service = GoogleSheetsGetPreParameters.GetSheetsService(context);
48-
ValueRange valueRange;
49-
try {
50-
valueRange = service.spreadsheets()
51-
.values()
52-
.get(googleSheetsActionRequest.getSpreadsheetId(), googleSheetsActionRequest.getSheetName())
53-
.execute();
54-
} catch (IOException e) {
55-
throw new RuntimeException(e);
56-
}
57-
List<Map<String, String>> result = transformToFinalValues(valueRange);
5837
return Mono.fromCallable(() -> {
38+
GoogleSheetsReadDataRequest googleSheetsActionRequest = (GoogleSheetsReadDataRequest) context.getGoogleSheetsActionRequest();
39+
Sheets service = GoogleSheetsGetPreParameters.GetSheetsService(context);
40+
ValueRange valueRange;
41+
valueRange = service.spreadsheets()
42+
.values()
43+
.get(googleSheetsActionRequest.getSpreadsheetId(), googleSheetsActionRequest.getSheetName())
44+
.execute();
45+
List<Map<String, String>> result = transformToFinalValues(valueRange);
5946
return QueryExecutionResult.success(result);
6047
})
61-
.onErrorResume(e -> Mono.just(error(GOOGLESHEETS_EXECUTION_ERROR, "GOOGLESHEETS_EXECUTION_ERROR", e.getMessage())))
6248
.subscribeOn(QueryExecutionUtils.querySharedScheduler());
6349
}
6450

0 commit comments

Comments
 (0)