Skip to content

Commit 3c766f5

Browse files
committed
refactor: Return first page and total count when searching for Confluence spaces, pages and attachments.
1 parent f312a0b commit 3c766f5

File tree

8 files changed

+90
-73
lines changed

8 files changed

+90
-73
lines changed

plugins/confluence/src/main/java/com/github/vogoltsov/vp/plugins/confluence/client/ConfluenceAttachmentRepository.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.github.vogoltsov.vp.plugins.confluence.client.dto.CreateAttachmentResponse;
5+
import com.github.vogoltsov.vp.plugins.confluence.client.dto.DataPage;
56
import com.github.vogoltsov.vp.plugins.confluence.client.dto.ListAttachmentsResponse;
67
import com.github.vogoltsov.vp.plugins.confluence.client.model.Attachment;
78

89
import java.io.ByteArrayInputStream;
9-
import java.util.Collections;
1010
import java.util.Comparator;
11-
import java.util.List;
1211
import java.util.Objects;
1312
import java.util.stream.Collectors;
1413

@@ -25,13 +24,16 @@ public static ConfluenceAttachmentRepository getInstance() {
2524
/**
2625
* Searches attachments by content id and text.
2726
*/
28-
public List<Attachment> search(String pageId, String text) {
27+
public DataPage<Attachment> search(String pageId, String text) {
2928
String lowerCaseText = text.toLowerCase();
30-
return findAllAttachments(pageId)
31-
.stream()
32-
.filter(attachment -> attachment.getTitle().toLowerCase().contains(lowerCaseText))
33-
.sorted(Comparator.comparing(Attachment::getTitle))
34-
.collect(Collectors.toList());
29+
return DataPage.of(
30+
findAllAttachments(pageId)
31+
.getResults()
32+
.stream()
33+
.filter(attachment -> attachment.getTitle().toLowerCase().contains(lowerCaseText))
34+
.sorted(Comparator.comparing(Attachment::getTitle))
35+
.collect(Collectors.toList())
36+
);
3537
}
3638

3739
/**
@@ -42,6 +44,7 @@ public Attachment findByPageIdAndAttachmentId(String pageId, String attachmentId
4244
return null;
4345
}
4446
return findAllAttachments(pageId)
47+
.getResults()
4548
.stream()
4649
.filter(attachment -> Objects.equals(attachment.getId(), attachmentId))
4750
.findAny()
@@ -53,19 +56,17 @@ public Attachment findByPageIdAndAttachmentId(String pageId, String attachmentId
5356
* Returns all attachments for content.
5457
* This method has a soft limit of {@code 100} attachments returned.
5558
*/
56-
private List<Attachment> findAllAttachments(String pageId) {
59+
private DataPage<Attachment> findAllAttachments(String pageId) {
5760
if (pageId == null) {
58-
return Collections.emptyList();
61+
return DataPage.empty();
5962
}
6063
return ConfluenceClient.getInstance().get("/rest/api/content/{pageId}/child/attachment")
6164
.routeParam("pageId", pageId)
6265
.queryString("expand", "container,container.space")
63-
.queryString("limit", "100")
66+
.queryString("limit", Integer.MAX_VALUE)
6467
.asObject(JsonNode.class)
6568
.ifFailure(ConfluenceClient.getInstance()::handleFailureResponse)
66-
.mapBody(ConfluenceClient.getInstance().map(ListAttachmentsResponse.class).andThen(
67-
ListAttachmentsResponse::getResults
68-
));
69+
.mapBody(ConfluenceClient.getInstance().map(ListAttachmentsResponse.class));
6970
}
7071

7172

plugins/confluence/src/main/java/com/github/vogoltsov/vp/plugins/confluence/client/ConfluencePageRepository.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.github.vogoltsov.vp.plugins.confluence.client;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.github.vogoltsov.vp.plugins.confluence.client.dto.DataPage;
45
import com.github.vogoltsov.vp.plugins.confluence.client.dto.SearchResult;
56
import com.github.vogoltsov.vp.plugins.confluence.client.dto.SearchResults;
67
import com.github.vogoltsov.vp.plugins.confluence.client.model.Page;
78
import com.github.vogoltsov.vp.plugins.confluence.util.cql.CQL;
89
import com.github.vogoltsov.vp.plugins.confluence.util.cql.CQLQuery;
910

10-
import java.util.List;
11-
import java.util.stream.Collectors;
12-
1311
/**
1412
* @author Vitaly Ogoltsov &lt;vitaly.ogoltsov@me.com&gt;
1513
*/
@@ -23,7 +21,7 @@ public static ConfluencePageRepository getInstance() {
2321
/**
2422
* Searches space pages by title.
2523
*/
26-
public List<Page> findBySpaceKeyAndText(String spaceKey, String text) {
24+
public DataPage<Page> findBySpaceKeyAndText(String spaceKey, String text) {
2725
CQLQuery cql = CQL.query(CQL.eq("type", "page"));
2826
if (spaceKey != null && !spaceKey.isEmpty()) {
2927
cql.and(CQL.eq("space.key", spaceKey));
@@ -50,23 +48,20 @@ public Page findById(String pageId) {
5048
CQL.eq("type", "page"),
5149
CQL.eq("id", pageId)
5250
))
53-
).stream().findAny().orElse(null);
51+
).getResults().stream().findAny().orElse(null);
5452
}
5553

5654

57-
private List<Page> search(CQLQuery cql) {
55+
private DataPage<Page> search(CQLQuery cql) {
5856
// always expand content space
5957
cql.expand("content.space");
6058
return ConfluenceClient.getInstance().search(cql)
6159
.asObject(JsonNode.class)
6260
.ifFailure(ConfluenceClient.getInstance()::handleFailureResponse)
6361
.mapBody(ConfluenceClient.getInstance().map(SearchResults.class).andThen(
64-
searchResults -> searchResults.getResults().stream()
65-
.filter(SearchResult.ContentSearchResult.class::isInstance)
66-
.map(searchResult -> ((SearchResult.ContentSearchResult) searchResult).getContent())
67-
.filter(Page.class::isInstance)
68-
.map(content -> (Page) content)
69-
.collect(Collectors.toList())
62+
searchResults -> searchResults.map(
63+
searchResult -> (Page) ((SearchResult.ContentSearchResult) searchResult).getContent()
64+
)
7065
));
7166
}
7267

plugins/confluence/src/main/java/com/github/vogoltsov/vp/plugins/confluence/client/ConfluenceSpaceRepository.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
package com.github.vogoltsov.vp.plugins.confluence.client;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.github.vogoltsov.vp.plugins.confluence.client.dto.DataPage;
45
import com.github.vogoltsov.vp.plugins.confluence.client.dto.SearchResult;
56
import com.github.vogoltsov.vp.plugins.confluence.client.dto.SearchResults;
67
import com.github.vogoltsov.vp.plugins.confluence.client.model.Space;
78
import com.github.vogoltsov.vp.plugins.confluence.util.RemoteAPIException;
89
import com.github.vogoltsov.vp.plugins.confluence.util.cql.CQL;
910
import com.github.vogoltsov.vp.plugins.confluence.util.cql.CQLQuery;
1011

11-
import java.util.Collections;
12-
import java.util.List;
1312
import java.util.Objects;
14-
import java.util.stream.Collectors;
1513

1614
/**
1715
* @author Vitaly Ogoltsov &lt;vitaly.ogoltsov@me.com&gt;
@@ -26,7 +24,7 @@ public static ConfluenceSpaceRepository getInstance() {
2624
/**
2725
* Searches all spaces by title and key.
2826
*/
29-
public List<Space> search(String text) {
27+
public DataPage<Space> search(String text) {
3028
CQLQuery cql = CQL.query(
3129
CQL.eq("type", "space")
3230
);
@@ -53,26 +51,25 @@ public Space findByKey(String spaceKey) {
5351
CQL.eq("type", "space"),
5452
CQL.eq("space.key", spaceKey)
5553
))
56-
).stream().findAny().orElse(null);
54+
).getResults().stream().findAny().orElse(null);
5755
}
5856

5957

60-
private List<Space> search(CQLQuery cql) {
58+
private DataPage<Space> search(CQLQuery cql) {
6159
try {
6260
return ConfluenceClient.getInstance().search(cql)
6361
.asObject(JsonNode.class)
6462
.ifFailure(ConfluenceClient.getInstance()::handleFailureResponse)
6563
.mapBody(ConfluenceClient.getInstance().map(SearchResults.class).andThen(
66-
searchResults -> searchResults.getResults().stream()
67-
.filter(SearchResult.SpaceSearchResult.class::isInstance)
68-
.map(searchResult -> ((SearchResult.SpaceSearchResult) searchResult).getSpace())
69-
.collect(Collectors.toList())
64+
searchResults -> searchResults.map(
65+
searchResult -> ((SearchResult.SpaceSearchResult) searchResult).getSpace()
66+
)
7067
));
7168
} catch (RemoteAPIException e) {
7269
// this is a special case to mitigate bug in Confluence REST API
7370
// see https://jira.atlassian.com/browse/CONFSERVER-55445
7471
if (Objects.equals(e.getApiMessage(), "java.lang.IllegalArgumentException: parameters should not be empty")) {
75-
return Collections.emptyList();
72+
return DataPage.empty();
7673
}
7774
throw e;
7875
}
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
package com.github.vogoltsov.vp.plugins.confluence.client.dto;
22

3+
import lombok.AccessLevel;
4+
import lombok.AllArgsConstructor;
35
import lombok.Data;
6+
import lombok.NoArgsConstructor;
47

8+
import java.util.ArrayList;
9+
import java.util.Collections;
510
import java.util.List;
11+
import java.util.function.Function;
12+
import java.util.stream.Collectors;
613

714
/**
815
* @author Vitaly Ogoltsov &lt;vitaly.ogoltsov@me.com&gt;
916
*/
1017
@Data
11-
public abstract class DataPage<T> {
18+
@NoArgsConstructor
19+
@AllArgsConstructor(access = AccessLevel.PRIVATE)
20+
public class DataPage<T> {
21+
22+
public static <T> DataPage<T> empty() {
23+
return new DataPage<>(Collections.emptyList(), 0, Integer.MAX_VALUE, 0, 0);
24+
}
25+
26+
public static <T> DataPage<T> of(List<T> results) {
27+
return new DataPage<>(new ArrayList<>(results), 0, results.size(), results.size(), results.size());
28+
}
29+
1230

1331
private List<T> results;
1432

@@ -17,4 +35,13 @@ public abstract class DataPage<T> {
1735
private int size;
1836
private int totalSize;
1937

38+
39+
public List<T> getResults() {
40+
return Collections.unmodifiableList(results);
41+
}
42+
43+
public <V> DataPage<V> map(Function<T, V> mapper) {
44+
return new DataPage<>(results.stream().map(mapper).collect(Collectors.toList()), start, limit, size, totalSize);
45+
}
46+
2047
}

plugins/confluence/src/main/java/com/github/vogoltsov/vp/plugins/confluence/dialog/ConfluenceAttachmentChooserDialog.java

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

33
import com.github.vogoltsov.vp.plugins.common.swing.ListTableModel;
44
import com.github.vogoltsov.vp.plugins.confluence.client.ConfluenceAttachmentRepository;
5+
import com.github.vogoltsov.vp.plugins.confluence.client.dto.DataPage;
56
import com.github.vogoltsov.vp.plugins.confluence.client.model.Attachment;
67
import com.github.vogoltsov.vp.plugins.confluence.client.model.Page;
78
import com.github.vogoltsov.vp.plugins.confluence.util.swing.SearchChooserDialog;
89
import lombok.RequiredArgsConstructor;
910

1011
import java.util.Collections;
11-
import java.util.List;
1212

1313
/**
1414
* @author Vitaly Ogoltsov &lt;vitaly.ogoltsov@me.com&gt;
@@ -42,7 +42,7 @@ protected ListTableModel<Attachment> getResultsDataModel() {
4242

4343

4444
@Override
45-
protected List<Attachment> doSearch(String text) {
45+
protected DataPage<Attachment> doSearch(String text) {
4646
return ConfluenceAttachmentRepository.getInstance().search(
4747
this.page != null ? this.page.getId() : null,
4848
text

plugins/confluence/src/main/java/com/github/vogoltsov/vp/plugins/confluence/dialog/ConfluencePageChooserDialog.java

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

33
import com.github.vogoltsov.vp.plugins.common.swing.ListTableModel;
44
import com.github.vogoltsov.vp.plugins.confluence.client.ConfluencePageRepository;
5+
import com.github.vogoltsov.vp.plugins.confluence.client.dto.DataPage;
56
import com.github.vogoltsov.vp.plugins.confluence.client.model.Page;
67
import com.github.vogoltsov.vp.plugins.confluence.client.model.Space;
78
import com.github.vogoltsov.vp.plugins.confluence.util.swing.SearchChooserDialog;
89
import lombok.RequiredArgsConstructor;
910

1011
import java.util.Collections;
11-
import java.util.List;
1212

1313
/**
1414
* @author Vitaly Ogoltsov &lt;vitaly.ogoltsov@me.com&gt;
@@ -42,7 +42,7 @@ protected ListTableModel<Page> getResultsDataModel() {
4242

4343

4444
@Override
45-
protected List<Page> doSearch(String text) {
45+
protected DataPage<Page> doSearch(String text) {
4646
return ConfluencePageRepository.getInstance().findBySpaceKeyAndText(
4747
this.space != null ? this.space.getKey() : null,
4848
text

plugins/confluence/src/main/java/com/github/vogoltsov/vp/plugins/confluence/dialog/ConfluenceSpaceChooserDialog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import com.github.vogoltsov.vp.plugins.common.swing.HelpPanel;
44
import com.github.vogoltsov.vp.plugins.common.swing.ListTableModel;
55
import com.github.vogoltsov.vp.plugins.confluence.client.ConfluenceSpaceRepository;
6+
import com.github.vogoltsov.vp.plugins.confluence.client.dto.DataPage;
67
import com.github.vogoltsov.vp.plugins.confluence.client.model.Space;
78
import com.github.vogoltsov.vp.plugins.confluence.util.swing.SearchChooserDialog;
89

910
import java.util.Arrays;
10-
import java.util.List;
1111

1212
/**
1313
* @author Vitaly Ogoltsov &lt;vitaly.ogoltsov@me.com&gt;
@@ -47,7 +47,7 @@ protected ListTableModel<Space> getResultsDataModel() {
4747

4848

4949
@Override
50-
protected List<Space> doSearch(String text) {
50+
protected DataPage<Space> doSearch(String text) {
5151
return ConfluenceSpaceRepository.getInstance().search(text);
5252
}
5353

0 commit comments

Comments
 (0)