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

Annotation of /Sprout/ERDBTypeImage.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3