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

Annotation of /Sprout/ERDBTypeImage.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3