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.Connection;
35 import java.sql.SQLException;
36 import java.sql.Statement;
37 import java.util.HashMap;
40 import mir.entity.Entity;
41 import mir.entity.EntityList;
42 import mir.log.LoggerWrapper;
43 import mir.storage.StorageObject;
44 import mir.storage.StorageObjectExc;
45 import mir.storage.StorageObjectFailure;
46 import mircoders.storage.DatabaseContent;
47 import mircoders.storage.DatabaseContentToMedia;
48 import mircoders.storage.DatabaseContentToTopics;
49 import freemarker.template.SimpleScalar;
50 import freemarker.template.TemplateModel;
51 import freemarker.template.TemplateModelException;
54 * this class implements mapping of one line of the database table content
57 * @version $Id: EntityContent.java,v 1.18 2003/04/10 03:31:47 zapata Exp $
58 * @author mir-coders group
63 public class EntityContent extends Entity
66 String mirconf_extLinkName = configuration.getString("Producer.ExtLinkName");
67 String mirconf_intLinkName = configuration.getString("Producer.IntLinkName");
68 String mirconf_mailLinkName = configuration.getString("Producer.MailLinkName");
69 String mirconf_imageRoot = configuration.getString("Producer.ImageRoot");
71 //this should always be transient i.e it can never be stored in the db
72 //or ObjectStore. (so the ObjectStore should only be caching what comes
73 //directly out of the DB. @todo confirm this with rk. -mh
74 Map _entCache = new HashMap();
75 Boolean _hasMedia = null;
79 public EntityContent()
83 logger = new LoggerWrapper("Entity.Content");
86 public EntityContent(StorageObject theStorage) {
89 setStorage(theStorage);
96 * set is_produced flag for the article
99 public void setProduced(boolean yesno) throws StorageObjectFailure
101 String value = (yesno) ? "1":"0";
102 if (value.equals( getValue("is_produced") )) return;
104 Connection con=null;Statement stmt=null;
105 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
107 con = theStorageObject.getPooledCon();
108 /** @todo should be preparedStatement: faster!! */
109 stmt = con.createStatement();
110 theStorageObject.executeUpdate(stmt,sql);
112 catch (StorageObjectFailure e) {
113 throwStorageObjectFailure(e, "\n -- set produced failed");
115 catch (SQLException e) {
116 throwStorageObjectFailure(e, "\n -- set produced failed");
119 theStorageObject.freeConnection(con,stmt);
124 * Deattaches media from an article
128 * @throws StorageObjectFailure
130 public void dettach(String anArticleId, String aMediaId) throws StorageObjectFailure
134 DatabaseContentToMedia.getInstance().delete(anArticleId, aMediaId);
137 throwStorageObjectFailure(e, "\n -- failed to get instance");
145 * Attaches media to an article
148 * @throws StorageObjectFailure
151 public void attach(String aMediaId) throws StorageObjectFailure
153 if (aMediaId!=null) {
155 DatabaseContentToMedia.getInstance().addMedia(getId(),aMediaId);
157 catch(StorageObjectFailure e){
158 throwStorageObjectFailure(e, "attach: could not get the instance");
163 logger.error("EntityContent: attach without mid");
168 * overridden method getValue to include formatted date into every
172 public TemplateModel get(java.lang.String key) throws TemplateModelException
175 if (_entCache.containsKey(key)) {
176 return (TemplateModel)_entCache.get(key);
178 if (key.equals("to_comments")) {
180 _entCache.put(key, getComments());
181 return (TemplateModel)_entCache.get(key);
183 catch (Exception ex) {
184 logger.warn("EntityContent.getComments: could not fetch data " + ex.toString());
186 throw new TemplateModelException(ex.toString());
189 if (key.equals("to_media_images")) {
191 _entCache.put(key, getImagesForContent());
192 return (TemplateModel)_entCache.get(key);
194 catch (Exception ex) {
195 logger.warn("EntityContent.getImagesForContent: could not fetch data " + ex.toString());
196 throw new TemplateModelException(ex.toString());
199 if (key.equals("to_media_audio")) {
201 _entCache.put(key, getAudioForContent());
202 return (TemplateModel)_entCache.get(key);
204 catch (Exception ex) {
205 logger.warn("EntityContent.getAudioForContent: could not fetch data " + ex.toString());
206 throw new TemplateModelException(ex.toString());
209 if (key.equals("to_media_video")) {
211 _entCache.put(key, getVideoForContent());
212 return (TemplateModel)_entCache.get(key);
214 catch (Exception ex) {
215 logger.warn("EntityContent.getVideoForContent: could not fetch data " + ex.toString());
216 throw new TemplateModelException(ex.toString());
219 if (key.equals("to_media_other")) {
221 _entCache.put(key, getOtherMediaForContent());
222 return (TemplateModel)_entCache.get(key);
224 catch (Exception ex) {
225 logger.warn("EntityContent.getOtherMediaForContent: could not fetch data " + ex.toString());
226 throw new TemplateModelException(ex.toString());
229 else if (key.equals("to_topics")) {
232 DatabaseContentToTopics.getInstance().getTopics(this));
233 return (TemplateModel)_entCache.get(key);
235 catch (Exception ex) {
236 logger.warn("EntityContent.getTopics: could not fetch data " + ex.toString());
237 throw new TemplateModelException(ex.toString());
241 return new SimpleScalar(getValue(key));
249 * overridden method setValues to patch creator_main_url
251 public void setValues(Map theStringValues) {
252 if (theStringValues != null) {
253 if (theStringValues.containsKey("creator_main_url")){
254 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
255 theStringValues.remove("creator_main_url");
257 else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
258 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
262 super.setValues(theStringValues);
266 * fetches all the comments belonging to an article
268 * @return freemarker.template.SimpleList
270 private EntityList getComments() throws StorageObjectFailure {
271 return ((DatabaseContent)theStorageObject).getComments(this);
274 private boolean hasMedia() throws StorageObjectFailure
276 if (_hasMedia == null) {
279 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
280 } catch (StorageObjectExc e) {
281 throw new StorageObjectFailure(e);
284 return _hasMedia.booleanValue();
287 //######## @todo all of the following getBlahForContent should have
288 // and optimized version where LIMIT=1 sql for list view.
289 private EntityList getImagesForContent()
290 throws StorageObjectFailure, TemplateModelException
293 return DatabaseContentToMedia.getInstance().getImages(this);
298 private EntityList getAudioForContent()
299 throws StorageObjectFailure, TemplateModelException
302 return DatabaseContentToMedia.getInstance().getAudio(this) ;
307 private EntityList getVideoForContent()
308 throws StorageObjectFailure, TemplateModelException
311 return DatabaseContentToMedia.getInstance().getVideo(this) ;
316 private EntityList getOtherMediaForContent()
317 throws StorageObjectFailure, TemplateModelException
320 return DatabaseContentToMedia.getInstance().getOther(this);