1 package mircoders.entity;
7 import java.lang.reflect.*;
9 import freemarker.template.*;
16 import mircoders.storage.*;
19 * this class implements mapping of one line of the database table content
22 * @version $Revision: 1.8 $ $Date: 2002/06/28 20:37:33 $
23 * @author $Author: mh $
25 * $Log: EntityContent.java,v $
26 * Revision 1.8 2002/06/28 20:37:33 mh
27 * make date_formatted be webdb_create_short instead to be more consistent. also make date2webdb.. use webdb_create
33 public class EntityContent extends Entity
36 String mirconf_extLinkName = MirConfig.getProp("Producer.ExtLinkName");
37 String mirconf_intLinkName = MirConfig.getProp("Producer.IntLinkName");
38 String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
39 String mirconf_imageRoot = MirConfig.getProp("Producer.ImageRoot");
41 //this should always be transient i.e it can never be stored in the db
42 //or ObjectStore. (so the ObjectStore should only be caching what comes
43 //directly out of the DB. @todo confirm this with rk. -mh
44 HashMap _entCache = new HashMap();
45 Boolean _hasMedia = null;
49 public EntityContent()
52 //content_data is now filed-type "text"
53 //streamedInput = new ArrayList();
54 //streamedInput.add("content_data");
57 public EntityContent(StorageObject theStorage) {
59 setStorage(theStorage);
66 * set is_produced flag for the article
69 public void setProduced(boolean yesno) throws StorageObjectException
71 String value = (yesno) ? "1":"0";
72 if (value.equals( getValue("is_produced") )) return;
74 Connection con=null;Statement stmt=null;
75 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
77 con = theStorageObject.getPooledCon();
78 /** @todo should be preparedStatement: faster!! */
79 stmt = con.createStatement();
80 theStorageObject.executeUpdate(stmt,sql);
81 } catch (StorageObjectException e) {
82 throwStorageObjectException(e, "\n -- set produced failed");
83 } catch (SQLException e) {
84 throwStorageObjectException(e, "\n -- set produced failed");
86 theStorageObject.freeConnection(con,stmt);
92 * make openposting to newswire
95 public void newswire() throws StorageObjectException
97 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
99 theStorageObject.executeUpdate(sql);
100 } catch (StorageObjectException e) {
101 throwStorageObjectException(e, "\n -- newswire failed");
102 } catch (SQLException e) {
103 throwStorageObjectException(e, "\n -- newswire failed");
111 public void dettach(String cid,String mid) throws StorageObjectException
115 DatabaseContentToMedia.getInstance().delete(cid,mid);
116 } catch (Exception e){
117 throwStorageObjectException(e, "\n -- failed to get instance");
119 //set Content to unproduced
128 public void attach(String mid) throws StorageObjectException
131 //write media-id mid and content-id in table content_x_media
133 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
134 } catch(StorageObjectException e){
135 throwStorageObjectException(e, "attach: could not get the instance");
137 //set Content to unproduced
140 theLog.printError("EntityContent: attach without mid");
145 * overridden method getValue to include formatted date into every
149 public String getValue(String field)
151 String returnField = null;
154 if (field.equals("date_formatted") || field.equals("webdb_create_short") )
156 if (hasValueForField("date"))
157 returnField = StringUtil.webdbDate2readableDate(getValue("webdb_create"));
159 else if (field.equals("description_parsed"))
160 returnField = getDescriptionParsed();
161 else if (field.equals("description_sentence"))
162 returnField = getDescriptionSentence();
163 else if (field.equals("content_data_parsed"))
164 returnField = getContentDataParsed();
166 return super.getValue(field);
171 public TemplateModel get(java.lang.String key) throws TemplateModelException
174 if (_entCache.containsKey(key)) {
175 return (TemplateModel)_entCache.get(key);
177 if (key.equals("to_comments")) {
179 _entCache.put(key, getComments());
180 return (TemplateModel)_entCache.get(key);
181 } catch (Exception ex) {
182 theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
183 throw new TemplateModelException(ex.toString());
186 if (key.equals("to_media_images")) {
188 _entCache.put(key, getImagesForContent());
189 return (TemplateModel)_entCache.get(key);
191 catch (Exception ex) {
192 theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
193 throw new TemplateModelException(ex.toString());
196 if (key.equals("to_media_audio")) {
198 _entCache.put(key, getAudioForContent());
199 return (TemplateModel)_entCache.get(key);
201 catch (Exception ex) {
202 theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
203 throw new TemplateModelException(ex.toString());
206 if (key.equals("to_media_video")) {
208 _entCache.put(key, getVideoForContent());
209 return (TemplateModel)_entCache.get(key);
211 catch (Exception ex) {
212 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
213 throw new TemplateModelException(ex.toString());
216 if (key.equals("to_media_other")) {
218 _entCache.put(key, getOtherMediaForContent());
219 return (TemplateModel)_entCache.get(key);
221 catch (Exception ex) {
222 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
223 throw new TemplateModelException(ex.toString());
226 else if (key.equals("to_media_icon")) {
228 _entCache.put(key, getUploadedMediaForNewswire());
229 return (TemplateModel)_entCache.get(key);
231 catch (Exception ex) {
232 theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
233 throw new TemplateModelException(ex.toString());
236 else if (key.equals("to_topics")) {
239 DatabaseContentToTopics.getInstance().getTopics(this));
240 return (TemplateModel)_entCache.get(key);
242 catch (Exception ex) {
243 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
244 throw new TemplateModelException(ex.toString());
248 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");
266 } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
267 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
271 super.setValues(theStringValues);
275 private String getContentDataParsed() {
276 String returnField = getValue("content_data");
277 if (returnField!=null & returnField.length()>0 ) {
278 returnField=StringUtil.deleteForbiddenTags(returnField);
279 //create http-links and email-links
280 if (getValue("is_html").equals("0")) {
281 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
282 mirconf_mailLinkName,mirconf_extLinkName,
283 mirconf_intLinkName);
285 returnField = StringUtil.decodeHTMLinTags(returnField);
290 private String getDescriptionSentence() {
291 String returnField = getValue("description");
292 if (returnField != null && returnField.length()>0) {
293 returnField = StringUtil.removeHTMLTags(returnField);
294 int endOfFirstSentence=StringUtil.findEndOfSentence(returnField,0);
295 if (endOfFirstSentence > 0){
296 returnField = returnField.substring(0,endOfFirstSentence);
302 private String getDescriptionParsed() {
303 String returnField = getValue("description");
304 if (returnField != null && returnField.length()>0) {
305 returnField = StringUtil.deleteForbiddenTags(returnField);
306 if (getValue("is_html").equals("0")) {
307 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
308 mirconf_mailLinkName,mirconf_extLinkName,
309 mirconf_intLinkName);
311 returnField = StringUtil.decodeHTMLinTags(returnField);
317 * fetches all the comments belonging to an article
319 * @return freemarker.template.SimpleList
321 private EntityList getComments() throws StorageObjectException {
322 return ((DatabaseContent)theStorageObject).getComments(this);
325 // @todo this needs to optimized. expensive SQL
326 private SimpleHash getUploadedMediaForNewswire()
327 throws StorageObjectException, TemplateModelException
329 // fetching/setting the images
330 // return to_media_icons
331 String tinyIcon = null, iconAlt = null;
332 MirMedia mediaHandler = null;
333 EntityUploadedMedia uploadedMedia;
335 SimpleHash returnHash = new SimpleHash();
337 EntityList upMediaEntityList =
338 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
339 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
341 for (int n=0; n < upMediaEntityList.size();n++) {
342 uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
343 mediaType = uploadedMedia.getMediaType();
345 mediaHandler = MediaHelper.getHandler( mediaType );
346 } catch (MirMediaException ex) {
347 throw new TemplateModelException(ex.toString());
349 //the "best" media type to show
350 if (mediaHandler.isVideo()) {
351 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
354 } else if (mediaHandler.isAudio()) {
355 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
357 } else if (tinyIcon == null && !mediaHandler.isImage()) {
358 tinyIcon = mediaHandler.getTinyIcon();
359 iconAlt = mediaHandler.getIconAlt();
363 //it only has image(s)
364 if (tinyIcon == null) {
365 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
368 // uploadedMedia Entity list is empty.
371 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
374 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
375 returnHash.put("icon_alt", iconAlt);
379 private boolean hasMedia() throws StorageObjectException
381 if (_hasMedia == null) {
383 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
385 return _hasMedia.booleanValue();
388 //######## @todo all of the following getBlahForContent should have
389 // and optimized version where LIMIT=1 sql for list view.
390 private EntityList getImagesForContent()
391 throws StorageObjectException, TemplateModelException
394 return DatabaseContentToMedia.getInstance().getImages(this);
399 private EntityList getAudioForContent()
400 throws StorageObjectException, TemplateModelException
403 return DatabaseContentToMedia.getInstance().getAudio(this) ;
408 private EntityList getVideoForContent()
409 throws StorageObjectException, TemplateModelException
412 return DatabaseContentToMedia.getInstance().getVideo(this) ;
417 private EntityList getOtherMediaForContent()
418 throws StorageObjectException, TemplateModelException
421 return DatabaseContentToMedia.getInstance().getOther(this);