47b7ef9b6a9e4ee08c215c96cc9c94de3bc05ab1
[mir.git] / source / mircoders / entity / EntityContent.java
1 package mircoders.entity;
2
3 import java.lang.*;
4 import java.io.*;
5 import java.util.*;
6 import java.sql.*;
7 import java.lang.reflect.*;
8
9 import freemarker.template.*;
10
11 import mir.entity.*;
12 import mir.misc.*;
13 import mir.media.*;
14 import mir.storage.*;
15
16 import mircoders.storage.*;
17
18 /**
19  * this class implements mapping of one line of the database table content
20  * to a java object
21  *
22  * @author RK
23  * @version 2001
24  */
25
26
27 public class EntityContent extends Entity
28 {
29
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");
34
35         // constructors
36
37         public EntityContent()
38         {
39                 super();
40     //content_data is now filed-type "text"
41                 //streamedInput = new ArrayList();
42                 //streamedInput.add("content_data");
43         }
44
45         public EntityContent(StorageObject theStorage) {
46                 this();
47                 setStorage(theStorage);
48         }
49
50         //
51         // methods
52
53  /**
54         * set is_produced flag for the article
55         */
56
57         public void setProduced(boolean yesno) throws StorageObjectException
58         {
59                 String value = (yesno) ? "1":"0";
60                 if (value.equals( getValue("is_produced") )) return;
61
62     Connection con=null;Statement stmt=null;
63     String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
64                 try {
65                         con = theStorageObject.getPooledCon();
66                         /** @todo should be preparedStatement: faster!! */
67                         stmt = con.createStatement();
68                         theStorageObject.executeUpdate(stmt,sql);
69                 } catch (StorageObjectException e) {
70             throwStorageObjectException(e, "\n -- set produced failed");
71                 } catch (SQLException e) {
72             throwStorageObjectException(e, "\n -- set produced failed");
73                 } finally {
74                         theStorageObject.freeConnection(con,stmt);
75                 }
76         }
77
78
79  /**
80         * make openposting to newswire
81         */
82
83         public void newswire() throws StorageObjectException
84         {
85                 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
86                 try {
87                                 theStorageObject.executeUpdate(sql);
88                 } catch (StorageObjectException e) {
89             throwStorageObjectException(e, "\n -- newswire failed");
90                 } catch (SQLException e) {
91             throwStorageObjectException(e, "\n -- newswire failed");
92                 }
93         }
94
95
96  /**
97         * dettach from media
98         */
99         public void dettach(String cid,String mid) throws StorageObjectException
100         {
101                 if (mid!=null){
102                         try{
103                                 DatabaseContentToMedia.getInstance().delete(cid,mid);
104                         } catch (Exception e){
105                 throwStorageObjectException(e, "\n -- failed to get instance");
106                         }
107                         //set Content to unproduced
108                         setProduced(false);
109                 }
110         }
111
112  /**
113         * attach to media
114         */
115
116         public void attach(String mid) throws StorageObjectException
117         {
118                 if (mid!=null) {
119                         //write media-id mid and content-id in table content_x_media
120                         try{
121                                 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
122                         } catch(StorageObjectException e){
123                                 throwStorageObjectException(e, "attach: could not get the instance");
124                         }
125                         //set Content to unproduced
126                         setProduced(false);
127                 }       else {
128                         theLog.printError("EntityContent: attach without mid");
129                 }
130         }
131
132         /**
133          * overridden method getValue to include formatted date into every
134          * entityContent
135          */
136
137         public String getValue(String field)
138   {
139     String returnField = null;
140     if (field!=null)
141     {
142       if (field.equals("date_formatted"))
143       {
144                   if (hasValueForField("date"))
145         returnField = StringUtil.webdbDate2readableDate(getValue("date"));
146                 }
147       else if (field.equals("description_parsed"))
148         returnField = getDescriptionParsed();
149       else if (field.equals("content_data_parsed"))
150         returnField = getContentDataParsed();
151       else
152         return super.getValue(field);
153     }
154     return returnField;
155         }
156
157   public TemplateModel get(java.lang.String key) throws TemplateModelException
158   {
159     if (key!=null) {
160       if (key.equals("to_comments")) {
161         try {
162           return getComments();
163         }
164         catch (Exception ex) {
165           theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
166           throw new TemplateModelException(ex.toString());
167         }
168       }
169       if (key.equals("to_media_images")) {
170         try {
171           return getImagesForContent();
172         }
173         catch (Exception ex) {
174           theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
175           throw new TemplateModelException(ex.toString());
176         }
177       }
178       if (key.equals("to_media_audio")) {
179         try {
180           return getAudioForContent();
181         }
182         catch (Exception ex) {
183           theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
184           throw new TemplateModelException(ex.toString());
185         }
186       }
187       if (key.equals("to_media_video")) {
188         try {
189           return getVideoForContent();
190         }
191         catch (Exception ex) {
192           theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
193           throw new TemplateModelException(ex.toString());
194         }
195       }
196       if (key.equals("to_media_other")) {
197         try {
198           return getOtherMediaForContent();
199         }
200         catch (Exception ex) {
201           theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
202           throw new TemplateModelException(ex.toString());
203         }
204       }
205       else if (key.equals("to_media_icon")) {
206         try {
207           return getUploadedMediaForNewswire();
208         }
209         catch (Exception ex) {
210           theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
211           throw new TemplateModelException(ex.toString());
212         }
213       }
214       else if (key.equals("to_topics")) {
215         try {
216           DatabaseContentToTopics.getInstance().getTopics(this);
217         }
218         catch (Exception ex) {
219           theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
220           throw new TemplateModelException(ex.toString());
221         }
222       }
223       else {
224         return new SimpleScalar(getValue(key));
225       }
226
227     }
228     return null;
229   }
230
231
232
233
234         /**
235          * overridden method setValues to patch creator_main_url
236          */
237         public void setValues(HashMap theStringValues) {
238                 if (theStringValues != null) {
239                         if (theStringValues.containsKey("creator_main_url")){
240                                 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
241                                         theStringValues.remove("creator_main_url");
242         } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
243           theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
244         }
245       }
246                 }
247                 super.setValues(theStringValues);
248         }
249
250
251   private String getContentDataParsed() {
252     String returnField = getValue("content_data");
253     if (returnField!=null & returnField.length()>0 ) {
254       returnField=StringUtil.deleteForbiddenTags(returnField);
255       //create http-links and email-links
256       if (getValue("is_html").equals("0")) {
257         returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
258                                             mirconf_mailLinkName,mirconf_extLinkName,
259                                             mirconf_intLinkName);
260       }
261       returnField = StringUtil.decodeHTMLinTags(returnField);
262     }
263     return returnField;
264   }
265
266
267   private String getDescriptionParsed() {
268     String returnField = getValue("description");
269     if (returnField != null && returnField.length()>0) {
270       returnField = StringUtil.deleteForbiddenTags(returnField);
271       if (getValue("is_html").equals("0")) {
272         returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
273                                             mirconf_mailLinkName,mirconf_extLinkName,
274                                             mirconf_intLinkName);
275       }
276       returnField = StringUtil.decodeHTMLinTags(returnField);
277     }
278     return returnField;
279   }
280
281         /**
282          * fetches all the comments belonging to an article
283          *
284          * @return freemarker.template.SimpleList
285          */
286         private SimpleList getComments() throws StorageObjectException {
287                 return ((DatabaseContent)theStorageObject).getComments(this);
288         }
289
290   // @todo this needs to optimized. expensive SQL
291   private SimpleHash getUploadedMediaForNewswire()
292     throws StorageObjectException, TemplateModelException
293   {
294     // fetching/setting the images
295     // return to_media_icons
296     String        tinyIcon = null, iconAlt = null;
297     MirMedia      mediaHandler = null;
298     EntityUploadedMedia uploadedMedia;
299     Entity        mediaType;
300     SimpleHash    returnHash = new SimpleHash();
301
302     EntityList upMediaEntityList =
303                     DatabaseContentToMedia.getInstance().getUploadedMedia(this);
304     if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
305
306       for (int n=0; n < upMediaEntityList.size();n++) {
307         uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
308         mediaType = uploadedMedia.getMediaType();
309         try {
310           mediaHandler = MediaHelper.getHandler( mediaType );
311         } catch (MirMediaException ex) {
312           throw new TemplateModelException(ex.toString());
313         }
314         //the "best" media type to show
315         if (mediaHandler.isVideo()) {
316           tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
317           iconAlt = "Video";
318           break;
319         } else if (mediaHandler.isAudio()) {
320           tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
321           iconAlt = "Audio";
322         } else if (tinyIcon == null && !mediaHandler.isImage()) {
323           tinyIcon = mediaHandler.getTinyIcon();
324           iconAlt = mediaHandler.getIconAlt();
325         }
326       }
327       //it only has image(s)
328       if (tinyIcon == null) {
329         tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
330         iconAlt = "Image";
331       }
332     // uploadedMedia Entity list is empty.
333     // we only have text
334     } else {
335       tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
336       iconAlt = "Text";
337     }
338     returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
339     returnHash.put("icon_alt", iconAlt);
340     return returnHash;
341   }
342
343   //######## @todo all of the following getBlahForContent should have
344   // and optimized version where LIMIT=1 sql for list view.
345   private EntityList getImagesForContent()
346     throws StorageObjectException, TemplateModelException
347   {
348     return DatabaseContentToMedia.getInstance().getImages(this);
349   }
350
351   private EntityList getAudioForContent()
352     throws StorageObjectException, TemplateModelException
353   {
354     return DatabaseContentToMedia.getInstance().getAudio(this) ;
355   }
356
357   private SimpleList getVideoForContent()
358     throws StorageObjectException, TemplateModelException
359   {
360     return DatabaseContentToMedia.getInstance().getVideo(this) ;
361   }
362
363   private SimpleList getOtherMediaForContent()
364     throws StorageObjectException, TemplateModelException
365   {
366     return HTMLTemplateProcessor.makeSimpleList(
367              DatabaseContentToMedia.getInstance().getOther(this) );
368   }
369
370 }