it's a bad thing w/ our persistance layer to override getValues, for keys that exist...
[mir.git] / source / mircoders / entity / EntityUploadedMedia.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.entity;
33
34 import freemarker.template.SimpleList;
35 import freemarker.template.SimpleScalar;
36 import freemarker.template.TemplateModel;
37 import freemarker.template.TemplateModelException;
38 import mir.entity.Entity;
39 import mir.media.MediaHelper;
40 import mir.media.MirMedia;
41 import mir.storage.StorageObject;
42 import mir.storage.StorageObjectException;
43 import mir.misc.NumberUtils;
44 import mircoders.storage.DatabaseUploadedMedia;
45
46 import java.util.HashMap;
47
48 /**
49  * Diese Klasse enthält die Daten eines MetaObjekts
50  *
51  * @author mh, mir-coders group
52  * @version $Id: EntityUploadedMedia.java,v 1.9.2.6 2002/11/29 21:09:03 mh Exp $
53  */
54
55
56 public class EntityUploadedMedia extends Entity {
57
58
59   public EntityUploadedMedia() {
60     super();
61   }
62
63   public EntityUploadedMedia(StorageObject theStorage) {
64     this();
65     setStorage(theStorage);
66   }
67
68   public void setValues(HashMap theStringValues) {
69     if (theStringValues != null) {
70       if (!theStringValues.containsKey("is_published"))
71         theStringValues.put("is_published", "0");
72     }
73     super.setValues(theStringValues);
74   }
75
76
77   /**
78    * fetches the MediaType entry assiciated w/ this media
79    *
80    * @return mir.entity.Entity
81    */
82   public Entity getMediaType() throws StorageObjectException {
83     Entity ent = null;
84     try {
85       ent = DatabaseUploadedMedia.getInstance().getMediaType(this);
86     }
87     catch (StorageObjectException e) {
88       throwStorageObjectException(e, "get MediaType failed -- ");
89     }
90     return ent;
91   }
92
93   public String getValue(String key) {
94     String returnValue = null;
95
96     if (key != null) {
97       if (key.equals("big_icon"))
98         returnValue = getBigIconName();
99       else if (key.equals("descr") || key.equals("media_descr"))
100         returnValue = getDescr();
101       else if (key.equals("mediatype"))
102         returnValue = getMediaTypeString();
103       else if (key.equals("mimetype"))
104         returnValue = getMimeType();
105       else if (key.equals("human_readable_size")) {
106         String size = super.getValue("size");
107         if (size != null)
108           returnValue = NumberUtils.humanReadableSize(Double.parseDouble(size));
109       }
110       else
111         returnValue = super.getValue(key);
112     }
113     return returnValue;
114   }
115
116   public TemplateModel get(java.lang.String key) throws TemplateModelException {
117     if (key.equals("url"))
118       return getUrl();
119     return new SimpleScalar(getValue(key));
120   }
121
122   // @todo  all these methods should be merged into 1
123   // and the MediaHandler should be cached somehow.
124   private String getMediaTypeString() {
125     MirMedia mediaHandler = null;
126     Entity mediaType = null;
127
128     try {
129       mediaType = getMediaType();
130       mediaHandler = MediaHelper.getHandler(mediaType);
131       String t;
132       if (mediaHandler.isAudio())
133         return "audio";
134       else if (mediaHandler.isImage())
135         return "image";
136       else if (mediaHandler.isVideo())
137         return "video";
138       else
139         return "other";
140     }
141     catch (Exception ex) {
142       theLog.printWarning("-- getMediaTypeString: could not fetch data "
143                           + this.getClass().toString() + " " + ex.toString());
144     }
145     return null;
146   }
147
148   private String getBigIconName() {
149     MirMedia mediaHandler = null;
150     Entity mediaType = null;
151
152     try {
153       mediaType = getMediaType();
154       mediaHandler = MediaHelper.getHandler(mediaType);
155       return mediaHandler.getBigIconName();
156     }
157     catch (Exception ex) {
158       theLog.printWarning("-- getBigIconName: could not fetch data "
159                           + this.getClass().toString() + " " + ex.toString());
160     }
161     return null;
162   }
163
164   private SimpleList getUrl() {
165     MirMedia mediaHandler = null;
166     Entity mediaType = null;
167
168     try {
169       mediaType = getMediaType();
170       mediaHandler = MediaHelper.getHandler(mediaType);
171       return mediaHandler.getURL(this, mediaType);
172     }
173     catch (Exception ex) {
174       theLog.printWarning("-- getUrl: could not fetch data "
175                           + this.getClass().toString() + " " + ex.toString());
176     }
177     return null;
178   }
179
180   private String getDescr() {
181     MirMedia mediaHandler = null;
182     Entity mediaType = null;
183
184     try {
185       mediaType = getMediaType();
186       mediaHandler = MediaHelper.getHandler(mediaType);
187       return mediaHandler.getDescr(mediaType);
188     }
189     catch (Exception ex) {
190       theLog.printWarning("-- getDescr: could not fetch data "
191                           + this.getClass().toString() + " " + ex.toString());
192     }
193     return null;
194   }
195   private String getMimeType() {
196     Entity mediaType = null;
197
198     try {
199       mediaType = getMediaType();
200       return mediaType.getValue("mime_type");
201     }
202     catch (Exception ex) {
203       theLog.printWarning("-- getBigIconName: could not fetch data "
204                           + this.getClass().toString() + " " + ex.toString());
205     }
206     return null;
207   }
208
209 }