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