-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBatchImportOperation.m
172 lines (159 loc) · 5.58 KB
/
BatchImportOperation.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
//
// BatchImportOperation.m
// spires
//
// Created by Yuji on 09/02/07.
// Copyright 2009 Y. Tachikawa. All rights reserved.
//
#import "BatchImportOperation.h"
#import "Article.h"
#import "ArticleData.h"
#import "JournalEntry.h"
#import "AllArticleList.h"
#import "AppDelegate.h"
#import "MOC.h"
#import "NSString+magic.h"
#import "ProtoArticle.h"
@implementation BatchImportOperation
{
NSArray*elements;
NSString*query;
NSManagedObjectContext*secondMOC;
NSMutableSet*generated;
BOOL updatesCitations;
void (^whenDone)(BatchImportOperation*op);
}
@synthesize generated;
@synthesize secondMOC;
-(BatchImportOperation*)initWithProtoArticles:(NSArray *)d
originalQuery:(NSString*)q
updatesCitations:(BOOL)b
usingMOC:(NSManagedObjectContext *)moc_
whenDone:(void(^)(BatchImportOperation*op))wd
{
self=[super init];
elements=d;
query=[q copy];
updatesCitations=b;
/* NSInteger cap=[[NSUserDefaults standardUserDefaults] integerForKey:@"batchImportCap"];
if(cap<100)cap=100;
if([elements count]>cap){
elements=[elements objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,cap)]];
}*/
secondMOC=moc_;
whenDone=wd;
generated=[NSMutableSet set];
return self;
}
-(BOOL)isEqual:(id)obj
{
return self==obj;
}
-(NSString*)description
{
return @"registering";
}
#pragma mark setters from XML
#pragma mark Main Logic
-(void)treatElements:(NSMutableArray*)a withKey:(NSString*)key
{
if([a count]==0)
return ;
NSMutableDictionary*dict=[NSMutableDictionary dictionary];
for(NSObject<ProtoArticle>*e in a){
NSObject<NSCopying>*v=[e valueForKey:key];
dict[v] = e;
}
NSArray*values=[dict allKeys];
values=[values sortedArrayUsingSelector:@selector(compare:)];
NSEntityDescription*entity=[NSEntityDescription entityForName:@"ArticleData" inManagedObjectContext:secondMOC];
NSFetchRequest*req=[[NSFetchRequest alloc]init];
[req setEntity:entity];
NSPredicate*pred=[NSPredicate predicateWithFormat:@"%K IN %@",key,values];
[req setPredicate:pred];
[req setIncludesPropertyValues:NO];
[req setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:key ascending:YES]]];
NSError*error=nil;
NSArray*datas=[secondMOC executeFetchRequest:req error:&error];
for(ArticleData*data in datas){
if(!data.article){
NSLog(@"inconsistency! stray ArticleData found and removed: %@",data);
[secondMOC deleteObject:data];
continue;
}
NSObject<NSCopying>*v=[data valueForKey:key];
NSObject<ProtoArticle>*e=dict[v];
[e populatePropertiesOfArticle:data.article];
[generated addObject:data.article];
[a removeObject:e];
}
NSEntityDescription*articleEntity=[NSEntityDescription entityForName:@"Article" inManagedObjectContext:secondMOC];
for(NSObject<ProtoArticle>*e in a){
Article*article=(Article*)[[NSManagedObject alloc] initWithEntity:articleEntity insertIntoManagedObjectContext:secondMOC];
[e populatePropertiesOfArticle:article];
[generated addObject:article];
}
}
-(void)batchAddEntriesOfSPIRES:(NSArray*)a
{
NSMutableArray*lookForEprint=[NSMutableArray array];
NSMutableArray*lookForSpiresKey=[NSMutableArray array];
NSMutableArray*lookForDOI=[NSMutableArray array];
NSMutableArray*lookForTitle=[NSMutableArray array];
for(NSObject<ProtoArticle>*element in a){
if(element.eprint){
[lookForEprint addObject:element];
}else if(element.spiresKey){
[lookForSpiresKey addObject:element];
}else if(element.doi){
[lookForDOI addObject:element];
}else if(element.title){
[lookForTitle addObject:element];
}
}
[self treatElements:lookForEprint withKey:@"eprint"];
[self treatElements:lookForSpiresKey withKey:@"spiresKey"];
[self treatElements:lookForDOI withKey:@"doi"];
[self treatElements:lookForTitle withKey:@"title"];
// you shouldn't mix dispatch to the main thread and performSelectorOnMainThread,
// they're not guaranteed to be serialized!
// but the code that motivated this comment was gone.
AllArticleList*allArticleList=[AllArticleList allArticleListInMOC:secondMOC];
[allArticleList addArticles:generated];
if([query hasPrefix:@"c "]){
Article*citedByTarget=[Article articleForQuery:query inMOC:secondMOC];
if(!citedByTarget){
NSLog(@"citedBy target article not found. strange.");
}else{
NSLog(@"added to %@",citedByTarget.title);
[citedByTarget addCitedBy:generated];
}
}
if([query hasPrefix:@"r "]){
Article*refersToTarget=[Article articleForQuery:query inMOC:secondMOC];
if(!refersToTarget){
NSLog(@"refersTo target article not found. strange.");
}else{
NSLog(@"added to %@",refersToTarget.title);
[refersToTarget addRefersTo:generated];
}
}
/*
if(updatesCitations && generated.count>0){
NSOperation* op=[[InspireCitationNumberRefreshOperation alloc] initWithArticles:generated];
[op setQueuePriority:NSOperationQueuePriorityVeryLow];
[[OperationQueues spiresQueue] addOperation:op];
}
*/
}
#pragma mark entry point
-(void)main
{
[secondMOC performBlockAndWait:^{
[self batchAddEntriesOfSPIRES:elements];
if(whenDone){
whenDone(self);
}
}];
}
@end