Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import org.springframework.social.zotero.exception.ZoteroConnectionException;

public interface GroupCollectionsOperations extends ZoteroOperations {

ZoteroResponse<Collection> getTopCollections(String groupId, int start, int numberOfItems, String sortBy,
Expand All @@ -16,5 +18,7 @@ ZoteroResponse<Collection> getTopCollections(String groupId, int start, int numb
ZoteroResponse<Collection> getCollectionsVersions(String groupId, Long groupVersion);

ZoteroResponse<Collection> getCollectionsByKey(String groupId, List<String> keys);

ItemCreationResponse createCollection(String groupId, String collectionName, String parentCollection) throws ZoteroConnectionException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@
import org.springframework.social.zotero.api.Collection;
import org.springframework.social.zotero.api.GroupCollectionsOperations;
import org.springframework.social.zotero.api.Item;
import org.springframework.social.zotero.api.ItemCreationResponse;
import org.springframework.social.zotero.api.ZoteroRequestHeaders;
import org.springframework.social.zotero.api.ZoteroResponse;
import org.springframework.social.zotero.exception.ZoteroConnectionException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

public class GroupCollectionsTemplate extends AbstractZoteroOperations implements GroupCollectionsOperations {

Expand Down Expand Up @@ -199,4 +203,27 @@ private long getLatestVersion(HttpHeaders responseHeaders) {
}
return -1;
}

@Override
public ItemCreationResponse createCollection(String groupId, String collectionName, String parentCollection) throws ZoteroConnectionException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs javadoc (especially addressing my question below)

String url = String.format("groups/%s/collections", groupId);

ObjectMapper mapper = new ObjectMapper();

JsonNode dataAsJson = mapper.createObjectNode()
.put("name", collectionName)
.put("parentCollection", parentCollection);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if there is no parent collection? will this be null or should it just be omitted? the Zotero API documentation doesn't seem to say anything about that, have you tried it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested this, when there's parent collection, the new collection gets added as child to parent collection. Else the new collection is added directly to group. I have kept this because as there might be cases when parent collection is present.


ArrayNode jsonArray = mapper.createArrayNode();
jsonArray.add(dataAsJson);

HttpEntity<ArrayNode> data = new HttpEntity<ArrayNode>(jsonArray);

try {
return restTemplate.exchange(buildUri(url, false), HttpMethod.POST, data, ItemCreationResponse.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use the buildGroupUri method or is there a reason it is not used here?

.getBody();
} catch (RestClientException e) {
throw new ZoteroConnectionException("Could not create item.", e);
}
}
}