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;
36 import mir.entity.Entity;
37 import mir.entity.EntityList;
38 import mir.log.LoggerWrapper;
39 import mir.media.MediaHelper;
40 import mir.media.MirMedia;
41 import mir.misc.NumberUtils;
42 import mir.storage.StorageObject;
43 import mir.storage.StorageObjectFailure;
44 import mircoders.storage.DatabaseContentToMedia;
45 import mircoders.storage.DatabaseUploadedMedia;
46 import freemarker.template.SimpleList;
47 import freemarker.template.SimpleScalar;
48 import freemarker.template.TemplateModel;
49 import freemarker.template.TemplateModelException;
53 * @author mh, mir-coders group
54 * @version $Id: EntityUploadedMedia.java,v 1.23 2003/03/09 03:53:11 zapata Exp $
58 public class EntityUploadedMedia extends Entity {
61 public EntityUploadedMedia() {
64 logger = new LoggerWrapper("Entity.UploadedMedia");
67 public EntityUploadedMedia(StorageObject theStorage) {
69 setStorage(theStorage);
72 public void update() throws StorageObjectFailure {
74 EntityList contentList = DatabaseContentToMedia.getInstance().getContent(this);
75 if (contentList!=null && contentList.size()>0) {
76 for(int i=0;i<contentList.size();i++) {
77 EntityContent contentEnt = (EntityContent)contentList.elementAt(i);
78 contentEnt.setProduced(false);
83 public void setValues(Map theStringValues) {
84 if (theStringValues != null) {
85 if (!theStringValues.containsKey("is_published"))
86 theStringValues.put("is_published", "0");
88 super.setValues(theStringValues);
93 * fetches the MediaType entry assiciated w/ this media
95 * @return mir.entity.Entity
97 public Entity getMediaType() throws StorageObjectFailure {
100 ent = DatabaseUploadedMedia.getInstance().getMediaType(this);
102 catch (StorageObjectFailure e) {
103 throwStorageObjectFailure(e, "get MediaType failed -- ");
108 public String getValue(String key) {
109 String returnValue = null;
112 if (key.equals("big_icon"))
113 returnValue = getBigIconName();
114 else if (key.equals("descr") || key.equals("media_descr"))
115 returnValue = getDescr();
116 else if (key.equals("mediatype"))
117 returnValue = getMediaTypeString();
118 else if (key.equals("mimetype"))
119 returnValue = getMimeType();
120 else if (key.equals("human_readable_size")) {
121 String size = super.getValue("size");
123 returnValue = NumberUtils.humanReadableSize(Double.parseDouble(size));
126 returnValue = super.getValue(key);
131 public TemplateModel get(java.lang.String key) throws TemplateModelException {
132 if (key.equals("url"))
134 return new SimpleScalar(getValue(key));
137 // @todo all these methods should be merged into 1
138 // and the MediaHandler should be cached somehow.
139 private String getMediaTypeString() {
140 MirMedia mediaHandler = null;
141 Entity mediaType = null;
144 mediaType = getMediaType();
145 mediaHandler = MediaHelper.getHandler(mediaType);
147 if (mediaHandler.isAudio())
149 else if (mediaHandler.isImage())
151 else if (mediaHandler.isVideo())
156 catch (Exception ex) {
157 logger.warn("EntityUploadedMedia.getMediaTypeString: could not fetch data: " + ex.toString());
162 private String getBigIconName() {
163 MirMedia mediaHandler = null;
164 Entity mediaType = null;
167 mediaType = getMediaType();
168 mediaHandler = MediaHelper.getHandler(mediaType);
169 return mediaHandler.getBigIconName();
171 catch (Exception ex) {
172 logger.warn("EntityUploadedMedia.getBigIconName: could not fetch data: " + ex.toString());
177 private SimpleList getUrl() {
178 MirMedia mediaHandler = null;
179 Entity mediaType = null;
182 mediaType = getMediaType();
183 mediaHandler = MediaHelper.getHandler(mediaType);
184 return mediaHandler.getURL(this, mediaType);
186 catch (Throwable t) {
187 logger.warn("EntityUploadedMedia.getUrl: could not fetch data: " + t.toString());
192 private String getDescr() {
193 MirMedia mediaHandler = null;
194 Entity mediaType = null;
197 mediaType = getMediaType();
198 mediaHandler = MediaHelper.getHandler(mediaType);
199 return mediaHandler.getDescr(mediaType);
201 catch (Exception ex) {
202 logger.warn("EntityUploadedMedia.getDescr: could not fetch data: " + ex.toString());
206 private String getMimeType() {
207 Entity mediaType = null;
210 mediaType = getMediaType();
211 return mediaType.getValue("mime_type");
213 catch (Exception ex) {
214 logger.warn("EntityUploadedMedia.getBigIconName: could not fetch data: " + ex.toString());