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

Annotation of /Sprout/ERDBTypeImage.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (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 :     my $typeString = $et->sqlType();
191 :    
192 :     Return the SQL data type for this field type.
193 :    
194 :     =cut
195 :    
196 :     sub sqlType {
197 :     return "LONGTEXT";
198 :     }
199 :    
200 :     =head3 indexMod
201 :    
202 :     my $length = $et->indexMod();
203 :    
204 :     Return the index modifier for this field type. The index modifier is the number of
205 :     characters to be indexed. If it is undefined, the field cannot be indexed. If it
206 :     is an empty string, the entire field is indexed. The default is an empty string.
207 :    
208 :     =cut
209 :    
210 :     sub indexMod {
211 :     return undef;
212 :     }
213 :    
214 :     =head3 sortType
215 :    
216 :     my $letter = $et->sortType();
217 :    
218 :     Return the sorting type for this field type. The sorting type is C<n> for integers,
219 :     C<g> for floating-point numbers, and the empty string for character fields.
220 :     The default is the empty string.
221 :    
222 :     =cut
223 :    
224 :     sub sortType {
225 :     return "";
226 :     }
227 :    
228 :     =head3 documentation
229 :    
230 :     my $docText = $et->documentation();
231 :    
232 :     Return the documentation text for this field type. This should be in TWiki markup
233 :     format, though HTML will also work.
234 :    
235 :     =cut
236 :    
237 :     sub documentation() {
238 :     return 'PNG format graphical image';
239 :     }
240 :    
241 :     =head3 name
242 :    
243 :     my $name = $et->name();
244 :    
245 :     Return the name of this type, as it will appear in the XML database definition.
246 :    
247 :     =cut
248 :    
249 :     sub name() {
250 :     return "image";
251 :     }
252 :    
253 :     =head3 default
254 :    
255 :     my $defaultValue = $et->default();
256 :    
257 :     Default value to be used for fields of this type if no default value is
258 : parrello 1.4 specified in the database definition or in an L<ERDBLoadGroup/Put>
259 : parrello 1.1 call during a loader operation. The default is undefined, which means
260 :     an error will be thrown during the load.
261 :    
262 :     =cut
263 :    
264 :     sub default {
265 :     return undef;
266 :     }
267 :    
268 :     =head3 align
269 :    
270 :     my $alignment = $et->align();
271 :    
272 :     Return the display alignment for fields of this type: either C<left>, C<right>, or
273 :     C<center>. The default is C<left>.
274 :    
275 :     =cut
276 :    
277 :     sub align {
278 :     return 'center';
279 :     }
280 :    
281 :     =head3 html
282 :    
283 :     my $html = $et->html($value);
284 :    
285 :     Return the HTML for displaying the content of a field of this type in an output
286 :     table. The default is the raw value, html-escaped.
287 :    
288 :     =cut
289 :    
290 :     sub html {
291 :     my ($self, $value) = @_;
292 :     # The incoming value here is a GD graphic image. We need to store it
293 :     # to a temporary file.
294 :     my $sessionID = FIGRules::NewSessionID();
295 :     my $fileName = FIGRules::NewSessionID() . "image$$.png";
296 : parrello 1.3 my $oh = Open(undef, ">$ERDBExtras::temp/$fileName");
297 : parrello 1.1 print $oh $value->png();
298 :     close $oh;
299 : parrello 1.3 my $retVal = CGI::img({ src => "$ERDBExtras::temp_url/$fileName" });
300 : parrello 1.1 return $retVal;
301 :     }
302 :    
303 : parrello 1.2 =head3 objectType
304 :    
305 :     my $type = $et->objectType();
306 :    
307 :     Return the PERL type for fields of this type. An undefined value means it's
308 :     a scalar; otherwise, it should be the package name (suitable for a C<use> clause).
309 :    
310 :     =cut
311 :    
312 :     sub objectType {
313 :     return "GD::Image";
314 :     }
315 : parrello 1.1
316 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3