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

Annotation of /Sprout/CustomAttributes.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3