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