forked from apaffenholz/PolyViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RetrieveFromDBController.m
246 lines (184 loc) · 9.24 KB
/
RetrieveFromDBController.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
/***********************************************************************
* Created by Andreas Paffenholz on 04/18/12.
* Copyright 2012-2014 by Andreas Paffenholz.
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version: http://www.gnu.org/licenses/gpl.txt.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* RetrieveFromDBController.m
* PolyViewer
**************************************************************************/
#import "RetrieveFromDBController.h"
#import "PolymakeObjectController.h"
#import "AppController.h"
#import "PolymakeInstanceWrapper.h"
#import "DatabaseAccess.h"
@implementation RetrieveFromDBController
@synthesize databases = _databases;
@synthesize collections = _collections;
/***************************************************************/
// initialization, deallocation, window loading
-(id)init {
self = [super init];
if (self) {
_databases = nil;
_collections = nil;
_IDs = nil;
}
return self;
}
/***************************************************************/
-(void)dealloc {
[_databases dealloc];
[_collections dealloc];
[_IDs dealloc];
[super dealloc];
}
/***************************************************************/
- (void)windowDidLoad {
NSLog(@"[RetrieveFromDBController windowDidLoad] called");
[_reportNumberOfResultsLabel setStringValue:@"no results"];
_databases = [[[NSApp delegate] databaseNames] retain];
NSLog(@"[RetrieveFromDBController windowDidLoad] adding databases");
if ( [_databases count] > 0 ) {
[databaseSelection addItemsWithObjectValues:_databases];
[databaseSelection selectItemAtIndex:0];
[databaseSelection setObjectValue:[databaseSelection objectValueOfSelectedItem]];
}
NSLog(@"[RetrieveFromDBController windowDidLoad] database names are %@",_databases);
}
/***************************************************************/
// window actions
- (IBAction)retrieveFromDB:(id)sender {
NSLog(@"[RetrieveFromDBController retrieveFromDB] called");
NSLog(@"[RetrieveFromDBController retrieveFromDB] index of selected item: %ld",(long)[databaseSelection indexOfSelectedItem]);
NSString * _database = (NSString *)[_databases objectAtIndex:[databaseSelection indexOfSelectedItem]];
NSString * _collection = (NSString *)[_collections objectAtIndex:[collectionSelection indexOfSelectedItem]];
NSString * _ID = (NSString *)[_IDs objectAtIndex:[_idTableView selectedRow]];
NSLog(@"[RetrieveFromDBController retrieveFromDB] selected item: %@",_database);
PolymakeObjectController * pf = [[PolymakeObjectController alloc] init];
if ( [pf readFromDatabase:_database andCollection:_collection withID:_ID] ) {
[pf makeWindowControllers];
[pf showWindows];
[[NSDocumentController sharedDocumentController] addDocument:pf];
}
[pf release];
}
/***************************************************************/
// action if the query button is pressed
- (IBAction)queryDB:(id)sender {
NSLog(@"[RetrieveFromDBController queryDB:sender] entered");
NSString * selectedDatabase = [databaseSelection objectValueOfSelectedItem];
NSString * selectedCollection = [collectionSelection objectValueOfSelectedItem];
NSInteger count = 0;
if ( [selectedCollection length] != 0 ) {
count = [[[NSApp delegate] databaseConnection] queryDBwithDatabase:selectedDatabase
andCollection:selectedCollection];
NSString * numberReportTotal = [NSString stringWithFormat:@"elements in database: %ld", count];
[_reportTotalNumberOfResultsLabel setStringValue:numberReportTotal];
if ( count > [[[[NSApp delegate] databaseConnection] amount] intValue] )
[_reportTotalNumberOfResultsLabel setTextColor:[NSColor colorWithCalibratedRed:0.914 green:0.686 blue:0.227 alpha:1]];
}
_IDs = nil;
}
/***************************************************************/
- (IBAction) getIdsForCurrentSelections:(id)sender {
[self updateCollection];
NSLog(@"[RetrieveFromDBController GetIdsForCurrentSelections] starting table reload");
[_idTableView reloadData];
}
/***************************************************************/
// combo boxes:
// - databases : _databases
// - collections in databases: _collections
// - IDs of objects in database: _IDs
- (void)comboBoxSelectionDidChange:(NSNotification *)notification {
NSLog(@"[RetrieveFromDBController comboBoxSelectionDidChange] entered");
id myObject = [notification object];
if ( myObject == databaseSelection ) { // the selected database has changed, so we retrieve the list of collections
[self updateCollectionList];
} else {
// currently do nothing if other sections change
}
}
/***************************************************************/
// editing of text fields
// currently does nothing
// amount and skip attached via bindings
- (void)controlTextDidEndEditing:(NSNotification *)notification {
NSTextField *textField = [notification object];
NSLog(@"[RetrieveDromDBController controlTextDidChange] %@", [textField stringValue]);
}
// other methods
/***************************************************************/
- (void) updateCollectionList {
NSString * selectedDatabase = [databaseSelection objectValueOfSelectedItem];
NSLog(@"[RetrieveFromDBController updateCollectionList] selected database: %@", selectedDatabase);
if ( _collections != nil )
[_collections release];
if ( _databases != nil ) {
_collections = [[[NSApp delegate] collectionNamesOfDatabase:selectedDatabase] retain];
NSLog(@"[RetrieveFromDBController updateCollectionList] got collections: %@", _collections);
NSLog(@"[RetrieveFromDBController updateCollectionList] of size: %lu", (unsigned long)[_collections count]);
[collectionSelection removeAllItems];
[collectionSelection addItemsWithObjectValues:_collections];
if ( [_collections count] > 0 ) {
[collectionSelection selectItemAtIndex:0];
[collectionSelection setObjectValue:[collectionSelection objectValueOfSelectedItem]];
if ( [_IDs count] > 0 ) {
_IDs = nil;
[_idTableView reloadData];
}
}
}
}
/***************************************************************/
- (void)updateCollection {
NSString * selectedDatabase = [databaseSelection objectValueOfSelectedItem];
NSString * selectedCollection = [collectionSelection objectValueOfSelectedItem];
NSLog(@"[RetrieveFromDBController updateCollection] selected database: %@", selectedDatabase);
NSLog(@"[RetrieveFromDBController updateCollection] selected collection: %@", selectedCollection);
if ( [selectedCollection length] != 0 ) {
/*
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter release];
*/
if ( _IDs != nil )
[_IDs release];
_IDs = [[NSApp delegate] idsForDatabase:selectedDatabase
andCollection:selectedCollection
withAddtionalProperties:[[[NSApp delegate] databaseConnection] additionalPropertiesAsString]
restrictToAmount:[[[[NSApp delegate] databaseConnection] amount] intValue]
startingAt:[[[[NSApp delegate] databaseConnection] skip] intValue]];
[_IDs retain];
// report how many IDs actually match the query
NSString * numberReport = [NSString stringWithFormat:@"query size: %lu",(unsigned long)[_IDs count]];
[_reportTotalNumberOfResultsLabel setTextColor:[NSColor colorWithCalibratedWhite:0 alpha:1.0]];
[_reportNumberOfResultsLabel setStringValue:numberReport];
}
}
/***************************************************************/
- (NSInteger) numberOfRowsInTableView:(NSTableView *)tv {
NSLog(@"[RetrieveFromDBController numberOfRowsInTableView] called");
return [_IDs count];
}
/***************************************************************/
- (id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)column row:(NSInteger) row {
NSLog(@"[RetrieveFromDBController tableView ObjectValueTableColumn column row] called for row %ld", (long)row);
NSString * value = _IDs[row];
return value;
}
/****************/
- (void) tableViewSelectionDidChange: (NSNotification *) notification {
NSLog(@"[RetrieveFromDBController tableViewSelectionDidChange] called");
int row = [_idTableView selectedRow];
if ( row != -1 ) {
}
}
@end