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