1 package mircoders.entity.adapter;
4 import mir.entity.adapter.*;
5 import mircoders.storage.*;
7 public class CommentAdapterDefinition extends EntityAdapterDefinition {
8 private static CommentAdapterDefinition instance;
10 public static CommentAdapterDefinition getInstance() {
11 synchronized (CommentAdapterDefinition.class) {
12 if (instance == null) {
13 instance = new CommentAdapterDefinition();
19 public CommentAdapterDefinition() {
22 addDBDateField("creationdate", "webdb_create");
23 addCalculatedField("to_content", new CommentToContentField());
26 public EntityAdapter makeEntityAdapter(Entity anEntity) {
27 return new EntityAdapter(anEntity, this);
30 private class CommentToContentField implements CalculatedField {
31 public Object getValue(EntityAdapter anEntityAdapter) {
34 DatabaseContent.getInstance(),
35 "id="+anEntityAdapter.get("to_media"),
37 ContentAdapterDefinition.getInstance());
40 throw new RuntimeException(t.getMessage());
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");
54 // if (field.equals("date_formatted"))
56 // if (hasValueForField("date"))
57 // returnField = StringUtil.webdbDate2readableDate(getValue("date"));
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();
67 // if (key.equals("to_comments")) {
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());
76 // if (key.equals("to_media_images")) {
78 // _entCache.put(key, getImagesForContent());
79 // return (TemplateModel)_entCache.get(key);
81 // catch (Exception ex) {
82 // theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
83 // throw new TemplateModelException(ex.toString());
86 // if (key.equals("to_media_audio")) {
88 //// _entCache.put(key, getAudioForContent());
89 // return (TemplateModel)_entCache.get(key);
91 // catch (Exception ex) {
92 // theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
93 // throw new TemplateModelException(ex.toString());
96 // if (key.equals("to_media_video")) {
98 // _entCache.put(key, getVideoForContent());
99 /* return (TemplateModel)_entCache.get(key);
101 catch (Exception ex) {
102 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
103 throw new TemplateModelException(ex.toString());
106 if (key.equals("to_media_other")) {
108 _entCache.put(key, getOtherMediaForContent());
109 return (TemplateModel)_entCache.get(key);
111 catch (Exception ex) {
112 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
113 throw new TemplateModelException(ex.toString());
116 else if (key.equals("to_media_icon")) {
118 _entCache.put(key, getUploadedMediaForNewswire());
119 return (TemplateModel)_entCache.get(key);
121 catch (Exception ex) {
122 theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
123 throw new TemplateModelException(ex.toString());
126 else if (key.equals("to_topics")) {
129 DatabaseContentToTopics.getInstance().getTopics(this));
130 return (TemplateModel)_entCache.get(key);
132 catch (Exception ex) {
133 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
134 throw new TemplateModelException(ex.toString());
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);
153 returnField = StringUtil.decodeHTMLinTags(returnField);
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);
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);
179 returnField = StringUtil.decodeHTMLinTags(returnField);
185 private EntityList getComments() throws StorageObjectException {
186 return ((DatabaseContent)theStorageObject).getComments(this);
189 // @todo this needs to optimized. expensive SQL
190 private SimpleHash getUploadedMediaForNewswire()
191 throws StorageObjectException, TemplateModelException
193 // fetching/setting the images
194 // return to_media_icons
195 String tinyIcon = null, iconAlt = null;
196 MirMedia mediaHandler = null;
197 EntityUploadedMedia uploadedMedia;
199 SimpleHash returnHash = new SimpleHash();
201 EntityList upMediaEntityList =
202 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
203 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
205 for (int n=0; n < upMediaEntityList.size();n++) {
206 uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
207 mediaType = uploadedMedia.getMediaType();
209 mediaHandler = MediaHelper.getHandler( mediaType );
210 } catch (MirMediaException ex) {
211 throw new TemplateModelException(ex.toString());
213 //the "best" media type to show
214 if (mediaHandler.isVideo()) {
215 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
218 } else if (mediaHandler.isAudio()) {
219 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
221 } else if (tinyIcon == null && !mediaHandler.isImage()) {
222 tinyIcon = mediaHandler.getTinyIcon();
223 iconAlt = mediaHandler.getIconAlt();
227 //it only has image(s)
228 if (tinyIcon == null) {
229 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
232 // uploadedMedia Entity list is empty.
235 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
238 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
239 returnHash.put("icon_alt", iconAlt);
243 private boolean hasMedia() throws StorageObjectException
245 if (_hasMedia == null) {
247 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
249 return _hasMedia.booleanValue();
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
258 return DatabaseContentToMedia.getInstance().getImages(this);
263 private EntityList getAudioForContent()
264 throws StorageObjectException, TemplateModelException
267 return DatabaseContentToMedia.getInstance().getAudio(this) ;
272 private EntityList getVideoForContent()
273 throws StorageObjectException, TemplateModelException
276 return DatabaseContentToMedia.getInstance().getVideo(this) ;
281 private EntityList getOtherMediaForContent()
282 throws StorageObjectException, TemplateModelException
285 return DatabaseContentToMedia.getInstance().getOther(this);
304 // if (field.equals("date_formatted"))
306 // if (hasValueForField("webdb_create"))
307 // returnField = StringUtil.dateToReadableDate(getValue("webdb_create"));
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);