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 addDBDateField("changedate", "webdb_lastchange");
28 // String mirconf_extLinkName = MirConfig.getProp("Producer.ExtLinkName");
29 // String mirconf_intLinkName = MirConfig.getProp("Producer.IntLinkName");
30 // String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
31 // String mirconf_imageRoot = MirConfig.getProp("Producer.ImageRoot");
34 // if (field.equals("date_formatted"))
36 // if (hasValueForField("date"))
37 // returnField = StringUtil.webdbDate2readableDate(getValue("date"));
39 // else if (field.equals("description_parsed"))
40 // returnField = getDescriptionParsed();
41 // else if (field.equals("description_sentence"))
42 // returnField = getDescriptionSentence();
43 // else if (field.equals("content_data_parsed"))
44 // returnField = getContentDataParsed();
47 // if (key.equals("to_comments")) {
49 // _entCache.put(key, getComments());
50 // return (TemplateModel)_entCache.get(key);
51 // } catch (Exception ex) {
52 // theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
53 // throw new TemplateModelException(ex.toString());
56 // if (key.equals("to_media_images")) {
58 // _entCache.put(key, getImagesForContent());
59 // return (TemplateModel)_entCache.get(key);
61 // catch (Exception ex) {
62 // theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
63 // throw new TemplateModelException(ex.toString());
66 // if (key.equals("to_media_audio")) {
68 //// _entCache.put(key, getAudioForContent());
69 // return (TemplateModel)_entCache.get(key);
71 // catch (Exception ex) {
72 // theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
73 // throw new TemplateModelException(ex.toString());
76 // if (key.equals("to_media_video")) {
78 // _entCache.put(key, getVideoForContent());
79 /* return (TemplateModel)_entCache.get(key);
81 catch (Exception ex) {
82 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
83 throw new TemplateModelException(ex.toString());
86 if (key.equals("to_media_other")) {
88 _entCache.put(key, getOtherMediaForContent());
89 return (TemplateModel)_entCache.get(key);
91 catch (Exception ex) {
92 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
93 throw new TemplateModelException(ex.toString());
96 else if (key.equals("to_media_icon")) {
98 _entCache.put(key, getUploadedMediaForNewswire());
99 return (TemplateModel)_entCache.get(key);
101 catch (Exception ex) {
102 theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
103 throw new TemplateModelException(ex.toString());
106 else if (key.equals("to_topics")) {
109 DatabaseContentToTopics.getInstance().getTopics(this));
110 return (TemplateModel)_entCache.get(key);
112 catch (Exception ex) {
113 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
114 throw new TemplateModelException(ex.toString());
123 private String getContentDataParsed() {
124 String returnField = getValue("content_data");
125 if (returnField!=null & returnField.length()>0 ) {
126 returnField=StringUtil.deleteForbiddenTags(returnField);
127 //create http-links and email-links
128 if (getValue("is_html").equals("0")) {
129 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
130 mirconf_mailLinkName,mirconf_extLinkName,
131 mirconf_intLinkName);
133 returnField = StringUtil.decodeHTMLinTags(returnField);
138 private String getDescriptionSentence() {
139 String returnField = getValue("description");
140 if (returnField != null && returnField.length()>0) {
141 returnField = StringUtil.removeHTMLTags(returnField);
142 int endOfFirstSentence=StringUtil.findEndOfSentence(returnField,0);
143 if (endOfFirstSentence > 0){
144 returnField = returnField.substring(0,endOfFirstSentence);
150 private String getDescriptionParsed() {
151 String returnField = getValue("description");
152 if (returnField != null && returnField.length()>0) {
153 returnField = StringUtil.deleteForbiddenTags(returnField);
154 if (getValue("is_html").equals("0")) {
155 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
156 mirconf_mailLinkName,mirconf_extLinkName,
157 mirconf_intLinkName);
159 returnField = StringUtil.decodeHTMLinTags(returnField);
165 private EntityList getComments() throws StorageObjectException {
166 return ((DatabaseContent)theStorageObject).getComments(this);
169 // @todo this needs to optimized. expensive SQL
170 private SimpleHash getUploadedMediaForNewswire()
171 throws StorageObjectException, TemplateModelException
173 // fetching/setting the images
174 // return to_media_icons
175 String tinyIcon = null, iconAlt = null;
176 MirMedia mediaHandler = null;
177 EntityUploadedMedia uploadedMedia;
179 SimpleHash returnHash = new SimpleHash();
181 EntityList upMediaEntityList =
182 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
183 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
185 for (int n=0; n < upMediaEntityList.size();n++) {
186 uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
187 mediaType = uploadedMedia.getMediaType();
189 mediaHandler = MediaHelper.getHandler( mediaType );
190 } catch (MirMediaException ex) {
191 throw new TemplateModelException(ex.toString());
193 //the "best" media type to show
194 if (mediaHandler.isVideo()) {
195 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
198 } else if (mediaHandler.isAudio()) {
199 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
201 } else if (tinyIcon == null && !mediaHandler.isImage()) {
202 tinyIcon = mediaHandler.getTinyIcon();
203 iconAlt = mediaHandler.getIconAlt();
207 //it only has image(s)
208 if (tinyIcon == null) {
209 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
212 // uploadedMedia Entity list is empty.
215 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
218 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
219 returnHash.put("icon_alt", iconAlt);
223 private boolean hasMedia() throws StorageObjectException
225 if (_hasMedia == null) {
227 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
229 return _hasMedia.booleanValue();
232 //######## @todo all of the following getBlahForContent should have
233 // and optimized version where LIMIT=1 sql for list view.
234 private EntityList getImagesForContent()
235 throws StorageObjectException, TemplateModelException
238 return DatabaseContentToMedia.getInstance().getImages(this);
243 private EntityList getAudioForContent()
244 throws StorageObjectException, TemplateModelException
247 return DatabaseContentToMedia.getInstance().getAudio(this) ;
252 private EntityList getVideoForContent()
253 throws StorageObjectException, TemplateModelException
256 return DatabaseContentToMedia.getInstance().getVideo(this) ;
261 private EntityList getOtherMediaForContent()
262 throws StorageObjectException, TemplateModelException
265 return DatabaseContentToMedia.getInstance().getOther(this);
284 // if (field.equals("date_formatted"))
286 // if (hasValueForField("webdb_create"))
287 // returnField = StringUtil.dateToReadableDate(getValue("webdb_create"));
290 // else if (field.equals("description_parsed")) {
291 // /** @todo the config stuff should be moved to StringUtil */
292 // String extLinkName = MirConfig.getProp("Producer.ExtLinkName");
293 // String intLinkName = MirConfig.getProp("Producer.IntLinkName");
294 // String mailLinkName = MirConfig.getProp("Producer.MailLinkName");
295 // String imageRoot = MirConfig.getProp("Producer.ImageRoot");
296 // returnField = StringUtil.createHTML(getValue("description"),imageRoot,mailLinkName,extLinkName,intLinkName);