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.12 2002/11/27 08:20:14 mh 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);
107 } catch (StorageObjectException e) {
108 throwStorageObjectException(e, "\n -- set produced failed");
109 } catch (SQLException e) {
110 throwStorageObjectException(e, "\n -- set produced failed");
112 theStorageObject.freeConnection(con,stmt);
118 * make openposting to newswire
121 public void newswire() throws StorageObjectException
123 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
125 theStorageObject.executeUpdate(sql);
126 } catch (StorageObjectException e) {
127 throwStorageObjectException(e, "\n -- newswire failed");
128 } catch (SQLException e) {
129 throwStorageObjectException(e, "\n -- newswire failed");
137 public void dettach(String cid,String mid) throws StorageObjectException
141 DatabaseContentToMedia.getInstance().delete(cid,mid);
142 } catch (Exception e){
143 throwStorageObjectException(e, "\n -- failed to get instance");
145 //set Content to unproduced
154 public void attach(String mid) throws StorageObjectException
157 //write media-id mid and content-id in table content_x_media
159 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
160 } catch(StorageObjectException e){
161 throwStorageObjectException(e, "attach: could not get the instance");
163 //set Content to unproduced
166 theLog.printError("EntityContent: attach without mid");
171 * overridden method getValue to include formatted date into every
175 public String getValue(String field)
177 String returnField = null;
180 if (field.equals("date_formatted") || field.equals("webdb_create_short") )
182 if (hasValueForField("date"))
183 returnField = StringUtil.webdbDate2readableDate(getValue("webdb_create"));
185 else if (field.equals("description_parsed"))
186 returnField = getDescriptionParsed();
187 else if (field.equals("description_sentence"))
188 returnField = getDescriptionSentence();
189 else if (field.equals("content_data_parsed"))
190 returnField = getContentDataParsed();
192 return super.getValue(field);
197 public TemplateModel get(java.lang.String key) throws TemplateModelException
200 if (_entCache.containsKey(key)) {
201 return (TemplateModel)_entCache.get(key);
203 if (key.equals("to_comments")) {
205 _entCache.put(key, getComments());
206 return (TemplateModel)_entCache.get(key);
207 } catch (Exception ex) {
208 theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
209 throw new TemplateModelException(ex.toString());
212 if (key.equals("to_media_images")) {
214 _entCache.put(key, getImagesForContent());
215 return (TemplateModel)_entCache.get(key);
217 catch (Exception ex) {
218 theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
219 throw new TemplateModelException(ex.toString());
222 if (key.equals("to_media_audio")) {
224 _entCache.put(key, getAudioForContent());
225 return (TemplateModel)_entCache.get(key);
227 catch (Exception ex) {
228 theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
229 throw new TemplateModelException(ex.toString());
232 if (key.equals("to_media_video")) {
234 _entCache.put(key, getVideoForContent());
235 return (TemplateModel)_entCache.get(key);
237 catch (Exception ex) {
238 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
239 throw new TemplateModelException(ex.toString());
242 if (key.equals("to_media_other")) {
244 _entCache.put(key, getOtherMediaForContent());
245 return (TemplateModel)_entCache.get(key);
247 catch (Exception ex) {
248 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
249 throw new TemplateModelException(ex.toString());
252 else if (key.equals("to_media_icon")) {
254 _entCache.put(key, getUploadedMediaForNewswire());
255 return (TemplateModel)_entCache.get(key);
257 catch (Exception ex) {
258 theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
259 throw new TemplateModelException(ex.toString());
262 else if (key.equals("to_topics")) {
265 DatabaseContentToTopics.getInstance().getTopics(this));
266 return (TemplateModel)_entCache.get(key);
268 catch (Exception ex) {
269 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
270 throw new TemplateModelException(ex.toString());
274 return new SimpleScalar(getValue(key));
285 * overridden method setValues to patch creator_main_url
287 public void setValues(HashMap theStringValues) {
288 if (theStringValues != null) {
289 if (theStringValues.containsKey("creator_main_url")){
290 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
291 theStringValues.remove("creator_main_url");
292 } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
293 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
297 super.setValues(theStringValues);
301 private String getContentDataParsed() {
302 String returnField = getValue("content_data");
303 if ((returnField!=null) && (returnField.length()>0) ) {
304 returnField=StringUtil.deleteForbiddenTags(returnField);
305 //create http-links and email-links
306 if (getValue("is_html").equals("0")) {
307 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
308 mirconf_mailLinkName,mirconf_extLinkName,
309 mirconf_intLinkName);
315 private String getDescriptionSentence() {
316 String returnField = getValue("description");
317 if (returnField != null && returnField.length()>0) {
318 returnField = StringUtil.removeHTMLTags(returnField);
319 int endOfFirstSentence=StringUtil.findEndOfSentence(returnField,0);
320 if (endOfFirstSentence > 0){
321 returnField = returnField.substring(0,endOfFirstSentence);
327 private String getDescriptionParsed() {
328 String returnField = getValue("description");
329 if (returnField != null && returnField.length()>0) {
330 returnField = StringUtil.deleteForbiddenTags(returnField);
331 if (getValue("is_html").equals("0")) {
332 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
333 mirconf_mailLinkName,mirconf_extLinkName,
334 mirconf_intLinkName);
341 * fetches all the comments belonging to an article
343 * @return freemarker.template.SimpleList
345 private EntityList getComments() throws StorageObjectException {
346 return ((DatabaseContent)theStorageObject).getComments(this);
349 // @todo this needs to optimized. expensive SQL
350 private SimpleHash getUploadedMediaForNewswire()
351 throws StorageObjectException, TemplateModelException
353 // fetching/setting the images
354 // return to_media_icons
355 String tinyIcon = null, iconAlt = null;
356 MirMedia mediaHandler = null;
357 EntityUploadedMedia uploadedMedia;
359 SimpleHash returnHash = new SimpleHash();
361 EntityList upMediaEntityList =
362 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
363 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
365 for (int n=0; n < upMediaEntityList.size();n++) {
366 uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
367 mediaType = uploadedMedia.getMediaType();
369 mediaHandler = MediaHelper.getHandler( mediaType );
370 } catch (MirMediaException ex) {
371 throw new TemplateModelException(ex.toString());
373 //the "best" media type to show
374 if (mediaHandler.isVideo()) {
375 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
378 } else if (mediaHandler.isAudio()) {
379 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
381 } else if (tinyIcon == null && !mediaHandler.isImage()) {
382 tinyIcon = mediaHandler.getTinyIconName();
383 iconAlt = mediaHandler.getIconAltName();
387 //it only has image(s)
388 if (tinyIcon == null) {
389 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
392 // uploadedMedia Entity list is empty.
395 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
398 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
399 returnHash.put("icon_alt", iconAlt);
403 private boolean hasMedia() throws StorageObjectException
405 if (_hasMedia == null) {
407 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
409 return _hasMedia.booleanValue();
412 //######## @todo all of the following getBlahForContent should have
413 // and optimized version where LIMIT=1 sql for list view.
414 private EntityList getImagesForContent()
415 throws StorageObjectException, TemplateModelException
418 return DatabaseContentToMedia.getInstance().getImages(this);
423 private EntityList getAudioForContent()
424 throws StorageObjectException, TemplateModelException
427 return DatabaseContentToMedia.getInstance().getAudio(this) ;
432 private EntityList getVideoForContent()
433 throws StorageObjectException, TemplateModelException
436 return DatabaseContentToMedia.getInstance().getVideo(this) ;
441 private EntityList getOtherMediaForContent()
442 throws StorageObjectException, TemplateModelException
445 return DatabaseContentToMedia.getInstance().getOther(this);