Mir goes GPL
[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 mircoders.storage.DatabaseUploadedMedia;
44
45 import java.util.HashMap;
46
47 /**
48  * Diese Klasse enthält die Daten eines MetaObjekts
49  *
50  * @author RK
51  * @version 29.6.1999
52  */
53
54
55 public class EntityUploadedMedia extends Entity {
56
57
58   public EntityUploadedMedia() {
59     super();
60   }
61
62   public EntityUploadedMedia(StorageObject theStorage) {
63     this();
64     setStorage(theStorage);
65   }
66
67   public void setValues(HashMap theStringValues) {
68     if (theStringValues != null) {
69       if (!theStringValues.containsKey("is_published"))
70         theStringValues.put("is_published", "0");
71     }
72     super.setValues(theStringValues);
73   }
74
75
76   /**
77    * fetches the MediaType entry assiciated w/ this media
78    *
79    * @return mir.entity.Entity
80    */
81   public Entity getMediaType() throws StorageObjectException {
82     Entity ent = null;
83     try {
84       ent = DatabaseUploadedMedia.getInstance().getMediaType(this);
85     }
86     catch (StorageObjectException e) {
87       throwStorageObjectException(e, "get MediaType failed -- ");
88     }
89     return ent;
90   }
91
92   public String getValue(String key) {
93     String returnValue = null;
94
95     if (key != null) {
96       if (key.equals("big_icon"))
97         returnValue = getBigIcon();
98       else if (key.equals("descr"))
99         returnValue = getDescr();
100       else if (key.equals("mediatype"))
101         returnValue = getMediaTypeString();
102       else if (key.equals("mimetype"))
103         returnValue = getMimeType();
104       else
105         returnValue = super.getValue(key);
106     }
107     return returnValue;
108   }
109
110   public TemplateModel get(java.lang.String key) throws TemplateModelException {
111     if (key.equals("url"))
112       return getUrl();
113     return new SimpleScalar(getValue(key));
114   }
115
116   // @todo  all these methods should be merged into 1
117   // and the MediaHandler should be cached somehow.
118   private String getMediaTypeString() {
119     MirMedia mediaHandler = null;
120     Entity mediaType = null;
121
122     try {
123       mediaType = getMediaType();
124       mediaHandler = MediaHelper.getHandler(mediaType);
125       String t;
126       if (mediaHandler.isAudio())
127         return "audio";
128       else if (mediaHandler.isImage())
129         return "image";
130       else if (mediaHandler.isVideo())
131         return "video";
132       else
133         return "other";
134     }
135     catch (Exception ex) {
136       theLog.printWarning("-- getMediaTypeString: could not fetch data "
137                           + this.getClass().toString() + " " + ex.toString());
138     }
139     return null;
140   }
141
142   private String getBigIcon() {
143     MirMedia mediaHandler = null;
144     Entity mediaType = null;
145
146     try {
147       mediaType = getMediaType();
148       mediaHandler = MediaHelper.getHandler(mediaType);
149       return mediaHandler.getBigIcon();
150     }
151     catch (Exception ex) {
152       theLog.printWarning("-- getBigIcon: could not fetch data "
153                           + this.getClass().toString() + " " + ex.toString());
154     }
155     return null;
156   }
157
158   private SimpleList getUrl() {
159     MirMedia mediaHandler = null;
160     Entity mediaType = null;
161
162     try {
163       mediaType = getMediaType();
164       mediaHandler = MediaHelper.getHandler(mediaType);
165       return mediaHandler.getURL(this, mediaType);
166     }
167     catch (Exception ex) {
168       theLog.printWarning("-- getUrl: could not fetch data "
169                           + this.getClass().toString() + " " + ex.toString());
170     }
171     return null;
172   }
173
174   private String getDescr() {
175     MirMedia mediaHandler = null;
176     Entity mediaType = null;
177
178     try {
179       mediaType = getMediaType();
180       mediaHandler = MediaHelper.getHandler(mediaType);
181       return mediaHandler.getDescr(mediaType);
182     }
183     catch (Exception ex) {
184       theLog.printWarning("-- getDescr: could not fetch data "
185                           + this.getClass().toString() + " " + ex.toString());
186     }
187     return null;
188   }
189   private String getMimeType() {
190     Entity mediaType = null;
191
192     try {
193       mediaType = getMediaType();
194       return mediaType.getValue("mime_type");
195     }
196     catch (Exception ex) {
197       theLog.printWarning("-- getBigIcon: could not fetch data "
198                           + this.getClass().toString() + " " + ex.toString());
199     }
200     return null;
201   }
202
203 }