aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorBibiko <bibiko@eva.mpg.de>2010-02-16 23:11:38 +0000
committerBibiko <bibiko@eva.mpg.de>2010-02-16 23:11:38 +0000
commit72c4522db57b9699e1dc324f62dbeb036405f2c6 (patch)
treedb10818cac8e148a91137b1258241f34da6bf184 /Source
parent429e77d0512d3d36a4ca8e9aa994be48906c1445 (diff)
downloadsequelpro-72c4522db57b9699e1dc324f62dbeb036405f2c6.tar.gz
sequelpro-72c4522db57b9699e1dc324f62dbeb036405f2c6.tar.bz2
sequelpro-72c4522db57b9699e1dc324f62dbeb036405f2c6.zip
• csv field mapper
- added chance to align the csv field and table field names via 'matching names' which uses the Levenshtein distance [first approach] if csv file's first line is an header
Diffstat (limited to 'Source')
-rw-r--r--Source/SPFieldMapperController.h10
-rw-r--r--Source/SPFieldMapperController.m48
2 files changed, 53 insertions, 5 deletions
diff --git a/Source/SPFieldMapperController.h b/Source/SPFieldMapperController.h
index 62139b7d..4b9a3178 100644
--- a/Source/SPFieldMapperController.h
+++ b/Source/SPFieldMapperController.h
@@ -37,10 +37,11 @@
IBOutlet id importFieldNamesHeaderSwitch;
IBOutlet id importButton;
IBOutlet NSPopUpButton *alignByPopup;
-
+ IBOutlet NSMenuItem *matchingNameMenuItem;
+
id theDelegate;
id fieldMappingImportArray;
-
+
NSInteger fieldMappingCurrentRow;
NSMutableArray *fieldMappingArray;
NSMutableArray *fieldMappingTableColumnNames;
@@ -49,7 +50,7 @@
NSMutableArray *fieldMappingButtonOptions;
NSMutableArray *fieldMappingOperatorOptions;
NSMutableArray *fieldMappingOperatorArray;
-
+
NSNumber *doImport;
NSNumber *doNotImport;
NSNumber *isEqual;
@@ -87,10 +88,13 @@
- (IBAction)changeTableTarget:(id)sender;
- (IBAction)changeImportMethod:(id)sender;
- (IBAction)changeFieldAlignment:(id)sender;
+- (IBAction)changeHasHeaderCheckbox:(id)sender;
- (IBAction)stepRow:(id)sender;
+- (IBAction)addGlobalSourceVariable:(id)sender;
- (IBAction)closeSheet:(id)sender;
// Others
+- (void)matchHeaderNames;
- (void)setupFieldMappingArray;
- (void)updateFieldMappingButtonCell;
- (void)updateFieldMappingOperatorOptions;
diff --git a/Source/SPFieldMapperController.m b/Source/SPFieldMapperController.m
index 84dfc4c8..42ac3b0d 100644
--- a/Source/SPFieldMapperController.m
+++ b/Source/SPFieldMapperController.m
@@ -27,6 +27,7 @@
#import "SPTableData.h"
#import "TablesList.h"
#import "SPArrayAdditions.h"
+#import "SPStringAdditions.h"
#import "SPConstants.h"
@implementation SPFieldMapperController
@@ -95,6 +96,7 @@
[importFieldNamesHeaderSwitch setState:importFieldNamesHeader];
+ [self changeHasHeaderCheckbox:self];
[self changeTableTarget:self];
[[self window] makeFirstResponder:fieldMapperTableView];
if([fieldMappingTableColumnNames count])
@@ -162,7 +164,7 @@
- (BOOL)importFieldNamesHeader
{
- return importFieldNamesHeader;
+ return ([importFieldNamesHeaderSwitch state] == NSOnState)?YES:NO;
}
#pragma mark -
@@ -279,7 +281,9 @@
for(i=possibleImports; i>=0; i--)
[fieldMappingArray replaceObjectAtIndex:possibleImports-i withObject:[NSNumber numberWithInteger:i]];
break;
-
+ case 2: // try to align header and table target field names via Levenshtein distance
+ [self matchHeaderNames];
+ break;
}
[fieldMapperTableView reloadData];
}
@@ -304,6 +308,46 @@
[rowUpButton setEnabled:(fieldMappingCurrentRow != ([fieldMappingImportArray count]-1))];
}
+- (IBAction)changeHasHeaderCheckbox:(id)sender
+{
+ [matchingNameMenuItem setEnabled:([importFieldNamesHeaderSwitch state] == NSOnState)?YES:NO];
+}
+
+- (IBAction)addGlobalSourceVariable:(id)sender
+{
+
+}
+
+#pragma mark -
+#pragma mark Others
+
+- (void)matchHeaderNames
+{
+ if(![fieldMappingImportArray count]) return;
+
+ NSMutableArray *fileHeaderNames = [NSMutableArray array];
+ [fileHeaderNames setArray:NSArrayObjectAtIndex(fieldMappingImportArray, 0)];
+ NSMutableArray *tableHeaderNames = [NSMutableArray array];
+ [tableHeaderNames setArray:fieldMappingTableColumnNames];
+
+ NSInteger i,j;
+ NSMutableArray *matchedHeaderNames = [NSMutableArray array];
+ for(i=0; i < [tableHeaderNames count]; i++) {
+ CGFloat minDist = 1e6;
+ NSInteger minIndex = 0;
+ for(j=0; j < [fileHeaderNames count]; j++) {
+ NSString *headerName = [NSArrayObjectAtIndex(fileHeaderNames,j) lowercaseString];
+ CGFloat dist = [[NSArrayObjectAtIndex(tableHeaderNames,i) lowercaseString] levenshteinDistanceWithWord:headerName];
+ if(dist < minDist && ![matchedHeaderNames containsObject:headerName]) {
+ minDist = dist;
+ minIndex = j;
+ }
+ if(dist == 0.0f) [matchedHeaderNames addObject:headerName];
+ }
+ [fieldMappingArray replaceObjectAtIndex:i withObject:[NSNumber numberWithInteger:minIndex]];
+ }
+}
+
/*
* Sets up the fieldMapping array to be shown in the tableView
*/