[Bio] / Sprout / CustomAttributes.pm Repository:
ViewVC logotype

Annotation of /Sprout/CustomAttributes.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     package CustomAttributes;
4 :    
5 :     require Exporter;
6 :     use ERDB;
7 : parrello 1.4 @ISA = qw(ERDB);
8 : parrello 1.1 use strict;
9 :     use Tracer;
10 :     use ERDBLoad;
11 :    
12 :     =head1 Custom SEED Attribute Manager
13 :    
14 :     =head2 Introduction
15 :    
16 :     The Custom SEED Attributes Manager allows the user to upload and retrieve
17 :     custom data for SEED objects. It uses the B<ERDB> database system to
18 : parrello 1.10 store the attributes.
19 :    
20 :     Attributes are organized by I<attribute key>. Attribute values are
21 :     assigned to I<objects>. In the real world, objects have types and IDs;
22 :     however, to the attribute database only the ID matters. This will create
23 :     a problem if we have a single ID that applies to two objects of different
24 :     types, but it is more consistent with the original attribute implementation
25 : parrello 1.11 in the SEED (which this implementation replaces).
26 : parrello 1.10
27 : parrello 1.11 The actual attribute values are stored as a relationship between the attribute
28 :     keys and the objects. There can be multiple values for a single key/object pair.
29 : parrello 1.1
30 :     The full suite of ERDB retrieval capabilities is provided. In addition,
31 :     custom methods are provided specific to this application. To get all
32 : parrello 1.6 the values of the attribute C<essential> in a specified B<Feature>, you
33 : parrello 1.1 would code
34 :    
35 : parrello 1.10 my @values = $attrDB->GetAttributes($fid, 'essential');
36 : parrello 1.1
37 : parrello 1.10 where I<$fid> contains the ID of the desired feature.
38 : parrello 1.1
39 : parrello 1.10 New attribute keys must be defined before they can be used. A web interface
40 :     is provided for this purpose.
41 : parrello 1.1
42 :     =head2 FIG_Config Parameters
43 :    
44 :     The following configuration parameters are used to manage custom attributes.
45 :    
46 :     =over 4
47 :    
48 :     =item attrDbms
49 :    
50 :     Type of database manager used: C<mysql> for MySQL or C<pg> for PostGres.
51 :    
52 :     =item attrDbName
53 :    
54 :     Name of the attribute database.
55 :    
56 :     =item attrHost
57 :    
58 :     Name of the host server for the database. If omitted, the current host
59 :     is used.
60 :    
61 :     =item attrUser
62 :    
63 :     User name for logging in to the database.
64 :    
65 :     =item attrPass
66 :    
67 :     Password for logging in to the database.
68 :    
69 :     =item attrPort
70 :    
71 :     TCP/IP port for accessing the database.
72 :    
73 :     =item attrSock
74 :    
75 :     Socket name used to access the database. If omitted, the default socket
76 :     will be used.
77 :    
78 :     =item attrDBD
79 :    
80 :     Fully-qualified file name for the database definition XML file. This file
81 :     functions as data to the attribute management process, so if the data is
82 :     moved, this file must go with it.
83 :    
84 :     =back
85 :    
86 :     =head2 Public Methods
87 :    
88 :     =head3 new
89 :    
90 : parrello 1.3 C<< my $attrDB = CustomAttributes->new($splitter); >>
91 : parrello 1.1
92 : parrello 1.10 Construct a new CustomAttributes object.
93 : parrello 1.3
94 :     =over 4
95 :    
96 :     =item splitter
97 :    
98 :     Value to be used to split attribute values into sections in the
99 :     L</Fig Replacement Methods>. The default is a double colon C<::>.
100 :     If you do not use the replacement methods, you do not need to
101 :     worry about this parameter.
102 :    
103 :     =back
104 : parrello 1.1
105 :     =cut
106 :    
107 :     sub new {
108 :     # Get the parameters.
109 : parrello 1.3 my ($class, $splitter) = @_;
110 : parrello 1.1 # Connect to the database.
111 :     my $dbh = DBKernel->new($FIG_Config::attrDbms, $FIG_Config::attrDbName,
112 :     $FIG_Config::attrUser, $FIG_Config::attrPass,
113 :     $FIG_Config::attrPort, $FIG_Config::attrHost,
114 :     $FIG_Config::attrSock);
115 :     # Create the ERDB object.
116 :     my $xmlFileName = $FIG_Config::attrDBD;
117 :     my $retVal = ERDB::new($class, $dbh, $xmlFileName);
118 : parrello 1.3 # Store the splitter value.
119 :     $retVal->{splitter} = (defined($splitter) ? $splitter : '::');
120 : parrello 1.1 # Return the result.
121 :     return $retVal;
122 :     }
123 :    
124 : parrello 1.10 =head3 StoreAttributeKey
125 :    
126 :     C<< $attrDB->StoreAttributeKey($attributeName, $type, $notes, \@groups); >>
127 :    
128 :     Create or update an attribute for the database.
129 :    
130 :     =over 4
131 : parrello 1.1
132 :     =item attributeName
133 :    
134 :     Name of the attribute. It must be a valid ERDB field name, consisting entirely of
135 :     letters, digits, and hyphens, with a letter at the beginning. If it does not
136 :     exist already, it will be created.
137 :    
138 :     =item type
139 :    
140 :     Data type of the attribute. This must be a valid ERDB data type name.
141 :    
142 :     =item notes
143 :    
144 :     Descriptive notes about the attribute. It is presumed to be raw text, not HTML.
145 :    
146 : parrello 1.10 =item groups
147 : parrello 1.1
148 : parrello 1.10 Reference to a list of the groups to which the attribute should be associated.
149 :     This will replace any groups to which the attribute is currently attached.
150 : parrello 1.1
151 :     =back
152 :    
153 :     =cut
154 :    
155 : parrello 1.3 sub StoreAttributeKey {
156 : parrello 1.1 # Get the parameters.
157 : parrello 1.10 my ($self, $attributeName, $type, $notes, $groups) = @_;
158 : parrello 1.8 # Declare the return variable.
159 :     my $retVal;
160 : parrello 1.1 # Get the data type hash.
161 :     my %types = ERDB::GetDataTypes();
162 :     # Validate the initial input values.
163 :     if (! ERDB::ValidateFieldName($attributeName)) {
164 :     Confess("Invalid attribute name \"$attributeName\" specified.");
165 :     } elsif (! $notes || length($notes) < 25) {
166 :     Confess("Missing or incomplete description for $attributeName.");
167 :     } elsif (! exists $types{$type}) {
168 :     Confess("Invalid data type \"$type\" for $attributeName.");
169 :     } else {
170 : parrello 1.10 # Okay, we're ready to begin. See if this key exists.
171 :     my $attribute = $self->GetEntity('AttributeKey', $attributeName);
172 :     if (defined($attribute)) {
173 :     # It does, so we do an update.
174 :     $self->UpdateEntity('AttributeKey', $attributeName,
175 :     { description => $notes, 'data-type' => $type });
176 :     # Detach the key from its current groups.
177 :     $self->Disconnect('IsInGroup', 'AttributeKey', $attributeName);
178 :     } else {
179 :     # It doesn't, so we do an insert.
180 :     $self->InsertObject('AttributeKey', { id => $attributeName,
181 :     description => $notes, 'data-type' => $type });
182 : parrello 1.8 }
183 : parrello 1.10 # Attach the key to the specified groups. (We presume the groups already
184 :     # exist.)
185 :     for my $group (@{$groups}) {
186 :     $self->InsertObject('IsInGroup', { 'from-link' => $attributeName,
187 :     'to-link' => $group });
188 : parrello 1.1 }
189 :     }
190 :     }
191 :    
192 : parrello 1.3 =head3 LoadAttributeKey
193 : parrello 1.1
194 : parrello 1.10 C<< my $stats = $attrDB->LoadAttributeKey($keyName, $fh, $keyCol, $dataCol, %options); >>
195 : parrello 1.1
196 :     Load the specified attribute from the specified file. The file should be a
197 :     tab-delimited file with internal tab and new-line characters escaped. This is
198 :     the typical TBL-style file used by most FIG applications. One of the columns
199 : parrello 1.10 in the input file must contain the appropriate object id value and the other the
200 : parrello 1.1 corresponding attribute value.
201 :    
202 :     =over 4
203 :    
204 : parrello 1.10 =item keyName
205 : parrello 1.1
206 : parrello 1.10 Key of the attribute to load.
207 : parrello 1.1
208 :     =item fh
209 :    
210 :     Open file handle for the input file.
211 :    
212 : parrello 1.10 =item idCol
213 : parrello 1.1
214 : parrello 1.10 Index (0-based) of the column containing the ID field. The ID field should
215 : parrello 1.1 contain the ID of an instance of the named entity.
216 :    
217 :     =item dataCol
218 :    
219 :     Index (0-based) of the column containing the data value field.
220 :    
221 : parrello 1.10 =item options
222 :    
223 :     Hash specifying the options for this load.
224 :    
225 : parrello 1.1 =item RETURN
226 :    
227 :     Returns a statistics object for the load process.
228 :    
229 :     =back
230 :    
231 : parrello 1.10 The available options are as follows.
232 :    
233 :     =over 4
234 :    
235 :     =item erase
236 :    
237 :     If TRUE, the key's values will all be erased before loading. (Doing so
238 :     makes for a faster load.)
239 :    
240 :     =back
241 :    
242 : parrello 1.1 =cut
243 :    
244 : parrello 1.3 sub LoadAttributeKey {
245 : parrello 1.1 # Get the parameters.
246 : parrello 1.10 my ($self, $keyName, $fh, $idCol, $dataCol, %options) = @_;
247 : parrello 1.1 # Create the return variable.
248 : parrello 1.11 my $retVal = Stats->new("lineIn", "shortLine", "newObject");
249 : parrello 1.10 # Compute the minimum number of fields required in each input line.
250 :     my $minCols = ($idCol < $dataCol ? $idCol : $idCol) + 1;
251 :     # Insure the attribute key exists.
252 :     my $found = $self->GetEntity('AttributeKey', $keyName);
253 :     if (! defined $found) {
254 :     Confess("Attribute key \"$keyName\" not found in database.");
255 : parrello 1.1 } else {
256 : parrello 1.11 # Erase the key's current values.
257 :     $self->EraseAttribute($keyName);
258 :     # Save a list of the object IDs we need to add.
259 :     my %objectIDs = ();
260 : parrello 1.10 # Loop through the input file.
261 :     while (! eof $fh) {
262 :     # Get the next line of the file.
263 :     my @fields = Tracer::GetLine($fh);
264 :     $retVal->Add(lineIn => 1);
265 :     # Now we need to validate the line.
266 :     if (scalar(@fields) < $minCols) {
267 :     $retVal->Add(shortLine => 1);
268 :     } else {
269 :     # It's valid, so get the ID and value.
270 :     my ($id, $value) = ($fields[$idCol], $fields[$dataCol]);
271 :     # Denote we're using this input line.
272 :     $retVal->Add(lineUsed => 1);
273 : parrello 1.11 # Now the fun begins. Find out if we need to create a target object record for this object ID.
274 :     if (! exists $objectIDs{$id}) {
275 :     my $found = $self->Exists('TargetObject', $id);
276 :     if (! $found) {
277 :     $self->InsertObject('TargetObject', { id => $id });
278 : parrello 1.10 }
279 : parrello 1.11 $objectIDs{$id} = 1;
280 :     $retVal->Add(newObject => 1);
281 : parrello 1.1 }
282 : parrello 1.11 # Now we insert the attribute.
283 :     $self->InsertObject('HasValueFor', { from => $keyName, to => $id, value => $value });
284 :     $retVal->Add(newValue => 1);
285 : parrello 1.1 }
286 :     }
287 :     }
288 :     # Return the statistics.
289 :     return $retVal;
290 :     }
291 :    
292 :    
293 : parrello 1.3 =head3 DeleteAttributeKey
294 :    
295 : parrello 1.10 C<< my $stats = $attrDB->DeleteAttributeKey($attributeName); >>
296 : parrello 1.1
297 :     Delete an attribute from the custom attributes database.
298 :    
299 :     =over 4
300 :    
301 : parrello 1.10 =item attributeName
302 : parrello 1.1
303 : parrello 1.10 Name of the attribute to delete.
304 : parrello 1.1
305 : parrello 1.10 =item RETURN
306 : parrello 1.1
307 : parrello 1.10 Returns a statistics object describing the effects of the deletion.
308 : parrello 1.1
309 :     =back
310 :    
311 :     =cut
312 :    
313 : parrello 1.3 sub DeleteAttributeKey {
314 : parrello 1.1 # Get the parameters.
315 : parrello 1.10 my ($self, $attributeName) = @_;
316 :     # Delete the attribute key.
317 :     my $retVal = $self->Delete('AttributeKey', $attributeName);
318 :     # Return the result.
319 :     return $retVal;
320 :    
321 :     }
322 :    
323 :     =head3 NewName
324 :    
325 :     C<< my $text = CustomAttributes::NewName(); >>
326 :    
327 :     Return the string used to indicate the user wants to add a new attribute.
328 :    
329 :     =cut
330 :    
331 :     sub NewName {
332 :     return "(new)";
333 : parrello 1.1 }
334 :    
335 :     =head3 ControlForm
336 :    
337 : parrello 1.10 C<< my $formHtml = $attrDB->ControlForm($cgi, $name, \%keys); >>
338 : parrello 1.1
339 :     Return a form that can be used to control the creation and modification of
340 : parrello 1.10 attributes. Only a subset of the attribute keys will be displayed, as
341 :     determined by the incoming list.
342 : parrello 1.1
343 :     =over 4
344 :    
345 :     =item cgi
346 :    
347 :     CGI query object used to create HTML.
348 :    
349 :     =item name
350 :    
351 :     Name to give to the form. This should be unique for the web page.
352 :    
353 : parrello 1.10 =item keys
354 :    
355 :     Reference to a hash mapping attribute keys to n-tuples. Each tuple will contain the
356 :     attribute's data type, its description, and a list of the groups in which it participates.
357 :    
358 : parrello 1.1 =item RETURN
359 :    
360 : parrello 1.10 Returns the HTML for a form that can be used to submit instructions to the C<Attributes.cgi> script
361 :     for loading, creating, displaying, changing, or deleting an attribute. Note that only the form
362 :     controls are generated. The form tags are left to the caller.
363 : parrello 1.1
364 :     =back
365 :    
366 :     =cut
367 :    
368 :     sub ControlForm {
369 :     # Get the parameters.
370 : parrello 1.10 my ($self, $cgi, $name, $keys) = @_;
371 : parrello 1.1 # Declare the return list.
372 :     my @retVal = ();
373 :     # We'll put the controls in a table. Nothing else ever seems to look nice.
374 :     push @retVal, $cgi->start_table({ border => 2, cellpadding => 2 });
375 :     # The first row is for selecting the field name.
376 :     push @retVal, $cgi->Tr($cgi->th("Select a Field"),
377 : parrello 1.10 $cgi->td($self->FieldMenu($cgi, 10, 'fieldName', $keys,
378 :     new => 1,
379 :     notes => "document.$name.notes.value",
380 :     type => "document.$name.dataType.value",
381 :     groups => "document.$name.groups")));
382 : parrello 1.1 # Now we set up a dropdown for the data types. The values will be the
383 :     # data type names, and the labels will be the descriptions.
384 :     my %types = ERDB::GetDataTypes();
385 :     my %labelMap = map { $_ => $types{$_}->{notes} } keys %types;
386 :     my $typeMenu = $cgi->popup_menu(-name => 'dataType',
387 :     -values => [sort keys %types],
388 : parrello 1.10 -labels => \%labelMap,
389 :     -default => 'string');
390 :     # Allow the user to specify a new field name. This is required if the
391 :     # user has selected the "(new)" marker. We put a little scriptlet in here that
392 :     # selects the (new) marker when the user enters the field.
393 :     push @retVal, "<script language=\"javaScript\">";
394 :     my $fieldField = "document.$name.fieldName";
395 :     my $newName = "\"" . NewName() . "\"";
396 :     push @retVal, $cgi->Tr($cgi->th("New Field Name"),
397 :     $cgi->td($cgi->textfield(-name => 'newName',
398 :     -size => 30,
399 :     -value => "",
400 :     -onFocus => "setIfEmpty($fieldField, $newName);")),
401 :     );
402 : parrello 1.1 push @retVal, $cgi->Tr($cgi->th("Data type"),
403 :     $cgi->td($typeMenu));
404 :     # The next row is for the notes.
405 :     push @retVal, $cgi->Tr($cgi->th("Description"),
406 :     $cgi->td($cgi->textarea(-name => 'notes',
407 :     -rows => 6,
408 :     -columns => 80))
409 :     );
410 : parrello 1.10 # Now we have the groups, which are implemented as a checkbox group.
411 :     my @groups = $self->GetGroups();
412 :     push @retVal, $cgi->Tr($cgi->th("Groups"),
413 :     $cgi->td($cgi->checkbox_group(-name=>'groups',
414 :     -values=> \@groups))
415 :     );
416 : parrello 1.1 # If the user wants to upload new values for the field, then we have
417 :     # an upload file name and column indicators.
418 :     push @retVal, $cgi->Tr($cgi->th("Upload Values"),
419 :     $cgi->td($cgi->filefield(-name => 'newValueFile',
420 :     -size => 20) .
421 :     " Key&nbsp;" .
422 :     $cgi->textfield(-name => 'keyCol',
423 :     -size => 3,
424 :     -default => 0) .
425 :     " Value&nbsp;" .
426 :     $cgi->textfield(-name => 'valueCol',
427 :     -size => 3,
428 :     -default => 1)
429 :     ),
430 :     );
431 : parrello 1.10 # Now the three buttons: STORE, SHOW, and DELETE.
432 : parrello 1.1 push @retVal, $cgi->Tr($cgi->th("&nbsp;"),
433 :     $cgi->td({align => 'center'},
434 :     $cgi->submit(-name => 'Delete', -value => 'DELETE') . " " .
435 : parrello 1.7 $cgi->submit(-name => 'Store', -value => 'STORE') . " " .
436 :     $cgi->submit(-name => 'Show', -value => 'SHOW')
437 : parrello 1.1 )
438 :     );
439 :     # Close the table and the form.
440 :     push @retVal, $cgi->end_table();
441 :     # Return the assembled HTML.
442 :     return join("\n", @retVal, "");
443 :     }
444 :    
445 : parrello 1.11 =head3 LoadAttributesFrom
446 :    
447 :     C<< my $stats = $attrDB->LoadAttributesFrom($fileName, %options); >>
448 :    
449 :     Load attributes from the specified tab-delimited file. Each line of the file must
450 :     contain an object ID in the first column, an attribute key name in the second
451 :     column, and attribute values in the remaining columns. The attribute values will
452 :     be assembled into a single value using the splitter code.
453 :    
454 :     =over 4
455 :    
456 :     =item fileName
457 :    
458 :     Name of the file from which to load the attributes.
459 :    
460 :     =item options
461 :    
462 :     Hash of options for modifying the load process.
463 :    
464 :     =item RETURN
465 :    
466 :     Returns a statistics object describing the load.
467 :    
468 :     =back
469 :    
470 :     Permissible option values are as follows.
471 :    
472 :     =over 4
473 :    
474 :     =item append
475 :    
476 :     If TRUE, then the attributes will be appended to existing data; otherwise, the
477 :     first time a key name is encountered, it will be erased.
478 :    
479 :     =back
480 :    
481 :     =cut
482 :    
483 :     sub LoadAttributesFrom {
484 :     # Get the parameters.
485 :     my ($self, $fileName, %options) = @_;
486 :     # Declare the return variable.
487 :     my $retVal = Stats->new('keys', 'values');
488 :     # Check for append mode.
489 :     my $append = ($options{append} ? 1 : 0);
490 :     # Create a hash of key names found.
491 :     my %keyHash = ();
492 :     # Open the file for input.
493 :     my $fh = Open(undef, "<$fileName");
494 :     # Loop through the file.
495 :     while (! eof $fh) {
496 :     my ($id, $key, @values) = Tracer::GetLine($fh);
497 :     $retVal->Add(linesIn => 1);
498 :     # Do some validation.
499 :     if (! defined($id)) {
500 :     # We ignore blank lines.
501 :     $retVal->Add(blankLines => 1);
502 :     } elsif (! defined($key)) {
503 :     # An ID without a key is a serious error.
504 :     my $lines = $retVal->Ask('linesIn');
505 :     Confess("Line $lines in $fileName has no attribute key.");
506 :     } else {
507 :     # Now we need to check for a new key.
508 :     if (! exists $keyHash{$key}) {
509 :     # This is a new key. Verify that it exists.
510 :     if (! $self->Exists('AttributeKey', $key)) {
511 :     my $line = $retVal->Ask('linesIn');
512 :     Confess("Attribute \"$key\" on line $line of $fileName not found in database.");
513 :     } else {
514 :     # Make sure we know this is no longer a new key.
515 :     $keyHash{$key} = 1;
516 :     $retVal->Add(keys => 1);
517 :     # If this is NOT append mode, erase the key.
518 :     if (! $append) {
519 :     $self->EraseAttribute($key);
520 :     }
521 :     }
522 :     Trace("Key $key found.") if T(3);
523 :     }
524 :     # Now we know the key is valid. Add this value.
525 :     $self->AddAttribute($id, $key, @values);
526 :     my $progress = $retVal->Add(values => 1);
527 :     Trace("$progress values loaded.") if T(3) && ($progress % 1000 == 0);
528 :    
529 :     }
530 :     }
531 :     # Return the result.
532 :     return $retVal;
533 :     }
534 :    
535 :     =head3 BackupAllAttributes
536 :    
537 :     C<< my $stats = $attrDB->BackupAllAttributes($fileName, %options); >>
538 :    
539 :     Backup all of the attributes to a file. The attributes will be stored in a
540 :     tab-delimited file suitable for reloading via L</LoadAttributesFrom>.
541 :    
542 :     =over 4
543 :    
544 :     =item fileName
545 :    
546 :     Name of the file to which the attribute data should be backed up.
547 :    
548 :     =item options
549 :    
550 :     Hash of options for the backup.
551 :    
552 :     =item RETURN
553 :    
554 :     Returns a statistics object describing the backup.
555 :    
556 :     =back
557 :    
558 :     Currently there are no options defined.
559 :    
560 :     =cut
561 :    
562 :     sub BackupAllAttributes {
563 :     # Get the parameters.
564 :     my ($self, $fileName, %options) = @_;
565 :     # Declare the return variable.
566 :     my $retVal = Stats->new();
567 :     # Get a list of the keys.
568 :     my @keys = $self->GetFlat(['AttributeKey'], "", [], 'AttributeKey(id)');
569 :     Trace(scalar(@keys) . " keys found during backup.") if T(2);
570 :     # Open the file for output.
571 :     my $fh = Open(undef, $fileName);
572 :     # Loop through the keys.
573 :     for my $key (@keys) {
574 :     Trace("Backing up attribute $key.") if T(3);
575 :     $retVal->Add(keys => 1);
576 :     # Loop through this key's values.
577 :     my $query = $self->Get(['HasValueFor'], "HasValueFor(to-link) = ?", [$key]);
578 :     my $valuesFound = 0;
579 :     while (my $line = $query->Fetch()) {
580 :     $valuesFound++;
581 :     # Get this row's data.
582 :     my @row = $line->Values(['HasValueFor(from-link)', 'HasValueFor(to-link)',
583 :     'HasValueFor(value)']);
584 :     # Write it to the file.
585 :     Tracer::PutLine($fh, \@row);
586 :     }
587 :     Trace("$valuesFound values backed up for key $key.") if T(3);
588 :     $retVal->Add(values => $valuesFound);
589 :     }
590 :     # Return the result.
591 :     return $retVal;
592 :     }
593 :    
594 : parrello 1.1 =head3 FieldMenu
595 :    
596 : parrello 1.10 C<< my $menuHtml = $attrDB->FieldMenu($cgi, $height, $name, $keys, %options); >>
597 : parrello 1.1
598 :     Return the HTML for a menu to select an attribute field. The menu will
599 :     be a standard SELECT/OPTION thing which is called "popup menu" in the
600 :     CGI package, but actually looks like a list. The list will contain
601 : parrello 1.10 one selectable row per field.
602 : parrello 1.1
603 :     =over 4
604 :    
605 :     =item cgi
606 :    
607 :     CGI query object used to generate HTML.
608 :    
609 :     =item height
610 :    
611 :     Number of lines to display in the list.
612 :    
613 :     =item name
614 :    
615 :     Name to give to the menu. This is the name under which the value will
616 :     appear when the form is submitted.
617 :    
618 : parrello 1.10 =item keys
619 :    
620 :     Reference to a hash mapping each attribute key name to a list reference,
621 :     the list itself consisting of the attribute data type, its description,
622 :     and a list of its groups.
623 :    
624 :     =item options
625 :    
626 :     Hash containing options that modify the generation of the menu.
627 :    
628 :     =item RETURN
629 :    
630 :     Returns the HTML to create a form field that can be used to select an
631 :     attribute from the custom attributes system.
632 :    
633 :     =back
634 :    
635 :     The permissible options are as follows.
636 :    
637 :     =over 4
638 :    
639 :     =item new
640 : parrello 1.1
641 :     If TRUE, then extra rows will be provided to allow the user to select
642 :     a new attribute. In other words, the user can select an existing
643 :     attribute, or can choose a C<(new)> marker to indicate a field to
644 :     be created in the parent entity.
645 :    
646 : parrello 1.10 =item notes
647 : parrello 1.1
648 :     If specified, the name of a variable for displaying the notes attached
649 :     to the field. This must be in Javascript form ready for assignment.
650 :     So, for example, if you have a variable called C<notes> that
651 :     represents a paragraph element, you should code C<notes.innerHTML>.
652 :     If it actually represents a form field you should code C<notes.value>.
653 :     If an C<innerHTML> coding is used, the text will be HTML-escaped before
654 :     it is copied in. Specifying this parameter generates Javascript for
655 :     displaying the field description when a field is selected.
656 :    
657 : parrello 1.10 =item type
658 : parrello 1.1
659 :     If specified, the name of a variable for displaying the field's
660 :     data type. Data types are a much more controlled vocabulary than
661 :     notes, so there is no worry about HTML translation. Instead, the
662 :     raw value is put into the specified variable. Otherwise, the same
663 :     rules apply to this value that apply to I<$noteControl>.
664 :    
665 : parrello 1.10 =item groups
666 : parrello 1.1
667 : parrello 1.10 If specified, the name of a multiple-selection list control (also called
668 :     a popup menu) which shall be used to display the selected groups.
669 : parrello 1.1
670 :     =back
671 :    
672 :     =cut
673 :    
674 :     sub FieldMenu {
675 :     # Get the parameters.
676 : parrello 1.10 my ($self, $cgi, $height, $name, $keys, %options) = @_;
677 :     # Reformat the list of keys.
678 :     my %keys = %{$keys};
679 :     # Add the (new) key, if needed.
680 :     if ($options{new}) {
681 :     $keys{NewName()} = ["string", ""];
682 : parrello 1.1 }
683 : parrello 1.10 # Get a sorted list of key.
684 :     my @keys = sort keys %keys;
685 :     # We need to create the name for the onChange function. This function
686 : parrello 1.1 # may not do anything, but we need to know the name to generate the HTML
687 :     # for the menu.
688 :     my $changeName = "${name}_setNotes";
689 :     my $retVal = $cgi->popup_menu({name => $name,
690 :     size => $height,
691 :     onChange => "$changeName(this.value)",
692 : parrello 1.10 values => \@keys,
693 :     });
694 : parrello 1.1 # Create the change function.
695 :     $retVal .= "\n<script language=\"javascript\">\n";
696 :     $retVal .= " function $changeName(fieldValue) {\n";
697 : parrello 1.10 # The function only has a body if we have a control to store data about the
698 :     # attribute.
699 :     if ($options{notes} || $options{type} || $options{groups}) {
700 : parrello 1.1 # Check to see if we're storing HTML or text into the note control.
701 : parrello 1.10 my $noteControl = $options{notes};
702 : parrello 1.1 my $htmlMode = ($noteControl && $noteControl =~ /innerHTML$/);
703 :     # We use a CASE statement based on the newly-selected field value. The
704 :     # field description will be stored in the JavaScript variable "myText"
705 :     # and the data type in "myType". Note the default data type is a normal
706 :     # string, but the default notes is an empty string.
707 :     $retVal .= " var myText = \"\";\n";
708 :     $retVal .= " var myType = \"string\";\n";
709 :     $retVal .= " switch (fieldValue) {\n";
710 : parrello 1.10 # Loop through the keys.
711 :     for my $key (@keys) {
712 :     # Generate this case.
713 :     $retVal .= " case \"$key\" :\n";
714 :     # Here we either want to update the note display, the
715 :     # type display, the group list, or a combination of them.
716 :     my ($type, $notes, @groups) = @{$keys{$key}};
717 :     if ($noteControl) {
718 :     # Insure it's in the proper form.
719 :     if ($htmlMode) {
720 :     $notes = ERDB::HTMLNote($notes);
721 : parrello 1.1 }
722 : parrello 1.10 # Escape it for use as a string literal.
723 :     $notes =~ s/\n/\\n/g;
724 :     $notes =~ s/"/\\"/g;
725 :     $retVal .= " myText = \"$notes\";\n";
726 :     }
727 :     if ($options{type}) {
728 :     # Here we want the type updated.
729 :     $retVal .= " myType = \"$type\";\n";
730 :     }
731 :     if ($options{groups}) {
732 :     # Here we want the groups shown. Get a list of this attribute's groups.
733 :     # We'll search through this list for each group to see if it belongs with
734 :     # our attribute.
735 :     my $groupLiteral = "=" . join("=", @groups) . "=";
736 :     # Now we need some variables containing useful code for the javascript. It's
737 :     # worth knowing we go through a bit of pain to insure $groupField[i] isn't
738 :     # parsed as an array element.
739 :     my $groupField = $options{groups};
740 :     my $currentField = $groupField . "[i]";
741 :     # Do the javascript.
742 :     $retVal .= " var groupList = \"$groupLiteral\";\n";
743 :     $retVal .= " for (var i = 0; i < $groupField.length; i++) {\n";
744 :     $retVal .= " var srchString = \"=\" + $currentField.value + \"=\";\n";
745 :     $retVal .= " var srchLoc = groupList.indexOf(srchString);\n";
746 :     $retVal .= " $currentField.checked = (srchLoc >= 0);\n";
747 :     $retVal .= " }\n";
748 : parrello 1.1 }
749 : parrello 1.10 # Close this case.
750 :     $retVal .= " break;\n";
751 : parrello 1.1 }
752 :     # Close the CASE statement and make the appropriate assignments.
753 :     $retVal .= " }\n";
754 :     if ($noteControl) {
755 :     $retVal .= " $noteControl = myText;\n";
756 :     }
757 : parrello 1.10 if ($options{type}) {
758 :     $retVal .= " $options{type} = myType;\n";
759 : parrello 1.1 }
760 :     }
761 :     # Terminate the change function.
762 :     $retVal .= " }\n";
763 :     $retVal .= "</script>\n";
764 :     # Return the result.
765 :     return $retVal;
766 :     }
767 :    
768 : parrello 1.10 =head3 GetGroups
769 : parrello 1.3
770 : parrello 1.10 C<< my @groups = $attrDB->GetGroups(); >>
771 : parrello 1.3
772 : parrello 1.10 Return a list of the available groups.
773 : parrello 1.3
774 :     =cut
775 :    
776 : parrello 1.10 sub GetGroups {
777 : parrello 1.3 # Get the parameters.
778 : parrello 1.10 my ($self) = @_;
779 :     # Get the groups.
780 :     my @retVal = $self->GetFlat(['AttributeGroup'], "", [], 'AttributeGroup(id)');
781 :     # Return them.
782 :     return @retVal;
783 : parrello 1.3 }
784 :    
785 : parrello 1.10 =head3 GetAttributeData
786 : parrello 1.3
787 : parrello 1.10 C<< my %keys = $attrDB->GetAttributeData($type, @list); >>
788 : parrello 1.3
789 : parrello 1.10 Return attribute data for the selected attributes. The attribute
790 :     data is a hash mapping each attribute key name to a n-tuple containing the
791 :     data type, the description, and the groups. This is the same format expected in
792 :     the L</FieldMenu> and L</ControlForm> methods for the list of attributes to display.
793 : parrello 1.3
794 :     =over 4
795 :    
796 : parrello 1.10 =item type
797 : parrello 1.4
798 : parrello 1.10 Type of attribute criterion: C<name> for attributes whose names begin with the
799 :     specified string, or C<group> for attributes in the specified group.
800 : parrello 1.4
801 : parrello 1.10 =item list
802 : parrello 1.4
803 : parrello 1.10 List containing the names of the groups or keys for the desired attributes.
804 : parrello 1.4
805 :     =item RETURN
806 :    
807 : parrello 1.10 Returns a hash mapping each attribute key name to its data type, description, and
808 :     parent groups.
809 : parrello 1.4
810 :     =back
811 :    
812 :     =cut
813 :    
814 : parrello 1.10 sub GetAttributeData {
815 : parrello 1.4 # Get the parameters.
816 : parrello 1.10 my ($self, $type, @list) = @_;
817 :     # Set up a hash to store the attribute data.
818 :     my %retVal = ();
819 :     # Loop through the list items.
820 :     for my $item (@list) {
821 :     # Set up a query for the desired attributes.
822 :     my $query;
823 :     if ($type eq 'name') {
824 :     # Here we're doing a generic name search. We need to escape it and then tack
825 :     # on a %.
826 :     my $parm = $item;
827 :     $parm =~ s/_/\\_/g;
828 :     $parm =~ s/%/\\%/g;
829 :     $parm .= "%";
830 :     # Ask for matching attributes. (Note that if the user passed in a null string
831 :     # he'll get everything.)
832 :     $query = $self->Get(['AttributeKey'], "AttributeKey(id) LIKE ?", [$parm]);
833 :     } elsif ($type eq 'group') {
834 :     $query = $self->Get(['IsInGroup', 'AttributeKey'], "IsInGroup(to-link) = ?", [$item]);
835 : parrello 1.4 } else {
836 : parrello 1.10 Confess("Unknown attribute query type \"$type\".");
837 :     }
838 :     while (my $row = $query->Fetch()) {
839 :     # Get this attribute's data.
840 :     my ($key, $type, $notes) = $row->Values(['AttributeKey(id)', 'AttributeKey(data-type)',
841 :     'AttributeKey(description)']);
842 :     # If it's new, get its groups and add it to the return hash.
843 :     if (! exists $retVal{$key}) {
844 :     my @groups = $self->GetFlat(['IsInGroup'], "IsInGroup(from-link) = ?",
845 :     [$key], 'IsInGroup(to-link)');
846 :     $retVal{$key} = [$type, $notes, @groups];
847 : parrello 1.4 }
848 :     }
849 :     }
850 :     # Return the result.
851 : parrello 1.10 return %retVal;
852 : parrello 1.4 }
853 :    
854 : parrello 1.3 =head2 FIG Method Replacements
855 :    
856 :     The following methods are used by B<FIG.pm> to replace the previous attribute functionality.
857 : parrello 1.10 Some of the old functionality is no longer present: controlled vocabulary is no longer
858 : parrello 1.3 supported and there is no longer any searching by URL. Fortunately, neither of these
859 :     capabilities were used in the old system.
860 :    
861 : parrello 1.4 The methods here are the only ones supported by the B<RemoteCustomAttributes> object.
862 :     The idea is that these methods represent attribute manipulation allowed by all users, while
863 :     the others are only for privileged users with access to the attribute server.
864 :    
865 : parrello 1.3 In the previous implementation, an attribute had a value and a URL. In the new implementation,
866 :     there is only a value. In this implementation, each attribute has only a value. These
867 :     methods will treat the value as a list with the individual elements separated by the
868 :     value of the splitter parameter on the constructor (L</new>). The default is double
869 :     colons C<::>.
870 :    
871 : parrello 1.10 So, for example, an old-style keyword with a value of C<essential> and a URL of
872 : parrello 1.3 C<http://www.sciencemag.org/cgi/content/abstract/293/5538/2266> using the default
873 :     splitter value would be stored as
874 :    
875 :     essential::http://www.sciencemag.org/cgi/content/abstract/293/5538/2266
876 :    
877 :     The best performance is achieved by searching for a particular key for a specified
878 :     feature or genome.
879 :    
880 :     =head3 GetAttributes
881 :    
882 : parrello 1.10 C<< my @attributeList = $attrDB->GetAttributes($objectID, $key, @values); >>
883 : parrello 1.3
884 :     In the database, attribute values are sectioned into pieces using a splitter
885 :     value specified in the constructor (L</new>). This is not a requirement of
886 :     the attribute system as a whole, merely a convenience for the purpose of
887 : parrello 1.10 these methods. If a value has multiple sections, each section
888 :     is matched against the corresponding criterion in the I<@valuePatterns> list.
889 : parrello 1.3
890 :     This method returns a series of tuples that match the specified criteria. Each tuple
891 :     will contain an object ID, a key, and one or more values. The parameters to this
892 : parrello 1.10 method therefore correspond structurally to the values expected in each tuple. In
893 :     addition, you can ask for a generic search by suffixing a percent sign (C<%>) to any
894 :     of the parameters. So, for example,
895 : parrello 1.3
896 : parrello 1.10 my @attributeList = $attrDB->GetAttributes('fig|100226.1.peg.1004', 'structure%', 1, 2);
897 : parrello 1.3
898 :     would return something like
899 :    
900 :     ['fig}100226.1.peg.1004', 'structure', 1, 2]
901 :     ['fig}100226.1.peg.1004', 'structure1', 1, 2]
902 :     ['fig}100226.1.peg.1004', 'structure2', 1, 2]
903 :     ['fig}100226.1.peg.1004', 'structureA', 1, 2]
904 :    
905 : parrello 1.10 Use of C<undef> in any position acts as a wild card (all values). You can also specify
906 :     a list reference in the ID column. Thus,
907 :    
908 :     my @attributeList = $attrDB->GetAttributes(['100226.1', 'fig|100226.1.%'], 'PUBMED');
909 :    
910 :     would get the PUBMED attribute data for Streptomyces coelicolor A3(2) and all its
911 :     features.
912 : parrello 1.3
913 :     In addition to values in multiple sections, a single attribute key can have multiple
914 :     values, so even
915 :    
916 : parrello 1.10 my @attributeList = $attrDB->GetAttributes($peg, 'virulent');
917 : parrello 1.3
918 :     which has no wildcard in the key or the object ID, may return multiple tuples.
919 :    
920 : parrello 1.10 Value matching in this system works very poorly, because of the way multiple values are
921 :     stored. For the object ID and key name, we create queries that filter for the desired
922 :     results. For the values, we do a comparison after the attributes are retrieved from the
923 :     database. As a result, queries in which filter only on value end up reading the entire
924 :     attribute table to find the desired results.
925 : parrello 1.3
926 :     =over 4
927 :    
928 :     =item objectID
929 :    
930 : parrello 1.10 ID of object whose attributes are desired. If the attributes are desired for multiple
931 :     objects, this parameter can be specified as a list reference. If the attributes are
932 :     desired for all objects, specify C<undef> or an empty string. Finally, you can specify
933 :     attributes for a range of object IDs by putting a percent sign (C<%>) at the end.
934 : parrello 1.3
935 :     =item key
936 :    
937 : parrello 1.10 Attribute key name. A value of C<undef> or an empty string will match all
938 :     attribute keys. If the values are desired for multiple keys, this parameter can be
939 :     specified as a list reference. Finally, you can specify attributes for a range of
940 :     keys by putting a percent sign (C<%>) at the end.
941 : parrello 1.3
942 : parrello 1.10 =item values
943 : parrello 1.3
944 :     List of the desired attribute values, section by section. If C<undef>
945 : parrello 1.10 or an empty string is specified, all values in that section will match. A
946 :     generic match can be requested by placing a percent sign (C<%>) at the end.
947 :     In that case, all values that match up to and not including the percent sign
948 :     will match.
949 : parrello 1.3
950 :     =item RETURN
951 :    
952 :     Returns a list of tuples. The first element in the tuple is an object ID, the
953 :     second is an attribute key, and the remaining elements are the sections of
954 :     the attribute value. All of the tuples will match the criteria set forth in
955 :     the parameter list.
956 :    
957 :     =back
958 :    
959 :     =cut
960 :    
961 :     sub GetAttributes {
962 : parrello 1.4 # Get the parameters.
963 : parrello 1.10 my ($self, $objectID, $key, @values) = @_;
964 :     # We will create one big honking query. The following hash will build the filter
965 :     # clause and a parameter list.
966 : parrello 1.11 my %data = ('HasValueFor(from-link)' => $key, 'HasValueFor(to-link)' => $objectID);
967 : parrello 1.10 my @filter = ();
968 :     my @parms = ();
969 :     # This next loop goes through the different fields that can be specified in the
970 :     # parameter list and generates filters for each.
971 :     for my $field (keys %data) {
972 :     # Accumulate filter information for this field. We will OR together all the
973 :     # elements accumulated to create the final result.
974 :     my @fieldFilter = ();
975 :     # Get the specified data from the caller.
976 :     my $fieldPattern = $data{$field};
977 :     # Only proceed if the pattern is one that won't match everything.
978 :     if (defined($fieldPattern) && $fieldPattern ne "" && $fieldPattern ne "%") {
979 :     # Convert the pattern to an array.
980 :     my @patterns = ();
981 :     if (ref $fieldPattern eq 'ARRAY') {
982 :     push @patterns, @{$fieldPattern};
983 :     } else {
984 :     push @patterns, $fieldPattern;
985 :     }
986 :     # Only proceed if the array is nonempty. The loop will work fine if the
987 :     # array is empty, but when we build the filter string at the end we'll
988 :     # get "()" in the filter list, which will result in an SQL syntax error.
989 :     if (@patterns) {
990 :     # Loop through the individual patterns.
991 :     for my $pattern (@patterns) {
992 :     # Check for a generic request.
993 :     if (substr($pattern, -1, 1) ne '%') {
994 :     # Here we have a normal request.
995 :     push @fieldFilter, "$field = ?";
996 :     push @parms, $pattern;
997 :     } else {
998 :     # Here we have a generate request, so we will use the LIKE operator to
999 :     # filter the field to this value pattern.
1000 :     push @fieldFilter, "$field LIKE ?";
1001 :     # We must convert the pattern value to an SQL match pattern. First
1002 : parrello 1.11 # we get a copy of it.
1003 :     my $actualPattern = $pattern;
1004 : parrello 1.10 # Now we escape the underscores. Underscores are an SQL wild card
1005 :     # character, but they are used frequently in key names and object IDs.
1006 : parrello 1.11 $actualPattern =~ s/_/\\_/g;
1007 : parrello 1.10 # Add the escaped pattern to the bound parameter list.
1008 :     push @parms, $actualPattern;
1009 :     }
1010 :     }
1011 :     # Form the filter for this field.
1012 :     my $fieldFilterString = join(" OR ", @fieldFilter);
1013 :     push @filter, "($fieldFilterString)";
1014 :     }
1015 :     }
1016 :     }
1017 :     # Now @filter contains one or more filter strings and @parms contains the parameter
1018 :     # values to bind to them.
1019 :     my $actualFilter = join(" AND ", @filter);
1020 : parrello 1.3 # Declare the return variable.
1021 :     my @retVal = ();
1022 : parrello 1.10 # Get the number of value sections we have to match.
1023 :     my $sectionCount = scalar(@values);
1024 :     # Now we're ready to make our query.
1025 : parrello 1.11 my $query = $self->Get(['HasValueFor'], $actualFilter, \@parms);
1026 : parrello 1.10 # Loop through the assignments found.
1027 :     while (my $row = $query->Fetch()) {
1028 :     # Get the current row's data.
1029 : parrello 1.11 my ($id, $key, $valueString) = $row->Values(['HasValueFor(to-link)', 'HasValueFor(from-link)',
1030 :     'HasValueFor(value)']);
1031 :     # Break the value into sections.
1032 :     my @sections = split($self->{splitter}, $valueString);
1033 :     # Match each section against the incoming values. We'll assume we're
1034 :     # okay unless we learn otherwise.
1035 :     my $matching = 1;
1036 :     for (my $i = 0; $i < $sectionCount && $matching; $i++) {
1037 :     # We need to check to see if this section is generic.
1038 :     if (substr($values[$i], -1, 1) eq '%') {
1039 :     my $matchLen = length($values[$i] - 1);
1040 :     $matching = substr($sections[$i], 0, $matchLen) eq
1041 :     substr($values[$i], 0, $matchLen);
1042 :     } else {
1043 :     $matching = ($sections[$i] eq $values[$i]);
1044 : parrello 1.10 }
1045 : parrello 1.11 }
1046 :     # If we match, output this row to the return list.
1047 :     if ($matching) {
1048 :     push @retVal, [$id, $key, @sections];
1049 : parrello 1.3 }
1050 :     }
1051 : parrello 1.10 # Return the rows found.
1052 : parrello 1.3 return @retVal;
1053 :     }
1054 :    
1055 :     =head3 AddAttribute
1056 :    
1057 :     C<< $attrDB->AddAttribute($objectID, $key, @values); >>
1058 :    
1059 :     Add an attribute key/value pair to an object. This method cannot add a new key, merely
1060 :     add a value to an existing key. Use L</StoreAttributeKey> to create a new key.
1061 :    
1062 :     =over 4
1063 :    
1064 :     =item objectID
1065 :    
1066 : parrello 1.10 ID of the object to which the attribute is to be added.
1067 : parrello 1.3
1068 :     =item key
1069 :    
1070 : parrello 1.10 Attribute key name.
1071 : parrello 1.3
1072 :     =item values
1073 :    
1074 :     One or more values to be associated with the key. The values are joined together with
1075 :     the splitter value before being stored as field values. This enables L</GetAttributes>
1076 :     to split them apart during retrieval. The splitter value defaults to double colons C<::>.
1077 :    
1078 :     =back
1079 :    
1080 :     =cut
1081 :    
1082 :     sub AddAttribute {
1083 :     # Get the parameters.
1084 : parrello 1.4 my ($self, $objectID, $key, @values) = @_;
1085 : parrello 1.3 # Don't allow undefs.
1086 :     if (! defined($objectID)) {
1087 :     Confess("No object ID specified for AddAttribute call.");
1088 :     } elsif (! defined($key)) {
1089 :     Confess("No attribute key specified for AddAttribute call.");
1090 :     } elsif (! @values) {
1091 :     Confess("No values specified in AddAttribute call for key $key.");
1092 :     } else {
1093 : parrello 1.11 # Okay, now we have some reason to believe we can do this. Form the values
1094 :     # into a scalar.
1095 : parrello 1.3 my $valueString = join($self->{splitter}, @values);
1096 : parrello 1.11 # Connect the object to the key.
1097 :     $self->InsertObject('HasValueFor', { 'from-link' => $key,
1098 :     'to-link' => $objectID,
1099 :     'value' => $valueString,
1100 :     });
1101 : parrello 1.3 }
1102 : parrello 1.10 # Return a one, indicating success. We do this for backward compatability.
1103 : parrello 1.3 return 1;
1104 :     }
1105 :    
1106 :     =head3 DeleteAttribute
1107 :    
1108 :     C<< $attrDB->DeleteAttribute($objectID, $key, @values); >>
1109 :    
1110 :     Delete the specified attribute key/value combination from the database.
1111 :    
1112 :     =over 4
1113 :    
1114 :     =item objectID
1115 :    
1116 : parrello 1.10 ID of the object whose attribute is to be deleted.
1117 : parrello 1.3
1118 :     =item key
1119 :    
1120 : parrello 1.10 Attribute key name.
1121 : parrello 1.3
1122 :     =item values
1123 :    
1124 : parrello 1.10 One or more values associated with the key. If no values are specified, then all values
1125 :     will be deleted. Otherwise, only a matching value will be deleted.
1126 : parrello 1.3
1127 :     =back
1128 :    
1129 :     =cut
1130 :    
1131 :     sub DeleteAttribute {
1132 :     # Get the parameters.
1133 : parrello 1.4 my ($self, $objectID, $key, @values) = @_;
1134 : parrello 1.3 # Don't allow undefs.
1135 :     if (! defined($objectID)) {
1136 :     Confess("No object ID specified for DeleteAttribute call.");
1137 :     } elsif (! defined($key)) {
1138 :     Confess("No attribute key specified for DeleteAttribute call.");
1139 : parrello 1.11 } elsif (scalar(@values) == 0) {
1140 :     # Here we erase the entire key.
1141 :     $self->EraseAttribute($key);
1142 : parrello 1.3 } else {
1143 : parrello 1.11 # Here we erase the matching values.
1144 :     my $valueString = join($self->{splitter}, @values);
1145 :     $self->DeleteRow('HasValueFor', $key, $objectID, { value => $valueString });
1146 : parrello 1.3 }
1147 :     # Return a one. This is for backward compatability.
1148 :     return 1;
1149 :     }
1150 :    
1151 :     =head3 ChangeAttribute
1152 :    
1153 :     C<< $attrDB->ChangeAttribute($objectID, $key, \@oldValues, \@newValues); >>
1154 :    
1155 :     Change the value of an attribute key/value pair for an object.
1156 :    
1157 :     =over 4
1158 :    
1159 :     =item objectID
1160 :    
1161 :     ID of the genome or feature to which the attribute is to be changed. In general, an ID that
1162 :     starts with C<fig|> is treated as a feature ID, and an ID that is all digits and periods
1163 :     is treated as a genome ID. For IDs of other types, this parameter should be a reference
1164 :     to a 2-tuple consisting of the entity type name followed by the object ID.
1165 :    
1166 :     =item key
1167 :    
1168 :     Attribute key name. This corresponds to the name of a field in the database.
1169 :    
1170 :     =item oldValues
1171 :    
1172 :     One or more values identifying the key/value pair to change.
1173 :    
1174 :     =item newValues
1175 :    
1176 :     One or more values to be put in place of the old values.
1177 :    
1178 :     =back
1179 :    
1180 :     =cut
1181 :    
1182 :     sub ChangeAttribute {
1183 :     # Get the parameters.
1184 : parrello 1.4 my ($self, $objectID, $key, $oldValues, $newValues) = @_;
1185 : parrello 1.3 # Don't allow undefs.
1186 :     if (! defined($objectID)) {
1187 :     Confess("No object ID specified for ChangeAttribute call.");
1188 :     } elsif (! defined($key)) {
1189 :     Confess("No attribute key specified for ChangeAttribute call.");
1190 :     } elsif (! defined($oldValues) || ref $oldValues ne 'ARRAY') {
1191 :     Confess("No old values specified in ChangeAttribute call for key $key.");
1192 :     } elsif (! defined($newValues) || ref $newValues ne 'ARRAY') {
1193 :     Confess("No new values specified in ChangeAttribute call for key $key.");
1194 :     } else {
1195 : parrello 1.10 # We do the change as a delete/add.
1196 : parrello 1.3 $self->DeleteAttribute($objectID, $key, @{$oldValues});
1197 :     $self->AddAttribute($objectID, $key, @{$newValues});
1198 :     }
1199 :     # Return a one. We do this for backward compatability.
1200 :     return 1;
1201 :     }
1202 :    
1203 : parrello 1.7 =head3 EraseAttribute
1204 :    
1205 : parrello 1.11 C<< $attrDB->EraseAttribute($key); >>
1206 : parrello 1.7
1207 :     Erase all values for the specified attribute key. This does not remove the
1208 :     key from the database; it merely removes all the values.
1209 :    
1210 :     =over 4
1211 :    
1212 :     =item key
1213 :    
1214 :     Key to erase.
1215 :    
1216 :     =back
1217 :    
1218 :     =cut
1219 :    
1220 :     sub EraseAttribute {
1221 :     # Get the parameters.
1222 : parrello 1.10 my ($self, $key) = @_;
1223 :     # Delete everything connected to the key. The "keepRoot" option keeps the key in the
1224 :     # datanase while deleting everything attached to it.
1225 :     $self->Delete('AttributeKey', $key, keepRoot => 1);
1226 : parrello 1.7 # Return a 1, for backward compatability.
1227 :     return 1;
1228 :     }
1229 :    
1230 : parrello 1.9 =head3 GetAttributeKeys
1231 :    
1232 : parrello 1.10 C<< my @keyList = $attrDB->GetAttributeKeys($groupName); >>
1233 : parrello 1.9
1234 : parrello 1.10 Return a list of the attribute keys for a particular group.
1235 : parrello 1.9
1236 :     =over 4
1237 :    
1238 : parrello 1.10 =item groupName
1239 : parrello 1.9
1240 : parrello 1.10 Name of the group whose keys are desired.
1241 : parrello 1.9
1242 :     =item RETURN
1243 :    
1244 : parrello 1.10 Returns a list of the attribute keys for the specified group.
1245 : parrello 1.9
1246 :     =back
1247 :    
1248 :     =cut
1249 :    
1250 :     sub GetAttributeKeys {
1251 :     # Get the parameters.
1252 : parrello 1.10 my ($self, $groupName) = @_;
1253 :     # Get the attributes for the specified group.
1254 :     my @groups = $self->GetFlat(['IsInGroup'], "IsInGroup(to-link) = ?", [$groupName],
1255 :     'IsInGroup(from-link)');
1256 : parrello 1.9 # Return the keys.
1257 : parrello 1.10 return sort @groups;
1258 : parrello 1.9 }
1259 :    
1260 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3