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");
25 public EntityAdapter makeEntityAdapter(Entity anEntity) {
26 return new EntityAdapter(anEntity, this);
29 private class CommentToContentField implements CalculatedField {
30 public Object getValue(EntityAdapter anEntityAdapter) {
33 DatabaseContent.getInstance(),
34 "id="+anEntityAdapter.get("to_media"),
36 ContentAdapterDefinition.getInstance());
39 throw new RuntimeException(t.getMessage());
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");
53 // if (field.equals("date_formatted"))
55 // if (hasValueForField("date"))
56 // returnField = StringUtil.webdbDate2readableDate(getValue("date"));
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();
66 // if (key.equals("to_comments")) {
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());
75 // if (key.equals("to_media_images")) {
77 // _entCache.put(key, getImagesForContent());
78 // return (TemplateModel)_entCache.get(key);
80 // catch (Exception ex) {
81 // theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
82 // throw new TemplateModelException(ex.toString());
85 // if (key.equals("to_media_audio")) {
87 //// _entCache.put(key, getAudioForContent());
88 // return (TemplateModel)_entCache.get(key);
90 // catch (Exception ex) {
91 // theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
92 // throw new TemplateModelException(ex.toString());
95 // if (key.equals("to_media_video")) {
97 // _entCache.put(key, getVideoForContent());
98 /* return (TemplateModel)_entCache.get(key);
100 catch (Exception ex) {
101 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
102 throw new TemplateModelException(ex.toString());
105 if (key.equals("to_media_other")) {
107 _entCache.put(key, getOtherMediaForContent());
108 return (TemplateModel)_entCache.get(key);
110 catch (Exception ex) {
111 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
112 throw new TemplateModelException(ex.toString());
115 else if (key.equals("to_media_icon")) {
117 _entCache.put(key, getUploadedMediaForNewswire());
118 return (TemplateModel)_entCache.get(key);
120 catch (Exception ex) {
121 theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
122 throw new TemplateModelException(ex.toString());
125 else if (key.equals("to_topics")) {
128 DatabaseContentToTopics.getInstance().getTopics(this));
129 return (TemplateModel)_entCache.get(key);
131 catch (Exception ex) {
132 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
133 throw new TemplateModelException(ex.toString());
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);
152 returnField = StringUtil.decodeHTMLinTags(returnField);
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);
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);
178 returnField = StringUtil.decodeHTMLinTags(returnField);
184 private EntityList getComments() throws StorageObjectException {
185 return ((DatabaseContent)theStorageObject).getComments(this);
188 // @todo this needs to optimized. expensive SQL
189 private SimpleHash getUploadedMediaForNewswire()
190 throws StorageObjectException, TemplateModelException
192 // fetching/setting the images
193 // return to_media_icons
194 String tinyIcon = null, iconAlt = null;
195 MirMedia mediaHandler = null;
196 EntityUploadedMedia uploadedMedia;
198 SimpleHash returnHash = new SimpleHash();
200 EntityList upMediaEntityList =
201 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
202 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
204 for (int n=0; n < upMediaEntityList.size();n++) {
205 uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
206 mediaType = uploadedMedia.getMediaType();
208 mediaHandler = MediaHelper.getHandler( mediaType );
209 } catch (MirMediaException ex) {
210 throw new TemplateModelException(ex.toString());
212 //the "best" media type to show
213 if (mediaHandler.isVideo()) {
214 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
217 } else if (mediaHandler.isAudio()) {
218 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
220 } else if (tinyIcon == null && !mediaHandler.isImage()) {
221 tinyIcon = mediaHandler.getTinyIcon();
222 iconAlt = mediaHandler.getIconAlt();
226 //it only has image(s)
227 if (tinyIcon == null) {
228 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
231 // uploadedMedia Entity list is empty.
234 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
237 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
238 returnHash.put("icon_alt", iconAlt);
242 private boolean hasMedia() throws StorageObjectException
244 if (_hasMedia == null) {
246 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
248 return _hasMedia.booleanValue();
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
257 return DatabaseContentToMedia.getInstance().getImages(this);
262 private EntityList getAudioForContent()
263 throws StorageObjectException, TemplateModelException
266 return DatabaseContentToMedia.getInstance().getAudio(this) ;
271 private EntityList getVideoForContent()
272 throws StorageObjectException, TemplateModelException
275 return DatabaseContentToMedia.getInstance().getVideo(this) ;
280 private EntityList getOtherMediaForContent()
281 throws StorageObjectException, TemplateModelException
284 return DatabaseContentToMedia.getInstance().getOther(this);
303 // if (field.equals("date_formatted"))
305 // if (hasValueForField("webdb_create"))
306 // returnField = StringUtil.dateToReadableDate(getValue("webdb_create"));
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);