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 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.
31 package mircoders.entity;
33 import java.sql.Connection;
34 import java.sql.SQLException;
35 import java.sql.Statement;
36 import java.util.HashMap;
39 import mir.entity.Entity;
40 import mir.entity.EntityList;
41 import mir.log.LoggerWrapper;
42 import mir.storage.StorageObject;
43 import mir.storage.StorageObjectExc;
44 import mir.storage.StorageObjectFailure;
45 import mircoders.storage.DatabaseContent;
46 import mircoders.storage.DatabaseContentToMedia;
47 import mircoders.storage.DatabaseContentToTopics;
48 import freemarker.template.SimpleScalar;
49 import freemarker.template.TemplateModel;
50 import freemarker.template.TemplateModelException;
53 * this class implements mapping of one line of the database table content
56 * @version $Id: EntityContent.java,v 1.19 2003/04/21 12:42:54 idfx Exp $
57 * @author mir-coders group
62 public class EntityContent extends Entity
65 String mirconf_extLinkName = configuration.getString("Producer.ExtLinkName");
66 String mirconf_intLinkName = configuration.getString("Producer.IntLinkName");
67 String mirconf_mailLinkName = configuration.getString("Producer.MailLinkName");
68 String mirconf_imageRoot = configuration.getString("Producer.ImageRoot");
70 //this should always be transient i.e it can never be stored in the db
71 //or ObjectStore. (so the ObjectStore should only be caching what comes
72 //directly out of the DB. @todo confirm this with rk. -mh
73 Map _entCache = new HashMap();
74 Boolean _hasMedia = null;
78 public EntityContent()
82 logger = new LoggerWrapper("Entity.Content");
85 public EntityContent(StorageObject theStorage) {
88 setStorage(theStorage);
95 * set is_produced flag for the article
98 public void setProduced(boolean yesno) throws StorageObjectFailure
100 String value = (yesno) ? "1":"0";
101 if (value.equals( getValue("is_produced") )) return;
103 Connection con=null;Statement stmt=null;
104 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
106 con = theStorageObject.getPooledCon();
107 /** @todo should be preparedStatement: faster!! */
108 stmt = con.createStatement();
109 theStorageObject.executeUpdate(stmt,sql);
111 catch (StorageObjectFailure e) {
112 throwStorageObjectFailure(e, "\n -- set produced failed");
114 catch (SQLException e) {
115 throwStorageObjectFailure(e, "\n -- set produced failed");
118 theStorageObject.freeConnection(con,stmt);
123 * Deattaches media from an article
127 * @throws StorageObjectFailure
129 public void dettach(String anArticleId, String aMediaId) throws StorageObjectFailure
133 DatabaseContentToMedia.getInstance().delete(anArticleId, aMediaId);
136 throwStorageObjectFailure(e, "\n -- failed to get instance");
144 * Attaches media to an article
147 * @throws StorageObjectFailure
150 public void attach(String aMediaId) throws StorageObjectFailure
152 if (aMediaId!=null) {
154 DatabaseContentToMedia.getInstance().addMedia(getId(),aMediaId);
156 catch(StorageObjectFailure e){
157 throwStorageObjectFailure(e, "attach: could not get the instance");
162 logger.error("EntityContent: attach without mid");
167 * overridden method getValue to include formatted date into every
171 public TemplateModel get(java.lang.String key) throws TemplateModelException
174 if (_entCache.containsKey(key)) {
175 return (TemplateModel)_entCache.get(key);
177 if (key.equals("to_comments")) {
179 _entCache.put(key, getComments());
180 return (TemplateModel)_entCache.get(key);
182 catch (Exception ex) {
183 logger.warn("EntityContent.getComments: could not fetch data " + ex.toString());
185 throw new TemplateModelException(ex.toString());
188 if (key.equals("to_media_images")) {
190 _entCache.put(key, getImagesForContent());
191 return (TemplateModel)_entCache.get(key);
193 catch (Exception ex) {
194 logger.warn("EntityContent.getImagesForContent: could not fetch data " + ex.toString());
195 throw new TemplateModelException(ex.toString());
198 if (key.equals("to_media_audio")) {
200 _entCache.put(key, getAudioForContent());
201 return (TemplateModel)_entCache.get(key);
203 catch (Exception ex) {
204 logger.warn("EntityContent.getAudioForContent: could not fetch data " + ex.toString());
205 throw new TemplateModelException(ex.toString());
208 if (key.equals("to_media_video")) {
210 _entCache.put(key, getVideoForContent());
211 return (TemplateModel)_entCache.get(key);
213 catch (Exception ex) {
214 logger.warn("EntityContent.getVideoForContent: could not fetch data " + ex.toString());
215 throw new TemplateModelException(ex.toString());
218 if (key.equals("to_media_other")) {
220 _entCache.put(key, getOtherMediaForContent());
221 return (TemplateModel)_entCache.get(key);
223 catch (Exception ex) {
224 logger.warn("EntityContent.getOtherMediaForContent: could not fetch data " + ex.toString());
225 throw new TemplateModelException(ex.toString());
228 else if (key.equals("to_topics")) {
231 DatabaseContentToTopics.getInstance().getTopics(this));
232 return (TemplateModel)_entCache.get(key);
234 catch (Exception ex) {
235 logger.warn("EntityContent.getTopics: could not fetch data " + ex.toString());
236 throw new TemplateModelException(ex.toString());
240 return new SimpleScalar(getValue(key));
248 * overridden method setValues to patch creator_main_url
250 public void setValues(Map theStringValues) {
251 if (theStringValues != null) {
252 if (theStringValues.containsKey("creator_main_url")){
253 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
254 theStringValues.remove("creator_main_url");
256 else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
257 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
261 super.setValues(theStringValues);
265 * fetches all the comments belonging to an article
267 * @return freemarker.template.SimpleList
269 private EntityList getComments() throws StorageObjectFailure {
270 return ((DatabaseContent)theStorageObject).getComments(this);
273 private boolean hasMedia() throws StorageObjectFailure
275 if (_hasMedia == null) {
278 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
279 } catch (StorageObjectExc e) {
280 throw new StorageObjectFailure(e);
283 return _hasMedia.booleanValue();
286 //######## @todo all of the following getBlahForContent should have
287 // and optimized version where LIMIT=1 sql for list view.
288 private EntityList getImagesForContent()
289 throws StorageObjectFailure, TemplateModelException
292 return DatabaseContentToMedia.getInstance().getImages(this);
297 private EntityList getAudioForContent()
298 throws StorageObjectFailure, TemplateModelException
301 return DatabaseContentToMedia.getInstance().getAudio(this) ;
306 private EntityList getVideoForContent()
307 throws StorageObjectFailure, TemplateModelException
310 return DatabaseContentToMedia.getInstance().getVideo(this) ;
315 private EntityList getOtherMediaForContent()
316 throws StorageObjectFailure, TemplateModelException
319 return DatabaseContentToMedia.getInstance().getOther(this);