support for translations + lots of misc. fixes
[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 java.util.HashMap;
35
36 import mir.entity.Entity;
37 import mir.entity.EntityList;
38 import mir.media.MediaHelper;
39 import mir.media.MirMedia;
40 import mir.misc.NumberUtils;
41 import mir.storage.StorageObject;
42 import mir.storage.StorageObjectExc;
43 import mir.storage.StorageObjectFailure;
44 import mircoders.storage.DatabaseContentToMedia;
45 import mircoders.storage.DatabaseUploadedMedia;
46 import freemarker.template.SimpleList;
47 import freemarker.template.SimpleScalar;
48 import freemarker.template.TemplateModel;
49 import freemarker.template.TemplateModelException;
50
51 /**
52  * Diese Klasse enth?lt die Daten eines MetaObjekts
53  *
54  * @author mh, mir-coders group
55  * @version $Id: EntityUploadedMedia.java,v 1.18 2003/02/20 16:05:33 zapata Exp $
56  */
57
58
59 public class EntityUploadedMedia extends Entity {
60
61
62   public EntityUploadedMedia() {
63     super();
64   }
65
66   public EntityUploadedMedia(StorageObject theStorage) {
67     this();
68     setStorage(theStorage);
69   }
70
71   public void update() throws StorageObjectFailure {
72     super.update();
73     EntityList contentList = DatabaseContentToMedia.getInstance().getContent(this);
74     if (contentList!=null && contentList.size()>0) {
75       for(int i=0;i<contentList.size();i++) {
76         EntityContent contentEnt = (EntityContent)contentList.elementAt(i);
77         contentEnt.setProduced(false);
78       }
79     }
80   }
81
82   public void setValues(HashMap theStringValues) {
83     if (theStringValues != null) {
84       if (!theStringValues.containsKey("is_published"))
85         theStringValues.put("is_published", "0");
86     }
87     super.setValues(theStringValues);
88   }
89
90
91   /**
92    * fetches the MediaType entry assiciated w/ this media
93    *
94    * @return mir.entity.Entity
95    */
96   public Entity getMediaType() throws StorageObjectFailure {
97     Entity ent = null;
98     try {
99       ent = DatabaseUploadedMedia.getInstance().getMediaType(this);
100     }
101     catch (StorageObjectFailure e) {
102       throwStorageObjectFailure(e, "get MediaType failed -- ");
103     }
104     return ent;
105   }
106
107   public String getValue(String key) {
108     String returnValue = null;
109
110     if (key != null) {
111       if (key.equals("big_icon"))
112         returnValue = getBigIconName();
113       else if (key.equals("descr") || key.equals("media_descr"))
114         returnValue = getDescr();
115       else if (key.equals("mediatype"))
116         returnValue = getMediaTypeString();
117       else if (key.equals("mimetype"))
118         returnValue = getMimeType();
119       else if (key.equals("human_readable_size")) {
120         String size = super.getValue("size");
121         if (size != null)
122           returnValue = NumberUtils.humanReadableSize(Double.parseDouble(size));
123       }
124       else
125         returnValue = super.getValue(key);
126     }
127     return returnValue;
128   }
129
130   public TemplateModel get(java.lang.String key) throws TemplateModelException {
131     if (key.equals("url"))
132       return getUrl();
133     return new SimpleScalar(getValue(key));
134   }
135
136   // @todo  all these methods should be merged into 1
137   // and the MediaHandler should be cached somehow.
138   private String getMediaTypeString() {
139     MirMedia mediaHandler = null;
140     Entity mediaType = null;
141
142     try {
143       mediaType = getMediaType();
144       mediaHandler = MediaHelper.getHandler(mediaType);
145       String t;
146       if (mediaHandler.isAudio())
147         return "audio";
148       else if (mediaHandler.isImage())
149         return "image";
150       else if (mediaHandler.isVideo())
151         return "video";
152       else
153         return "other";
154     }
155     catch (Exception ex) {
156       theLog.printWarning("-- getMediaTypeString: could not fetch data "
157                           + this.getClass().toString() + " " + ex.toString());
158     }
159     return null;
160   }
161
162   private String getBigIconName() {
163     MirMedia mediaHandler = null;
164     Entity mediaType = null;
165
166     try {
167       mediaType = getMediaType();
168       mediaHandler = MediaHelper.getHandler(mediaType);
169       return mediaHandler.getBigIconName();
170     }
171     catch (Exception ex) {
172       theLog.printWarning("-- getBigIconName: could not fetch data "
173                           + this.getClass().toString() + " " + ex.toString());
174     }
175     return null;
176   }
177
178   private SimpleList getUrl() {
179     MirMedia mediaHandler = null;
180     Entity mediaType = null;
181
182     try {
183       mediaType = getMediaType();
184       mediaHandler = MediaHelper.getHandler(mediaType);
185       return mediaHandler.getURL(this, mediaType);
186     }
187     catch (Exception ex) {
188       theLog.printWarning("-- getUrl: could not fetch data "
189                           + this.getClass().toString() + " " + ex.toString());
190     }
191     return null;
192   }
193
194   private String getDescr() {
195     MirMedia mediaHandler = null;
196     Entity mediaType = null;
197
198     try {
199       mediaType = getMediaType();
200       mediaHandler = MediaHelper.getHandler(mediaType);
201       return mediaHandler.getDescr(mediaType);
202     }
203     catch (Exception ex) {
204       theLog.printWarning("-- getDescr: could not fetch data "
205                           + this.getClass().toString() + " " + ex.toString());
206     }
207     return null;
208   }
209   private String getMimeType() {
210     Entity mediaType = null;
211
212     try {
213       mediaType = getMediaType();
214       return mediaType.getValue("mime_type");
215     }
216     catch (Exception ex) {
217       theLog.printWarning("-- getBigIconName: could not fetch data "
218                           + this.getClass().toString() + " " + ex.toString());
219     }
220     return null;
221   }
222
223 }