yet another rewrite of the producers...
[mir.git] / source / mircoders / entity / adapter / ContentAdapterDefinition.java
1 package mircoders.entity.adapter;
2
3 import mir.entity.*;
4 import mir.entity.adapter.*;
5 import mircoders.storage.*;
6 import mircoders.global.*;
7 import mircoders.localizer.*;
8
9 public class ContentAdapterDefinition extends EntityAdapterDefinition {
10   private static ContentAdapterDefinition instance;
11
12   public static ContentAdapterDefinition getInstance() {
13     synchronized (ContentAdapterDefinition.class) {
14       if (instance == null) {
15         instance = new ContentAdapterDefinition();
16       }
17       return instance;
18     }
19   }
20
21   public ContentAdapterDefinition() {
22     super();
23
24     addDateField("date", "date");
25     addCalculatedField("to_topics", new ContentToTopicsField());
26     addCalculatedField("to_comments", new ContentToCommentsField());
27     addCalculatedField("description_parsed", new FilteredField("description"));
28     addCalculatedField("content_data_parsed", new FilteredField("content_data"));
29 //      else if (field.equals(""))
30 //        returnField = getDescriptionParsed();
31 //      else if (field.equals("description_sentence"))
32 //        returnField = getDescriptionSentence();
33 //      else if (field.equals("content_data_parsed"))
34 //        returnField = getContentDataParsed();
35   }
36
37   private class FilteredField implements CalculatedField {
38     String fieldName;
39
40     public FilteredField(String aFieldName) {
41       fieldName = aFieldName;
42     }
43
44     public Object getValue(EntityAdapter anEntityAdapter) {
45       if (anEntityAdapter.get("is_html").equals("0")) {
46         return MirGlobal.localizer().producerTool().filterText((String) anEntityAdapter.get(fieldName));
47       }
48       else {
49         return anEntityAdapter.get(fieldName);
50       }
51     }
52   }
53
54   private class ContentToCommentsField implements CalculatedField {
55     public Object getValue(EntityAdapter anEntityAdapter) {
56       try {
57         return getRelation(
58                     DatabaseComment.getInstance(),
59                     "to_media="+anEntityAdapter.get("id"),
60                     "title",
61                     CommentAdapterDefinition.getInstance());
62       }
63       catch (Throwable t) {
64         throw new RuntimeException(t.getMessage());
65       }
66     }
67   }
68
69   private class ContentToTopicsField implements CalculatedField {
70     public Object getValue(EntityAdapter anEntityAdapter) {
71       try {
72         return getRelation(
73                     DatabaseTopics.getInstance(),
74                     "exists (select * from content_x_topic where content_id="+anEntityAdapter.get("id")+" and topic_id=id)",
75                     "title",
76                     TopicsAdapterDefinition.getInstance());
77       }
78       catch (Throwable t) {
79         throw new RuntimeException(t.getMessage());
80       }
81     }
82   }
83 }
84
85
86 //  String mirconf_extLinkName  = MirConfig.getProp("Producer.ExtLinkName");
87 //  String mirconf_intLinkName  = MirConfig.getProp("Producer.IntLinkName");
88 //  String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
89 //  String mirconf_imageRoot    = MirConfig.getProp("Producer.ImageRoot");
90
91
92 //      if (field.equals("date_formatted"))
93 //      {
94 //                if (hasValueForField("date"))
95 //              returnField = StringUtil.webdbDate2readableDate(getValue("date"));
96 //              }
97 //      else if (field.equals("description_parsed"))
98 //        returnField = getDescriptionParsed();
99 //      else if (field.equals("description_sentence"))
100 //        returnField = getDescriptionSentence();
101 //      else if (field.equals("content_data_parsed"))
102 //        returnField = getContentDataParsed();
103
104
105 //      if (key.equals("to_media_images")) {
106 //        try {
107 //          _entCache.put(key, getImagesForContent());
108 //          return (TemplateModel)_entCache.get(key);
109 //        }
110 //        catch (Exception ex) {
111 //          theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
112 //          throw new TemplateModelException(ex.toString());
113 //        }
114 //      }
115 //      if (key.equals("to_media_audio")) {
116 //        try {
117 ////          _entCache.put(key, getAudioForContent());
118 //          return (TemplateModel)_entCache.get(key);
119 //        }
120 //        catch (Exception ex) {
121 //          theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
122 //          throw new TemplateModelException(ex.toString());
123 //        }
124 //      }
125 //      if (key.equals("to_media_video")) {
126 //        try {
127 //          _entCache.put(key, getVideoForContent());
128 /*          return (TemplateModel)_entCache.get(key);
129         }
130         catch (Exception ex) {
131           theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
132           throw new TemplateModelException(ex.toString());
133         }
134       }
135       if (key.equals("to_media_other")) {
136         try {
137           _entCache.put(key, getOtherMediaForContent());
138           return (TemplateModel)_entCache.get(key);
139         }
140         catch (Exception ex) {
141           theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
142           throw new TemplateModelException(ex.toString());
143         }
144       }
145       else if (key.equals("to_media_icon")) {
146         try {
147           _entCache.put(key, getUploadedMediaForNewswire());
148           return (TemplateModel)_entCache.get(key);
149         }
150         catch (Exception ex) {
151           theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
152           throw new TemplateModelException(ex.toString());
153         }
154       }
155       else if (key.equals("to_topics")) {
156         try {
157           _entCache.put(key,
158                         DatabaseContentToTopics.getInstance().getTopics(this));
159           return (TemplateModel)_entCache.get(key);
160         }
161         catch (Exception ex) {
162           theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
163           throw new TemplateModelException(ex.toString());
164         }
165       }
166 */
167
168
169
170
171 /*
172   private String getContentDataParsed() {
173     String returnField = getValue("content_data");
174     if (returnField!=null & returnField.length()>0 ) {
175       returnField=StringUtil.deleteForbiddenTags(returnField);
176       //create http-links and email-links
177       if (getValue("is_html").equals("0")) {
178         returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
179                                             mirconf_mailLinkName,mirconf_extLinkName,
180                                             mirconf_intLinkName);
181       }
182       returnField = StringUtil.decodeHTMLinTags(returnField);
183     }
184     return returnField;
185   }
186
187   private String getDescriptionSentence() {
188     String returnField = getValue("description");
189     if (returnField != null && returnField.length()>0) {
190        returnField = StringUtil.removeHTMLTags(returnField);
191        int endOfFirstSentence=StringUtil.findEndOfSentence(returnField,0);
192        if (endOfFirstSentence > 0){
193          returnField = returnField.substring(0,endOfFirstSentence);
194        }
195     }
196     return returnField;
197   }
198
199   private String getDescriptionParsed() {
200     String returnField = getValue("description");
201     if (returnField != null && returnField.length()>0) {
202       returnField = StringUtil.deleteForbiddenTags(returnField);
203       if (getValue("is_html").equals("0")) {
204         returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
205                                             mirconf_mailLinkName,mirconf_extLinkName,
206                                             mirconf_intLinkName);
207       }
208       returnField = StringUtil.decodeHTMLinTags(returnField);
209     }
210     return returnField;
211   }
212
213
214         private EntityList getComments() throws StorageObjectException {
215                 return ((DatabaseContent)theStorageObject).getComments(this);
216         }
217
218   // @todo this needs to optimized. expensive SQL
219   private SimpleHash getUploadedMediaForNewswire()
220     throws StorageObjectException, TemplateModelException
221   {
222     // fetching/setting the images
223     // return to_media_icons
224     String        tinyIcon = null, iconAlt = null;
225     MirMedia      mediaHandler = null;
226     EntityUploadedMedia uploadedMedia;
227     Entity        mediaType;
228     SimpleHash    returnHash = new SimpleHash();
229
230     EntityList upMediaEntityList =
231                     DatabaseContentToMedia.getInstance().getUploadedMedia(this);
232     if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
233
234       for (int n=0; n < upMediaEntityList.size();n++) {
235         uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
236         mediaType = uploadedMedia.getMediaType();
237         try {
238           mediaHandler = MediaHelper.getHandler( mediaType );
239         } catch (MirMediaException ex) {
240           throw new TemplateModelException(ex.toString());
241         }
242         //the "best" media type to show
243         if (mediaHandler.isVideo()) {
244           tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
245           iconAlt = "Video";
246           break;
247         } else if (mediaHandler.isAudio()) {
248           tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
249           iconAlt = "Audio";
250         } else if (tinyIcon == null && !mediaHandler.isImage()) {
251           tinyIcon = mediaHandler.getTinyIcon();
252           iconAlt = mediaHandler.getIconAlt();
253         }
254
255       }
256       //it only has image(s)
257       if (tinyIcon == null) {
258         tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
259         iconAlt = "Image";
260       }
261     // uploadedMedia Entity list is empty.
262     // we only have text
263     } else {
264       tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
265       iconAlt = "Text";
266     }
267     returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
268     returnHash.put("icon_alt", iconAlt);
269     return returnHash;
270   }
271
272   private boolean hasMedia() throws StorageObjectException
273   {
274     if (_hasMedia == null) {
275       _hasMedia =
276         new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
277     }
278     return _hasMedia.booleanValue();
279   }
280
281   //######## @todo all of the following getBlahForContent should have
282   // and optimized version where LIMIT=1 sql for list view.
283   private EntityList getImagesForContent()
284     throws StorageObjectException, TemplateModelException
285   {
286     if (hasMedia())
287       return DatabaseContentToMedia.getInstance().getImages(this);
288     else
289       return null;
290   }
291
292   private EntityList getAudioForContent()
293     throws StorageObjectException, TemplateModelException
294   {
295     if (hasMedia())
296       return DatabaseContentToMedia.getInstance().getAudio(this) ;
297     else
298       return null;
299   }
300
301   private EntityList getVideoForContent()
302     throws StorageObjectException, TemplateModelException
303   {
304     if (hasMedia())
305       return DatabaseContentToMedia.getInstance().getVideo(this) ;
306     else
307       return null;
308   }
309
310   private EntityList getOtherMediaForContent()
311     throws StorageObjectException, TemplateModelException
312   {
313     if (hasMedia())
314       return DatabaseContentToMedia.getInstance().getOther(this);
315     else
316       return null;
317   }
318
319 }
320 */