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

Annotation of /Sprout/CustomAttributes.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.26 - (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 : parrello 1.13 use Stats;
12 : parrello 1.1
13 :     =head1 Custom SEED Attribute Manager
14 :    
15 :     =head2 Introduction
16 :    
17 :     The Custom SEED Attributes Manager allows the user to upload and retrieve
18 :     custom data for SEED objects. It uses the B<ERDB> database system to
19 : parrello 1.10 store the attributes.
20 :    
21 :     Attributes are organized by I<attribute key>. Attribute values are
22 :     assigned to I<objects>. In the real world, objects have types and IDs;
23 :     however, to the attribute database only the ID matters. This will create
24 :     a problem if we have a single ID that applies to two objects of different
25 :     types, but it is more consistent with the original attribute implementation
26 : parrello 1.11 in the SEED (which this implementation replaces).
27 : parrello 1.10
28 : parrello 1.11 The actual attribute values are stored as a relationship between the attribute
29 :     keys and the objects. There can be multiple values for a single key/object pair.
30 : parrello 1.1
31 : parrello 1.19 =head3 Object IDs
32 :    
33 :     The object ID is normally represented as
34 :    
35 :     I<type>:I<id>
36 :    
37 :     where I<type> is the object type (C<Role>, C<Coupling>, etc.) and I<id> is
38 :     the actual object ID. Note that the object type must consist of only upper- and
39 :     lower-case letters! Thus, C<GenomeGroup> is a valid object type, but
40 :     C<genome_group> is not. Given that restriction, the object ID
41 :    
42 :     Family:aclame|cluster10
43 :    
44 :     would represent the FIG family C<aclame|cluster10>. For historical reasons,
45 :     there are three exceptions: subsystems, genomes, and features do not need
46 :     a type. So, for PEG 3361 of Streptomyces coelicolor A3(2), you simply code
47 :    
48 :     fig|100226.1.peg.3361
49 :    
50 :     The methods L</ParseID> and L</FormID> can be used to make this all seem
51 :     more consistent. Given any object ID string, L</ParseID> will convert it to an
52 :     object type and ID, and given any object type and ID, L</FormID> will
53 :     convert it to an object ID string. The attribute database is pretty
54 :     freewheeling about what it will allow for an ID; however, for best
55 :     results, the type should match an entity type from a Sprout genetics
56 :     database. If this rule is followed, then the database object
57 :     corresponding to an ID in the attribute database could be retrieved using
58 :     L</GetTargetObject> method.
59 :    
60 :     my $object = CustomAttributes::GetTargetObject($sprout, $idValue);
61 :    
62 :     =head3 Retrieval and Logging
63 :    
64 : parrello 1.1 The full suite of ERDB retrieval capabilities is provided. In addition,
65 :     custom methods are provided specific to this application. To get all
66 : parrello 1.6 the values of the attribute C<essential> in a specified B<Feature>, you
67 : parrello 1.1 would code
68 :    
69 : parrello 1.10 my @values = $attrDB->GetAttributes($fid, 'essential');
70 : parrello 1.1
71 : parrello 1.10 where I<$fid> contains the ID of the desired feature.
72 : parrello 1.1
73 : parrello 1.20 Keys can be split into two pieces using the splitter value defined in the
74 :     constructor (the default is C<::>). The first piece of the key is called
75 :     the I<real key>. This portion of the key must be defined using the
76 :     web interface (C<Attributes.cgi>). The second portion of the key is called
77 :     the I<sub key>, and can take any value.
78 : parrello 1.1
79 : parrello 1.18 Major attribute activity is recorded in a log (C<attributes.log>) in the
80 :     C<$FIG_Config::var> directory. The log reports the user name, time, and
81 :     the details of the operation. The user name will almost always be unknown,
82 : parrello 1.20 the exception being when it is specified in this object's constructor
83 :     (see L</new>).
84 : parrello 1.18
85 : parrello 1.1 =head2 FIG_Config Parameters
86 :    
87 :     The following configuration parameters are used to manage custom attributes.
88 :    
89 :     =over 4
90 :    
91 :     =item attrDbms
92 :    
93 :     Type of database manager used: C<mysql> for MySQL or C<pg> for PostGres.
94 :    
95 :     =item attrDbName
96 :    
97 :     Name of the attribute database.
98 :    
99 :     =item attrHost
100 :    
101 :     Name of the host server for the database. If omitted, the current host
102 :     is used.
103 :    
104 :     =item attrUser
105 :    
106 :     User name for logging in to the database.
107 :    
108 :     =item attrPass
109 :    
110 :     Password for logging in to the database.
111 :    
112 :     =item attrPort
113 :    
114 :     TCP/IP port for accessing the database.
115 :    
116 :     =item attrSock
117 :    
118 :     Socket name used to access the database. If omitted, the default socket
119 :     will be used.
120 :    
121 :     =item attrDBD
122 :    
123 :     Fully-qualified file name for the database definition XML file. This file
124 :     functions as data to the attribute management process, so if the data is
125 :     moved, this file must go with it.
126 :    
127 :     =back
128 :    
129 :     =head2 Public Methods
130 :    
131 :     =head3 new
132 :    
133 : parrello 1.18 C<< my $attrDB = CustomAttributes->new(%options); >>
134 : parrello 1.1
135 : parrello 1.18 Construct a new CustomAttributes object. The following options are
136 :     supported.
137 : parrello 1.3
138 :     =over 4
139 :    
140 :     =item splitter
141 :    
142 :     Value to be used to split attribute values into sections in the
143 : parrello 1.18 L</Fig Replacement Methods>. The default is a double colon C<::>,
144 :     and should only be overridden in extreme circumstances.
145 :    
146 :     =item user
147 :    
148 :     Name of the current user. This will appear in the attribute log.
149 : parrello 1.3
150 :     =back
151 : parrello 1.1
152 :     =cut
153 :    
154 :     sub new {
155 :     # Get the parameters.
156 : parrello 1.18 my ($class, %options) = @_;
157 : parrello 1.1 # Connect to the database.
158 :     my $dbh = DBKernel->new($FIG_Config::attrDbms, $FIG_Config::attrDbName,
159 :     $FIG_Config::attrUser, $FIG_Config::attrPass,
160 :     $FIG_Config::attrPort, $FIG_Config::attrHost,
161 :     $FIG_Config::attrSock);
162 :     # Create the ERDB object.
163 :     my $xmlFileName = $FIG_Config::attrDBD;
164 :     my $retVal = ERDB::new($class, $dbh, $xmlFileName);
165 : parrello 1.3 # Store the splitter value.
166 : parrello 1.18 $retVal->{splitter} = $options{splitter} || '::';
167 :     # Store the user name.
168 :     $retVal->{user} = $options{user} || '<unknown>';
169 :     Trace("User $retVal->{user} selected for attribute object.") if T(3);
170 : parrello 1.1 # Return the result.
171 :     return $retVal;
172 :     }
173 :    
174 : parrello 1.10 =head3 StoreAttributeKey
175 :    
176 :     C<< $attrDB->StoreAttributeKey($attributeName, $type, $notes, \@groups); >>
177 :    
178 :     Create or update an attribute for the database.
179 :    
180 :     =over 4
181 : parrello 1.1
182 :     =item attributeName
183 :    
184 : parrello 1.20 Name of the attribute (the real key). If it does not exist already, it will be created.
185 : parrello 1.1
186 :     =item type
187 :    
188 :     Data type of the attribute. This must be a valid ERDB data type name.
189 :    
190 :     =item notes
191 :    
192 :     Descriptive notes about the attribute. It is presumed to be raw text, not HTML.
193 :    
194 : parrello 1.10 =item groups
195 : parrello 1.1
196 : parrello 1.10 Reference to a list of the groups to which the attribute should be associated.
197 :     This will replace any groups to which the attribute is currently attached.
198 : parrello 1.1
199 :     =back
200 :    
201 :     =cut
202 :    
203 : parrello 1.3 sub StoreAttributeKey {
204 : parrello 1.1 # Get the parameters.
205 : parrello 1.10 my ($self, $attributeName, $type, $notes, $groups) = @_;
206 : parrello 1.8 # Declare the return variable.
207 :     my $retVal;
208 : parrello 1.1 # Get the data type hash.
209 :     my %types = ERDB::GetDataTypes();
210 :     # Validate the initial input values.
211 : parrello 1.20 if ($attributeName =~ /$self->{splitter}/) {
212 : parrello 1.1 Confess("Invalid attribute name \"$attributeName\" specified.");
213 :     } elsif (! $notes || length($notes) < 25) {
214 :     Confess("Missing or incomplete description for $attributeName.");
215 :     } elsif (! exists $types{$type}) {
216 :     Confess("Invalid data type \"$type\" for $attributeName.");
217 :     } else {
218 : parrello 1.18 # Create a variable to hold the action to be displayed for the log (Add or Update).
219 :     my $action;
220 : parrello 1.10 # Okay, we're ready to begin. See if this key exists.
221 :     my $attribute = $self->GetEntity('AttributeKey', $attributeName);
222 :     if (defined($attribute)) {
223 :     # It does, so we do an update.
224 : parrello 1.18 $action = "Update Key";
225 : parrello 1.10 $self->UpdateEntity('AttributeKey', $attributeName,
226 :     { description => $notes, 'data-type' => $type });
227 :     # Detach the key from its current groups.
228 :     $self->Disconnect('IsInGroup', 'AttributeKey', $attributeName);
229 :     } else {
230 :     # It doesn't, so we do an insert.
231 : parrello 1.18 $action = "Insert Key";
232 : parrello 1.10 $self->InsertObject('AttributeKey', { id => $attributeName,
233 :     description => $notes, 'data-type' => $type });
234 : parrello 1.8 }
235 : parrello 1.10 # Attach the key to the specified groups. (We presume the groups already
236 :     # exist.)
237 :     for my $group (@{$groups}) {
238 :     $self->InsertObject('IsInGroup', { 'from-link' => $attributeName,
239 :     'to-link' => $group });
240 : parrello 1.1 }
241 : parrello 1.18 # Log the operation.
242 :     $self->LogOperation($action, $attributeName, "Group list is " . join(" ", @{$groups}));
243 : parrello 1.1 }
244 :     }
245 :    
246 :    
247 : parrello 1.3 =head3 DeleteAttributeKey
248 :    
249 : parrello 1.10 C<< my $stats = $attrDB->DeleteAttributeKey($attributeName); >>
250 : parrello 1.1
251 :     Delete an attribute from the custom attributes database.
252 :    
253 :     =over 4
254 :    
255 : parrello 1.10 =item attributeName
256 : parrello 1.1
257 : parrello 1.10 Name of the attribute to delete.
258 : parrello 1.1
259 : parrello 1.10 =item RETURN
260 : parrello 1.1
261 : parrello 1.10 Returns a statistics object describing the effects of the deletion.
262 : parrello 1.1
263 :     =back
264 :    
265 :     =cut
266 :    
267 : parrello 1.3 sub DeleteAttributeKey {
268 : parrello 1.1 # Get the parameters.
269 : parrello 1.10 my ($self, $attributeName) = @_;
270 :     # Delete the attribute key.
271 :     my $retVal = $self->Delete('AttributeKey', $attributeName);
272 : parrello 1.18 # Log this operation.
273 :     $self->LogOperation("Delete Key", $attributeName, "Key will no longer be available for use by anyone.");
274 : parrello 1.10 # Return the result.
275 :     return $retVal;
276 :    
277 :     }
278 :    
279 :     =head3 NewName
280 :    
281 :     C<< my $text = CustomAttributes::NewName(); >>
282 :    
283 :     Return the string used to indicate the user wants to add a new attribute.
284 :    
285 :     =cut
286 :    
287 :     sub NewName {
288 :     return "(new)";
289 : parrello 1.1 }
290 :    
291 :     =head3 ControlForm
292 :    
293 : parrello 1.10 C<< my $formHtml = $attrDB->ControlForm($cgi, $name, \%keys); >>
294 : parrello 1.1
295 :     Return a form that can be used to control the creation and modification of
296 : parrello 1.10 attributes. Only a subset of the attribute keys will be displayed, as
297 :     determined by the incoming list.
298 : parrello 1.1
299 :     =over 4
300 :    
301 :     =item cgi
302 :    
303 :     CGI query object used to create HTML.
304 :    
305 :     =item name
306 :    
307 :     Name to give to the form. This should be unique for the web page.
308 :    
309 : parrello 1.10 =item keys
310 :    
311 :     Reference to a hash mapping attribute keys to n-tuples. Each tuple will contain the
312 :     attribute's data type, its description, and a list of the groups in which it participates.
313 :    
314 : parrello 1.1 =item RETURN
315 :    
316 : parrello 1.10 Returns the HTML for a form that can be used to submit instructions to the C<Attributes.cgi> script
317 :     for loading, creating, displaying, changing, or deleting an attribute. Note that only the form
318 :     controls are generated. The form tags are left to the caller.
319 : parrello 1.1
320 :     =back
321 :    
322 :     =cut
323 :    
324 :     sub ControlForm {
325 :     # Get the parameters.
326 : parrello 1.10 my ($self, $cgi, $name, $keys) = @_;
327 : parrello 1.1 # Declare the return list.
328 :     my @retVal = ();
329 :     # We'll put the controls in a table. Nothing else ever seems to look nice.
330 :     push @retVal, $cgi->start_table({ border => 2, cellpadding => 2 });
331 :     # The first row is for selecting the field name.
332 :     push @retVal, $cgi->Tr($cgi->th("Select a Field"),
333 : parrello 1.10 $cgi->td($self->FieldMenu($cgi, 10, 'fieldName', $keys,
334 :     new => 1,
335 :     notes => "document.$name.notes.value",
336 :     type => "document.$name.dataType.value",
337 :     groups => "document.$name.groups")));
338 : parrello 1.1 # Now we set up a dropdown for the data types. The values will be the
339 :     # data type names, and the labels will be the descriptions.
340 :     my %types = ERDB::GetDataTypes();
341 :     my %labelMap = map { $_ => $types{$_}->{notes} } keys %types;
342 :     my $typeMenu = $cgi->popup_menu(-name => 'dataType',
343 :     -values => [sort keys %types],
344 : parrello 1.10 -labels => \%labelMap,
345 :     -default => 'string');
346 :     # Allow the user to specify a new field name. This is required if the
347 : parrello 1.25 # user has selected the "(new)" marker.
348 : parrello 1.10 my $fieldField = "document.$name.fieldName";
349 :     my $newName = "\"" . NewName() . "\"";
350 :     push @retVal, $cgi->Tr($cgi->th("New Field Name"),
351 :     $cgi->td($cgi->textfield(-name => 'newName',
352 :     -size => 30,
353 :     -value => "",
354 :     -onFocus => "setIfEmpty($fieldField, $newName);")),
355 :     );
356 : parrello 1.1 push @retVal, $cgi->Tr($cgi->th("Data type"),
357 :     $cgi->td($typeMenu));
358 :     # The next row is for the notes.
359 :     push @retVal, $cgi->Tr($cgi->th("Description"),
360 :     $cgi->td($cgi->textarea(-name => 'notes',
361 :     -rows => 6,
362 :     -columns => 80))
363 :     );
364 : parrello 1.10 # Now we have the groups, which are implemented as a checkbox group.
365 :     my @groups = $self->GetGroups();
366 :     push @retVal, $cgi->Tr($cgi->th("Groups"),
367 :     $cgi->td($cgi->checkbox_group(-name=>'groups',
368 :     -values=> \@groups))
369 :     );
370 : parrello 1.20 # Now the four buttons: STORE, SHOW, ERASE, and DELETE.
371 : parrello 1.1 push @retVal, $cgi->Tr($cgi->th("&nbsp;"),
372 : parrello 1.20 $cgi->td({align => 'center'}, join(" ",
373 :     $cgi->submit(-name => 'Delete', -value => 'DELETE'),
374 :     $cgi->submit(-name => 'Store', -value => 'STORE'),
375 :     $cgi->submit(-name => 'Erase', -value => 'ERASE'),
376 : parrello 1.7 $cgi->submit(-name => 'Show', -value => 'SHOW')
377 : parrello 1.20 ))
378 : parrello 1.1 );
379 :     # Close the table and the form.
380 :     push @retVal, $cgi->end_table();
381 :     # Return the assembled HTML.
382 :     return join("\n", @retVal, "");
383 :     }
384 :    
385 : parrello 1.11 =head3 LoadAttributesFrom
386 :    
387 :     C<< my $stats = $attrDB->LoadAttributesFrom($fileName, %options); >>
388 : parrello 1.24 s
389 : parrello 1.11 Load attributes from the specified tab-delimited file. Each line of the file must
390 :     contain an object ID in the first column, an attribute key name in the second
391 :     column, and attribute values in the remaining columns. The attribute values will
392 : parrello 1.20 be assembled into a single value using the splitter code. In addition, the key names may
393 :     contain a splitter. If this is the case, the portion of the key after the splitter is
394 :     treated as a subkey.
395 : parrello 1.11
396 :     =over 4
397 :    
398 :     =item fileName
399 :    
400 : parrello 1.20 Name of the file from which to load the attributes, or an open handle for the file.
401 :     (This last enables the method to be used in conjunction with the CGI form upload
402 :     control.)
403 : parrello 1.11
404 :     =item options
405 :    
406 :     Hash of options for modifying the load process.
407 :    
408 :     =item RETURN
409 :    
410 :     Returns a statistics object describing the load.
411 :    
412 :     =back
413 :    
414 :     Permissible option values are as follows.
415 :    
416 :     =over 4
417 :    
418 :     =item append
419 :    
420 :     If TRUE, then the attributes will be appended to existing data; otherwise, the
421 :     first time a key name is encountered, it will be erased.
422 :    
423 : parrello 1.20 =item archive
424 :    
425 :     If specified, the name of a file into which the incoming data file should be saved.
426 :    
427 :     =item objectType
428 :    
429 :     If specified, the specified object type will be prefixed to each object ID.
430 :    
431 : parrello 1.11 =back
432 :    
433 :     =cut
434 :    
435 :     sub LoadAttributesFrom {
436 :     # Get the parameters.
437 :     my ($self, $fileName, %options) = @_;
438 :     # Declare the return variable.
439 :     my $retVal = Stats->new('keys', 'values');
440 :     # Check for append mode.
441 :     my $append = ($options{append} ? 1 : 0);
442 :     # Create a hash of key names found.
443 :     my %keyHash = ();
444 : parrello 1.20 # Open the file for input. Note we must anticipate the possibility of an
445 :     # open filehandle being passed in.
446 :     my $fh;
447 : parrello 1.21 if (ref $fileName) {
448 : parrello 1.20 Trace("Using file opened by caller.") if T(3);
449 :     $fh = $fileName;
450 :     } else {
451 :     Trace("Attributes will be loaded from $fileName.") if T(3);
452 :     $fh = Open(undef, "<$fileName");
453 :     }
454 :     # Now check to see if we need to archive.
455 :     my $ah;
456 :     if ($options{archive}) {
457 :     $ah = Open(undef, ">$options{archive}");
458 :     Trace("Load file will be archived to $options{archive}.") if T(3);
459 :     }
460 :     # Finally, open a database transaction.
461 :     $self->BeginTran();
462 :     # Insure we recover from errors. If an error occurs, we will delete the archive file and
463 :     # roll back the updates.
464 :     eval {
465 :     # Loop through the file.
466 :     while (! eof $fh) {
467 :     # Read the current line.
468 :     my ($id, $key, @values) = Tracer::GetLine($fh);
469 :     $retVal->Add(linesIn => 1);
470 :     # Check to see if we need to fix up the object ID.
471 :     if ($options{objectType}) {
472 :     $id = "$options{objectType}:$id";
473 :     }
474 :     # Archive the line (if necessary).
475 :     if (defined $ah) {
476 :     Tracer::PutLine($ah, [$id, $key, @values]);
477 :     }
478 :     # Do some validation.
479 :     if (! $id) {
480 :     # We ignore blank lines.
481 :     $retVal->Add(blankLines => 1);
482 :     } elsif (substr($id, 0, 1) eq '#') {
483 :     # A line beginning with a pound sign is a comment.
484 :     $retVal->Add(comments => 1);
485 :     } elsif (! defined($key)) {
486 :     # An ID without a key is a serious error.
487 :     my $lines = $retVal->Ask('linesIn');
488 :     Confess("Line $lines in $fileName has no attribute key.");
489 : parrello 1.23 } elsif (! @values) {
490 :     # A line with no values is not allowed.
491 :     my $lines = $retVal->Ask('linesIn');
492 :     Trace("Line $lines for key $key has no attribute values.") if T(1);
493 :     $retVal->Add(skipped => 1);
494 : parrello 1.20 } else {
495 :     # The key contains a real part and an optional sub-part. We need the real part.
496 :     my ($realKey, $subKey) = $self->SplitKey($key);
497 :     # Now we need to check for a new key.
498 :     if (! exists $keyHash{$realKey}) {
499 :     if (! $self->Exists('AttributeKey', $realKey)) {
500 :     my $line = $retVal->Ask('linesIn');
501 :     Confess("Attribute \"$realKey\" on line $line of $fileName not found in database.");
502 :     } else {
503 :     # Make sure we know this is no longer a new key.
504 :     $keyHash{$realKey} = 1;
505 :     $retVal->Add(keys => 1);
506 :     # If this is NOT append mode, erase the key.
507 :     if (! $append) {
508 :     $self->EraseAttribute($realKey);
509 :     }
510 : parrello 1.11 }
511 : parrello 1.20 Trace("Key $realKey found.") if T(3);
512 : parrello 1.11 }
513 : parrello 1.20 # Everything is all set up, so add the value.
514 :     $self->AddAttribute($id, $key, @values);
515 :     my $progress = $retVal->Add(values => 1);
516 :     Trace("$progress values loaded.") if T(3) && ($progress % 1000 == 0);
517 : parrello 1.11 }
518 : parrello 1.20 }
519 :     };
520 :     # Check for an error.
521 :     if ($@) {
522 :     # Here we have an error. Roll back the transaction and delete the archive file.
523 :     my $message = $@;
524 :     Trace("Rolling back attribute updates due to error.") if T(1);
525 :     $self->RollbackTran();
526 :     if (defined $ah) {
527 :     Trace("Deleting archive file $options{archive}.") if T(1);
528 :     close $ah;
529 :     unlink $options{archive};
530 :     }
531 :     Confess("Error during attribute load: $message");
532 :     } else {
533 :     # Here the load worked. Commit the transaction and close the archive file.
534 :     Trace("Committing attribute upload.") if T(2);
535 :     $self->CommitTran();
536 :     if (defined $ah) {
537 :     Trace("Closing archive file $options{archive}.") if T(2);
538 :     close $ah;
539 : parrello 1.11 }
540 :     }
541 :     # Return the result.
542 :     return $retVal;
543 :     }
544 :    
545 : parrello 1.13 =head3 BackupKeys
546 :    
547 :     C<< my $stats = $attrDB->BackupKeys($fileName, %options); >>
548 :    
549 :     Backup the attribute key information from the attribute database.
550 :    
551 :     =over 4
552 :    
553 :     =item fileName
554 :    
555 :     Name of the output file.
556 :    
557 :     =item options
558 :    
559 :     Options for modifying the backup process.
560 :    
561 :     =item RETURN
562 :    
563 :     Returns a statistics object for the backup.
564 :    
565 :     =back
566 :    
567 :     Currently there are no options. The backup is straight to a text file in
568 :     tab-delimited format. Each key is backup up to two lines. The first line
569 :     is all of the data from the B<AttributeKey> table. The second is a
570 :     tab-delimited list of all the groups.
571 :    
572 :     =cut
573 :    
574 :     sub BackupKeys {
575 :     # Get the parameters.
576 :     my ($self, $fileName, %options) = @_;
577 :     # Declare the return variable.
578 :     my $retVal = Stats->new();
579 :     # Open the output file.
580 :     my $fh = Open(undef, ">$fileName");
581 :     # Set up to read the keys.
582 :     my $keyQuery = $self->Get(['AttributeKey'], "", []);
583 :     # Loop through the keys.
584 :     while (my $keyData = $keyQuery->Fetch()) {
585 :     $retVal->Add(key => 1);
586 :     # Get the fields.
587 :     my ($id, $type, $description) = $keyData->Values(['AttributeKey(id)', 'AttributeKey(data-type)',
588 :     'AttributeKey(description)']);
589 :     # Escape any tabs or new-lines in the description.
590 :     my $escapedDescription = Tracer::Escape($description);
591 :     # Write the key data to the output.
592 :     Tracer::PutLine($fh, [$id, $type, $escapedDescription]);
593 :     # Get the key's groups.
594 :     my @groups = $self->GetFlat(['IsInGroup'], "IsInGroup(from-link) = ?", [$id],
595 :     'IsInGroup(to-link)');
596 :     $retVal->Add(memberships => scalar(@groups));
597 :     # Write them to the output. Note we put a marker at the beginning to insure the line
598 :     # is nonempty.
599 :     Tracer::PutLine($fh, ['#GROUPS', @groups]);
600 :     }
601 : parrello 1.18 # Log the operation.
602 :     $self->LogOperation("Backup Keys", $fileName, $retVal->Display());
603 : parrello 1.13 # Return the result.
604 :     return $retVal;
605 :     }
606 :    
607 :     =head3 RestoreKeys
608 :    
609 :     C<< my $stats = $attrDB->RestoreKeys($fileName, %options); >>
610 :    
611 :     Restore the attribute keys and groups from a backup file.
612 :    
613 :     =over 4
614 :    
615 :     =item fileName
616 :    
617 :     Name of the file containing the backed-up keys. Each key has a pair of lines,
618 :     one containing the key data and one listing its groups.
619 :    
620 :     =back
621 :    
622 :     =cut
623 :    
624 :     sub RestoreKeys {
625 :     # Get the parameters.
626 :     my ($self, $fileName, %options) = @_;
627 :     # Declare the return variable.
628 :     my $retVal = Stats->new();
629 :     # Set up a hash to hold the group IDs.
630 :     my %groups = ();
631 :     # Open the file.
632 :     my $fh = Open(undef, "<$fileName");
633 :     # Loop until we're done.
634 :     while (! eof $fh) {
635 :     # Get a key record.
636 :     my ($id, $dataType, $description) = Tracer::GetLine($fh);
637 :     if ($id eq '#GROUPS') {
638 :     Confess("Group record found when key record expected.");
639 :     } elsif (! defined($description)) {
640 :     Confess("Invalid format found for key record.");
641 :     } else {
642 :     $retVal->Add("keyIn" => 1);
643 :     # Add this key to the database.
644 :     $self->InsertObject('AttributeKey', { id => $id, 'data-type' => $dataType,
645 :     description => Tracer::UnEscape($description) });
646 :     Trace("Attribute $id stored.") if T(3);
647 :     # Get the group line.
648 :     my ($marker, @groups) = Tracer::GetLine($fh);
649 :     if (! defined($marker)) {
650 :     Confess("End of file found where group record expected.");
651 :     } elsif ($marker ne '#GROUPS') {
652 :     Confess("Group record not found after key record.");
653 :     } else {
654 :     $retVal->Add(memberships => scalar(@groups));
655 :     # Connect the groups.
656 :     for my $group (@groups) {
657 :     # Find out if this is a new group.
658 :     if (! $groups{$group}) {
659 :     $retVal->Add(newGroup => 1);
660 :     # Add the group.
661 :     $self->InsertObject('AttributeGroup', { id => $group });
662 :     Trace("Group $group created.") if T(3);
663 :     # Make sure we know it's not new.
664 :     $groups{$group} = 1;
665 :     }
666 :     # Connect the group to our key.
667 :     $self->InsertObject('IsInGroup', { 'from-link' => $id, 'to-link' => $group });
668 :     }
669 :     Trace("$id added to " . scalar(@groups) . " groups.") if T(3);
670 :     }
671 :     }
672 :     }
673 : parrello 1.18 # Log the operation.
674 :     $self->LogOperation("Backup Keys", $fileName, $retVal->Display());
675 : parrello 1.13 # Return the result.
676 :     return $retVal;
677 :     }
678 :    
679 : parrello 1.20 =head3 ArchiveFileName
680 :    
681 :     C<< my $fileName = $ca->ArchiveFileName(); >>
682 :    
683 :     Compute a file name for archiving attribute input data. The file will be in the attribute log directory
684 :    
685 :     =cut
686 :    
687 :     sub ArchiveFileName {
688 :     # Get the parameters.
689 :     my ($self) = @_;
690 :     # Declare the return variable.
691 :     my $retVal;
692 :     # We start by turning the timestamp into something usable as a file name.
693 :     my $now = Tracer::Now();
694 :     $now =~ tr/ :\//___/;
695 :     # Next we get the directory name.
696 :     my $dir = "$FIG_Config::var/attributes";
697 :     if (! -e $dir) {
698 :     Trace("Creating attribute file directory $dir.") if T(1);
699 :     mkdir $dir;
700 :     }
701 :     # Put it together with the field name and the time stamp.
702 :     $retVal = "$dir/upload.$now";
703 :     # Modify the file name to insure it's unique.
704 :     my $seq = 0;
705 :     while (-e "$retVal.$seq.tbl") { $seq++ }
706 :     # Use the computed sequence number to get the correct file name.
707 :     $retVal .= ".$seq.tbl";
708 :     # Return the result.
709 :     return $retVal;
710 :     }
711 : parrello 1.13
712 : parrello 1.11 =head3 BackupAllAttributes
713 :    
714 :     C<< my $stats = $attrDB->BackupAllAttributes($fileName, %options); >>
715 :    
716 :     Backup all of the attributes to a file. The attributes will be stored in a
717 :     tab-delimited file suitable for reloading via L</LoadAttributesFrom>.
718 :    
719 :     =over 4
720 :    
721 :     =item fileName
722 :    
723 :     Name of the file to which the attribute data should be backed up.
724 :    
725 :     =item options
726 :    
727 :     Hash of options for the backup.
728 :    
729 :     =item RETURN
730 :    
731 :     Returns a statistics object describing the backup.
732 :    
733 :     =back
734 :    
735 :     Currently there are no options defined.
736 :    
737 :     =cut
738 :    
739 :     sub BackupAllAttributes {
740 :     # Get the parameters.
741 :     my ($self, $fileName, %options) = @_;
742 :     # Declare the return variable.
743 :     my $retVal = Stats->new();
744 :     # Get a list of the keys.
745 :     my @keys = $self->GetFlat(['AttributeKey'], "", [], 'AttributeKey(id)');
746 :     Trace(scalar(@keys) . " keys found during backup.") if T(2);
747 :     # Open the file for output.
748 : parrello 1.12 my $fh = Open(undef, ">$fileName");
749 : parrello 1.11 # Loop through the keys.
750 :     for my $key (@keys) {
751 :     Trace("Backing up attribute $key.") if T(3);
752 :     $retVal->Add(keys => 1);
753 :     # Loop through this key's values.
754 : parrello 1.12 my $query = $self->Get(['HasValueFor'], "HasValueFor(from-link) = ?", [$key]);
755 : parrello 1.11 my $valuesFound = 0;
756 :     while (my $line = $query->Fetch()) {
757 :     $valuesFound++;
758 :     # Get this row's data.
759 : parrello 1.20 my ($id, $key, $subKey, $value) = $line->Values(['HasValueFor(to-link)',
760 :     'HasValueFor(from-link)',
761 :     'HasValueFor(subkey)',
762 :     'HasValueFor(value)']);
763 :     # Check for a subkey.
764 :     if ($subKey ne '') {
765 :     $key = "$key$self->{splitter}$subKey";
766 :     }
767 : parrello 1.11 # Write it to the file.
768 : parrello 1.20 Tracer::PutLine($fh, [$id, $key, $value]);
769 : parrello 1.11 }
770 :     Trace("$valuesFound values backed up for key $key.") if T(3);
771 :     $retVal->Add(values => $valuesFound);
772 :     }
773 : parrello 1.18 # Log the operation.
774 :     $self->LogOperation("Backup Data", $fileName, $retVal->Display());
775 : parrello 1.11 # Return the result.
776 :     return $retVal;
777 :     }
778 :    
779 : parrello 1.1 =head3 FieldMenu
780 :    
781 : parrello 1.10 C<< my $menuHtml = $attrDB->FieldMenu($cgi, $height, $name, $keys, %options); >>
782 : parrello 1.1
783 :     Return the HTML for a menu to select an attribute field. The menu will
784 :     be a standard SELECT/OPTION thing which is called "popup menu" in the
785 :     CGI package, but actually looks like a list. The list will contain
786 : parrello 1.10 one selectable row per field.
787 : parrello 1.1
788 :     =over 4
789 :    
790 :     =item cgi
791 :    
792 :     CGI query object used to generate HTML.
793 :    
794 :     =item height
795 :    
796 :     Number of lines to display in the list.
797 :    
798 :     =item name
799 :    
800 :     Name to give to the menu. This is the name under which the value will
801 :     appear when the form is submitted.
802 :    
803 : parrello 1.10 =item keys
804 :    
805 :     Reference to a hash mapping each attribute key name to a list reference,
806 :     the list itself consisting of the attribute data type, its description,
807 :     and a list of its groups.
808 :    
809 :     =item options
810 :    
811 :     Hash containing options that modify the generation of the menu.
812 :    
813 :     =item RETURN
814 :    
815 :     Returns the HTML to create a form field that can be used to select an
816 :     attribute from the custom attributes system.
817 :    
818 :     =back
819 :    
820 :     The permissible options are as follows.
821 :    
822 :     =over 4
823 :    
824 :     =item new
825 : parrello 1.1
826 :     If TRUE, then extra rows will be provided to allow the user to select
827 :     a new attribute. In other words, the user can select an existing
828 :     attribute, or can choose a C<(new)> marker to indicate a field to
829 :     be created in the parent entity.
830 :    
831 : parrello 1.10 =item notes
832 : parrello 1.1
833 :     If specified, the name of a variable for displaying the notes attached
834 :     to the field. This must be in Javascript form ready for assignment.
835 :     So, for example, if you have a variable called C<notes> that
836 :     represents a paragraph element, you should code C<notes.innerHTML>.
837 :     If it actually represents a form field you should code C<notes.value>.
838 :     If an C<innerHTML> coding is used, the text will be HTML-escaped before
839 :     it is copied in. Specifying this parameter generates Javascript for
840 :     displaying the field description when a field is selected.
841 :    
842 : parrello 1.10 =item type
843 : parrello 1.1
844 :     If specified, the name of a variable for displaying the field's
845 :     data type. Data types are a much more controlled vocabulary than
846 :     notes, so there is no worry about HTML translation. Instead, the
847 :     raw value is put into the specified variable. Otherwise, the same
848 :     rules apply to this value that apply to I<$noteControl>.
849 :    
850 : parrello 1.10 =item groups
851 : parrello 1.1
852 : parrello 1.10 If specified, the name of a multiple-selection list control (also called
853 :     a popup menu) which shall be used to display the selected groups.
854 : parrello 1.1
855 :     =back
856 :    
857 :     =cut
858 :    
859 :     sub FieldMenu {
860 :     # Get the parameters.
861 : parrello 1.10 my ($self, $cgi, $height, $name, $keys, %options) = @_;
862 :     # Reformat the list of keys.
863 :     my %keys = %{$keys};
864 :     # Add the (new) key, if needed.
865 :     if ($options{new}) {
866 :     $keys{NewName()} = ["string", ""];
867 : parrello 1.1 }
868 : parrello 1.10 # Get a sorted list of key.
869 :     my @keys = sort keys %keys;
870 :     # We need to create the name for the onChange function. This function
871 : parrello 1.1 # may not do anything, but we need to know the name to generate the HTML
872 :     # for the menu.
873 :     my $changeName = "${name}_setNotes";
874 :     my $retVal = $cgi->popup_menu({name => $name,
875 :     size => $height,
876 :     onChange => "$changeName(this.value)",
877 : parrello 1.10 values => \@keys,
878 :     });
879 : parrello 1.1 # Create the change function.
880 :     $retVal .= "\n<script language=\"javascript\">\n";
881 :     $retVal .= " function $changeName(fieldValue) {\n";
882 : parrello 1.10 # The function only has a body if we have a control to store data about the
883 :     # attribute.
884 :     if ($options{notes} || $options{type} || $options{groups}) {
885 : parrello 1.1 # Check to see if we're storing HTML or text into the note control.
886 : parrello 1.10 my $noteControl = $options{notes};
887 : parrello 1.1 my $htmlMode = ($noteControl && $noteControl =~ /innerHTML$/);
888 :     # We use a CASE statement based on the newly-selected field value. The
889 :     # field description will be stored in the JavaScript variable "myText"
890 :     # and the data type in "myType". Note the default data type is a normal
891 :     # string, but the default notes is an empty string.
892 :     $retVal .= " var myText = \"\";\n";
893 :     $retVal .= " var myType = \"string\";\n";
894 :     $retVal .= " switch (fieldValue) {\n";
895 : parrello 1.10 # Loop through the keys.
896 :     for my $key (@keys) {
897 :     # Generate this case.
898 :     $retVal .= " case \"$key\" :\n";
899 :     # Here we either want to update the note display, the
900 :     # type display, the group list, or a combination of them.
901 :     my ($type, $notes, @groups) = @{$keys{$key}};
902 :     if ($noteControl) {
903 :     # Insure it's in the proper form.
904 :     if ($htmlMode) {
905 :     $notes = ERDB::HTMLNote($notes);
906 : parrello 1.1 }
907 : parrello 1.10 # Escape it for use as a string literal.
908 :     $notes =~ s/\n/\\n/g;
909 :     $notes =~ s/"/\\"/g;
910 :     $retVal .= " myText = \"$notes\";\n";
911 :     }
912 :     if ($options{type}) {
913 :     # Here we want the type updated.
914 :     $retVal .= " myType = \"$type\";\n";
915 :     }
916 :     if ($options{groups}) {
917 :     # Here we want the groups shown. Get a list of this attribute's groups.
918 :     # We'll search through this list for each group to see if it belongs with
919 :     # our attribute.
920 :     my $groupLiteral = "=" . join("=", @groups) . "=";
921 :     # Now we need some variables containing useful code for the javascript. It's
922 :     # worth knowing we go through a bit of pain to insure $groupField[i] isn't
923 :     # parsed as an array element.
924 :     my $groupField = $options{groups};
925 :     my $currentField = $groupField . "[i]";
926 :     # Do the javascript.
927 :     $retVal .= " var groupList = \"$groupLiteral\";\n";
928 :     $retVal .= " for (var i = 0; i < $groupField.length; i++) {\n";
929 :     $retVal .= " var srchString = \"=\" + $currentField.value + \"=\";\n";
930 :     $retVal .= " var srchLoc = groupList.indexOf(srchString);\n";
931 :     $retVal .= " $currentField.checked = (srchLoc >= 0);\n";
932 :     $retVal .= " }\n";
933 : parrello 1.1 }
934 : parrello 1.10 # Close this case.
935 :     $retVal .= " break;\n";
936 : parrello 1.1 }
937 :     # Close the CASE statement and make the appropriate assignments.
938 :     $retVal .= " }\n";
939 :     if ($noteControl) {
940 :     $retVal .= " $noteControl = myText;\n";
941 :     }
942 : parrello 1.10 if ($options{type}) {
943 :     $retVal .= " $options{type} = myType;\n";
944 : parrello 1.1 }
945 :     }
946 :     # Terminate the change function.
947 :     $retVal .= " }\n";
948 :     $retVal .= "</script>\n";
949 :     # Return the result.
950 :     return $retVal;
951 :     }
952 :    
953 : parrello 1.10 =head3 GetGroups
954 : parrello 1.3
955 : parrello 1.10 C<< my @groups = $attrDB->GetGroups(); >>
956 : parrello 1.3
957 : parrello 1.10 Return a list of the available groups.
958 : parrello 1.3
959 :     =cut
960 :    
961 : parrello 1.10 sub GetGroups {
962 : parrello 1.3 # Get the parameters.
963 : parrello 1.10 my ($self) = @_;
964 :     # Get the groups.
965 :     my @retVal = $self->GetFlat(['AttributeGroup'], "", [], 'AttributeGroup(id)');
966 :     # Return them.
967 :     return @retVal;
968 : parrello 1.3 }
969 :    
970 : parrello 1.10 =head3 GetAttributeData
971 : parrello 1.3
972 : parrello 1.10 C<< my %keys = $attrDB->GetAttributeData($type, @list); >>
973 : parrello 1.3
974 : parrello 1.10 Return attribute data for the selected attributes. The attribute
975 :     data is a hash mapping each attribute key name to a n-tuple containing the
976 :     data type, the description, and the groups. This is the same format expected in
977 :     the L</FieldMenu> and L</ControlForm> methods for the list of attributes to display.
978 : parrello 1.3
979 :     =over 4
980 :    
981 : parrello 1.10 =item type
982 : parrello 1.4
983 : parrello 1.10 Type of attribute criterion: C<name> for attributes whose names begin with the
984 :     specified string, or C<group> for attributes in the specified group.
985 : parrello 1.4
986 : parrello 1.10 =item list
987 : parrello 1.4
988 : parrello 1.10 List containing the names of the groups or keys for the desired attributes.
989 : parrello 1.4
990 :     =item RETURN
991 :    
992 : parrello 1.10 Returns a hash mapping each attribute key name to its data type, description, and
993 :     parent groups.
994 : parrello 1.4
995 :     =back
996 :    
997 :     =cut
998 :    
999 : parrello 1.10 sub GetAttributeData {
1000 : parrello 1.4 # Get the parameters.
1001 : parrello 1.10 my ($self, $type, @list) = @_;
1002 :     # Set up a hash to store the attribute data.
1003 :     my %retVal = ();
1004 :     # Loop through the list items.
1005 :     for my $item (@list) {
1006 :     # Set up a query for the desired attributes.
1007 :     my $query;
1008 :     if ($type eq 'name') {
1009 :     # Here we're doing a generic name search. We need to escape it and then tack
1010 :     # on a %.
1011 :     my $parm = $item;
1012 :     $parm =~ s/_/\\_/g;
1013 :     $parm =~ s/%/\\%/g;
1014 :     $parm .= "%";
1015 :     # Ask for matching attributes. (Note that if the user passed in a null string
1016 :     # he'll get everything.)
1017 :     $query = $self->Get(['AttributeKey'], "AttributeKey(id) LIKE ?", [$parm]);
1018 :     } elsif ($type eq 'group') {
1019 :     $query = $self->Get(['IsInGroup', 'AttributeKey'], "IsInGroup(to-link) = ?", [$item]);
1020 : parrello 1.4 } else {
1021 : parrello 1.10 Confess("Unknown attribute query type \"$type\".");
1022 :     }
1023 :     while (my $row = $query->Fetch()) {
1024 :     # Get this attribute's data.
1025 :     my ($key, $type, $notes) = $row->Values(['AttributeKey(id)', 'AttributeKey(data-type)',
1026 :     'AttributeKey(description)']);
1027 :     # If it's new, get its groups and add it to the return hash.
1028 :     if (! exists $retVal{$key}) {
1029 :     my @groups = $self->GetFlat(['IsInGroup'], "IsInGroup(from-link) = ?",
1030 :     [$key], 'IsInGroup(to-link)');
1031 :     $retVal{$key} = [$type, $notes, @groups];
1032 : parrello 1.4 }
1033 :     }
1034 :     }
1035 :     # Return the result.
1036 : parrello 1.10 return %retVal;
1037 : parrello 1.4 }
1038 :    
1039 : parrello 1.18 =head3 LogOperation
1040 :    
1041 :     C<< $ca->LogOperation($action, $target, $description); >>
1042 :    
1043 :     Write an operation description to the attribute activity log (C<$FIG_Config::var/attributes.log>).
1044 :    
1045 :     =over 4
1046 :    
1047 :     =item action
1048 :    
1049 :     Action being logged (e.g. C<Delete Group> or C<Load Key>).
1050 :    
1051 :     =item target
1052 :    
1053 :     ID of the key or group affected.
1054 :    
1055 :     =item description
1056 :    
1057 :     Short description of the action.
1058 :    
1059 :     =back
1060 :    
1061 :     =cut
1062 :    
1063 :     sub LogOperation {
1064 :     # Get the parameters.
1065 :     my ($self, $action, $target, $description) = @_;
1066 :     # Get the user ID.
1067 :     my $user = $self->{user};
1068 :     # Get a timestamp.
1069 :     my $timeString = Tracer::Now();
1070 :     # Open the log file for appending.
1071 :     my $oh = Open(undef, ">>$FIG_Config::var/attributes.log");
1072 :     # Write the data to it.
1073 :     Tracer::PutLine($oh, [$timeString, $user, $action, $target, $description]);
1074 :     # Close the log file.
1075 :     close $oh;
1076 :     }
1077 :    
1078 : parrello 1.15 =head2 Internal Utility Methods
1079 :    
1080 :     =head3 _KeywordString
1081 :    
1082 :     C<< my $keywordString = $ca->_KeywordString($key, $value); >>
1083 :    
1084 :     Compute the keyword string for a specified key/value pair. This consists of the
1085 :     key name and value converted to lower case with underscores translated to spaces.
1086 :    
1087 :     This method is for internal use only. It is called whenever we need to update or
1088 :     insert a B<HasValueFor> record.
1089 :    
1090 :     =over 4
1091 :    
1092 :     =item key
1093 :    
1094 :     Name of the relevant attribute key.
1095 :    
1096 :     =item target
1097 :    
1098 :     ID of the target object to which this key/value pair will be associated.
1099 :    
1100 :     =item value
1101 :    
1102 :     The value to store for this key/object combination.
1103 :    
1104 :     =item RETURN
1105 :    
1106 :     Returns the value that should be stored as the keyword string for the specified
1107 :     key/value pair.
1108 :    
1109 :     =back
1110 :    
1111 :     =cut
1112 :    
1113 :     sub _KeywordString {
1114 :     # Get the parameters.
1115 :     my ($self, $key, $value) = @_;
1116 :     # Get a copy of the key name and convert underscores to spaces.
1117 :     my $keywordString = $key;
1118 :     $keywordString =~ s/_/ /g;
1119 :     # Add the value convert it all to lower case.
1120 :     my $retVal = lc "$keywordString $value";
1121 :     # Return the result.
1122 :     return $retVal;
1123 :     }
1124 :    
1125 :     =head3 _QueryResults
1126 :    
1127 :     C<< my @attributeList = $attrDB->_QueryResults($query, @values); >>
1128 :    
1129 :     Match the results of a B<HasValueFor> query against value criteria and return
1130 :     the results. This is an internal method that splits the values coming back
1131 :     and matches the sections against the specified section patterns. It serves
1132 :     as the back end to L</GetAttributes> and L</FindAttributes>.
1133 :    
1134 :     =over 4
1135 :    
1136 :     =item query
1137 :    
1138 :     A query object that will return the desired B<HasValueFor> records.
1139 :    
1140 :     =item values
1141 :    
1142 :     List of the desired attribute values, section by section. If C<undef>
1143 :     or an empty string is specified, all values in that section will match. A
1144 :     generic match can be requested by placing a percent sign (C<%>) at the end.
1145 :     In that case, all values that match up to and not including the percent sign
1146 :     will match. You may also specify a regular expression enclosed
1147 :     in slashes. All values that match the regular expression will be returned. For
1148 :     performance reasons, only values have this extra capability.
1149 :    
1150 :     =item RETURN
1151 :    
1152 :     Returns a list of tuples. The first element in the tuple is an object ID, the
1153 :     second is an attribute key, and the remaining elements are the sections of
1154 :     the attribute value. All of the tuples will match the criteria set forth in
1155 :     the parameter list.
1156 :    
1157 :     =back
1158 :    
1159 :     =cut
1160 :    
1161 :     sub _QueryResults {
1162 :     # Get the parameters.
1163 :     my ($self, $query, @values) = @_;
1164 :     # Declare the return value.
1165 :     my @retVal = ();
1166 :     # Get the number of value sections we have to match.
1167 :     my $sectionCount = scalar(@values);
1168 :     # Loop through the assignments found.
1169 :     while (my $row = $query->Fetch()) {
1170 :     # Get the current row's data.
1171 : parrello 1.20 my ($id, $realKey, $subKey, $valueString) = $row->Values(['HasValueFor(to-link)',
1172 :     'HasValueFor(from-link)',
1173 :     'HasValueFor(subkey)',
1174 :     'HasValueFor(value)'
1175 :     ]);
1176 :     # Form the key from the real key and the sub key.
1177 :     my $key = $self->JoinKey($realKey, $subKey);
1178 : parrello 1.15 # Break the value into sections.
1179 :     my @sections = split($self->{splitter}, $valueString);
1180 :     # Match each section against the incoming values. We'll assume we're
1181 :     # okay unless we learn otherwise.
1182 :     my $matching = 1;
1183 :     for (my $i = 0; $i < $sectionCount && $matching; $i++) {
1184 :     # We need to check to see if this section is generic.
1185 :     my $value = $values[$i];
1186 :     Trace("Current value pattern is \"$value\".") if T(4);
1187 :     if (substr($value, -1, 1) eq '%') {
1188 :     Trace("Generic match used.") if T(4);
1189 :     # Here we have a generic match.
1190 : parrello 1.20 my $matchLen = length($values[$i]) - 1;
1191 : parrello 1.15 $matching = substr($sections[$i], 0, $matchLen) eq
1192 :     substr($values[$i], 0, $matchLen);
1193 :     } elsif ($value =~ m#^/(.+)/[a-z]*$#) {
1194 :     Trace("Regular expression detected.") if T(4);
1195 :     # Here we have a regular expression match.
1196 :     my $section = $sections[$i];
1197 :     $matching = eval("\$section =~ $value");
1198 :     } else {
1199 :     # Here we have a strict match.
1200 :     Trace("Strict match used.") if T(4);
1201 :     $matching = ($sections[$i] eq $values[$i]);
1202 :     }
1203 :     }
1204 :     # If we match, output this row to the return list.
1205 :     if ($matching) {
1206 :     push @retVal, [$id, $key, @sections];
1207 :     }
1208 :     }
1209 :     # Return the rows found.
1210 :     return @retVal;
1211 :     }
1212 :    
1213 : parrello 1.3 =head2 FIG Method Replacements
1214 :    
1215 :     The following methods are used by B<FIG.pm> to replace the previous attribute functionality.
1216 : parrello 1.10 Some of the old functionality is no longer present: controlled vocabulary is no longer
1217 : parrello 1.3 supported and there is no longer any searching by URL. Fortunately, neither of these
1218 :     capabilities were used in the old system.
1219 :    
1220 : parrello 1.4 The methods here are the only ones supported by the B<RemoteCustomAttributes> object.
1221 :     The idea is that these methods represent attribute manipulation allowed by all users, while
1222 :     the others are only for privileged users with access to the attribute server.
1223 :    
1224 : parrello 1.20 In the previous implementation, an attribute had a value and a URL. In this implementation,
1225 :     each attribute has only a value. These methods will treat the value as a list with the individual
1226 :     elements separated by the value of the splitter parameter on the constructor (L</new>). The default
1227 :     is double colons C<::>.
1228 : parrello 1.3
1229 : parrello 1.10 So, for example, an old-style keyword with a value of C<essential> and a URL of
1230 : parrello 1.3 C<http://www.sciencemag.org/cgi/content/abstract/293/5538/2266> using the default
1231 :     splitter value would be stored as
1232 :    
1233 :     essential::http://www.sciencemag.org/cgi/content/abstract/293/5538/2266
1234 :    
1235 :     The best performance is achieved by searching for a particular key for a specified
1236 :     feature or genome.
1237 :    
1238 :     =head3 GetAttributes
1239 :    
1240 : parrello 1.10 C<< my @attributeList = $attrDB->GetAttributes($objectID, $key, @values); >>
1241 : parrello 1.3
1242 :     In the database, attribute values are sectioned into pieces using a splitter
1243 :     value specified in the constructor (L</new>). This is not a requirement of
1244 :     the attribute system as a whole, merely a convenience for the purpose of
1245 : parrello 1.10 these methods. If a value has multiple sections, each section
1246 :     is matched against the corresponding criterion in the I<@valuePatterns> list.
1247 : parrello 1.3
1248 :     This method returns a series of tuples that match the specified criteria. Each tuple
1249 :     will contain an object ID, a key, and one or more values. The parameters to this
1250 : parrello 1.10 method therefore correspond structurally to the values expected in each tuple. In
1251 :     addition, you can ask for a generic search by suffixing a percent sign (C<%>) to any
1252 :     of the parameters. So, for example,
1253 : parrello 1.3
1254 : parrello 1.10 my @attributeList = $attrDB->GetAttributes('fig|100226.1.peg.1004', 'structure%', 1, 2);
1255 : parrello 1.3
1256 :     would return something like
1257 :    
1258 :     ['fig}100226.1.peg.1004', 'structure', 1, 2]
1259 :     ['fig}100226.1.peg.1004', 'structure1', 1, 2]
1260 :     ['fig}100226.1.peg.1004', 'structure2', 1, 2]
1261 :     ['fig}100226.1.peg.1004', 'structureA', 1, 2]
1262 :    
1263 : parrello 1.10 Use of C<undef> in any position acts as a wild card (all values). You can also specify
1264 :     a list reference in the ID column. Thus,
1265 :    
1266 :     my @attributeList = $attrDB->GetAttributes(['100226.1', 'fig|100226.1.%'], 'PUBMED');
1267 :    
1268 :     would get the PUBMED attribute data for Streptomyces coelicolor A3(2) and all its
1269 :     features.
1270 : parrello 1.3
1271 :     In addition to values in multiple sections, a single attribute key can have multiple
1272 :     values, so even
1273 :    
1274 : parrello 1.10 my @attributeList = $attrDB->GetAttributes($peg, 'virulent');
1275 : parrello 1.3
1276 :     which has no wildcard in the key or the object ID, may return multiple tuples.
1277 :    
1278 : parrello 1.10 Value matching in this system works very poorly, because of the way multiple values are
1279 : parrello 1.20 stored. For the object ID, key name, and first value, we create queries that filter for the
1280 :     desired results. On any filtering by value, we must do a comparison after the attributes are
1281 :     retrieved from the database, since the database has no notion of the multiple values, which
1282 :     are stored in a single string. As a result, queries in which filter only on value end up
1283 :     reading a lot more than they need to.
1284 : parrello 1.3
1285 :     =over 4
1286 :    
1287 :     =item objectID
1288 :    
1289 : parrello 1.10 ID of object whose attributes are desired. If the attributes are desired for multiple
1290 :     objects, this parameter can be specified as a list reference. If the attributes are
1291 :     desired for all objects, specify C<undef> or an empty string. Finally, you can specify
1292 :     attributes for a range of object IDs by putting a percent sign (C<%>) at the end.
1293 : parrello 1.3
1294 :     =item key
1295 :    
1296 : parrello 1.10 Attribute key name. A value of C<undef> or an empty string will match all
1297 :     attribute keys. If the values are desired for multiple keys, this parameter can be
1298 :     specified as a list reference. Finally, you can specify attributes for a range of
1299 :     keys by putting a percent sign (C<%>) at the end.
1300 : parrello 1.3
1301 : parrello 1.10 =item values
1302 : parrello 1.3
1303 :     List of the desired attribute values, section by section. If C<undef>
1304 : parrello 1.10 or an empty string is specified, all values in that section will match. A
1305 :     generic match can be requested by placing a percent sign (C<%>) at the end.
1306 :     In that case, all values that match up to and not including the percent sign
1307 : parrello 1.14 will match. You may also specify a regular expression enclosed
1308 :     in slashes. All values that match the regular expression will be returned. For
1309 :     performance reasons, only values have this extra capability.
1310 : parrello 1.3
1311 :     =item RETURN
1312 :    
1313 :     Returns a list of tuples. The first element in the tuple is an object ID, the
1314 :     second is an attribute key, and the remaining elements are the sections of
1315 :     the attribute value. All of the tuples will match the criteria set forth in
1316 :     the parameter list.
1317 :    
1318 :     =back
1319 :    
1320 :     =cut
1321 :    
1322 :     sub GetAttributes {
1323 : parrello 1.4 # Get the parameters.
1324 : parrello 1.10 my ($self, $objectID, $key, @values) = @_;
1325 : parrello 1.20 # This hash will map "HasValueFor" fields to patterns. We use it to build the
1326 :     # SQL statement.
1327 :     my %data;
1328 :     # Before we do anything else, we must parse the key. The key is treated by the
1329 :     # user as a single field, but to us it's actually a real key and a subkey.
1330 :     # If the key has no splitter and is exact, the real key is the original key
1331 :     # and the subkey is an empty string. If the key has a splitter, it is
1332 :     # split into two pieces and each piece is processed separately. If the key has
1333 :     # no splitter and is generic, the real key is the incoming key and the subkey
1334 :     # is allowed to be wild. Of course, this only matters if an actual key has
1335 :     # been specified.
1336 :     if (defined $key) {
1337 :     if ($key =~ /$self->{splitter}/) {
1338 :     # Here we have a two-part key, so we split it normally.
1339 :     my ($realKey, $subKey) = $self->SplitKey($key);
1340 :     $data{'HasValueFor(from-link)'} = $realKey;
1341 :     $data{'HasValueFor(subkey)'} = $subKey;
1342 :     } elsif (substr($key, -1, 1) eq '%') {
1343 :     $data{'HasValueFor(from-link)'} = $key;
1344 :     } else {
1345 :     $data{'HasValueFor(from-link)'} = $key;
1346 :     $data{'HasValueFor(subkey)'} = '';
1347 :     }
1348 :     }
1349 :     # Add the object ID to the key information.
1350 :     $data{'HasValueFor(to-link)'} = $objectID;
1351 :     # The first value represents a problem, because we can search it using SQL, but not
1352 :     # in the normal way. If the user specifies a generic search or exact match for
1353 :     # every alternative value (remember, the values may be specified as a list),
1354 :     # then we can create SQL filtering for it. If any of the values are specified
1355 :     # as a regular expression, however, that's a problem, because we need to read
1356 :     # every value to verify a match.
1357 :     if (@values > 0) {
1358 :     # Get the first value and put its alternatives in an array.
1359 :     my $valueParm = $values[0];
1360 :     my @valueList;
1361 :     if (ref $valueParm eq 'ARRAY') {
1362 :     @valueList = @{$valueParm};
1363 :     } else {
1364 :     @valueList = ($valueParm);
1365 :     }
1366 :     # Okay, now we have all the possible criteria for the first value in the list
1367 :     # @valueList. We'll copy the values to a new array in which they have been
1368 :     # converted to generic requests. If we find a regular-expression match
1369 :     # anywhere in the list, we toss the whole thing.
1370 :     my @valuePatterns = ();
1371 :     my $okValues = 1;
1372 :     for my $valuePattern (@valueList) {
1373 :     # Check the pattern type.
1374 :     if (substr($valuePattern, 0, 1) eq '/') {
1375 :     # Regular expressions invalidate the entire process.
1376 :     $okValues = 0;
1377 :     } elsif (substr($valuePattern, -1, 1) eq '%') {
1378 :     # A Generic pattern is passed in unmodified.
1379 :     push @valuePatterns, $valuePattern;
1380 :     } else {
1381 :     # An exact match is converted to generic.
1382 :     push @valuePatterns, "$valuePattern%";
1383 :     }
1384 :     }
1385 :     # If everything works, add the value data to the filtering hash.
1386 :     if ($okValues) {
1387 :     $data{'HasValueFor(value)'} = \@valuePatterns;
1388 :     }
1389 :     }
1390 :     # Create some lists to contain the filter fragments and parameter values.
1391 : parrello 1.10 my @filter = ();
1392 :     my @parms = ();
1393 :     # This next loop goes through the different fields that can be specified in the
1394 : parrello 1.20 # parameter list and generates filters for each. The %data hash that we built above
1395 :     # contains all the necessary information to do this.
1396 : parrello 1.10 for my $field (keys %data) {
1397 :     # Accumulate filter information for this field. We will OR together all the
1398 :     # elements accumulated to create the final result.
1399 :     my @fieldFilter = ();
1400 :     # Get the specified data from the caller.
1401 :     my $fieldPattern = $data{$field};
1402 :     # Only proceed if the pattern is one that won't match everything.
1403 :     if (defined($fieldPattern) && $fieldPattern ne "" && $fieldPattern ne "%") {
1404 :     # Convert the pattern to an array.
1405 :     my @patterns = ();
1406 :     if (ref $fieldPattern eq 'ARRAY') {
1407 :     push @patterns, @{$fieldPattern};
1408 :     } else {
1409 :     push @patterns, $fieldPattern;
1410 :     }
1411 :     # Only proceed if the array is nonempty. The loop will work fine if the
1412 :     # array is empty, but when we build the filter string at the end we'll
1413 :     # get "()" in the filter list, which will result in an SQL syntax error.
1414 :     if (@patterns) {
1415 :     # Loop through the individual patterns.
1416 :     for my $pattern (@patterns) {
1417 :     # Check for a generic request.
1418 :     if (substr($pattern, -1, 1) ne '%') {
1419 :     # Here we have a normal request.
1420 :     push @fieldFilter, "$field = ?";
1421 :     push @parms, $pattern;
1422 :     } else {
1423 : parrello 1.20 # Here we have a generic request, so we will use the LIKE operator to
1424 : parrello 1.10 # filter the field to this value pattern.
1425 :     push @fieldFilter, "$field LIKE ?";
1426 :     # We must convert the pattern value to an SQL match pattern. First
1427 : parrello 1.11 # we get a copy of it.
1428 :     my $actualPattern = $pattern;
1429 : parrello 1.10 # Now we escape the underscores. Underscores are an SQL wild card
1430 :     # character, but they are used frequently in key names and object IDs.
1431 : parrello 1.11 $actualPattern =~ s/_/\\_/g;
1432 : parrello 1.10 # Add the escaped pattern to the bound parameter list.
1433 :     push @parms, $actualPattern;
1434 :     }
1435 :     }
1436 :     # Form the filter for this field.
1437 :     my $fieldFilterString = join(" OR ", @fieldFilter);
1438 :     push @filter, "($fieldFilterString)";
1439 :     }
1440 :     }
1441 :     }
1442 :     # Now @filter contains one or more filter strings and @parms contains the parameter
1443 :     # values to bind to them.
1444 :     my $actualFilter = join(" AND ", @filter);
1445 :     # Now we're ready to make our query.
1446 : parrello 1.11 my $query = $self->Get(['HasValueFor'], $actualFilter, \@parms);
1447 : parrello 1.15 # Format the results.
1448 :     my @retVal = $self->_QueryResults($query, @values);
1449 : parrello 1.10 # Return the rows found.
1450 : parrello 1.3 return @retVal;
1451 :     }
1452 :    
1453 :     =head3 AddAttribute
1454 :    
1455 :     C<< $attrDB->AddAttribute($objectID, $key, @values); >>
1456 :    
1457 :     Add an attribute key/value pair to an object. This method cannot add a new key, merely
1458 :     add a value to an existing key. Use L</StoreAttributeKey> to create a new key.
1459 :    
1460 :     =over 4
1461 :    
1462 :     =item objectID
1463 :    
1464 : parrello 1.10 ID of the object to which the attribute is to be added.
1465 : parrello 1.3
1466 :     =item key
1467 :    
1468 : parrello 1.10 Attribute key name.
1469 : parrello 1.3
1470 :     =item values
1471 :    
1472 :     One or more values to be associated with the key. The values are joined together with
1473 :     the splitter value before being stored as field values. This enables L</GetAttributes>
1474 :     to split them apart during retrieval. The splitter value defaults to double colons C<::>.
1475 :    
1476 :     =back
1477 :    
1478 :     =cut
1479 :    
1480 :     sub AddAttribute {
1481 :     # Get the parameters.
1482 : parrello 1.4 my ($self, $objectID, $key, @values) = @_;
1483 : parrello 1.3 # Don't allow undefs.
1484 :     if (! defined($objectID)) {
1485 :     Confess("No object ID specified for AddAttribute call.");
1486 :     } elsif (! defined($key)) {
1487 :     Confess("No attribute key specified for AddAttribute call.");
1488 :     } elsif (! @values) {
1489 :     Confess("No values specified in AddAttribute call for key $key.");
1490 :     } else {
1491 : parrello 1.11 # Okay, now we have some reason to believe we can do this. Form the values
1492 :     # into a scalar.
1493 : parrello 1.3 my $valueString = join($self->{splitter}, @values);
1494 : parrello 1.20 # Split up the key.
1495 :     my ($realKey, $subKey) = $self->SplitKey($key);
1496 : parrello 1.11 # Connect the object to the key.
1497 : parrello 1.20 $self->InsertObject('HasValueFor', { 'from-link' => $realKey,
1498 : parrello 1.11 'to-link' => $objectID,
1499 : parrello 1.20 'subkey' => $subKey,
1500 : parrello 1.11 'value' => $valueString,
1501 :     });
1502 : parrello 1.3 }
1503 : parrello 1.10 # Return a one, indicating success. We do this for backward compatability.
1504 : parrello 1.3 return 1;
1505 :     }
1506 :    
1507 :     =head3 DeleteAttribute
1508 :    
1509 :     C<< $attrDB->DeleteAttribute($objectID, $key, @values); >>
1510 :    
1511 :     Delete the specified attribute key/value combination from the database.
1512 :    
1513 :     =over 4
1514 :    
1515 :     =item objectID
1516 :    
1517 : parrello 1.10 ID of the object whose attribute is to be deleted.
1518 : parrello 1.3
1519 :     =item key
1520 :    
1521 : parrello 1.10 Attribute key name.
1522 : parrello 1.3
1523 :     =item values
1524 :    
1525 : parrello 1.10 One or more values associated with the key. If no values are specified, then all values
1526 :     will be deleted. Otherwise, only a matching value will be deleted.
1527 : parrello 1.3
1528 :     =back
1529 :    
1530 :     =cut
1531 :    
1532 :     sub DeleteAttribute {
1533 :     # Get the parameters.
1534 : parrello 1.4 my ($self, $objectID, $key, @values) = @_;
1535 : parrello 1.3 # Don't allow undefs.
1536 :     if (! defined($objectID)) {
1537 :     Confess("No object ID specified for DeleteAttribute call.");
1538 :     } elsif (! defined($key)) {
1539 :     Confess("No attribute key specified for DeleteAttribute call.");
1540 :     } else {
1541 : parrello 1.20 # Split the key into the real key and the subkey.
1542 :     my ($realKey, $subKey) = $self->SplitKey($key);
1543 :     if ($subKey eq '' && scalar(@values) == 0) {
1544 :     # Here we erase the entire key for this object.
1545 :     $self->DeleteRow('HasValueFor', $key, $objectID);
1546 :     } else {
1547 :     # Here we erase the matching values.
1548 :     my $valueString = join($self->{splitter}, @values);
1549 :     $self->DeleteRow('HasValueFor', $realKey, $objectID,
1550 :     { subkey => $subKey, value => $valueString });
1551 :     }
1552 : parrello 1.3 }
1553 :     # Return a one. This is for backward compatability.
1554 :     return 1;
1555 :     }
1556 :    
1557 : parrello 1.16 =head3 DeleteMatchingAttributes
1558 :    
1559 :     C<< my @deleted = $attrDB->DeleteMatchingAttributes($objectID, $key, @values); >>
1560 :    
1561 :     Delete all attributes that match the specified criteria. This is equivalent to
1562 :     calling L</GetAttributes> and then invoking L</DeleteAttribute> for each
1563 :     row found.
1564 :    
1565 :     =over 4
1566 :    
1567 :     =item objectID
1568 :    
1569 :     ID of object whose attributes are to be deleted. If the attributes for multiple
1570 :     objects are to be deleted, this parameter can be specified as a list reference. If
1571 :     attributes are to be deleted for all objects, specify C<undef> or an empty string.
1572 :     Finally, you can delete attributes for a range of object IDs by putting a percent
1573 :     sign (C<%>) at the end.
1574 :    
1575 :     =item key
1576 :    
1577 :     Attribute key name. A value of C<undef> or an empty string will match all
1578 :     attribute keys. If the values are to be deletedfor multiple keys, this parameter can be
1579 :     specified as a list reference. Finally, you can delete attributes for a range of
1580 :     keys by putting a percent sign (C<%>) at the end.
1581 :    
1582 :     =item values
1583 :    
1584 :     List of the desired attribute values, section by section. If C<undef>
1585 :     or an empty string is specified, all values in that section will match. A
1586 :     generic match can be requested by placing a percent sign (C<%>) at the end.
1587 :     In that case, all values that match up to and not including the percent sign
1588 :     will match. You may also specify a regular expression enclosed
1589 :     in slashes. All values that match the regular expression will be deleted. For
1590 :     performance reasons, only values have this extra capability.
1591 :    
1592 :     =item RETURN
1593 :    
1594 :     Returns a list of tuples for the attributes that were deleted, in the
1595 :     same form as L</GetAttributes>.
1596 :    
1597 :     =back
1598 :    
1599 :     =cut
1600 :    
1601 :     sub DeleteMatchingAttributes {
1602 :     # Get the parameters.
1603 :     my ($self, $objectID, $key, @values) = @_;
1604 :     # Get the matching attributes.
1605 :     my @retVal = $self->GetAttributes($objectID, $key, @values);
1606 :     # Loop through the attributes, deleting them.
1607 :     for my $tuple (@retVal) {
1608 :     $self->DeleteAttribute(@{$tuple});
1609 :     }
1610 : parrello 1.18 # Log this operation.
1611 :     my $count = @retVal;
1612 :     $self->LogOperation("Mass Delete", $key, "$count matching attributes deleted.");
1613 : parrello 1.16 # Return the deleted attributes.
1614 :     return @retVal;
1615 :     }
1616 :    
1617 : parrello 1.3 =head3 ChangeAttribute
1618 :    
1619 :     C<< $attrDB->ChangeAttribute($objectID, $key, \@oldValues, \@newValues); >>
1620 :    
1621 :     Change the value of an attribute key/value pair for an object.
1622 :    
1623 :     =over 4
1624 :    
1625 :     =item objectID
1626 :    
1627 :     ID of the genome or feature to which the attribute is to be changed. In general, an ID that
1628 :     starts with C<fig|> is treated as a feature ID, and an ID that is all digits and periods
1629 :     is treated as a genome ID. For IDs of other types, this parameter should be a reference
1630 :     to a 2-tuple consisting of the entity type name followed by the object ID.
1631 :    
1632 :     =item key
1633 :    
1634 :     Attribute key name. This corresponds to the name of a field in the database.
1635 :    
1636 :     =item oldValues
1637 :    
1638 :     One or more values identifying the key/value pair to change.
1639 :    
1640 :     =item newValues
1641 :    
1642 :     One or more values to be put in place of the old values.
1643 :    
1644 :     =back
1645 :    
1646 :     =cut
1647 :    
1648 :     sub ChangeAttribute {
1649 :     # Get the parameters.
1650 : parrello 1.4 my ($self, $objectID, $key, $oldValues, $newValues) = @_;
1651 : parrello 1.3 # Don't allow undefs.
1652 :     if (! defined($objectID)) {
1653 :     Confess("No object ID specified for ChangeAttribute call.");
1654 :     } elsif (! defined($key)) {
1655 :     Confess("No attribute key specified for ChangeAttribute call.");
1656 :     } elsif (! defined($oldValues) || ref $oldValues ne 'ARRAY') {
1657 :     Confess("No old values specified in ChangeAttribute call for key $key.");
1658 :     } elsif (! defined($newValues) || ref $newValues ne 'ARRAY') {
1659 :     Confess("No new values specified in ChangeAttribute call for key $key.");
1660 :     } else {
1661 : parrello 1.10 # We do the change as a delete/add.
1662 : parrello 1.3 $self->DeleteAttribute($objectID, $key, @{$oldValues});
1663 :     $self->AddAttribute($objectID, $key, @{$newValues});
1664 :     }
1665 :     # Return a one. We do this for backward compatability.
1666 :     return 1;
1667 :     }
1668 :    
1669 : parrello 1.7 =head3 EraseAttribute
1670 :    
1671 : parrello 1.11 C<< $attrDB->EraseAttribute($key); >>
1672 : parrello 1.7
1673 :     Erase all values for the specified attribute key. This does not remove the
1674 :     key from the database; it merely removes all the values.
1675 :    
1676 :     =over 4
1677 :    
1678 :     =item key
1679 :    
1680 : parrello 1.20 Key to erase. This must be a real key; that is, it cannot have a subkey
1681 :     component.
1682 : parrello 1.7
1683 :     =back
1684 :    
1685 :     =cut
1686 :    
1687 :     sub EraseAttribute {
1688 :     # Get the parameters.
1689 : parrello 1.10 my ($self, $key) = @_;
1690 : parrello 1.16 # Delete everything connected to the key.
1691 :     $self->Disconnect('HasValueFor', 'AttributeKey', $key);
1692 : parrello 1.18 # Log the operation.
1693 :     $self->LogOperation("Erase Data", $key);
1694 : parrello 1.7 # Return a 1, for backward compatability.
1695 :     return 1;
1696 :     }
1697 :    
1698 : parrello 1.9 =head3 GetAttributeKeys
1699 :    
1700 : parrello 1.10 C<< my @keyList = $attrDB->GetAttributeKeys($groupName); >>
1701 : parrello 1.9
1702 : parrello 1.10 Return a list of the attribute keys for a particular group.
1703 : parrello 1.9
1704 :     =over 4
1705 :    
1706 : parrello 1.10 =item groupName
1707 : parrello 1.9
1708 : parrello 1.10 Name of the group whose keys are desired.
1709 : parrello 1.9
1710 :     =item RETURN
1711 :    
1712 : parrello 1.10 Returns a list of the attribute keys for the specified group.
1713 : parrello 1.9
1714 :     =back
1715 :    
1716 :     =cut
1717 :    
1718 :     sub GetAttributeKeys {
1719 :     # Get the parameters.
1720 : parrello 1.10 my ($self, $groupName) = @_;
1721 :     # Get the attributes for the specified group.
1722 :     my @groups = $self->GetFlat(['IsInGroup'], "IsInGroup(to-link) = ?", [$groupName],
1723 :     'IsInGroup(from-link)');
1724 : parrello 1.9 # Return the keys.
1725 : parrello 1.10 return sort @groups;
1726 : parrello 1.9 }
1727 :    
1728 : parrello 1.24 =head3 QueryAttributes
1729 :    
1730 :     C<< my @attributeData = $ca->QueryAttributes($filter, $filterParms); >>
1731 :    
1732 :     Return the attribute data based on an SQL filter clause. In the filter clause,
1733 :     the name C<$object> should be used for the object ID, C<$key> should be used for
1734 :     the key name, C<$subkey> for the subkey value, and C<$value> for the value field.
1735 :    
1736 :     =over 4
1737 :    
1738 :     =item filter
1739 :    
1740 :     Filter clause in the standard ERDB format, except that the field names are C<$object> for
1741 :     the object ID field, C<$key> for the key name field, C<$subkey> for the subkey field,
1742 :     and C<$value> for the value field. This abstraction enables us to hide the details of
1743 :     the database construction from the user.
1744 :    
1745 :     =item filterParms
1746 :    
1747 :     Parameters for the filter clause.
1748 :    
1749 :     =item RETURN
1750 :    
1751 :     Returns a list of tuples. Each tuple consists of an object ID, a key (with optional subkey), and
1752 :     one or more attribute values.
1753 :    
1754 :     =back
1755 :    
1756 :     =cut
1757 :    
1758 :     # This hash is used to drive the substitution process.
1759 :     my %AttributeParms = (object => 'HasValueFor(to-link)',
1760 :     key => 'HasValueFor(from-link)',
1761 :     subkey => 'HasValueFor(subkey)',
1762 :     value => 'HasValueFor(value)');
1763 :    
1764 :     sub QueryAttributes {
1765 :     # Get the parameters.
1766 :     my ($self, $filter, $filterParms) = @_;
1767 :     # Declare the return variable.
1768 :     my @retVal = ();
1769 :     # Make sue we have filter parameters.
1770 :     my $realParms = (defined($filterParms) ? $filterParms : []);
1771 :     # Create the query by converting the filter.
1772 :     my $realFilter = $filter;
1773 :     for my $name (keys %AttributeParms) {
1774 :     $realFilter =~ s/\$$name/$AttributeParms{$name}/g;
1775 :     }
1776 :     my $query = $self->Get(['HasValueFor'], $realFilter, $realParms);
1777 :     # Loop through the results, forming the output attribute tuples.
1778 :     while (my $result = $query->Fetch()) {
1779 :     # Get the four values from this query result row.
1780 :     my ($objectID, $key, $subkey, $value) = $result->Values([$AttributeParms{object},
1781 :     $AttributeParms{key},
1782 :     $AttributeParms{subkey},
1783 :     $AttributeParms{value}]);
1784 :     # Combine the key and the subkey.
1785 :     my $realKey = ($subkey ? $key . $self->{splitter} . $subkey : $key);
1786 :     # Split the value.
1787 :     my @values = split $self->{splitter}, $value;
1788 :     # Output the result.
1789 :     push @retVal, [$objectID, $realKey, @values];
1790 :     }
1791 :     # Return the result.
1792 :     return @retVal;
1793 :     }
1794 :    
1795 : parrello 1.20 =head2 Key and ID Manipulation Methods
1796 :    
1797 : parrello 1.19 =head3 ParseID
1798 :    
1799 :     C<< my ($type, $id) = CustomAttributes::ParseID($idValue); >>
1800 :    
1801 :     Determine the type and object ID corresponding to an ID value from the attribute database.
1802 :     Most ID values consist of a type name and an ID, separated by a colon (e.g. C<Family:aclame|cluster10>);
1803 :     however, Genomes, Features, and Subsystems are not stored with a type name, so we need to
1804 :     deduce the type from the ID value structure.
1805 :    
1806 :     The theory here is that you can plug the ID and type directly into a Sprout database method, as
1807 :     follows
1808 :    
1809 :     my ($type, $id) = CustomAttributes::ParseID($attrList[$num]->[0]);
1810 :     my $target = $sprout->GetEntity($type, $id);
1811 :    
1812 :     =over 4
1813 :    
1814 :     =item idValue
1815 :    
1816 :     ID value taken from the attribute database.
1817 :    
1818 :     =item RETURN
1819 :    
1820 :     Returns a two-element list. The first element is the type of object indicated by the ID value,
1821 :     and the second element is the actual object ID.
1822 :    
1823 :     =back
1824 :    
1825 :     =cut
1826 :    
1827 :     sub ParseID {
1828 :     # Get the parameters.
1829 :     my ($idValue) = @_;
1830 :     # Declare the return variables.
1831 :     my ($type, $id);
1832 :     # Parse the incoming ID. We first check for the presence of an entity name. Entity names
1833 :     # can only contain letters, which helps to insure typed object IDs don't collide with
1834 :     # subsystem names (which are untyped).
1835 :     if ($idValue =~ /^([A-Za-z]+):(.+)/) {
1836 :     # Here we have a typed ID.
1837 :     ($type, $id) = ($1, $2);
1838 : parrello 1.26 # Fix the case sensitivity on PDB IDs.
1839 :     if ($type eq 'PDB') { $id = lc $id; }
1840 : parrello 1.19 } elsif ($idValue =~ /fig\|/) {
1841 :     # Here we have a feature ID.
1842 :     ($type, $id) = (Feature => $idValue);
1843 :     } elsif ($idValue =~ /\d+\.\d+/) {
1844 :     # Here we have a genome ID.
1845 :     ($type, $id) = (Genome => $idValue);
1846 :     } else {
1847 :     # The default is a subsystem ID.
1848 :     ($type, $id) = (Subsystem => $idValue);
1849 :     }
1850 :     # Return the results.
1851 :     return ($type, $id);
1852 :     }
1853 :    
1854 :     =head3 FormID
1855 :    
1856 :     C<< my $idValue = CustomAttributes::FormID($type, $id); >>
1857 :    
1858 :     Convert an object type and ID pair into an object ID string for the attribute system. Subsystems,
1859 :     genomes, and features are stored in the database without type information, but all other object IDs
1860 :     must be prefixed with the object type.
1861 :    
1862 :     =over 4
1863 :    
1864 :     =item type
1865 :    
1866 :     Relevant object type.
1867 :    
1868 :     =item id
1869 :    
1870 :     ID of the object in question.
1871 :    
1872 :     =item RETURN
1873 :    
1874 :     Returns a string that will be recognized as an object ID in the attribute database.
1875 :    
1876 :     =back
1877 :    
1878 :     =cut
1879 :    
1880 :     sub FormID {
1881 :     # Get the parameters.
1882 :     my ($type, $id) = @_;
1883 :     # Declare the return variable.
1884 :     my $retVal;
1885 :     # Compute the ID string from the type.
1886 :     if (grep { $type eq $_ } qw(Feature Genome Subsystem)) {
1887 :     $retVal = $id;
1888 :     } else {
1889 :     $retVal = "$type:$id";
1890 :     }
1891 :     # Return the result.
1892 :     return $retVal;
1893 :     }
1894 :    
1895 :     =head3 GetTargetObject
1896 :    
1897 :     C<< my $object = CustomAttributes::GetTargetObject($erdb, $idValue); >>
1898 :    
1899 :     Return the database object corresponding to the specified attribute object ID. The
1900 :     object type associated with the ID value must correspond to an entity name in the
1901 :     specified database.
1902 :    
1903 :     =over 4
1904 :    
1905 :     =item erdb
1906 :    
1907 :     B<ERDB> object for accessing the target database.
1908 :    
1909 :     =item idValue
1910 :    
1911 :     ID value retrieved from the attribute database.
1912 :    
1913 :     =item RETURN
1914 :    
1915 : parrello 1.22 Returns a B<ERDBObject> for the attribute value's target object.
1916 : parrello 1.19
1917 :     =back
1918 :    
1919 :     =cut
1920 :    
1921 :     sub GetTargetObject {
1922 :     # Get the parameters.
1923 :     my ($erdb, $idValue) = @_;
1924 :     # Declare the return variable.
1925 :     my $retVal;
1926 :     # Get the type and ID for the target object.
1927 :     my ($type, $id) = ParseID($idValue);
1928 :     # Plug them into the GetEntity method.
1929 :     $retVal = $erdb->GetEntity($type, $id);
1930 :     # Return the resulting object.
1931 :     return $retVal;
1932 :     }
1933 :    
1934 : parrello 1.20 =head3 SplitKey
1935 :    
1936 :     C<< my ($realKey, $subKey) = $ca->SplitKey($key); >>
1937 :    
1938 :     Split an external key (that is, one passed in by a caller) into the real key and the sub key.
1939 :     The real and sub keys are separated by a splitter value (usually C<::>). If there is no splitter,
1940 :     then the sub key is presumed to be an empty string.
1941 :    
1942 :     =over 4
1943 :    
1944 :     =item key
1945 :    
1946 :     Incoming key to be split.
1947 :    
1948 :     =item RETURN
1949 :    
1950 :     Returns a two-element list, the first element of which is the real key and the second element of
1951 :     which is the sub key.
1952 :    
1953 :     =back
1954 :    
1955 :     =cut
1956 :    
1957 :     sub SplitKey {
1958 :     # Get the parameters.
1959 :     my ($self, $key) = @_;
1960 :     # Do the split.
1961 :     my ($realKey, $subKey) = split($self->{splitter}, $key, 2);
1962 :     # Insure the subkey has a value.
1963 :     if (! defined $subKey) {
1964 :     $subKey = '';
1965 :     }
1966 :     # Return the results.
1967 :     return ($realKey, $subKey);
1968 :     }
1969 :    
1970 :     =head3 JoinKey
1971 :    
1972 :     C<< my $key = $ca->JoinKey($realKey, $subKey); >>
1973 :    
1974 :     Join a real key and a subkey together to make an external key. The external key is the attribute key
1975 :     used by the caller. The real key and the subkey are how the keys are represented in the database. The
1976 :     real key is the key to the B<AttributeKey> entity. The subkey is an attribute of the B<HasValueFor>
1977 :     relationship.
1978 :    
1979 :     =over 4
1980 :    
1981 :     =item realKey
1982 :    
1983 :     The real attribute key.
1984 :    
1985 :     =item subKey
1986 :    
1987 :     The subordinate portion of the attribute key.
1988 :    
1989 :     =item RETURN
1990 :    
1991 :     Returns a single string representing both keys.
1992 :    
1993 :     =back
1994 :    
1995 :     =cut
1996 :    
1997 :     sub JoinKey {
1998 :     # Get the parameters.
1999 :     my ($self, $realKey, $subKey) = @_;
2000 :     # Declare the return variable.
2001 :     my $retVal;
2002 :     # Check for a subkey.
2003 :     if ($subKey eq '') {
2004 :     # No subkey, so the real key is the key.
2005 :     $retVal = $realKey;
2006 :     } else {
2007 :     # Subkey found, so the two pieces must be joined by a splitter.
2008 :     $retVal = "$realKey$self->{splitter}$subKey";
2009 :     }
2010 :     # Return the result.
2011 :     return $retVal;
2012 :     }
2013 :    
2014 : parrello 1.26
2015 :     =head3 AttributeTable
2016 :    
2017 :     C<< my $tableHtml = CustomAttributes::AttributeTable($cgi, @attrList); >>
2018 :    
2019 :     Format the attribute data into an HTML table.
2020 :    
2021 :     =over 4
2022 :    
2023 :     =item cgi
2024 :    
2025 :     CGI query object used to generate the HTML
2026 :    
2027 :     =item attrList
2028 :    
2029 :     List of attribute results, in the format returned by the L</GetAttributes> or
2030 :     L</QueryAttributes> methods.
2031 :    
2032 :     =item RETURN
2033 :    
2034 :     Returns an HTML table displaying the attribute keys and values.
2035 :    
2036 :     =back
2037 :    
2038 :     =cut
2039 :    
2040 :     sub AttributeTable {
2041 :     # Get the parameters.
2042 :     my ($cgi, @attrList) = @_;
2043 :     # Accumulate the table rows.
2044 :     my @html = ();
2045 :     for my $attrData (@attrList) {
2046 :     # Format the object ID and key.
2047 :     my @columns = map { CGI::escapeHTML($_) } @{$attrData}[0,1];
2048 :     # Now we format the values. These remain unchanged unless one of them is a URL.
2049 :     my $lastValue = scalar(@{$attrData}) - 1;
2050 :     push @columns, map { $_ =~ /^http:/ ? $cgi->a({ href => $_ }, $_) : $_ } @{$attrData}[2 .. $lastValue];
2051 :     # Assemble the values into a table row.
2052 :     push @html, $cgi->Tr($cgi->td(\@columns));
2053 :     }
2054 :     # Format the table in the return variable.
2055 :     my $retVal = $cgi->table({ border => 2 }, $cgi->Tr($cgi->th(['Object', 'Key', 'Values'])), @html);
2056 :     # Return it.
2057 :     return $retVal;
2058 :     }
2059 : parrello 1.1 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3