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("content_data_parsed"))
156 returnField = getContentDataParsed();
158 return super.getValue(field);
163 public TemplateModel get(java.lang.String key) throws TemplateModelException
166 if (_entCache.containsKey(key)) {
167 return (TemplateModel)_entCache.get(key);
169 if (key.equals("to_comments")) {
171 _entCache.put(key, getComments());
172 return (TemplateModel)_entCache.get(key);
173 } catch (Exception ex) {
174 theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
175 throw new TemplateModelException(ex.toString());
178 if (key.equals("to_media_images")) {
180 _entCache.put(key, getImagesForContent());
181 return (TemplateModel)_entCache.get(key);
183 catch (Exception ex) {
184 theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
185 throw new TemplateModelException(ex.toString());
188 if (key.equals("to_media_audio")) {
190 _entCache.put(key, getAudioForContent());
191 return (TemplateModel)_entCache.get(key);
193 catch (Exception ex) {
194 theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
195 throw new TemplateModelException(ex.toString());
198 if (key.equals("to_media_video")) {
200 _entCache.put(key, getVideoForContent());
201 return (TemplateModel)_entCache.get(key);
203 catch (Exception ex) {
204 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
205 throw new TemplateModelException(ex.toString());
208 if (key.equals("to_media_other")) {
210 _entCache.put(key, getOtherMediaForContent());
211 return (TemplateModel)_entCache.get(key);
213 catch (Exception ex) {
214 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
215 throw new TemplateModelException(ex.toString());
218 else if (key.equals("to_media_icon")) {
220 _entCache.put(key, getUploadedMediaForNewswire());
221 return (TemplateModel)_entCache.get(key);
223 catch (Exception ex) {
224 theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
225 throw new TemplateModelException(ex.toString());
228 else if (key.equals("to_topics")) {
231 DatabaseContentToTopics.getInstance().getTopics(this));
232 return (TemplateModel)_entCache.get(key);
234 catch (Exception ex) {
235 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
236 throw new TemplateModelException(ex.toString());
240 return new SimpleScalar(getValue(key));
251 * overridden method setValues to patch creator_main_url
253 public void setValues(HashMap theStringValues) {
254 if (theStringValues != null) {
255 if (theStringValues.containsKey("creator_main_url")){
256 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
257 theStringValues.remove("creator_main_url");
258 } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
259 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
263 super.setValues(theStringValues);
267 private String getContentDataParsed() {
268 String returnField = getValue("content_data");
269 if (returnField!=null & returnField.length()>0 ) {
270 returnField=StringUtil.deleteForbiddenTags(returnField);
271 //create http-links and email-links
272 if (getValue("is_html").equals("0")) {
273 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
274 mirconf_mailLinkName,mirconf_extLinkName,
275 mirconf_intLinkName);
277 returnField = StringUtil.decodeHTMLinTags(returnField);
283 private String getDescriptionParsed() {
284 String returnField = getValue("description");
285 if (returnField != null && returnField.length()>0) {
286 returnField = StringUtil.deleteForbiddenTags(returnField);
287 if (getValue("is_html").equals("0")) {
288 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
289 mirconf_mailLinkName,mirconf_extLinkName,
290 mirconf_intLinkName);
292 returnField = StringUtil.decodeHTMLinTags(returnField);
298 * fetches all the comments belonging to an article
300 * @return freemarker.template.SimpleList
302 private EntityList getComments() throws StorageObjectException {
303 return ((DatabaseContent)theStorageObject).getComments(this);
306 // @todo this needs to optimized. expensive SQL
307 private SimpleHash getUploadedMediaForNewswire()
308 throws StorageObjectException, TemplateModelException
310 // fetching/setting the images
311 // return to_media_icons
312 String tinyIcon = null, iconAlt = null;
313 MirMedia mediaHandler = null;
314 EntityUploadedMedia uploadedMedia;
316 SimpleHash returnHash = new SimpleHash();
318 EntityList upMediaEntityList =
319 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
320 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
322 for (int n=0; n < upMediaEntityList.size();n++) {
323 uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
324 mediaType = uploadedMedia.getMediaType();
326 mediaHandler = MediaHelper.getHandler( mediaType );
327 } catch (MirMediaException ex) {
328 throw new TemplateModelException(ex.toString());
330 //the "best" media type to show
331 if (mediaHandler.isVideo()) {
332 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
335 } else if (mediaHandler.isAudio()) {
336 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
338 } else if (tinyIcon == null && !mediaHandler.isImage()) {
339 tinyIcon = mediaHandler.getTinyIcon();
340 iconAlt = mediaHandler.getIconAlt();
344 //it only has image(s)
345 if (tinyIcon == null) {
346 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
349 // uploadedMedia Entity list is empty.
352 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
355 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
356 returnHash.put("icon_alt", iconAlt);
360 private boolean hasMedia() throws StorageObjectException
362 if (_hasMedia == null) {
364 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
366 return _hasMedia.booleanValue();
369 //######## @todo all of the following getBlahForContent should have
370 // and optimized version where LIMIT=1 sql for list view.
371 private EntityList getImagesForContent()
372 throws StorageObjectException, TemplateModelException
375 return DatabaseContentToMedia.getInstance().getImages(this);
380 private EntityList getAudioForContent()
381 throws StorageObjectException, TemplateModelException
384 return DatabaseContentToMedia.getInstance().getAudio(this) ;
389 private EntityList getVideoForContent()
390 throws StorageObjectException, TemplateModelException
393 return DatabaseContentToMedia.getInstance().getVideo(this) ;
398 private EntityList getOtherMediaForContent()
399 throws StorageObjectException, TemplateModelException
402 return DatabaseContentToMedia.getInstance().getOther(this);