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;
38 import java.lang.reflect.*;
40 import freemarker.template.*;
47 import mircoders.storage.*;
50 * this class implements mapping of one line of the database table content
53 * @version $Id: EntityContent.java,v 1.13 2002/12/02 12:33:23 zapata Exp $
54 * @author mir-coders group
59 public class EntityContent extends Entity
62 String mirconf_extLinkName = MirConfig.getProp("Producer.ExtLinkName");
63 String mirconf_intLinkName = MirConfig.getProp("Producer.IntLinkName");
64 String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
65 String mirconf_imageRoot = MirConfig.getProp("Producer.ImageRoot");
67 //this should always be transient i.e it can never be stored in the db
68 //or ObjectStore. (so the ObjectStore should only be caching what comes
69 //directly out of the DB. @todo confirm this with rk. -mh
70 HashMap _entCache = new HashMap();
71 Boolean _hasMedia = null;
75 public EntityContent()
78 //content_data is now filed-type "text"
79 //streamedInput = new ArrayList();
80 //streamedInput.add("content_data");
83 public EntityContent(StorageObject theStorage) {
85 setStorage(theStorage);
92 * set is_produced flag for the article
95 public void setProduced(boolean yesno) throws StorageObjectException
97 String value = (yesno) ? "1":"0";
98 if (value.equals( getValue("is_produced") )) return;
100 Connection con=null;Statement stmt=null;
101 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
103 con = theStorageObject.getPooledCon();
104 /** @todo should be preparedStatement: faster!! */
105 stmt = con.createStatement();
106 theStorageObject.executeUpdate(stmt,sql);
108 catch (StorageObjectException e) {
109 throwStorageObjectException(e, "\n -- set produced failed");
111 catch (SQLException e) {
112 throwStorageObjectException(e, "\n -- set produced failed");
115 theStorageObject.freeConnection(con,stmt);
121 * make openposting to newswire
124 public void newswire() throws StorageObjectException
126 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
128 theStorageObject.executeUpdate(sql);
129 } catch (StorageObjectException e) {
130 throwStorageObjectException(e, "\n -- newswire failed");
131 } catch (SQLException e) {
132 throwStorageObjectException(e, "\n -- newswire failed");
140 public void dettach(String cid,String mid) throws StorageObjectException
144 DatabaseContentToMedia.getInstance().delete(cid,mid);
147 throwStorageObjectException(e, "\n -- failed to get instance");
150 //set Content to unproduced
159 public void attach(String mid) throws StorageObjectException
162 //write media-id mid and content-id in table content_x_media
164 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
165 } catch(StorageObjectException e){
166 throwStorageObjectException(e, "attach: could not get the instance");
168 //set Content to unproduced
171 theLog.printError("EntityContent: attach without mid");
176 * overridden method getValue to include formatted date into every
180 public TemplateModel get(java.lang.String key) throws TemplateModelException
183 if (_entCache.containsKey(key)) {
184 return (TemplateModel)_entCache.get(key);
186 if (key.equals("to_comments")) {
188 _entCache.put(key, getComments());
189 return (TemplateModel)_entCache.get(key);
191 catch (Exception ex) {
192 theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
194 throw new TemplateModelException(ex.toString());
197 if (key.equals("to_media_images")) {
199 _entCache.put(key, getImagesForContent());
200 return (TemplateModel)_entCache.get(key);
202 catch (Exception ex) {
203 theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
204 throw new TemplateModelException(ex.toString());
207 if (key.equals("to_media_audio")) {
209 _entCache.put(key, getAudioForContent());
210 return (TemplateModel)_entCache.get(key);
212 catch (Exception ex) {
213 theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
214 throw new TemplateModelException(ex.toString());
217 if (key.equals("to_media_video")) {
219 _entCache.put(key, getVideoForContent());
220 return (TemplateModel)_entCache.get(key);
222 catch (Exception ex) {
223 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
224 throw new TemplateModelException(ex.toString());
227 if (key.equals("to_media_other")) {
229 _entCache.put(key, getOtherMediaForContent());
230 return (TemplateModel)_entCache.get(key);
232 catch (Exception ex) {
233 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
234 throw new TemplateModelException(ex.toString());
237 else if (key.equals("to_topics")) {
240 DatabaseContentToTopics.getInstance().getTopics(this));
241 return (TemplateModel)_entCache.get(key);
243 catch (Exception ex) {
244 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
245 throw new TemplateModelException(ex.toString());
249 return new SimpleScalar(getValue(key));
257 * overridden method setValues to patch creator_main_url
259 public void setValues(HashMap theStringValues) {
260 if (theStringValues != null) {
261 if (theStringValues.containsKey("creator_main_url")){
262 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
263 theStringValues.remove("creator_main_url");
265 else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
266 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
270 super.setValues(theStringValues);
274 * fetches all the comments belonging to an article
276 * @return freemarker.template.SimpleList
278 private EntityList getComments() throws StorageObjectException {
279 return ((DatabaseContent)theStorageObject).getComments(this);
282 private boolean hasMedia() throws StorageObjectException
284 if (_hasMedia == null) {
286 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
288 return _hasMedia.booleanValue();
291 //######## @todo all of the following getBlahForContent should have
292 // and optimized version where LIMIT=1 sql for list view.
293 private EntityList getImagesForContent()
294 throws StorageObjectException, TemplateModelException
297 return DatabaseContentToMedia.getInstance().getImages(this);
302 private EntityList getAudioForContent()
303 throws StorageObjectException, TemplateModelException
306 return DatabaseContentToMedia.getInstance().getAudio(this) ;
311 private EntityList getVideoForContent()
312 throws StorageObjectException, TemplateModelException
315 return DatabaseContentToMedia.getInstance().getVideo(this) ;
320 private EntityList getOtherMediaForContent()
321 throws StorageObjectException, TemplateModelException
324 return DatabaseContentToMedia.getInstance().getOther(this);