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 $Revision: 1.10 $ $Date: 2002/09/01 22:05:53 $
54 * @author $Author: mh $
56 * $Log: EntityContent.java,v $
57 * Revision 1.10 2002/09/01 22:05:53 mh
60 * Revision 1.9.2.1 2002/09/01 21:31:43 mh
63 * Revision 1.9 2002/07/19 20:55:49 mh
64 * commented out decodeHTML, as we don't encodeHTML anymore
66 * Revision 1.8 2002/06/28 20:37:33 mh
67 * make date_formatted be webdb_create_short instead to be more consistent. also make date2webdb.. use webdb_create
73 public class EntityContent extends Entity
76 String mirconf_extLinkName = MirConfig.getProp("Producer.ExtLinkName");
77 String mirconf_intLinkName = MirConfig.getProp("Producer.IntLinkName");
78 String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
79 String mirconf_imageRoot = MirConfig.getProp("Producer.ImageRoot");
81 //this should always be transient i.e it can never be stored in the db
82 //or ObjectStore. (so the ObjectStore should only be caching what comes
83 //directly out of the DB. @todo confirm this with rk. -mh
84 HashMap _entCache = new HashMap();
85 Boolean _hasMedia = null;
89 public EntityContent()
92 //content_data is now filed-type "text"
93 //streamedInput = new ArrayList();
94 //streamedInput.add("content_data");
97 public EntityContent(StorageObject theStorage) {
99 setStorage(theStorage);
106 * set is_produced flag for the article
109 public void setProduced(boolean yesno) throws StorageObjectException
111 String value = (yesno) ? "1":"0";
112 if (value.equals( getValue("is_produced") )) return;
114 Connection con=null;Statement stmt=null;
115 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
117 con = theStorageObject.getPooledCon();
118 /** @todo should be preparedStatement: faster!! */
119 stmt = con.createStatement();
120 theStorageObject.executeUpdate(stmt,sql);
121 } catch (StorageObjectException e) {
122 throwStorageObjectException(e, "\n -- set produced failed");
123 } catch (SQLException e) {
124 throwStorageObjectException(e, "\n -- set produced failed");
126 theStorageObject.freeConnection(con,stmt);
132 * make openposting to newswire
135 public void newswire() throws StorageObjectException
137 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
139 theStorageObject.executeUpdate(sql);
140 } catch (StorageObjectException e) {
141 throwStorageObjectException(e, "\n -- newswire failed");
142 } catch (SQLException e) {
143 throwStorageObjectException(e, "\n -- newswire failed");
151 public void dettach(String cid,String mid) throws StorageObjectException
155 DatabaseContentToMedia.getInstance().delete(cid,mid);
156 } catch (Exception e){
157 throwStorageObjectException(e, "\n -- failed to get instance");
159 //set Content to unproduced
168 public void attach(String mid) throws StorageObjectException
171 //write media-id mid and content-id in table content_x_media
173 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
174 } catch(StorageObjectException e){
175 throwStorageObjectException(e, "attach: could not get the instance");
177 //set Content to unproduced
180 theLog.printError("EntityContent: attach without mid");
185 * overridden method getValue to include formatted date into every
189 public String getValue(String field)
191 String returnField = null;
194 if (field.equals("date_formatted") || field.equals("webdb_create_short") )
196 if (hasValueForField("date"))
197 returnField = StringUtil.webdbDate2readableDate(getValue("webdb_create"));
199 else if (field.equals("description_parsed"))
200 returnField = getDescriptionParsed();
201 else if (field.equals("description_sentence"))
202 returnField = getDescriptionSentence();
203 else if (field.equals("content_data_parsed"))
204 returnField = getContentDataParsed();
206 return super.getValue(field);
211 public TemplateModel get(java.lang.String key) throws TemplateModelException
214 if (_entCache.containsKey(key)) {
215 return (TemplateModel)_entCache.get(key);
217 if (key.equals("to_comments")) {
219 _entCache.put(key, getComments());
220 return (TemplateModel)_entCache.get(key);
221 } catch (Exception ex) {
222 theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
223 throw new TemplateModelException(ex.toString());
226 if (key.equals("to_media_images")) {
228 _entCache.put(key, getImagesForContent());
229 return (TemplateModel)_entCache.get(key);
231 catch (Exception ex) {
232 theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
233 throw new TemplateModelException(ex.toString());
236 if (key.equals("to_media_audio")) {
238 _entCache.put(key, getAudioForContent());
239 return (TemplateModel)_entCache.get(key);
241 catch (Exception ex) {
242 theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
243 throw new TemplateModelException(ex.toString());
246 if (key.equals("to_media_video")) {
248 _entCache.put(key, getVideoForContent());
249 return (TemplateModel)_entCache.get(key);
251 catch (Exception ex) {
252 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
253 throw new TemplateModelException(ex.toString());
256 if (key.equals("to_media_other")) {
258 _entCache.put(key, getOtherMediaForContent());
259 return (TemplateModel)_entCache.get(key);
261 catch (Exception ex) {
262 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
263 throw new TemplateModelException(ex.toString());
266 else if (key.equals("to_media_icon")) {
268 _entCache.put(key, getUploadedMediaForNewswire());
269 return (TemplateModel)_entCache.get(key);
271 catch (Exception ex) {
272 theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
273 throw new TemplateModelException(ex.toString());
276 else if (key.equals("to_topics")) {
279 DatabaseContentToTopics.getInstance().getTopics(this));
280 return (TemplateModel)_entCache.get(key);
282 catch (Exception ex) {
283 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
284 throw new TemplateModelException(ex.toString());
288 return new SimpleScalar(getValue(key));
299 * overridden method setValues to patch creator_main_url
301 public void setValues(HashMap theStringValues) {
302 if (theStringValues != null) {
303 if (theStringValues.containsKey("creator_main_url")){
304 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
305 theStringValues.remove("creator_main_url");
306 } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
307 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
311 super.setValues(theStringValues);
315 private String getContentDataParsed() {
316 String returnField = getValue("content_data");
317 if ((returnField!=null) && (returnField.length()>0) ) {
318 returnField=StringUtil.deleteForbiddenTags(returnField);
319 //create http-links and email-links
320 if (getValue("is_html").equals("0")) {
321 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
322 mirconf_mailLinkName,mirconf_extLinkName,
323 mirconf_intLinkName);
325 // commented this out as I don't think it necessary as we don't
326 // "encodeHTML" in the first place anymore.. -mh 2002.07.19
327 //returnField = StringUtil.decodeHTMLinTags(returnField);
332 private String getDescriptionSentence() {
333 String returnField = getValue("description");
334 if (returnField != null && returnField.length()>0) {
335 returnField = StringUtil.removeHTMLTags(returnField);
336 int endOfFirstSentence=StringUtil.findEndOfSentence(returnField,0);
337 if (endOfFirstSentence > 0){
338 returnField = returnField.substring(0,endOfFirstSentence);
344 private String getDescriptionParsed() {
345 String returnField = getValue("description");
346 if (returnField != null && returnField.length()>0) {
347 returnField = StringUtil.deleteForbiddenTags(returnField);
348 if (getValue("is_html").equals("0")) {
349 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
350 mirconf_mailLinkName,mirconf_extLinkName,
351 mirconf_intLinkName);
353 returnField = StringUtil.decodeHTMLinTags(returnField);
359 * fetches all the comments belonging to an article
361 * @return freemarker.template.SimpleList
363 private EntityList getComments() throws StorageObjectException {
364 return ((DatabaseContent)theStorageObject).getComments(this);
367 // @todo this needs to optimized. expensive SQL
368 private SimpleHash getUploadedMediaForNewswire()
369 throws StorageObjectException, TemplateModelException
371 // fetching/setting the images
372 // return to_media_icons
373 String tinyIcon = null, iconAlt = null;
374 MirMedia mediaHandler = null;
375 EntityUploadedMedia uploadedMedia;
377 SimpleHash returnHash = new SimpleHash();
379 EntityList upMediaEntityList =
380 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
381 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
383 for (int n=0; n < upMediaEntityList.size();n++) {
384 uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
385 mediaType = uploadedMedia.getMediaType();
387 mediaHandler = MediaHelper.getHandler( mediaType );
388 } catch (MirMediaException ex) {
389 throw new TemplateModelException(ex.toString());
391 //the "best" media type to show
392 if (mediaHandler.isVideo()) {
393 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
396 } else if (mediaHandler.isAudio()) {
397 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
399 } else if (tinyIcon == null && !mediaHandler.isImage()) {
400 tinyIcon = mediaHandler.getTinyIcon();
401 iconAlt = mediaHandler.getIconAlt();
405 //it only has image(s)
406 if (tinyIcon == null) {
407 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
410 // uploadedMedia Entity list is empty.
413 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
416 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
417 returnHash.put("icon_alt", iconAlt);
421 private boolean hasMedia() throws StorageObjectException
423 if (_hasMedia == null) {
425 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
427 return _hasMedia.booleanValue();
430 //######## @todo all of the following getBlahForContent should have
431 // and optimized version where LIMIT=1 sql for list view.
432 private EntityList getImagesForContent()
433 throws StorageObjectException, TemplateModelException
436 return DatabaseContentToMedia.getInstance().getImages(this);
441 private EntityList getAudioForContent()
442 throws StorageObjectException, TemplateModelException
445 return DatabaseContentToMedia.getInstance().getAudio(this) ;
450 private EntityList getVideoForContent()
451 throws StorageObjectException, TemplateModelException
454 return DatabaseContentToMedia.getInstance().getVideo(this) ;
459 private EntityList getOtherMediaForContent()
460 throws StorageObjectException, TemplateModelException
463 return DatabaseContentToMedia.getInstance().getOther(this);