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;
39 import freemarker.template.SimpleScalar;
40 import freemarker.template.TemplateModel;
41 import freemarker.template.TemplateModelException;
43 import mir.log.LoggerWrapper;
44 import mir.entity.Entity;
45 import mir.entity.EntityList;
46 import mir.storage.StorageObject;
47 import mir.storage.StorageObjectExc;
48 import mir.storage.StorageObjectFailure;
49 import mircoders.storage.DatabaseContent;
50 import mircoders.storage.DatabaseContentToMedia;
51 import mircoders.storage.DatabaseContentToTopics;
54 * this class implements mapping of one line of the database table content
57 * @version $Id: EntityContent.java,v 1.15 2003/02/23 05:00:13 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 HashMap _entCache = new HashMap();
75 Boolean _hasMedia = null;
79 public EntityContent()
83 logger = new LoggerWrapper("Entity.Content");
86 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);
124 * make openposting to newswire
127 public void newswire() throws StorageObjectFailure
129 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
131 theStorageObject.executeUpdate(sql);
132 } catch (StorageObjectFailure e) {
133 throwStorageObjectFailure(e, "\n -- newswire failed");
134 } catch (SQLException e) {
135 throwStorageObjectFailure(e, "\n -- newswire failed");
143 public void dettach(String cid,String mid) throws StorageObjectFailure
147 DatabaseContentToMedia.getInstance().delete(cid,mid);
150 throwStorageObjectFailure(e, "\n -- failed to get instance");
153 //set Content to unproduced
162 public void attach(String mid) throws StorageObjectFailure
165 //write media-id mid and content-id in table content_x_media
167 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
168 } catch(StorageObjectFailure e){
169 throwStorageObjectFailure(e, "attach: could not get the instance");
171 //set Content to unproduced
175 logger.error("EntityContent: attach without mid");
180 * overridden method getValue to include formatted date into every
184 public TemplateModel get(java.lang.String key) throws TemplateModelException
187 if (_entCache.containsKey(key)) {
188 return (TemplateModel)_entCache.get(key);
190 if (key.equals("to_comments")) {
192 _entCache.put(key, getComments());
193 return (TemplateModel)_entCache.get(key);
195 catch (Exception ex) {
196 logger.warn("EntityContent.getComments: could not fetch data " + ex.toString());
198 throw new TemplateModelException(ex.toString());
201 if (key.equals("to_media_images")) {
203 _entCache.put(key, getImagesForContent());
204 return (TemplateModel)_entCache.get(key);
206 catch (Exception ex) {
207 logger.warn("EntityContent.getImagesForContent: could not fetch data " + ex.toString());
208 throw new TemplateModelException(ex.toString());
211 if (key.equals("to_media_audio")) {
213 _entCache.put(key, getAudioForContent());
214 return (TemplateModel)_entCache.get(key);
216 catch (Exception ex) {
217 logger.warn("EntityContent.getAudioForContent: could not fetch data " + ex.toString());
218 throw new TemplateModelException(ex.toString());
221 if (key.equals("to_media_video")) {
223 _entCache.put(key, getVideoForContent());
224 return (TemplateModel)_entCache.get(key);
226 catch (Exception ex) {
227 logger.warn("EntityContent.getVideoForContent: could not fetch data " + ex.toString());
228 throw new TemplateModelException(ex.toString());
231 if (key.equals("to_media_other")) {
233 _entCache.put(key, getOtherMediaForContent());
234 return (TemplateModel)_entCache.get(key);
236 catch (Exception ex) {
237 logger.warn("EntityContent.getOtherMediaForContent: could not fetch data " + ex.toString());
238 throw new TemplateModelException(ex.toString());
241 else if (key.equals("to_topics")) {
244 DatabaseContentToTopics.getInstance().getTopics(this));
245 return (TemplateModel)_entCache.get(key);
247 catch (Exception ex) {
248 logger.warn("EntityContent.getTopics: could not fetch data " + ex.toString());
249 throw new TemplateModelException(ex.toString());
253 return new SimpleScalar(getValue(key));
261 * overridden method setValues to patch creator_main_url
263 public void setValues(HashMap theStringValues) {
264 if (theStringValues != null) {
265 if (theStringValues.containsKey("creator_main_url")){
266 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
267 theStringValues.remove("creator_main_url");
269 else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
270 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
274 super.setValues(theStringValues);
278 * fetches all the comments belonging to an article
280 * @return freemarker.template.SimpleList
282 private EntityList getComments() throws StorageObjectFailure {
283 return ((DatabaseContent)theStorageObject).getComments(this);
286 private boolean hasMedia() throws StorageObjectFailure
288 if (_hasMedia == null) {
291 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
292 } catch (StorageObjectExc e) {
293 throw new StorageObjectFailure(e);
296 return _hasMedia.booleanValue();
299 //######## @todo all of the following getBlahForContent should have
300 // and optimized version where LIMIT=1 sql for list view.
301 private EntityList getImagesForContent()
302 throws StorageObjectFailure, TemplateModelException
305 return DatabaseContentToMedia.getInstance().getImages(this);
310 private EntityList getAudioForContent()
311 throws StorageObjectFailure, TemplateModelException
314 return DatabaseContentToMedia.getInstance().getAudio(this) ;
319 private EntityList getVideoForContent()
320 throws StorageObjectFailure, TemplateModelException
323 return DatabaseContentToMedia.getInstance().getVideo(this) ;
328 private EntityList getOtherMediaForContent()
329 throws StorageObjectFailure, TemplateModelException
332 return DatabaseContentToMedia.getInstance().getOther(this);