organizing imports
[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
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;
50
51 /**
52  *
53  * @author mh, mir-coders group
54  * @version $Id: EntityUploadedMedia.java,v 1.22 2003/03/05 19:23:15 idfx Exp $
55  */
56
57
58 public class EntityUploadedMedia extends Entity {
59
60
61   public EntityUploadedMedia() {
62     super();
63
64     logger = new LoggerWrapper("Entity.UploadedMedia");
65   }
66
67   public EntityUploadedMedia(StorageObject theStorage) {
68     this();
69     setStorage(theStorage);
70   }
71
72   public void update() throws StorageObjectFailure {
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(Map 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 StorageObjectFailure {
98     Entity ent = null;
99     try {
100       ent = DatabaseUploadedMedia.getInstance().getMediaType(this);
101     }
102     catch (StorageObjectFailure e) {
103       throwStorageObjectFailure(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       logger.warn("EntityUploadedMedia.getMediaTypeString: could not fetch data: " + ex.toString());
158     }
159     return null;
160   }
161
162   private String getBigIconName() {
163     MirMedia mediaHandler = null;
164     Entity mediaType = null;
165
166     try {
167       mediaType = getMediaType();
168       mediaHandler = MediaHelper.getHandler(mediaType);
169       return mediaHandler.getBigIconName();
170     }
171     catch (Exception ex) {
172       logger.warn("EntityUploadedMedia.getBigIconName: could not fetch data: " + ex.toString());
173     }
174     return null;
175   }
176
177   private SimpleList getUrl() {
178     MirMedia mediaHandler = null;
179     Entity mediaType = null;
180
181     try {
182       mediaType = getMediaType();
183       mediaHandler = MediaHelper.getHandler(mediaType);
184       return mediaHandler.getURL(this, mediaType);
185     }
186     catch (Exception ex) {
187       logger.warn("EntityUploadedMedia.getUrl: could not fetch data: " + ex.toString());
188     }
189     return null;
190   }
191
192   private String getDescr() {
193     MirMedia mediaHandler = null;
194     Entity mediaType = null;
195
196     try {
197       mediaType = getMediaType();
198       mediaHandler = MediaHelper.getHandler(mediaType);
199       return mediaHandler.getDescr(mediaType);
200     }
201     catch (Exception ex) {
202       logger.warn("EntityUploadedMedia.getDescr: could not fetch data: " + ex.toString());
203     }
204     return null;
205   }
206   private String getMimeType() {
207     Entity mediaType = null;
208
209     try {
210       mediaType = getMediaType();
211       return mediaType.getValue("mime_type");
212     }
213     catch (Exception ex) {
214       logger.warn("EntityUploadedMedia.getBigIconName: could not fetch data: " + ex.toString());
215     }
216     return null;
217   }
218
219 }