1 package mircoders.entity.adapter;
4 import mir.entity.adapter.*;
5 import mircoders.storage.*;
6 import mircoders.global.*;
7 import mircoders.localizer.*;
9 public class ContentAdapterDefinition extends EntityAdapterDefinition {
10 private static ContentAdapterDefinition instance;
12 public static ContentAdapterDefinition getInstance() {
13 synchronized (ContentAdapterDefinition.class) {
14 if (instance == null) {
15 instance = new ContentAdapterDefinition();
21 public ContentAdapterDefinition() {
24 addDateField("date", "date");
25 addCalculatedField("to_topics", new ContentToTopicsField());
26 addCalculatedField("to_comments", new ContentToCommentsField());
27 addCalculatedField("description_parsed", new FilteredField("description"));
28 addCalculatedField("content_data_parsed", new FilteredField("content_data"));
29 // else if (field.equals(""))
30 // returnField = getDescriptionParsed();
31 // else if (field.equals("description_sentence"))
32 // returnField = getDescriptionSentence();
33 // else if (field.equals("content_data_parsed"))
34 // returnField = getContentDataParsed();
37 private class FilteredField implements CalculatedField {
40 public FilteredField(String aFieldName) {
41 fieldName = aFieldName;
44 public Object getValue(EntityAdapter anEntityAdapter) {
45 if (anEntityAdapter.get("is_html").equals("0")) {
46 return MirGlobal.localizer().producerTool().filterText((String) anEntityAdapter.get(fieldName));
49 return anEntityAdapter.get(fieldName);
54 private class ContentToCommentsField implements CalculatedField {
55 public Object getValue(EntityAdapter anEntityAdapter) {
58 DatabaseComment.getInstance(),
59 "to_media="+anEntityAdapter.get("id"),
61 CommentAdapterDefinition.getInstance());
64 throw new RuntimeException(t.getMessage());
69 private class ContentToTopicsField implements CalculatedField {
70 public Object getValue(EntityAdapter anEntityAdapter) {
73 DatabaseTopics.getInstance(),
74 "exists (select * from content_x_topic where content_id="+anEntityAdapter.get("id")+" and topic_id=id)",
76 TopicsAdapterDefinition.getInstance());
79 throw new RuntimeException(t.getMessage());
86 // String mirconf_extLinkName = MirConfig.getProp("Producer.ExtLinkName");
87 // String mirconf_intLinkName = MirConfig.getProp("Producer.IntLinkName");
88 // String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
89 // String mirconf_imageRoot = MirConfig.getProp("Producer.ImageRoot");
92 // if (field.equals("date_formatted"))
94 // if (hasValueForField("date"))
95 // returnField = StringUtil.webdbDate2readableDate(getValue("date"));
97 // else if (field.equals("description_parsed"))
98 // returnField = getDescriptionParsed();
99 // else if (field.equals("description_sentence"))
100 // returnField = getDescriptionSentence();
101 // else if (field.equals("content_data_parsed"))
102 // returnField = getContentDataParsed();
105 // if (key.equals("to_media_images")) {
107 // _entCache.put(key, getImagesForContent());
108 // return (TemplateModel)_entCache.get(key);
110 // catch (Exception ex) {
111 // theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
112 // throw new TemplateModelException(ex.toString());
115 // if (key.equals("to_media_audio")) {
117 //// _entCache.put(key, getAudioForContent());
118 // return (TemplateModel)_entCache.get(key);
120 // catch (Exception ex) {
121 // theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
122 // throw new TemplateModelException(ex.toString());
125 // if (key.equals("to_media_video")) {
127 // _entCache.put(key, getVideoForContent());
128 /* return (TemplateModel)_entCache.get(key);
130 catch (Exception ex) {
131 theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
132 throw new TemplateModelException(ex.toString());
135 if (key.equals("to_media_other")) {
137 _entCache.put(key, getOtherMediaForContent());
138 return (TemplateModel)_entCache.get(key);
140 catch (Exception ex) {
141 theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
142 throw new TemplateModelException(ex.toString());
145 else if (key.equals("to_media_icon")) {
147 _entCache.put(key, getUploadedMediaForNewswire());
148 return (TemplateModel)_entCache.get(key);
150 catch (Exception ex) {
151 theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
152 throw new TemplateModelException(ex.toString());
155 else if (key.equals("to_topics")) {
158 DatabaseContentToTopics.getInstance().getTopics(this));
159 return (TemplateModel)_entCache.get(key);
161 catch (Exception ex) {
162 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
163 throw new TemplateModelException(ex.toString());
172 private String getContentDataParsed() {
173 String returnField = getValue("content_data");
174 if (returnField!=null & returnField.length()>0 ) {
175 returnField=StringUtil.deleteForbiddenTags(returnField);
176 //create http-links and email-links
177 if (getValue("is_html").equals("0")) {
178 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
179 mirconf_mailLinkName,mirconf_extLinkName,
180 mirconf_intLinkName);
182 returnField = StringUtil.decodeHTMLinTags(returnField);
187 private String getDescriptionSentence() {
188 String returnField = getValue("description");
189 if (returnField != null && returnField.length()>0) {
190 returnField = StringUtil.removeHTMLTags(returnField);
191 int endOfFirstSentence=StringUtil.findEndOfSentence(returnField,0);
192 if (endOfFirstSentence > 0){
193 returnField = returnField.substring(0,endOfFirstSentence);
199 private String getDescriptionParsed() {
200 String returnField = getValue("description");
201 if (returnField != null && returnField.length()>0) {
202 returnField = StringUtil.deleteForbiddenTags(returnField);
203 if (getValue("is_html").equals("0")) {
204 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
205 mirconf_mailLinkName,mirconf_extLinkName,
206 mirconf_intLinkName);
208 returnField = StringUtil.decodeHTMLinTags(returnField);
214 private EntityList getComments() throws StorageObjectException {
215 return ((DatabaseContent)theStorageObject).getComments(this);
218 // @todo this needs to optimized. expensive SQL
219 private SimpleHash getUploadedMediaForNewswire()
220 throws StorageObjectException, TemplateModelException
222 // fetching/setting the images
223 // return to_media_icons
224 String tinyIcon = null, iconAlt = null;
225 MirMedia mediaHandler = null;
226 EntityUploadedMedia uploadedMedia;
228 SimpleHash returnHash = new SimpleHash();
230 EntityList upMediaEntityList =
231 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
232 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
234 for (int n=0; n < upMediaEntityList.size();n++) {
235 uploadedMedia = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
236 mediaType = uploadedMedia.getMediaType();
238 mediaHandler = MediaHelper.getHandler( mediaType );
239 } catch (MirMediaException ex) {
240 throw new TemplateModelException(ex.toString());
242 //the "best" media type to show
243 if (mediaHandler.isVideo()) {
244 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
247 } else if (mediaHandler.isAudio()) {
248 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
250 } else if (tinyIcon == null && !mediaHandler.isImage()) {
251 tinyIcon = mediaHandler.getTinyIcon();
252 iconAlt = mediaHandler.getIconAlt();
256 //it only has image(s)
257 if (tinyIcon == null) {
258 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
261 // uploadedMedia Entity list is empty.
264 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
267 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
268 returnHash.put("icon_alt", iconAlt);
272 private boolean hasMedia() throws StorageObjectException
274 if (_hasMedia == null) {
276 new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
278 return _hasMedia.booleanValue();
281 //######## @todo all of the following getBlahForContent should have
282 // and optimized version where LIMIT=1 sql for list view.
283 private EntityList getImagesForContent()
284 throws StorageObjectException, TemplateModelException
287 return DatabaseContentToMedia.getInstance().getImages(this);
292 private EntityList getAudioForContent()
293 throws StorageObjectException, TemplateModelException
296 return DatabaseContentToMedia.getInstance().getAudio(this) ;
301 private EntityList getVideoForContent()
302 throws StorageObjectException, TemplateModelException
305 return DatabaseContentToMedia.getInstance().getVideo(this) ;
310 private EntityList getOtherMediaForContent()
311 throws StorageObjectException, TemplateModelException
314 return DatabaseContentToMedia.getInstance().getOther(this);