introduce an update method that overrides and calls the one from the superclass in...
[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 freemarker.template.SimpleList;
35 import freemarker.template.SimpleScalar;
36 import freemarker.template.TemplateModel;
37 import freemarker.template.TemplateModelException;
38
39 import mir.entity.Entity;
40 import mir.entity.EntityList;
41 import mir.media.MediaHelper;
42 import mir.media.MirMedia;
43 import mir.storage.StorageObject;
44 import mir.storage.StorageObjectException;
45 import mir.misc.NumberUtils;
46 import mircoders.storage.DatabaseUploadedMedia;
47 import mircoders.storage.DatabaseContentToMedia;
48
49 import java.sql.SQLException;
50 import java.util.HashMap;
51
52 /**
53  * Diese Klasse enthält die Daten eines MetaObjekts
54  *
55  * @author mh, mir-coders group
56  * @version $Id: EntityUploadedMedia.java,v 1.9.2.7 2002/12/05 08:47:06 mh Exp $
57  */
58
59
60 public class EntityUploadedMedia extends Entity {
61
62
63   public EntityUploadedMedia() {
64     super();
65   }
66
67   public EntityUploadedMedia(StorageObject theStorage) {
68     this();
69     setStorage(theStorage);
70   }
71
72   public void update() throws StorageObjectException {
73     super.update();
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);
79       }
80     }
81   }
82
83   public void setValues(HashMap theStringValues) {
84     if (theStringValues != null) {
85       if (!theStringValues.containsKey("is_published"))
86         theStringValues.put("is_published", "0");
87     }
88     super.setValues(theStringValues);
89   }
90
91
92   /**
93    * fetches the MediaType entry assiciated w/ this media
94    *
95    * @return mir.entity.Entity
96    */
97   public Entity getMediaType() throws StorageObjectException {
98     Entity ent = null;
99     try {
100       ent = DatabaseUploadedMedia.getInstance().getMediaType(this);
101     }
102     catch (StorageObjectException e) {
103       throwStorageObjectException(e, "get MediaType failed -- ");
104     }
105     return ent;
106   }
107
108   public String getValue(String key) {
109     String returnValue = null;
110
111     if (key != 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");
122         if (size != null)
123           returnValue = NumberUtils.humanReadableSize(Double.parseDouble(size));
124       }
125       else
126         returnValue = super.getValue(key);
127     }
128     return returnValue;
129   }
130
131   public TemplateModel get(java.lang.String key) throws TemplateModelException {
132     if (key.equals("url"))
133       return getUrl();
134     return new SimpleScalar(getValue(key));
135   }
136
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;
142
143     try {
144       mediaType = getMediaType();
145       mediaHandler = MediaHelper.getHandler(mediaType);
146       String t;
147       if (mediaHandler.isAudio())
148         return "audio";
149       else if (mediaHandler.isImage())
150         return "image";
151       else if (mediaHandler.isVideo())
152         return "video";
153       else
154         return "other";
155     }
156     catch (Exception ex) {
157       theLog.printWarning("-- getMediaTypeString: could not fetch data "
158                           + this.getClass().toString() + " " + 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       theLog.printWarning("-- getBigIconName: could not fetch data "
174                           + this.getClass().toString() + " " + ex.toString());
175     }
176     return null;
177   }
178
179   private SimpleList getUrl() {
180     MirMedia mediaHandler = null;
181     Entity mediaType = null;
182
183     try {
184       mediaType = getMediaType();
185       mediaHandler = MediaHelper.getHandler(mediaType);
186       return mediaHandler.getURL(this, mediaType);
187     }
188     catch (Exception ex) {
189       theLog.printWarning("-- getUrl: could not fetch data "
190                           + this.getClass().toString() + " " + ex.toString());
191     }
192     return null;
193   }
194
195   private String getDescr() {
196     MirMedia mediaHandler = null;
197     Entity mediaType = null;
198
199     try {
200       mediaType = getMediaType();
201       mediaHandler = MediaHelper.getHandler(mediaType);
202       return mediaHandler.getDescr(mediaType);
203     }
204     catch (Exception ex) {
205       theLog.printWarning("-- getDescr: could not fetch data "
206                           + this.getClass().toString() + " " + ex.toString());
207     }
208     return null;
209   }
210   private String getMimeType() {
211     Entity mediaType = null;
212
213     try {
214       mediaType = getMediaType();
215       return mediaType.getValue("mime_type");
216     }
217     catch (Exception ex) {
218       theLog.printWarning("-- getBigIconName: could not fetch data "
219                           + this.getClass().toString() + " " + ex.toString());
220     }
221     return null;
222   }
223
224 }