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;
34 import java.sql.SQLException;
\r
35 import java.util.Map;
\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;
52 * @author mh, mir-coders group
53 * @version $Id: EntityUploadedMedia.java,v 1.25 2003/04/10 03:31:47 zapata Exp $
57 public class EntityUploadedMedia extends Entity {
60 public EntityUploadedMedia() {
63 logger = new LoggerWrapper("Entity.UploadedMedia");
66 public EntityUploadedMedia(StorageObject theStorage) {
68 setStorage(theStorage);
71 public void update() throws StorageObjectFailure {
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()+")");
76 catch (SQLException e) {
77 throwStorageObjectFailure(e, "EntityAudio :: update :: failed!! ");
81 public void setValues(Map theStringValues) {
82 if (theStringValues != null) {
83 if (!theStringValues.containsKey("is_published"))
84 theStringValues.put("is_published", "0");
86 super.setValues(theStringValues);
91 * fetches the MediaType entry assiciated w/ this media
93 * @return mir.entity.Entity
95 public Entity getMediaType() throws StorageObjectFailure {
98 ent = DatabaseUploadedMedia.getInstance().getMediaType(this);
100 catch (StorageObjectFailure e) {
101 throwStorageObjectFailure(e, "get MediaType failed -- ");
106 public String getValue(String key) {
107 String returnValue = 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");
121 returnValue = NumberUtils.humanReadableSize(Double.parseDouble(size));
124 returnValue = super.getValue(key);
129 public TemplateModel get(java.lang.String key) throws TemplateModelException {
130 if (key.equals("url"))
132 return new SimpleScalar(getValue(key));
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;
142 mediaType = getMediaType();
143 mediaHandler = MediaHelper.getHandler(mediaType);
145 if (mediaHandler.isAudio())
147 else if (mediaHandler.isImage())
149 else if (mediaHandler.isVideo())
154 catch (Exception ex) {
155 logger.warn("EntityUploadedMedia.getMediaTypeString: could not fetch data: " + ex.toString());
160 private String getBigIconName() {
161 MirMedia mediaHandler = null;
162 Entity mediaType = null;
165 mediaType = getMediaType();
166 mediaHandler = MediaHelper.getHandler(mediaType);
167 return mediaHandler.getBigIconName();
169 catch (Exception ex) {
170 logger.warn("EntityUploadedMedia.getBigIconName: could not fetch data: " + ex.toString());
175 private SimpleList getUrl() {
176 MirMedia mediaHandler = null;
177 Entity mediaType = null;
180 mediaType = getMediaType();
181 mediaHandler = MediaHelper.getHandler(mediaType);
182 return mediaHandler.getURL(this, mediaType);
184 catch (Throwable t) {
185 logger.warn("EntityUploadedMedia.getUrl: could not fetch data: " + t.toString());
190 private String getDescr() {
191 MirMedia mediaHandler = null;
192 Entity mediaType = null;
195 mediaType = getMediaType();
196 mediaHandler = MediaHelper.getHandler(mediaType);
197 return mediaHandler.getDescr(mediaType);
199 catch (Exception ex) {
200 logger.warn("EntityUploadedMedia.getDescr: could not fetch data: " + ex.toString());
204 private String getMimeType() {
205 Entity mediaType = null;
208 mediaType = getMediaType();
209 return mediaType.getValue("mime_type");
211 catch (Exception ex) {
212 logger.warn("EntityUploadedMedia.getBigIconName: could not fetch data: " + ex.toString());