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

Annotation of /Sprout/ERDBTypeImage.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     #
4 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
5 :     # for Interpretations of Genomes. All Rights Reserved.
6 :     #
7 :     # This file is part of the SEED Toolkit.
8 :     #
9 :     # The SEED Toolkit is free software. You can redistribute
10 :     # it and/or modify it under the terms of the SEED Toolkit
11 :     # Public License.
12 :     #
13 :     # You should have received a copy of the SEED Toolkit Public License
14 :     # along with this program; if not write to the University of Chicago
15 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
16 :     # Genomes at veronika@thefig.info or download a copy from
17 :     # http://www.theseed.org/LICENSE.TXT.
18 :     #
19 :    
20 :     package ERDBTypeImage;
21 :    
22 :     use strict;
23 :     use Tracer;
24 :     use ERDB;
25 :     use GD;
26 :     use MIME::Base64;
27 : parrello 1.3 use ERDBExtras;
28 : parrello 1.1 use base qw(ERDBType);
29 :    
30 :     =head1 ERDB Image Type Definition
31 :    
32 :     =head2 Introduction
33 :    
34 :     This object represents the data type for images stored in the database. Each
35 :     image must be a PERL B<GD> object. It is stored in the database as a base64 MIME
36 :     string computed from the PNG file format.
37 :    
38 :     =head3 new
39 :    
40 :     my $et = ERDBTypeImage->new();
41 :    
42 :     Construct a new ERDBTypeImage descriptor.
43 :    
44 :     =cut
45 :    
46 :     sub new {
47 :     # Get the parameters.
48 :     my ($class) = @_;
49 :     # Create the ERDBTypeImage object.
50 :     my $retVal = { };
51 :     # Bless and return it.
52 :     bless $retVal, $class;
53 :     return $retVal;
54 :     }
55 :    
56 : parrello 1.6 =head3 NewSessionID
57 :    
58 :     my $id = ERDBTypeImage::NewSessionID();
59 :    
60 :     Generate a new session ID for the current user.
61 :    
62 :     =cut
63 :    
64 :     sub NewSessionID {
65 :     # Declare the return variable.
66 :     my $retVal;
67 :     # Get a digest encoder.
68 :     Trace("Retrieving digest encoder.") if T(3);
69 :     my $md5 = Digest::MD5->new();
70 :     # Add the PID, the IP, and the time stamp. Note that the time stamp is
71 :     # actually two numbers, and we get them both because we're in list
72 :     # context.
73 :     Trace("Assembling pieces.") if T(3);
74 :     $md5->add($$, $ENV{REMOTE_ADDR}, $ENV{REMOTE_PORT}, gettimeofday());
75 :     # Hash up all this identifying data.
76 :     Trace("Producing result.") if T(3);
77 :     $retVal = $md5->hexdigest();
78 :     # Return the result.
79 :     return $retVal;
80 :     }
81 :    
82 :    
83 : parrello 1.1 =head2 Virtual Methods
84 :    
85 :     =head3 averageLength
86 :    
87 :     my $value = $et->averageLength();
88 :    
89 :     Return the average length of a data item of this field type when it is stored in the
90 :     database. This value is used to compute the expected size of a database table.
91 :    
92 :     =cut
93 :    
94 :     sub averageLength {
95 :     return 50000;
96 :     }
97 :    
98 :     =head3 prettySortValue
99 :    
100 :     my $value = $et->prettySortValue();
101 :    
102 :     Number indicating where fields of this type should go in relation to other
103 :     fields. The value should be somewhere between C<1> and C<5>. A value outside
104 :     that range will make terrible things happen.
105 :    
106 :     =cut
107 :    
108 :     sub prettySortValue() {
109 :     return 5;
110 :     }
111 :    
112 :     =head3 validate
113 :    
114 :     my $okFlag = $et->validate($value);
115 :    
116 :     Return an error message if the specified value is invalid for this field type.
117 :    
118 :     The parameters are as follows.
119 :    
120 :     =over 4
121 :    
122 :     =item value
123 :    
124 :     Value of this type, for validation.
125 :    
126 :     =item RETURN
127 :    
128 :     Returns an empty string if the specified field is valid, and an error message
129 :     otherwise.
130 :    
131 :     =back
132 :    
133 :     =cut
134 :    
135 :     sub validate {
136 :     # Get the parameters.
137 :     my ($self, $value) = @_;
138 :     # Assume it's valid until we prove otherwise.
139 :     my $retVal = "";
140 :     # Only a real GD object is valid.
141 :     if (! UNIVERSAL::isa($value, 'GD::Image')) {
142 :     $retVal = "Invalid image value.";
143 :     }
144 :     # Return the determination.
145 :     return $retVal;
146 :     }
147 :    
148 :     =head3 encode
149 :    
150 :     my $string = $et->encode($value, $mode);
151 :    
152 :     Encode a value of this field type for storage in the database (or in a database load
153 :     file.)
154 :    
155 :     The parameters are as follows.
156 :    
157 :     =over 4
158 :    
159 :     =item value
160 :    
161 :     Value of this type, for encoding.
162 :    
163 :     =item mode
164 :    
165 :     TRUE if the value is being encoding for placement in a load file, FALSE if it
166 :     is being encoded for use as an SQL statement parameter. In most cases, the
167 :     encoding is the same for both modes.
168 :    
169 :     =back
170 :    
171 :     =cut
172 :    
173 :     sub encode {
174 :     # Get the parameters.
175 :     my ($self, $value, $mode) = @_;
176 :     # Encode the value.
177 :     my $retVal = encode_base64($value->png(), "");
178 :     # Return the result.
179 :     return $retVal;
180 :     }
181 :    
182 :     =head3 decode
183 :    
184 :     my $value = $et->decode($string);
185 :    
186 :     Decode a string from the database into a value of this field type.
187 :    
188 :     The parameters are as follows.
189 :    
190 :     =over 4
191 :    
192 :     =item string
193 :    
194 :     String from the database to be decoded.
195 :    
196 :     =item RETURN
197 :    
198 :     Returns a value of the desired type.
199 :    
200 :     =back
201 :    
202 :     =cut
203 :    
204 :     sub decode {
205 :     # Get the parameters.
206 :     my ($self, $string) = @_;
207 :     # Decode the value.
208 :     my $pngData = decode_base64($string);
209 :     my $retVal = GD::Image->newFromPngData($pngData);
210 :     # Return the result.
211 :     return $retVal;
212 :     }
213 :    
214 :     =head3 sqlType
215 :    
216 : parrello 1.5 my $typeString = $et->sqlType($dbh);
217 : parrello 1.1
218 :     Return the SQL data type for this field type.
219 :    
220 : parrello 1.5 =over 4
221 :    
222 :     =item dbh
223 :    
224 :     Open L<DBKernel> handle for the database in question. This is used when the
225 :     datatype may be different depending on the DBMS used.
226 :    
227 :     =item RETURN
228 :    
229 :     Returns the datatype string to be used when creating a field of this type in
230 :     an SQL table.
231 :    
232 :     =back
233 :    
234 : parrello 1.1 =cut
235 :    
236 :     sub sqlType {
237 : parrello 1.5 my ($self, $dbh) = @_;
238 :     my $retVal = "TEXT";
239 :     if ($dbh->dbms eq 'mysql') {
240 :     $retVal = "LONGTEXT";
241 :     }
242 :     return $retVal;
243 : parrello 1.1 }
244 :    
245 :     =head3 indexMod
246 :    
247 :     my $length = $et->indexMod();
248 :    
249 :     Return the index modifier for this field type. The index modifier is the number of
250 :     characters to be indexed. If it is undefined, the field cannot be indexed. If it
251 :     is an empty string, the entire field is indexed. The default is an empty string.
252 :    
253 :     =cut
254 :    
255 :     sub indexMod {
256 :     return undef;
257 :     }
258 :    
259 :     =head3 sortType
260 :    
261 :     my $letter = $et->sortType();
262 :    
263 :     Return the sorting type for this field type. The sorting type is C<n> for integers,
264 :     C<g> for floating-point numbers, and the empty string for character fields.
265 :     The default is the empty string.
266 :    
267 :     =cut
268 :    
269 :     sub sortType {
270 :     return "";
271 :     }
272 :    
273 :     =head3 documentation
274 :    
275 :     my $docText = $et->documentation();
276 :    
277 :     Return the documentation text for this field type. This should be in TWiki markup
278 :     format, though HTML will also work.
279 :    
280 :     =cut
281 :    
282 :     sub documentation() {
283 :     return 'PNG format graphical image';
284 :     }
285 :    
286 :     =head3 name
287 :    
288 :     my $name = $et->name();
289 :    
290 :     Return the name of this type, as it will appear in the XML database definition.
291 :    
292 :     =cut
293 :    
294 :     sub name() {
295 :     return "image";
296 :     }
297 :    
298 :     =head3 default
299 :    
300 :     my $defaultValue = $et->default();
301 :    
302 :     Default value to be used for fields of this type if no default value is
303 : parrello 1.4 specified in the database definition or in an L<ERDBLoadGroup/Put>
304 : parrello 1.1 call during a loader operation. The default is undefined, which means
305 :     an error will be thrown during the load.
306 :    
307 :     =cut
308 :    
309 :     sub default {
310 :     return undef;
311 :     }
312 :    
313 :     =head3 align
314 :    
315 :     my $alignment = $et->align();
316 :    
317 :     Return the display alignment for fields of this type: either C<left>, C<right>, or
318 :     C<center>. The default is C<left>.
319 :    
320 :     =cut
321 :    
322 :     sub align {
323 :     return 'center';
324 :     }
325 :    
326 :     =head3 html
327 :    
328 :     my $html = $et->html($value);
329 :    
330 :     Return the HTML for displaying the content of a field of this type in an output
331 :     table. The default is the raw value, html-escaped.
332 :    
333 :     =cut
334 :    
335 :     sub html {
336 :     my ($self, $value) = @_;
337 :     # The incoming value here is a GD graphic image. We need to store it
338 :     # to a temporary file.
339 : parrello 1.6 my $sessionID = NewSessionID();
340 :     my $fileName = $sessionID . "image$$.png";
341 : parrello 1.3 my $oh = Open(undef, ">$ERDBExtras::temp/$fileName");
342 : parrello 1.1 print $oh $value->png();
343 :     close $oh;
344 : parrello 1.3 my $retVal = CGI::img({ src => "$ERDBExtras::temp_url/$fileName" });
345 : parrello 1.1 return $retVal;
346 :     }
347 :    
348 : parrello 1.2 =head3 objectType
349 :    
350 :     my $type = $et->objectType();
351 :    
352 :     Return the PERL type for fields of this type. An undefined value means it's
353 :     a scalar; otherwise, it should be the package name (suitable for a C<use> clause).
354 :    
355 :     =cut
356 :    
357 :     sub objectType {
358 :     return "GD::Image";
359 :     }
360 : parrello 1.1
361 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3