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