Skip to content

Commit

Permalink
Rewrote the handling of protoarticles
Browse files Browse the repository at this point in the history
  • Loading branch information
yujitach committed Aug 11, 2015
1 parent f0019ce commit 5172cd2
Show file tree
Hide file tree
Showing 11 changed files with 322 additions and 93 deletions.
2 changes: 1 addition & 1 deletion BatchImportOperation.h
Expand Up @@ -13,7 +13,7 @@
@class Article;
@class ArticleList;
@interface BatchImportOperation : NSOperation
-(BatchImportOperation*)initWithXMLData:(NSData*)d
-(BatchImportOperation*)initWithProtoArticles:(NSArray*)d
originalQuery:(NSString*)q;
@property(readonly) NSMutableSet*generated;
@end
32 changes: 12 additions & 20 deletions BatchImportOperation.m
Expand Up @@ -15,24 +15,23 @@
#import "AppDelegate.h"
#import "MOC.h"
#import "NSString+magic.h"
#import "InspireXMLArticle.h"
#import "ProtoArticle.h"

@implementation BatchImportOperation
{
NSData*xmlData;
NSArray*elements;
NSString*query;
NSManagedObjectContext*secondMOC;
NSMutableSet*generated;
dispatch_group_t group;
}
@synthesize generated;
-(BatchImportOperation*)initWithXMLData:(NSData*)d
-(BatchImportOperation*)initWithProtoArticles:(NSArray *)d
originalQuery:(NSString*)q;
{
self=[super init];
xmlData=d;
elements=d;
query=[q copy];
[xmlData writeToFile:@"/tmp/spiresTemporary.xml" atomically:YES];
/* NSInteger cap=[[NSUserDefaults standardUserDefaults] integerForKey:@"batchImportCap"];
if(cap<100)cap=100;
if([elements count]>cap){
Expand All @@ -55,13 +54,11 @@ -(NSString*)description

#pragma mark setters from XML

-(void)populatePropertiesOfArticle:(Article*)o fromProtoArticle:(ProtoArticle*)element
-(void)populatePropertiesOfArticle:(Article*)o fromProtoArticle:(NSObject<ProtoArticle>*)element
{
o.spiresKey=@([element.spiresKey integerValue]);
o.inspireKey=@([element.inspireKey integerValue]);
// Here I'm cheating: -setAuthorNames: puts the collaboration name in the author list,
// so "collaboration" needs to be set up before that
for(NSString*key in [@"eprint,title,collaboration,doi,abstract,comments,citecount,pages,date" componentsSeparatedByString:@","]){
for(NSString*key in [@"spiresKey,inspireKey,eprint,title,collaboration,doi,abstract,comments,citecount,pages,date" componentsSeparatedByString:@","]){
NSObject*x=[element valueForKey:key];
if(x){
[o setValue:x forKey:key];
Expand Down Expand Up @@ -93,8 +90,8 @@ -(void)treatElements:(NSMutableArray*)a withKey:(NSString*)key
if([a count]==0)
return ;
NSMutableDictionary*dict=[NSMutableDictionary dictionary];
for(ProtoArticle*e in a){
NSString*v=[e valueForKey:key];
for(NSObject<ProtoArticle>*e in a){
NSObject<NSCopying>*v=[e valueForKey:key];
dict[v] = e;
}

Expand All @@ -116,17 +113,14 @@ -(void)treatElements:(NSMutableArray*)a withKey:(NSString*)key
[secondMOC deleteObject:data];
continue;
}
NSString*v=[data valueForKey:key];
if([v isKindOfClass:[NSNumber class]]){
v=[(NSNumber*)v stringValue];
}
ProtoArticle*e=dict[v];
NSObject<NSCopying>*v=[data valueForKey:key];
NSObject<ProtoArticle>*e=dict[v];
[self populatePropertiesOfArticle:data.article fromProtoArticle:e];
[generated addObject:data.article];
[a removeObject:e];
}
NSEntityDescription*articleEntity=[NSEntityDescription entityForName:@"Article" inManagedObjectContext:secondMOC];
for(ProtoArticle*e in a){
for(NSObject<ProtoArticle>*e in a){
Article*article=(Article*)[[NSManagedObject alloc] initWithEntity:articleEntity insertIntoManagedObjectContext:secondMOC];
[self populatePropertiesOfArticle:article fromProtoArticle:e];
[generated addObject:article];
Expand All @@ -138,7 +132,7 @@ -(void)batchAddEntriesOfSPIRES:(NSArray*)a
NSMutableArray*lookForSpiresKey=[NSMutableArray array];
NSMutableArray*lookForDOI=[NSMutableArray array];
NSMutableArray*lookForTitle=[NSMutableArray array];
for(ProtoArticle*element in a){
for(NSObject<ProtoArticle>*element in a){
if(element.eprint){
[lookForEprint addObject:element];
}else if(element.spiresKey){
Expand Down Expand Up @@ -192,8 +186,6 @@ -(void)main
{

[secondMOC performBlockAndWait:^{
NSArray*elements=[InspireXMLArticle articlesFromXMLData:xmlData];
NSLog(@"spires returned %d entries",(int)[elements count]);
[self batchAddEntriesOfSPIRES:elements];
[secondMOC save:NULL];
}];
Expand Down
2 changes: 1 addition & 1 deletion InspireXMLArticle.h → InspireXMLParser.h
Expand Up @@ -8,7 +8,7 @@

#import "ProtoArticle.h"

@interface InspireXMLArticle : ProtoArticle
@interface InspireXMLParser : NSObject<NSXMLParserDelegate>
+(NSString*)usedTags;
+(NSArray*)articlesFromXMLData:(NSData*)data;
@end
82 changes: 29 additions & 53 deletions InspireXMLArticle.m → InspireXMLParser.m
Expand Up @@ -6,28 +6,31 @@
//
//

#import "InspireXMLArticle.h"
#import "InspireXMLParser.h"
#import "LightweightArticle.h"

@interface InspireXMLArticle ()
-(void)addAuthor:(NSString*)name;
@interface InspireXMLParser ()
@property NSMutableArray*articles;
@end


@interface InspireXMLParser: NSObject<NSXMLParserDelegate>
-(instancetype)initWithXMLData:(NSData*)data;
@property (readonly) NSMutableArray*articles;
@end


@implementation InspireXMLParser
{
NSMutableString*currentString;
NSString*currentTag;
NSString*currentCode;
NSMutableDictionary*subfieldDic;
InspireXMLArticle*currentArticle;
LightweightArticle*currentArticle;
}
@synthesize articles;
+(NSString*)usedTags
{
return @"001,970,100,700,710,520,037,245,300,773,961,024";
}
+(NSArray*)articlesFromXMLData:(NSData*)data
{
InspireXMLParser*parser=[[InspireXMLParser alloc] initWithXMLData:data];
return parser.articles;
}
-(instancetype)initWithXMLData:(NSData*)data
{
self=[super init];
Expand All @@ -46,7 +49,7 @@ -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName name
{
currentString=[NSMutableString string];
if([elementName isEqualToString:@"record"]){
currentArticle=[[InspireXMLArticle alloc] init];
currentArticle=[[LightweightArticle alloc] init];
}else if([elementName isEqualToString:@"controlfield"]){

}else if([elementName isEqualToString:@"datafield"]){
Expand All @@ -62,36 +65,37 @@ -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namesp
[articles addObject:currentArticle];
currentArticle=nil;
}else if([elementName isEqualToString:@"controlfield"]){
[currentArticle setValue:currentString forKey:@"inspireKey"];
currentArticle.inspireKey=@([currentString integerValue]);
}else if([elementName isEqualToString:@"datafield"]){
if([currentTag isEqualToString:@"970"]){
NSString*s=subfieldDic[@"a"];
[currentArticle setValue:[s substringFromIndex:[@"SPIRES-" length]] forKey:@"spiresKey"];
NSString*spiresKey=[s substringFromIndex:[@"SPIRES-" length]];
currentArticle.spiresKey=@([spiresKey integerValue]);
}else if([currentTag isEqualToString:@"100"]){
[currentArticle addAuthor:subfieldDic[@"a"]];
}else if([currentTag isEqualToString:@"700"]){
[currentArticle addAuthor:subfieldDic[@"a"]];
}else if([currentTag isEqualToString:@"710"]){
[currentArticle setValue:subfieldDic[@"g"] forKey:@"collaboration"];
currentArticle.collaboration=subfieldDic[@"g"];
}else if([currentTag isEqualToString:@"520"]){
if([subfieldDic[@"9"] isEqualToString:@"arXiv"]){
[currentArticle setValue:subfieldDic[@"a"] forKey:@"abstract"];
currentArticle.abstract=subfieldDic[@"a"];
}
}else if([currentTag isEqualToString:@"037"]){
if([subfieldDic[@"9"] isEqualToString:@"arXiv"]){
[currentArticle setValue:subfieldDic[@"a"] forKey:@"eprint"];
currentArticle.eprint=subfieldDic[@"a"];
}
}else if([currentTag isEqualToString:@"245"]){
[currentArticle setValue:subfieldDic[@"a"] forKey:@"title"];
currentArticle.title=subfieldDic[@"a"];
}else if([currentTag isEqualToString:@"300"]){
[currentArticle setValue:@([subfieldDic[@"a"] integerValue]) forKey:@"pages"];
currentArticle.pages=@([subfieldDic[@"a"] integerValue]);
}else if([currentTag isEqualToString:@"773"]){
NSString*title=subfieldDic[@"p"];
if(title && ![title isEqualToString:@""]){
[currentArticle setValue:title forKey:@"journalTitle"];
[currentArticle setValue:subfieldDic[@"v"] forKey:@"journalVolume"];
[currentArticle setValue:subfieldDic[@"c"] forKey:@"journalPage"];
[currentArticle setValue:@([subfieldDic[@"y"] integerValue]) forKey:@"journalYear"];
currentArticle.journalTitle=title;
currentArticle.journalVolume=subfieldDic[@"v"];
currentArticle.journalPage=subfieldDic[@"c"];
currentArticle.journalYear=@([subfieldDic[@"y"] integerValue]);
}
}else if([currentTag isEqualToString:@"961"]){
NSString*dateString=subfieldDic[@"x"];
Expand All @@ -100,11 +104,11 @@ -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namesp
dateString=[dateString stringByAppendingString:@"-00"];
}
NSDate*date=[NSDate dateWithString:[NSString stringWithFormat:@"%@ 00:00:00 +0000",dateString]];
[currentArticle setValue:date forKey:@"date"];
currentArticle.date=date;
}
}else if([currentTag isEqualToString:@"024"]){
if([subfieldDic[@"2"] isEqualToString:@"DOI"]){
[currentArticle setValue:subfieldDic[@"a" ] forKey:@"doi"];
currentArticle.doi=subfieldDic[@"a"];
}
}
subfieldDic=nil;
Expand All @@ -119,31 +123,3 @@ -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
}
@end

@implementation InspireXMLArticle
{
NSMutableArray*authorArray;
}
+(NSString*)usedTags
{
return @"001,970,100,700,710,520,037,245,300,773,961,024";
}
+(NSArray*)articlesFromXMLData:(NSData*)data
{
InspireXMLParser*parser=[[InspireXMLParser alloc] initWithXMLData:data];
return parser.articles;
}
-(instancetype)init
{
self=[super init];
authorArray=[NSMutableArray array];
return self;
}
-(NSArray*)authors
{
return authorArray;
}
-(void)addAuthor:(NSString*)name
{
[authorArray addObject:name];
}
@end
30 changes: 30 additions & 0 deletions LightweightArticle.h
@@ -0,0 +1,30 @@
//
// LightweightArticle.h
// inspire
//
// Created by Yuji on 2015/08/11.
//
//

#import <Foundation/Foundation.h>
#import "ProtoArticle.h"

@interface LightweightArticle : NSObject<ProtoArticle>
@property (strong) NSString*title;
@property (strong) NSNumber*inspireKey;
@property (strong) NSNumber*spiresKey;
@property (strong) NSString*eprint;
@property (strong) NSArray*authors;
@property (strong) NSString*abstract;
@property (strong) NSString*collaboration;
@property (strong) NSNumber*pages;
@property (strong) NSNumber*citecount;
@property (strong) NSDate*date;
@property (strong) NSString*doi;
@property (strong) NSString*comments;
@property (strong) NSString*journalTitle;
@property (strong) NSString*journalVolume;
@property (strong) NSString*journalPage;
@property (strong) NSNumber*journalYear;
-(void)addAuthor:(NSString*)author;
@end

0 comments on commit 5172cd2

Please sign in to comment.