2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
32 package mircoders.entity;
35 import java.sql.SQLException;
37 import freemarker.template.SimpleList;
38 import freemarker.template.SimpleScalar;
39 import freemarker.template.TemplateModel;
40 import freemarker.template.TemplateModelException;
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;
55 * @author mh, mir-coders group
56 * @version $Id: EntityUploadedMedia.java,v 1.24 2003/03/16 19:54:45 zapata Exp $
60 public class EntityUploadedMedia extends Entity {
63 public EntityUploadedMedia() {
66 logger = new LoggerWrapper("Entity.UploadedMedia");
69 public EntityUploadedMedia(StorageObject theStorage) {
71 setStorage(theStorage);
74 public void update() throws StorageObjectFailure {
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()+")");
79 catch (SQLException e) {
80 throwStorageObjectFailure(e, "EntityAudio :: update :: failed!! ");
84 public void setValues(Map theStringValues) {
85 if (theStringValues != null) {
86 if (!theStringValues.containsKey("is_published"))
87 theStringValues.put("is_published", "0");
89 super.setValues(theStringValues);
94 * fetches the MediaType entry assiciated w/ this media
96 * @return mir.entity.Entity
98 public Entity getMediaType() throws StorageObjectFailure {
101 ent = DatabaseUploadedMedia.getInstance().getMediaType(this);
103 catch (StorageObjectFailure e) {
104 throwStorageObjectFailure(e, "get MediaType failed -- ");
109 public String getValue(String key) {
110 String returnValue = 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");
124 returnValue = NumberUtils.humanReadableSize(Double.parseDouble(size));
127 returnValue = super.getValue(key);
132 public TemplateModel get(java.lang.String key) throws TemplateModelException {
133 if (key.equals("url"))
135 return new SimpleScalar(getValue(key));
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;
145 mediaType = getMediaType();
146 mediaHandler = MediaHelper.getHandler(mediaType);
148 if (mediaHandler.isAudio())
150 else if (mediaHandler.isImage())
152 else if (mediaHandler.isVideo())
157 catch (Exception ex) {
158 logger.warn("EntityUploadedMedia.getMediaTypeString: could not fetch data: " + ex.toString());
163 private String getBigIconName() {
164 MirMedia mediaHandler = null;
165 Entity mediaType = null;
168 mediaType = getMediaType();
169 mediaHandler = MediaHelper.getHandler(mediaType);
170 return mediaHandler.getBigIconName();
172 catch (Exception ex) {
173 logger.warn("EntityUploadedMedia.getBigIconName: could not fetch data: " + ex.toString());
178 private SimpleList getUrl() {
179 MirMedia mediaHandler = null;
180 Entity mediaType = null;
183 mediaType = getMediaType();
184 mediaHandler = MediaHelper.getHandler(mediaType);
185 return mediaHandler.getURL(this, mediaType);
187 catch (Throwable t) {
188 logger.warn("EntityUploadedMedia.getUrl: could not fetch data: " + t.toString());
193 private String getDescr() {
194 MirMedia mediaHandler = null;
195 Entity mediaType = null;
198 mediaType = getMediaType();
199 mediaHandler = MediaHelper.getHandler(mediaType);
200 return mediaHandler.getDescr(mediaType);
202 catch (Exception ex) {
203 logger.warn("EntityUploadedMedia.getDescr: could not fetch data: " + ex.toString());
207 private String getMimeType() {
208 Entity mediaType = null;
211 mediaType = getMediaType();
212 return mediaType.getValue("mime_type");
214 catch (Exception ex) {
215 logger.warn("EntityUploadedMedia.getBigIconName: could not fetch data: " + ex.toString());