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
27 public class EntityContent extends Entity
30 String mirconf_extLinkName = MirConfig.getProp("Producer.ExtLinkName");
31 String mirconf_intLinkName = MirConfig.getProp("Producer.IntLinkName");
32 String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
33 String mirconf_imageRoot = MirConfig.getProp("Producer.ImageRoot");
35 //this should always be transient i.e it can never be stored in the db
36 //or ObjectStore. (so the ObjectStore should only be caching what comes
37 //directly out of the DB. @todo confirm this with rk. -mh
38 HashMap _entCache = new HashMap();
39 Boolean _hasMedia = null;
43 public EntityContent()
46 //content_data is now filed-type "text"
47 //streamedInput = new ArrayList();
48 //streamedInput.add("content_data");
51 public EntityContent(StorageObject theStorage) {
53 setStorage(theStorage);
60 * set is_produced flag for the article
63 public void setProduced(boolean yesno) throws StorageObjectException
65 String value = (yesno) ? "1":"0";
66 if (value.equals( getValue("is_produced") )) return;
68 Connection con=null;Statement stmt=null;
69 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
71 con = theStorageObject.getPooledCon();
72 /** @todo should be preparedStatement: faster!! */
73 stmt = con.createStatement();
74 theStorageObject.executeUpdate(stmt,sql);
75 } catch (StorageObjectException e) {
76 throwStorageObjectException(e, "\n -- set produced failed");
77 } catch (SQLException e) {
78 throwStorageObjectException(e, "\n -- set produced failed");
80 theStorageObject.freeConnection(con,stmt);
86 * make openposting to newswire
89 public void newswire() throws StorageObjectException
91 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
93 theStorageObject.executeUpdate(sql);
94 } catch (StorageObjectException e) {
95 throwStorageObjectException(e, "\n -- newswire failed");
96 } catch (SQLException e) {
97 throwStorageObjectException(e, "\n -- newswire failed");
105 public void dettach(String cid,String mid) throws StorageObjectException
109 DatabaseContentToMedia.getInstance().delete(cid,mid);
110 } catch (Exception e){
111 throwStorageObjectException(e, "\n -- failed to get instance");
113 //set Content to unproduced
122 public void attach(String mid) throws StorageObjectException
125 //write media-id mid and content-id in table content_x_media
127 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
128 } catch(StorageObjectException e){
129 throwStorageObjectException(e, "attach: could not get the instance");
131 //set Content to unproduced
134 theLog.printError("EntityContent: attach without mid");
139 * overridden method getValue to include formatted date into every
143 public String getValue(String field)
145 String returnField = null;
148 if (field.equals("date_formatted"))
150 if (hasValueForField("date"))
151 returnField = StringUtil.webdbDate2readableDate(getValue("date"));
153 else if (field.equals("description_parsed"))
154 returnField = getDescriptionParsed();
155 else if (field.equals("description_sentence"))
156 returnField = getDescriptionSentence();
157 else if (field.equals("content_data_parsed"))
158 returnField = getContentDataParsed();
160 return super.getValue(field);
165 public TemplateModel get(java.lang.String key) throws TemplateModelException
168 if (_entCache.containsKey(key)) {
169 return (TemplateModel)_entCache.get(key);
171 if (key.equals("to_comments")) {
173 _entCache.put(key, getComments());
174 return (TemplateModel)_entCache.get(key);
175 } catch (Exception ex) {
176 theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
177 throw new TemplateModelException(ex.toString());
180 if (key.equals("to_media_images")) {
182 _entCache.put(key, getImagesForContent());
183 return (TemplateModel)_entCache.get(key);
185 catch (Exception ex) {
186 theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
187 throw new TemplateModelException(ex.toString());
190 if (key.equals("to_media_audio")) {
192 _entCache.put(key, getAudioForContent());
193 return (TemplateModel)_entCache.get(key);
195 catch (Exception ex) {
196 theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
197 throw new TemplateModelException(ex.toString());
200 if (key.equals("to_media_video")) {
202 _entCache.put(key, getVideoForContent());
203 return (TemplateModel)_entCache.get(key);
205 catch (Exception ex) {
206 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
207 throw new TemplateModelException(ex.toString());
210 if (key.equals("to_media_other")) {
212 _entCache.put(key, getOtherMediaForContent());
213 return (TemplateModel)_entCache.get(key);
215 catch (Exception ex) {
216 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
217 throw new TemplateModelException(ex.toString());
220 else if (key.equals("to_media_icon")) {
222 _entCache.put(key, getUploadedMediaForNewswire());
223 return (TemplateModel)_entCache.get(key);
225 catch (Exception ex) {
226 theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
227 throw new TemplateModelException(ex.toString());
230 else if (key.equals("to_topics")) {
233 DatabaseContentToTopics.getInstance().getTopics(this));
234 return (TemplateModel)_entCache.get(key);
236 catch (Exception ex) {
237 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
238 throw new TemplateModelException(ex.toString());
242 return new SimpleScalar(getValue(key));
253 * overridden method setValues to patch creator_main_url
255 public void setValues(HashMap theStringValues) {
256 if (theStringValues != null) {
257 if (theStringValues.containsKey("creator_main_url")){
258 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
259 theStringValues.remove("creator_main_url");
260 } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
261 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
265 super.setValues(theStringValues);
269 private String getContentDataParsed() {
270 String returnField = getValue("content_data");
271 if (returnField!=null & returnField.length()>0 ) {
272 returnField=StringUtil.deleteForbiddenTags(returnField);
273 //create http-links and email-links
274 if (getValue("is_html").equals("0")) {
275 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
276 mirconf_mailLinkName,mirconf_extLinkName,
277 mirconf_intLinkName);
279 returnField = StringUtil.decodeHTMLinTags(returnField);
284 private String getDescriptionSentence() {
285 String returnField = getValue("description");
286 if (returnField != null && returnField.length()>0) {
287 returnField = StringUtil.removeHTMLTags(returnField);
288 int endOfFirstSentence=StringUtil.findEndOfSentence(returnField,0);
289 if (endOfFirstSentence > 0){
290 returnField = returnField.substring(0,endOfFirstSentence);
296 private String getDescriptionParsed() {
297 String returnField = getValue("description");
298 if (returnField != null && returnField.length()>0) {
299 returnField = StringUtil.deleteForbiddenTags(returnField);
300 if (getValue("is_html").equals("0")) {
301 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
302 mirconf_mailLinkName,mirconf_extLinkName,
303 mirconf_intLinkName);
305 returnField = StringUtil.decodeHTMLinTags(returnField);
311 * fetches all the comments belonging to an article
313 * @return freemarker.template.SimpleList
315 private EntityList getComments() throws StorageObjectException {
316 return ((DatabaseContent)theStorageObject).getComments(this);
319 // @todo this needs to optimized. expensive SQL
320 private SimpleHash getUploadedMediaForNewswire()
321 throws StorageObjectException, TemplateModelException
323 // fetching/setting the images
324 // return to_media_icons
325 String tinyIcon = null, iconAlt = null;
326 MirMedia mediaHandler = null;
327 EntityUploadedMedia uploadedMedia;
329 SimpleHash returnHash = new SimpleHash();
331 EntityList upMediaEntityList =
332 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
333 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
335 for (int n=0; n < upMediaEntityList.size();n++) {
336 uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
337 mediaType = uploadedMedia.getMediaType();
339 mediaHandler = MediaHelper.getHandler( mediaType );
340 } catch (MirMediaException ex) {
341 throw new TemplateModelException(ex.toString());
343 //the "best" media type to show
344 if (mediaHandler.isVideo()) {
345 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
348 } else if (mediaHandler.isAudio()) {
349 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
351 } else if (tinyIcon == null && !mediaHandler.isImage()) {
352 tinyIcon = mediaHandler.getTinyIcon();
353 iconAlt = mediaHandler.getIconAlt();
357 //it only has image(s)
358 if (tinyIcon == null) {
359 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
362 // uploadedMedia Entity list is empty.
365 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
368 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
369 returnHash.put("icon_alt", iconAlt);
373 private boolean hasMedia() throws StorageObjectException
375 if (_hasMedia == null) {
377 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
379 return _hasMedia.booleanValue();
382 //######## @todo all of the following getBlahForContent should have
383 // and optimized version where LIMIT=1 sql for list view.
384 private EntityList getImagesForContent()
385 throws StorageObjectException, TemplateModelException
388 return DatabaseContentToMedia.getInstance().getImages(this);
393 private EntityList getAudioForContent()
394 throws StorageObjectException, TemplateModelException
397 return DatabaseContentToMedia.getInstance().getAudio(this) ;
402 private EntityList getVideoForContent()
403 throws StorageObjectException, TemplateModelException
406 return DatabaseContentToMedia.getInstance().getVideo(this) ;
411 private EntityList getOtherMediaForContent()
412 throws StorageObjectException, TemplateModelException
415 return DatabaseContentToMedia.getInstance().getOther(this);