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.9 $ $Date: 2002/07/19 20:55:49 $
23 * @author $Author: mh $
25 * $Log: EntityContent.java,v $
26 * Revision 1.9 2002/07/19 20:55:49 mh
27 * commented out decodeHTML, as we don't encodeHTML anymore
29 * Revision 1.8 2002/06/28 20:37:33 mh
30 * make date_formatted be webdb_create_short instead to be more consistent. also make date2webdb.. use webdb_create
36 public class EntityContent extends Entity
39 String mirconf_extLinkName = MirConfig.getProp("Producer.ExtLinkName");
40 String mirconf_intLinkName = MirConfig.getProp("Producer.IntLinkName");
41 String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
42 String mirconf_imageRoot = MirConfig.getProp("Producer.ImageRoot");
44 //this should always be transient i.e it can never be stored in the db
45 //or ObjectStore. (so the ObjectStore should only be caching what comes
46 //directly out of the DB. @todo confirm this with rk. -mh
47 HashMap _entCache = new HashMap();
48 Boolean _hasMedia = null;
52 public EntityContent()
55 //content_data is now filed-type "text"
56 //streamedInput = new ArrayList();
57 //streamedInput.add("content_data");
60 public EntityContent(StorageObject theStorage) {
62 setStorage(theStorage);
69 * set is_produced flag for the article
72 public void setProduced(boolean yesno) throws StorageObjectException
74 String value = (yesno) ? "1":"0";
75 if (value.equals( getValue("is_produced") )) return;
77 Connection con=null;Statement stmt=null;
78 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
80 con = theStorageObject.getPooledCon();
81 /** @todo should be preparedStatement: faster!! */
82 stmt = con.createStatement();
83 theStorageObject.executeUpdate(stmt,sql);
84 } catch (StorageObjectException e) {
85 throwStorageObjectException(e, "\n -- set produced failed");
86 } catch (SQLException e) {
87 throwStorageObjectException(e, "\n -- set produced failed");
89 theStorageObject.freeConnection(con,stmt);
95 * make openposting to newswire
98 public void newswire() throws StorageObjectException
100 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
102 theStorageObject.executeUpdate(sql);
103 } catch (StorageObjectException e) {
104 throwStorageObjectException(e, "\n -- newswire failed");
105 } catch (SQLException e) {
106 throwStorageObjectException(e, "\n -- newswire failed");
114 public void dettach(String cid,String mid) throws StorageObjectException
118 DatabaseContentToMedia.getInstance().delete(cid,mid);
119 } catch (Exception e){
120 throwStorageObjectException(e, "\n -- failed to get instance");
122 //set Content to unproduced
131 public void attach(String mid) throws StorageObjectException
134 //write media-id mid and content-id in table content_x_media
136 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
137 } catch(StorageObjectException e){
138 throwStorageObjectException(e, "attach: could not get the instance");
140 //set Content to unproduced
143 theLog.printError("EntityContent: attach without mid");
148 * overridden method getValue to include formatted date into every
152 public String getValue(String field)
154 String returnField = null;
157 if (field.equals("date_formatted") || field.equals("webdb_create_short") )
159 if (hasValueForField("date"))
160 returnField = StringUtil.webdbDate2readableDate(getValue("webdb_create"));
162 else if (field.equals("description_parsed"))
163 returnField = getDescriptionParsed();
164 else if (field.equals("description_sentence"))
165 returnField = getDescriptionSentence();
166 else if (field.equals("content_data_parsed"))
167 returnField = getContentDataParsed();
169 return super.getValue(field);
174 public TemplateModel get(java.lang.String key) throws TemplateModelException
177 if (_entCache.containsKey(key)) {
178 return (TemplateModel)_entCache.get(key);
180 if (key.equals("to_comments")) {
182 _entCache.put(key, getComments());
183 return (TemplateModel)_entCache.get(key);
184 } catch (Exception ex) {
185 theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
186 throw new TemplateModelException(ex.toString());
189 if (key.equals("to_media_images")) {
191 _entCache.put(key, getImagesForContent());
192 return (TemplateModel)_entCache.get(key);
194 catch (Exception ex) {
195 theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
196 throw new TemplateModelException(ex.toString());
199 if (key.equals("to_media_audio")) {
201 _entCache.put(key, getAudioForContent());
202 return (TemplateModel)_entCache.get(key);
204 catch (Exception ex) {
205 theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
206 throw new TemplateModelException(ex.toString());
209 if (key.equals("to_media_video")) {
211 _entCache.put(key, getVideoForContent());
212 return (TemplateModel)_entCache.get(key);
214 catch (Exception ex) {
215 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
216 throw new TemplateModelException(ex.toString());
219 if (key.equals("to_media_other")) {
221 _entCache.put(key, getOtherMediaForContent());
222 return (TemplateModel)_entCache.get(key);
224 catch (Exception ex) {
225 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
226 throw new TemplateModelException(ex.toString());
229 else if (key.equals("to_media_icon")) {
231 _entCache.put(key, getUploadedMediaForNewswire());
232 return (TemplateModel)_entCache.get(key);
234 catch (Exception ex) {
235 theLog.printWarning("-- getUploadedMediaForNewswire: 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));
262 * overridden method setValues to patch creator_main_url
264 public void setValues(HashMap theStringValues) {
265 if (theStringValues != null) {
266 if (theStringValues.containsKey("creator_main_url")){
267 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
268 theStringValues.remove("creator_main_url");
269 } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
270 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
274 super.setValues(theStringValues);
278 private String getContentDataParsed() {
279 String returnField = getValue("content_data");
280 if ((returnField!=null) && (returnField.length()>0) ) {
281 returnField=StringUtil.deleteForbiddenTags(returnField);
282 //create http-links and email-links
283 if (getValue("is_html").equals("0")) {
284 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
285 mirconf_mailLinkName,mirconf_extLinkName,
286 mirconf_intLinkName);
288 // commented this out as I don't think it necessary as we don't
289 // "encodeHTML" in the first place anymore.. -mh 2002.07.19
290 //returnField = StringUtil.decodeHTMLinTags(returnField);
295 private String getDescriptionSentence() {
296 String returnField = getValue("description");
297 if (returnField != null && returnField.length()>0) {
298 returnField = StringUtil.removeHTMLTags(returnField);
299 int endOfFirstSentence=StringUtil.findEndOfSentence(returnField,0);
300 if (endOfFirstSentence > 0){
301 returnField = returnField.substring(0,endOfFirstSentence);
307 private String getDescriptionParsed() {
308 String returnField = getValue("description");
309 if (returnField != null && returnField.length()>0) {
310 returnField = StringUtil.deleteForbiddenTags(returnField);
311 if (getValue("is_html").equals("0")) {
312 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
313 mirconf_mailLinkName,mirconf_extLinkName,
314 mirconf_intLinkName);
316 returnField = StringUtil.decodeHTMLinTags(returnField);
322 * fetches all the comments belonging to an article
324 * @return freemarker.template.SimpleList
326 private EntityList getComments() throws StorageObjectException {
327 return ((DatabaseContent)theStorageObject).getComments(this);
330 // @todo this needs to optimized. expensive SQL
331 private SimpleHash getUploadedMediaForNewswire()
332 throws StorageObjectException, TemplateModelException
334 // fetching/setting the images
335 // return to_media_icons
336 String tinyIcon = null, iconAlt = null;
337 MirMedia mediaHandler = null;
338 EntityUploadedMedia uploadedMedia;
340 SimpleHash returnHash = new SimpleHash();
342 EntityList upMediaEntityList =
343 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
344 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
346 for (int n=0; n < upMediaEntityList.size();n++) {
347 uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
348 mediaType = uploadedMedia.getMediaType();
350 mediaHandler = MediaHelper.getHandler( mediaType );
351 } catch (MirMediaException ex) {
352 throw new TemplateModelException(ex.toString());
354 //the "best" media type to show
355 if (mediaHandler.isVideo()) {
356 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
359 } else if (mediaHandler.isAudio()) {
360 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
362 } else if (tinyIcon == null && !mediaHandler.isImage()) {
363 tinyIcon = mediaHandler.getTinyIcon();
364 iconAlt = mediaHandler.getIconAlt();
368 //it only has image(s)
369 if (tinyIcon == null) {
370 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
373 // uploadedMedia Entity list is empty.
376 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
379 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
380 returnHash.put("icon_alt", iconAlt);
384 private boolean hasMedia() throws StorageObjectException
386 if (_hasMedia == null) {
388 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
390 return _hasMedia.booleanValue();
393 //######## @todo all of the following getBlahForContent should have
394 // and optimized version where LIMIT=1 sql for list view.
395 private EntityList getImagesForContent()
396 throws StorageObjectException, TemplateModelException
399 return DatabaseContentToMedia.getInstance().getImages(this);
404 private EntityList getAudioForContent()
405 throws StorageObjectException, TemplateModelException
408 return DatabaseContentToMedia.getInstance().getAudio(this) ;
413 private EntityList getVideoForContent()
414 throws StorageObjectException, TemplateModelException
417 return DatabaseContentToMedia.getInstance().getVideo(this) ;
422 private EntityList getOtherMediaForContent()
423 throws StorageObjectException, TemplateModelException
426 return DatabaseContentToMedia.getInstance().getOther(this);