restructuring producer startpage
[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                 Connection con=null;Statement stmt=null;
60                 String value = (yesno) ? "1":"0";
61                 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
62                 try {
63                         con = theStorageObject.getPooledCon();
64                         /** @todo should be preparedStatement: faster!! */
65                         stmt = con.createStatement();
66                         theStorageObject.executeUpdate(stmt,sql);
67                 } catch (StorageObjectException e) {
68             throwStorageObjectException(e, "\n -- set produced failed");
69                 } catch (SQLException e) {
70             throwStorageObjectException(e, "\n -- set produced failed");
71                 } finally {
72                         theStorageObject.freeConnection(con,stmt);
73                 }
74         }
75
76
77  /**
78         * make openposting to newswire
79         */
80
81         public void newswire() throws StorageObjectException
82         {
83                 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
84                 try {
85                                 theStorageObject.executeUpdate(sql);
86                 } catch (StorageObjectException e) {
87             throwStorageObjectException(e, "\n -- newswire failed");
88                 } catch (SQLException e) {
89             throwStorageObjectException(e, "\n -- newswire failed");
90                 }
91         }
92
93
94  /**
95         * dettach from media
96         */
97         public void dettach(String cid,String mid) throws StorageObjectException
98         {
99                 if (mid!=null){
100                         try{
101                                 DatabaseContentToMedia.getInstance().delete(cid,mid);
102                         } catch (Exception e){
103                 throwStorageObjectException(e, "\n -- failed to get instance");
104                         }
105                         //set Content to unproduced
106                         setProduced(false);
107                 }
108         }
109
110  /**
111         * attach to media
112         */
113
114         public void attach(String mid) throws StorageObjectException
115         {
116                 if (mid!=null) {
117                         //write media-id mid and content-id in table content_x_media
118                         try{
119                                 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
120                         } catch(StorageObjectException e){
121                                 throwStorageObjectException(e, "attach: could not get the instance");
122                         }
123                         //set Content to unproduced
124                         setProduced(false);
125                 }       else {
126                         theLog.printError("EntityContent: attach without mid");
127                 }
128         }
129
130         /**
131          * overridden method getValue to include formatted date into every
132          * entityContent
133          */
134
135         public String getValue(String field)
136   {
137     String returnField = null;
138     if (field!=null)
139     {
140       if (field.equals("date_formatted"))
141       {
142                   if (hasValueForField("date"))
143         returnField = StringUtil.webdbDate2readableDate(getValue("date"));
144                 }
145       else if (field.equals("description_parsed"))
146         returnField = getDescriptionParsed();
147       else if (field.equals("content_data_parsed"))
148         returnField = getContentDataParsed();
149       else
150         return super.getValue(field);
151     }
152     return returnField;
153         }
154
155   public TemplateModel get(java.lang.String key) throws TemplateModelException
156   {
157     if (key!=null) {
158       if (key.equals("to_comments")) {
159         try {
160           return getComments();
161         }
162         catch (Exception ex) {
163           theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
164           throw new TemplateModelException(ex.toString());
165         }
166       }
167       if (key.equals("to_media")) {
168         try {
169           return getUploadedMediaForContent();
170         }
171         catch (Exception ex) {
172           theLog.printWarning("-- getUploadedMediaForContent: could not fetch data " + ex.toString());
173           throw new TemplateModelException(ex.toString());
174         }
175       }
176       else if (key.equals("to_media_icon")) {
177         try {
178           return getUploadedMediaForNewswire();
179         }
180         catch (Exception ex) {
181           theLog.printWarning("-- getUploadedMediaForContent: could not fetch data " + ex.toString());
182           throw new TemplateModelException(ex.toString());
183         }
184       }
185       else if (key.equals("to_media_list")) {
186         try {
187           return getUploadedMediaForContent();
188         }
189         catch (Exception ex) {
190           theLog.printWarning("-- getUploadedMediaForContent: could not fetch data " + ex.toString());
191           throw new TemplateModelException(ex.toString());
192         }
193       }
194       else if (key.equals("to_topics")) {
195         try {
196           HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(this));
197         }
198         catch (Exception ex) {
199           theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
200           throw new TemplateModelException(ex.toString());
201         }
202       }
203       else return new SimpleScalar(getValue(key));
204
205     }
206     return null;
207   }
208
209
210
211
212         /**
213          * overridden method setValues to patch creator_main_url
214          */
215         public void setValues(HashMap theStringValues) {
216                 if (theStringValues != null) {
217                         if (theStringValues.containsKey("creator_main_url")){
218                                 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
219                                         theStringValues.remove("creator_main_url");
220         } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
221           theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
222         }
223       }
224                 }
225                 super.setValues(theStringValues);
226         }
227
228
229   private String getContentDataParsed() {
230     String returnField = getValue("content_data");
231     if (returnField!=null & returnField.length()>0 ) {
232       returnField=StringUtil.deleteForbiddenTags(returnField);
233       //create http-links and email-links
234       if (getValue("is_html").equals("0")) {
235         returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
236                                             mirconf_mailLinkName,mirconf_extLinkName,
237                                             mirconf_intLinkName);
238       }
239       returnField = StringUtil.decodeHTMLinTags(returnField);
240     }
241     return returnField;
242   }
243
244
245   private String getDescriptionParsed() {
246     String returnField = getValue("description");
247     if (returnField != null && returnField.length()>0) {
248       returnField = StringUtil.deleteForbiddenTags(returnField);
249       if (getValue("is_html").equals("0")) {
250         returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
251                                             mirconf_mailLinkName,mirconf_extLinkName,
252                                             mirconf_intLinkName);
253       }
254       returnField = StringUtil.decodeHTMLinTags(returnField);
255     }
256     return returnField;
257   }
258
259         /**
260          * fetches all the comments belonging to an article
261          *
262          * @return freemarker.template.SimpleList
263          */
264         public SimpleList getComments() throws StorageObjectException {
265                 return ((DatabaseContent)theStorageObject).getComments(this);
266         }
267
268   private SimpleHash getUploadedMediaForNewswire()
269     throws StorageObjectException, TemplateModelException
270   {
271     // fetching/setting the images
272     // return to_media_icons
273     String        tinyIcon = null, iconAlt = null, mediaHandlerName = null;
274     MirMedia      mediaHandler = null;
275     EntityMedia   uploadedMedia;
276     Entity        mediaType;
277     Class         mediaHandlerClass;
278     SimpleHash    returnHash = new SimpleHash();
279
280     EntityList upMediaEntityList = DatabaseContentToMedia.getInstance().getUploadedMedia(this);
281     if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
282
283       for (int n=0; n < upMediaEntityList.size();n++) {
284         uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
285         mediaType = uploadedMedia.getMediaType();
286
287         //must of had a non-existant to_media_type entry..
288         //let's save our ass.
289         if (mediaType != null) {
290             mediaHandlerName = mediaType.getValue("classname");
291             try {
292               mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
293               mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
294             }
295             catch (Exception ex) {
296               throw new TemplateModelException(ex.toString());
297             }
298
299             //the "best" media type to show
300             if (mediaHandler.isVideo()) {
301               tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
302               iconAlt = "Video";
303               break;
304             } else if (mediaHandler.isAudio()) {
305               tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
306               iconAlt = "Audio";
307             } else if (tinyIcon == null && !mediaHandler.isImage()) {
308               tinyIcon = mediaHandler.getTinyIcon();
309               iconAlt = mediaHandler.getIconAlt();
310             }
311         }
312       }
313       //it only has image(s)
314       if (tinyIcon == null) {
315         tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
316         iconAlt = "Image";
317       }
318
319     // uploadedMedia Entity list is empty.
320     // we only have text
321     } else {
322       tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
323       iconAlt = "Text";
324     }
325
326     returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
327     returnHash.put("icon_alt", iconAlt);
328     return returnHash;
329   }
330
331   private SimpleHash getUploadedMediaForList()
332     throws StorageObjectException, TemplateModelException
333   {
334     SimpleHash returnHash = new SimpleHash();
335     //media to content
336     EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(this);
337     if (currentMediaList!=null && currentMediaList.getCount()>=1) {
338       SimpleList          mediaListAudio = new SimpleList();
339       SimpleList          mediaListImages = new SimpleList();
340       SimpleList          mediaListVideo = new SimpleList();
341       SimpleList          mediaListOther = new SimpleList();
342       EntityMedia         upMedia;
343       Entity              mediaType;
344       SimpleHash          upMediaSimpleHash;
345       Class               mediaHandlerClass=null,mediaStorageClass;
346       MirMedia            mediaHandler=null;
347       String              mediaHandlerName=null,mediaStorageName;
348       Database            mediaStorage=null;
349
350       for (int n=0; n < currentMediaList.size();n++) {
351         upMedia = (EntityMedia)currentMediaList.elementAt(n);
352         upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
353         mediaType = upMedia.getMediaType();
354         //must be a non-existant to_media_type entry..
355         if (mediaType != null) {
356           mediaHandlerName = mediaType.getValue("classname");
357           mediaStorageName = mediaType.getValue("tablename");
358           try {
359             mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
360             mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
361             mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
362             Method m = mediaStorageClass.getMethod("getInstance", null);
363             mediaStorage = (Database)m.invoke(null, null);
364           }
365           catch (Exception ex) {
366             throw new TemplateModelException(ex.toString());
367           }
368
369           //we most likely need further info
370           upMedia = (EntityMedia)mediaStorage.selectById(upMedia.getId());
371           try {
372            upMediaSimpleHash.put("url", mediaHandler.getListView(upMedia, mediaType));
373           }
374           catch (Exception ex) {
375             throw new TemplateModelException(ex.toString());
376           }
377
378           // putting media in the apropriate list container
379           if (upMedia.getValue("is_published").equals("1")) {
380             if (mediaHandler.isImage()) {
381               mediaListImages.add(upMediaSimpleHash);
382             } else if (mediaHandler.isAudio()) {
383               mediaListAudio.add(upMediaSimpleHash);
384             } else if (mediaHandler.isVideo()) {
385               mediaListVideo.add(upMediaSimpleHash);
386             } else {
387               mediaListOther.add(upMediaSimpleHash);
388             }
389           } //end if is_published
390         } //end if media_type != null
391       } //end for
392       returnHash.put("to_media_audio", mediaListAudio);
393       returnHash.put("to_media_images", mediaListImages);
394       returnHash.put("to_media_video", mediaListVideo);
395       returnHash.put("to_media_other", mediaListOther);
396     } //end if currentMediaList != null
397     return returnHash;
398  }
399
400   private SimpleList getUploadedMediaForContent()
401     throws StorageObjectException, TemplateModelException
402   {
403     SimpleList returnList=null;
404     EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(this);
405     if (currentMediaList!=null && currentMediaList.getCount()>=1)
406     {
407       Entity              upMedia,mediaType;
408       SimpleHash          upMediaSimpleHash;
409       Class               mediaHandlerClass,mediaStorageClass;
410       String              mediaStorageName,mediaHandlerName=null;
411       MirMedia            mediaHandler=null;
412       Database            mediaStorage=null;
413
414       returnList = new SimpleList();
415
416       for (int n=0; n < currentMediaList.size();n++) {
417         upMedia = currentMediaList.elementAt(n);
418         upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
419         mediaType = ((EntityMedia)upMedia).getMediaType();
420         //must be a non-existant to_media_type entry..
421         if (mediaType != null) {
422           mediaHandlerName = mediaType.getValue("classname");
423           mediaStorageName = mediaType.getValue("tablename");
424           try {
425             mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
426             mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
427             mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
428             Method m = mediaStorageClass.getMethod("getInstance", null);
429             mediaStorage = (Database)m.invoke(null, null);
430           }
431           catch (Exception ex) {
432             throw new TemplateModelException(ex.toString());
433           }
434
435           //we most likely need further info
436           upMedia = mediaStorage.selectById(upMedia.getId());
437           try {
438             upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
439           }
440           catch (Exception ex) {
441             throw new TemplateModelException(ex.toString());
442           }
443           upMediaSimpleHash.put("type",mediaType.getValue("classname"));
444           returnList.add(upMediaSimpleHash);
445         } //end if media_type != null
446       } //end for
447     } //end if currentMediaList != null
448     return returnList;
449   }
450
451 }