*** empty log message ***
[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  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mircoders.entity;
31
32 import java.sql.SQLException;
33 import java.util.List;
34 import java.util.Map;
35
36 import mir.entity.Entity;
37 import mir.log.LoggerWrapper;
38 import mir.media.MediaHelper;
39 import mir.media.MirMedia;
40 import mir.misc.NumberUtils;
41 import mir.storage.StorageObject;
42 import mir.storage.StorageObjectFailure;
43 import mircoders.storage.DatabaseUploadedMedia;
44
45 /**
46  *
47  * @author mh, mir-coders group
48  * @version $Id: EntityUploadedMedia.java,v 1.26.2.3 2003/11/26 19:21:04 rk Exp $
49  */
50
51
52 public class EntityUploadedMedia extends Entity {
53
54
55   public EntityUploadedMedia() {
56     super();
57
58     logger = new LoggerWrapper("Entity.UploadedMedia");
59   }
60
61   public EntityUploadedMedia(StorageObject theStorage) {
62     this();
63     setStorage(theStorage);
64   }
65
66   public void update() throws StorageObjectFailure {
67     super.update();
68     try {    
69       theStorageObject.executeUpdate( "update content set is_produced='0' where exists(select * from content_x_media where to_content=content.id and to_media=" + getId()+")");
70     }
71     catch (SQLException e) {
72       throwStorageObjectFailure(e, "EntityUploadedMedia :: update :: failed!! ");
73     }
74   }
75
76   public void setValues(Map theStringValues) {
77     if (theStringValues != null) {
78       if (!theStringValues.containsKey("is_published"))
79         theStringValues.put("is_published", "0");
80     }
81     super.setValues(theStringValues);
82   }
83
84
85   /**
86    * fetches the MediaType entry assiciated w/ this media
87    *
88    * @return mir.entity.Entity
89    */
90   public Entity getMediaType() throws StorageObjectFailure {
91     Entity ent = null;
92     try {
93       ent = DatabaseUploadedMedia.getInstance().getMediaType(this);
94     }
95     catch (StorageObjectFailure e) {
96       throwStorageObjectFailure(e, "get MediaType failed -- ");
97     }
98     return ent;
99   }
100
101   public String getValue(String key) {
102     String returnValue = null;
103
104     if (key != null) {
105       if (key.equals("big_icon"))
106         returnValue = getBigIconName();
107       else if (key.equals("descr") || key.equals("media_descr"))
108         returnValue = getDescr();
109       else if (key.equals("mediatype"))
110         returnValue = getMediaTypeString();
111       else if (key.equals("mimetype"))
112         returnValue = getMimeType();
113       else if (key.equals("human_readable_size")) {
114         String size = super.getValue("size");
115         if (size != null)
116           returnValue = NumberUtils.humanReadableSize(Double.parseDouble(size));
117       }
118       else
119         returnValue = super.getValue(key);
120     }
121     return returnValue;
122   }
123
124   // @todo  all these methods should be merged into 1
125   // and the MediaHandler should be cached somehow.
126   private String getMediaTypeString() {
127     MirMedia mediaHandler = null;
128     Entity mediaType = null;
129
130     try {
131       mediaType = getMediaType();
132       mediaHandler = MediaHelper.getHandler(mediaType);
133       String t;
134       if (mediaHandler.isAudio())
135         return "audio";
136       else if (mediaHandler.isImage())
137         return "image";
138       else if (mediaHandler.isVideo())
139         return "video";
140       else
141         return "other";
142     }
143     catch (Exception ex) {
144       logger.warn("EntityUploadedMedia.getMediaTypeString: could not fetch data: " + ex.toString());
145     }
146     return null;
147   }
148
149   private String getBigIconName() {
150     MirMedia mediaHandler = null;
151     Entity mediaType = null;
152
153     try {
154       mediaType = getMediaType();
155       mediaHandler = MediaHelper.getHandler(mediaType);
156       return mediaHandler.getBigIconName();
157     }
158     catch (Exception ex) {
159       logger.warn("EntityUploadedMedia.getBigIconName: could not fetch data: " + ex.toString());
160     }
161     return null;
162   }
163
164   private List getUrl() {
165     MirMedia mediaHandler = null;
166     Entity mediaType = null;
167
168     try {
169       mediaType = getMediaType();
170       mediaHandler = MediaHelper.getHandler(mediaType);
171       return mediaHandler.getURL(this, mediaType);
172     }
173     catch (Throwable t) {
174       logger.warn("EntityUploadedMedia.getUrl: could not fetch data: " + t.toString());
175     }
176     return null;
177   }
178
179   private String getDescr() {
180     MirMedia mediaHandler = null;
181     Entity mediaType = null;
182
183     try {
184       mediaType = getMediaType();
185       mediaHandler = MediaHelper.getHandler(mediaType);
186       return mediaHandler.getDescr(mediaType);
187     }
188     catch (Exception ex) {
189       logger.warn("EntityUploadedMedia.getDescr: could not fetch data: " + ex.toString());
190     }
191     return null;
192   }
193   private String getMimeType() {
194     Entity mediaType = null;
195
196     try {
197       mediaType = getMediaType();
198       return mediaType.getValue("mime_type");
199     }
200     catch (Exception ex) {
201       logger.warn("EntityUploadedMedia.getBigIconName: could not fetch data: " + ex.toString());
202     }
203     return null;
204   }
205
206 }