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 mir.entity.Entity;
40 import mir.entity.EntityList;
41 import mir.storage.StorageObject;
42 import mir.storage.StorageObjectExc;
43 import mir.storage.StorageObjectFailure;
44 import mircoders.storage.DatabaseContent;
45 import mircoders.storage.DatabaseContentToMedia;
46 import mircoders.storage.DatabaseContentToTopics;
47 import freemarker.template.SimpleScalar;
48 import freemarker.template.TemplateModel;
49 import freemarker.template.TemplateModelException;
52 * this class implements mapping of one line of the database table content
55 * @version $Id: EntityContent.java,v 1.14 2003/01/25 17:50:34 idfx Exp $
56 * @author mir-coders group
61 public class EntityContent extends Entity
64 String mirconf_extLinkName = configuration.getString("Producer.ExtLinkName");
65 String mirconf_intLinkName = configuration.getString("Producer.IntLinkName");
66 String mirconf_mailLinkName = configuration.getString("Producer.MailLinkName");
67 String mirconf_imageRoot = configuration.getString("Producer.ImageRoot");
69 //this should always be transient i.e it can never be stored in the db
70 //or ObjectStore. (so the ObjectStore should only be caching what comes
71 //directly out of the DB. @todo confirm this with rk. -mh
72 HashMap _entCache = new HashMap();
73 Boolean _hasMedia = null;
77 public EntityContent()
80 //content_data is now filed-type "text"
81 //streamedInput = new ArrayList();
82 //streamedInput.add("content_data");
85 public EntityContent(StorageObject theStorage) {
87 setStorage(theStorage);
94 * set is_produced flag for the article
97 public void setProduced(boolean yesno) throws StorageObjectFailure
99 String value = (yesno) ? "1":"0";
100 if (value.equals( getValue("is_produced") )) return;
102 Connection con=null;Statement stmt=null;
103 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
105 con = theStorageObject.getPooledCon();
106 /** @todo should be preparedStatement: faster!! */
107 stmt = con.createStatement();
108 theStorageObject.executeUpdate(stmt,sql);
110 catch (StorageObjectFailure e) {
111 throwStorageObjectFailure(e, "\n -- set produced failed");
113 catch (SQLException e) {
114 throwStorageObjectFailure(e, "\n -- set produced failed");
117 theStorageObject.freeConnection(con,stmt);
123 * make openposting to newswire
126 public void newswire() throws StorageObjectFailure
128 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
130 theStorageObject.executeUpdate(sql);
131 } catch (StorageObjectFailure e) {
132 throwStorageObjectFailure(e, "\n -- newswire failed");
133 } catch (SQLException e) {
134 throwStorageObjectFailure(e, "\n -- newswire failed");
142 public void dettach(String cid,String mid) throws StorageObjectFailure
146 DatabaseContentToMedia.getInstance().delete(cid,mid);
149 throwStorageObjectFailure(e, "\n -- failed to get instance");
152 //set Content to unproduced
161 public void attach(String mid) throws StorageObjectFailure
164 //write media-id mid and content-id in table content_x_media
166 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
167 } catch(StorageObjectFailure e){
168 throwStorageObjectFailure(e, "attach: could not get the instance");
170 //set Content to unproduced
173 theLog.printError("EntityContent: attach without mid");
178 * overridden method getValue to include formatted date into every
182 public TemplateModel get(java.lang.String key) throws TemplateModelException
185 if (_entCache.containsKey(key)) {
186 return (TemplateModel)_entCache.get(key);
188 if (key.equals("to_comments")) {
190 _entCache.put(key, getComments());
191 return (TemplateModel)_entCache.get(key);
193 catch (Exception ex) {
194 theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
196 throw new TemplateModelException(ex.toString());
199 if (key.equals("to_media_images")) {
201 _entCache.put(key, getImagesForContent());
202 return (TemplateModel)_entCache.get(key);
204 catch (Exception ex) {
205 theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
206 throw new TemplateModelException(ex.toString());
209 if (key.equals("to_media_audio")) {
211 _entCache.put(key, getAudioForContent());
212 return (TemplateModel)_entCache.get(key);
214 catch (Exception ex) {
215 theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
216 throw new TemplateModelException(ex.toString());
219 if (key.equals("to_media_video")) {
221 _entCache.put(key, getVideoForContent());
222 return (TemplateModel)_entCache.get(key);
224 catch (Exception ex) {
225 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
226 throw new TemplateModelException(ex.toString());
229 if (key.equals("to_media_other")) {
231 _entCache.put(key, getOtherMediaForContent());
232 return (TemplateModel)_entCache.get(key);
234 catch (Exception ex) {
235 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
236 throw new TemplateModelException(ex.toString());
239 else if (key.equals("to_topics")) {
242 DatabaseContentToTopics.getInstance().getTopics(this));
243 return (TemplateModel)_entCache.get(key);
245 catch (Exception ex) {
246 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
247 throw new TemplateModelException(ex.toString());
251 return new SimpleScalar(getValue(key));
259 * overridden method setValues to patch creator_main_url
261 public void setValues(HashMap theStringValues) {
262 if (theStringValues != null) {
263 if (theStringValues.containsKey("creator_main_url")){
264 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
265 theStringValues.remove("creator_main_url");
267 else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
268 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
272 super.setValues(theStringValues);
276 * fetches all the comments belonging to an article
278 * @return freemarker.template.SimpleList
280 private EntityList getComments() throws StorageObjectFailure {
281 return ((DatabaseContent)theStorageObject).getComments(this);
284 private boolean hasMedia() throws StorageObjectFailure
286 if (_hasMedia == null) {
289 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
290 } catch (StorageObjectExc e) {
291 throw new StorageObjectFailure(e);
294 return _hasMedia.booleanValue();
297 //######## @todo all of the following getBlahForContent should have
298 // and optimized version where LIMIT=1 sql for list view.
299 private EntityList getImagesForContent()
300 throws StorageObjectFailure, TemplateModelException
303 return DatabaseContentToMedia.getInstance().getImages(this);
308 private EntityList getAudioForContent()
309 throws StorageObjectFailure, TemplateModelException
312 return DatabaseContentToMedia.getInstance().getAudio(this) ;
317 private EntityList getVideoForContent()
318 throws StorageObjectFailure, TemplateModelException
321 return DatabaseContentToMedia.getInstance().getVideo(this) ;
326 private EntityList getOtherMediaForContent()
327 throws StorageObjectFailure, TemplateModelException
330 return DatabaseContentToMedia.getInstance().getOther(this);