/
MDLDocument.m
435 lines (385 loc) · 27.8 KB
/
MDLDocument.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//
// MDLDocument.m
//
// Copyright (c) 2012-2013 shazino (shazino SAS), http://www.shazino.com/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "MDLDocument.h"
#import "MDLMendeleyAPIClient.h"
#import "MDLAuthor.h"
#import "MDLPublication.h"
#import "MDLCategory.h"
#import "MDLSubcategory.h"
#import "MDLGroup.h"
#import "MDLFile.h"
#import "AFNetworking.h"
NSString * const MDLDocumentTypeGeneric = @"Generic";
@interface MDLDocument ()
+ (MDLDocument *)documentWithRawDocument:(NSDictionary *)rawDocument;
+ (NSDictionary *)detailsContentForDocument:(MDLDocument *)document;
+ (void)fetchDocumentsWithPath:(NSString *)path public:(BOOL)public parameters:(NSDictionary *)parameters atPage:(NSUInteger)pageIndex count:(NSUInteger)count success:(void (^)(NSArray *, NSUInteger, NSUInteger, NSUInteger, NSUInteger))success failure:(void (^)(NSError *))failure;
@end
@implementation MDLDocument
+ (MDLDocument *)createDocumentWithTitle:(NSString *)title parameters:(NSDictionary *)parameters success:(void (^)(MDLDocument *))success failure:(void (^)(NSError *))failure
{
MDLDocument *newDocument = [MDLDocument new];
newDocument.title = title;
newDocument.type = parameters[@"type"] ?: MDLDocumentTypeGeneric;
NSMutableDictionary *bodyContent = [NSMutableDictionary dictionaryWithDictionary:parameters];
bodyContent[@"type"] = newDocument.type;
bodyContent[@"title"] = newDocument.title;
[[MDLMendeleyAPIClient sharedClient] postPath:@"/oapi/library/documents/"
bodyKey:@"document"
bodyContent:bodyContent
success:^(AFHTTPRequestOperation *operation, id responseDictionary) {
newDocument.identifier = responseDictionary[@"document_id"];
if (success)
success(newDocument);
} failure:failure];
return newDocument;
}
+ (MDLDocument *)createDocument:(MDLDocument *)document
success:(void (^)(MDLDocument *))success
failure:(void (^)(NSError *))failure
{
NSDictionary *bodyContent = [MDLDocument detailsContentForDocument:document];
[[MDLMendeleyAPIClient sharedClient] postPath:@"/oapi/library/documents/"
bodyKey:@"document"
bodyContent:bodyContent
success:^(AFHTTPRequestOperation *operation, id responseDictionary) {
MDLDocument *newDocument = [MDLDocument new];
newDocument.identifier = responseDictionary[@"document_id"];
if (success)
success(newDocument);
} failure:failure];
return document;
}
+ (MDLDocument *)documentWithRawDocument:(NSDictionary *)rawDocument
{
MDLDocument *document = [MDLDocument new];
if (![rawDocument isKindOfClass:[NSDictionary class]])
return document;
document.identifier = (rawDocument[@"id"]) ? rawDocument[@"id"] : rawDocument[@"uuid"];
document.title = rawDocument[@"title"];
document.type = rawDocument[@"type"];
document.DOI = rawDocument[@"doi"];
document.version = [NSNumber numberOrNumberFromString:rawDocument[@"version"]];
return document;
}
+ (NSDictionary *)detailsContentForDocument:(MDLDocument *)document
{
NSMutableDictionary *bodyContent = [NSMutableDictionary dictionary];
bodyContent[@"issue"] = document.issue ?: @"";
bodyContent[@"citation_key"] = document.citationKey ?: @"";
bodyContent[@"pages"] = document.pages ?: @"";
bodyContent[@"title"] = document.title ?: @"";
bodyContent[@"volume"] = document.volume ?: @"";
bodyContent[@"year"] = document.year ?: @"";
bodyContent[@"publisher"] = document.publisher ?: @"";
bodyContent[@"published_in"] = document.publication.name ?: @"";
bodyContent[@"keywords"] = document.keywords ?: @"";
bodyContent[@"tags"] = document.tags ?: @"";
bodyContent[@"notes"] = document.notes ?: @"";
bodyContent[@"doi"] = document.DOI ?: @"";
bodyContent[@"pmid"] = document.PubMedIdentifier ?: @"";
bodyContent[@"type"] = document.type ?: MDLDocumentTypeGeneric;
if (document.starred)
bodyContent[@"isStarred"] = document.starred.boolValue ? @"1" : @"0";
if (document.read)
bodyContent[@"isRead"] = document.read.boolValue ? @"1" : @"0";
NSMutableArray *URLsStrings = [NSMutableArray array];
for (NSURL *URL in document.URLs) {
[URLsStrings addObject:[URL absoluteString]];
}
bodyContent[@"url"] = [URLsStrings componentsJoinedByString:@"\n"];
NSMutableArray *authors = [NSMutableArray array];
for (MDLAuthor *author in document.authors) {
if (author.forename.length > 0 || author.surname.length > 0)
[authors addObject:@{
@"forename": author.forename ?: @"",
@"surname": author.surname ?: @""}];
else if (author.name)
[authors addObject:author.name];
}
bodyContent[@"authors"] = authors;
return bodyContent;
}
+ (void)fetchDocumentsWithPath:(NSString *)path public:(BOOL)public parameters:(NSDictionary *)parameters atPage:(NSUInteger)pageIndex count:(NSUInteger)count success:(void (^)(NSArray *, NSUInteger, NSUInteger, NSUInteger, NSUInteger))success failure:(void (^)(NSError *))failure
{
NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionaryWithDictionary:parameters];
mutableParameters[@"page"] = @(pageIndex);
mutableParameters[@"items"] = @(count);
[[MDLMendeleyAPIClient sharedClient] getPath:path
requiresAuthentication:!public
parameters:mutableParameters
success:^(AFHTTPRequestOperation *operation, NSDictionary *responseDictionary) {
NSArray *rawDocuments = responseDictionary[@"documents"];
NSMutableArray *documents = [NSMutableArray array];
for (NSDictionary *rawDocument in rawDocuments)
{
MDLDocument *document = [MDLDocument documentWithRawDocument:rawDocument];
document.inUserLibrary = @(!public);
if (document)
[documents addObject:document];
}
if (success)
success(documents, [responseDictionary responseTotalResults], [responseDictionary responseTotalPages], [responseDictionary responsePageIndex], [responseDictionary responseItemsPerPage]);
}
failure:failure];
}
+ (void)searchWithTerms:(NSString *)terms atPage:(NSUInteger)pageIndex count:(NSUInteger)count success:(void (^)(NSArray *, NSUInteger, NSUInteger, NSUInteger, NSUInteger))success failure:(void (^)(NSError *))failure
{
NSString *encodedTerms = [terms stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
encodedTerms = [encodedTerms stringByReplacingOccurrencesOfString:@":" withString:@"%3A"];
[self fetchDocumentsWithPath:[NSString stringWithFormat:@"/oapi/documents/search/%@/", encodedTerms] public:YES parameters:nil atPage:pageIndex count:count success:success failure:failure];
}
+ (void)searchWithGenericTerms:(NSString *)genericTerms authors:(NSString *)authors title:(NSString *)title year:(NSNumber *)year tags:(NSString *)tags atPage:(NSUInteger)pageIndex count:(NSUInteger)count success:(void (^)(NSArray *, NSUInteger, NSUInteger, NSUInteger, NSUInteger))success failure:(void (^)(NSError *))failure
{
NSMutableArray *terms = [NSMutableArray array];
if ([genericTerms length] > 0)
[terms addObject:genericTerms];
if ([authors length] > 0)
[terms addObject:[NSString stringWithFormat:@"author:%@", authors]];
if ([title length] > 0)
[terms addObject:[NSString stringWithFormat:@"title:%@", title]];
if (year)
[terms addObject:[NSString stringWithFormat:@"year:%@", [year stringValue]]];
if ([tags length] > 0)
[terms addObject:[NSString stringWithFormat:@"tags:%@", tags]];
[self searchWithTerms:[terms componentsJoinedByString:@" "] atPage:pageIndex count:count success:success failure:failure];
}
+ (void)searchTagged:(NSString *)tag category:(MDLCategory *)category subcategory:(MDLSubcategory *)subcategory atPage:(NSUInteger)pageIndex count:(NSUInteger)count success:(void (^)(NSArray *, NSUInteger, NSUInteger, NSUInteger, NSUInteger))success failure:(void (^)(NSError *))failure
{
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
if (category)
parameters[@"cat"] = category.identifier;
if (subcategory)
parameters[@"subcat"] = subcategory.identifier;
[self fetchDocumentsWithPath:[NSString stringWithFormat:@"/oapi/documents/tagged/%@/", tag] public:YES parameters:parameters atPage:pageIndex count:count success:success failure:failure];
}
+ (void)searchAuthoredWithName:(NSString *)name year:(NSNumber *)year atPage:(NSUInteger)pageIndex count:(NSUInteger)count success:(void (^)(NSArray *, NSUInteger, NSUInteger, NSUInteger, NSUInteger))success failure:(void (^)(NSError *))failure
{
[self fetchDocumentsWithPath:[NSString stringWithFormat:@"/oapi/documents/authored/%@/", name] public:YES parameters:(year) ? @{@"year" : year} : nil atPage:pageIndex count:count success:success failure:failure];
}
+ (void)fetchTopDocumentsInPublicLibraryForCategory:(NSString *)categoryIdentifier upAndComing:(BOOL)upAndComing success:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure
{
[[MDLMendeleyAPIClient sharedClient] getPath:@"/oapi/stats/papers/"
requiresAuthentication:NO
parameters:[NSDictionary parametersForCategory:categoryIdentifier upAndComing:upAndComing]
success:^(AFHTTPRequestOperation *operation, NSArray *responseArray) {
NSMutableArray *documents = [NSMutableArray array];
for (NSDictionary *rawDocument in responseArray)
[documents addObject:[MDLDocument documentWithRawDocument:rawDocument]];
if (success)
success(documents);
} failure:failure];
}
+ (void)fetchDocumentsInUserLibraryAtPage:(NSUInteger)pageIndex count:(NSUInteger)count success:(void (^)(NSArray *, NSUInteger, NSUInteger, NSUInteger, NSUInteger))success failure:(void (^)(NSError *))failure
{
[self fetchDocumentsWithPath:@"/oapi/library/" public:NO parameters:nil atPage:pageIndex count:count success:success failure:failure];
}
+ (void)fetchAuthoredDocumentsInUserLibraryAtPage:(NSUInteger)pageIndex count:(NSUInteger)count success:(void (^)(NSArray *, NSUInteger, NSUInteger, NSUInteger, NSUInteger))success failure:(void (^)(NSError *))failure
{
[self fetchDocumentsWithPath:@"/oapi/library/documents/authored/" public:NO parameters:nil atPage:pageIndex count:count success:success failure:failure];
}
- (BOOL)isInUserLibrary
{
return [self.inUserLibrary boolValue];
}
- (AFHTTPRequestOperation *)uploadFileAtURL:(NSURL *)fileURL success:(void (^)(MDLFile *newFile))success failure:(void (^)(NSError *))failure
{
if (!self.identifier)
{
failure([NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:nil]);
return nil;
}
NSString *path = [NSString stringWithFormat:@"/oapi/library/documents/%@/", self.identifier];
return [[MDLMendeleyAPIClient sharedClient] putPath:path
fileAtURL:fileURL
success:^(AFHTTPRequestOperation *operation, NSString *fileHash, id responseObject) {
MDLFile *file = [MDLFile fileWithDateAdded:nil
extension:[fileURL pathExtension]
hash:fileHash
size:nil
document:nil];
if (success)
success(file);
}
failure:failure];
}
- (void)fetchDetailsSuccess:(void (^)(MDLDocument *))success failure:(void (^)(NSError *))failure
{
NSString *path;
if (self.group)
path = [NSString stringWithFormat:@"/oapi/library/groups/%@/%@/", self.group.identifier, self.identifier];
else
path = [NSString stringWithFormat:(self.isInUserLibrary) ? @"/oapi/library/documents/%@/" : @"/oapi/documents/details/%@/", self.identifier];
[[MDLMendeleyAPIClient sharedClient] getPath:path
requiresAuthentication:self.isInUserLibrary || (self.group.type == MDLGroupTypePrivate)
parameters:nil
success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
if (![responseObject isKindOfClass:[NSDictionary class]]) {
if (failure)
failure(nil);
return;
}
self.abstract = responseObject[@"abstract"];
self.addedDate = [NSDate dateWithTimeIntervalSince1970:[[NSNumber numberOrNumberFromString:responseObject[@"added"]] doubleValue]];
self.canonicalIdentifier = responseObject[@"canonical_id"];
self.cast = responseObject[@"cast"];
self.citationKey = responseObject[@"citation_key"];
self.deletionPending = [NSNumber boolNumberFromNumberOrString:responseObject[@"deletionPending"]];
self.discipline = responseObject[@"discipline"];
self.DOI = responseObject[@"doi"];
self.editors = responseObject[@"editors"];
if ([responseObject[@"folders_ids"] isKindOfClass:[NSArray class]])
self.foldersIdentifiers = responseObject[@"folders_ids"];
self.identifier = (responseObject[@"id"]) ?: (responseObject[@"uuid"]) ?: nil ;
self.identifiers = ([responseObject[@"identifiers"] isKindOfClass:[NSDictionary class]]) ? responseObject[@"identifiers"] : nil;
self.institution = responseObject[@"institution"];
self.authored = [NSNumber boolNumberFromNumberOrString:responseObject[@"isAuthor"]];
self.read = [NSNumber boolNumberFromNumberOrString:responseObject[@"isRead"]];
self.starred = [NSNumber boolNumberFromNumberOrString:responseObject[@"isStarred"]];
self.issue = responseObject[@"issue"];
self.keywords = responseObject[@"keywords"];
self.mendeleyURL = [NSURL URLWithString:responseObject[@"mendeley_url"]];
self.modifiedDate = [NSDate dateWithTimeIntervalSince1970:[[NSNumber numberOrNumberFromString:responseObject[@"modified"]] doubleValue]];
self.notes = responseObject[@"notes"];
self.openAccess = [NSNumber boolNumberFromNumberOrString:responseObject[@"oa_journal"]];
self.pages = responseObject[@"pages"];
self.PubMedIdentifier = responseObject[@"pmid"];
self.producers = responseObject[@"producers"];
if (responseObject[@"publication_outlet"])
self.publicationOutlet = [MDLPublication publicationWithName:responseObject[@"publication_outlet"]];
else if (responseObject[@"published_in"])
self.publication = [MDLPublication publicationWithName:responseObject[@"published_in"]];
self.publisher = responseObject[@"publisher"];
self.subdiscipline = responseObject[@"subdiscipline"];
self.tags = responseObject[@"tags"];
self.title = responseObject[@"title"];
self.translators = responseObject[@"translators"];
self.type = responseObject[@"type"];
self.version = [NSNumber numberOrNumberFromString:responseObject[@"version"]];
self.volume = responseObject[@"volume"];
self.year = [NSNumber numberOrNumberFromString:responseObject[@"year"]];
if (!self.DOI && self.identifiers[@"doi"])
self.DOI = self.identifiers[@"doi"];
NSMutableArray *URLs = [NSMutableArray array];
for (NSString *URLString in [responseObject[@"url"] componentsSeparatedByString:@"\n"]) {
NSURL *URL = [NSURL URLWithString:URLString];
if (URL)
[URLs addObject:URL];
}
self.URLs = URLs;
NSMutableArray *authors = [NSMutableArray array];
for (NSDictionary *authorDictionary in responseObject[@"authors"]) {
MDLAuthor *author = [MDLAuthor authorWithForename:authorDictionary[@"forename"] surname:authorDictionary[@"surname"]];
if (author)
[authors addObject:author];
}
self.authors = authors;
if (responseObject[@"files"])
{
NSMutableArray *files = [NSMutableArray array];
NSDateFormatter *fileDateFormatter = [[NSDateFormatter alloc] init];
fileDateFormatter.dateFormat = @"y-M-d H:m:s";
for (NSDictionary *fileDictionary in responseObject[@"files"]) {
MDLFile *file = [MDLFile fileWithDateAdded:[fileDateFormatter dateFromString:fileDictionary[@"date_added"]] extension:fileDictionary[@"file_extension"] hash:fileDictionary[@"file_hash"] size:[NSNumber numberOrNumberFromString:fileDictionary[@"file_size"]] document:self];
if (file)
[files addObject:file];
}
self.files = files;
}
else if (responseObject[@"file_url"])
self.files = @[[MDLFile fileWithPublicURL:[NSURL URLWithString:responseObject[@"file_url"]] document:self]];
if (success)
success(self);
}
failure:failure];
}
- (void)updateDetailsSuccess:(void (^)(MDLDocument *))success failure:(void (^)(NSError *))failure
{
NSDictionary *bodyContent = [MDLDocument detailsContentForDocument:self];
[[MDLMendeleyAPIClient sharedClient] postPath:[NSString stringWithFormat:@"/oapi/library/documents/%@/", self.identifier]
bodyKey:@"document"
bodyContent:bodyContent
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success)
success(self);
}
failure:failure];
}
- (void)markAsRead:(BOOL)read success:(void (^)(MDLDocument *))success failure:(void (^)(NSError *))failure
{
[[MDLMendeleyAPIClient sharedClient] postPath:[NSString stringWithFormat:@"/oapi/library/documents/%@/", self.identifier]
bodyKey:@"document" bodyContent:@{@"isRead": read ? @"1" : @"0"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.read = @(read);
if (success)
success(self);
} failure:failure];
}
- (void)markAsStarred:(BOOL)starred success:(void (^)(MDLDocument *))success failure:(void (^)(NSError *))failure
{
[[MDLMendeleyAPIClient sharedClient] postPath:[NSString stringWithFormat:@"/oapi/library/documents/%@/", self.identifier]
bodyKey:@"document" bodyContent:@{@"isStarred": starred ? @"1" : @"0"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.starred = @(starred);
if (success)
success(self);
} failure:failure];
}
- (void)moveToTrashSuccess:(void (^)(MDLDocument *))success failure:(void (^)(NSError *))failure
{
[[MDLMendeleyAPIClient sharedClient] postPath:[NSString stringWithFormat:@"/oapi/library/documents/%@/", self.identifier]
bodyKey:@"document" bodyContent:@{@"deletionPending": @"1"} success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.deletionPending = @(YES);
if (success)
success(self);
} failure:failure];
}
- (void)fetchRelatedDocumentsAtPage:(NSUInteger)pageIndex count:(NSUInteger)count success:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure
{
[[MDLMendeleyAPIClient sharedClient] getPath:[NSString stringWithFormat:@"/oapi/documents/related/%@/", self.identifier]
requiresAuthentication:NO
parameters:@{@"page" : @(pageIndex), @"items" : @(count)}
success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
NSMutableArray *documents = [NSMutableArray array];
for (NSDictionary *rawDocument in responseObject[@"documents"])
[documents addObject:[MDLDocument documentWithRawDocument:rawDocument]];
if (success)
success(documents);
} failure:failure];
}
- (void)importToUserLibrarySuccess:(void (^)(NSString *newDocumentIdentifier))success failure:(void (^)(NSError *))failure
{
[[MDLMendeleyAPIClient sharedClient] postPath:@"/oapi/library/documents/"
bodyKey:@"canonical_id"
bodyContent:self.identifier
success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success)
success(responseObject[@"document_id"]);
} failure:failure];
}
- (void)deleteSuccess:(void (^)())success failure:(void (^)(NSError *))failure
{
[[MDLMendeleyAPIClient sharedClient] deletePath:[NSString stringWithFormat:@"/oapi/library/documents/%@/", self.identifier]
parameters:nil
success:^(AFHTTPRequestOperation *requestOperation, id responseObject) { if (success) success(); }
failure:failure];
}
@end