1 package mircoders.entity;
7 import java.lang.reflect.*;
9 import freemarker.template.*;
16 import mircoders.storage.*;
19 * this class implements mapping of one line of the database table content
27 public class EntityContent extends Entity
30 String mirconf_extLinkName = MirConfig.getProp("Producer.ExtLinkName");
31 String mirconf_intLinkName = MirConfig.getProp("Producer.IntLinkName");
32 String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
33 String mirconf_imageRoot = MirConfig.getProp("Producer.ImageRoot");
37 public EntityContent()
40 //content_data is now filed-type "text"
41 //streamedInput = new ArrayList();
42 //streamedInput.add("content_data");
45 public EntityContent(StorageObject theStorage) {
47 setStorage(theStorage);
54 * set is_produced flag for the article
57 public void setProduced(boolean yesno) throws StorageObjectException
59 Connection con=null;Statement stmt=null;
60 String value = (yesno) ? "1":"0";
61 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
63 con = theStorageObject.getPooledCon();
64 /** @todo should be preparedStatement: faster!! */
65 stmt = con.createStatement();
66 theStorageObject.executeUpdate(stmt,sql);
67 } catch (StorageObjectException e) {
68 throwStorageObjectException(e, "\n -- set produced failed");
69 } catch (SQLException e) {
70 throwStorageObjectException(e, "\n -- set produced failed");
72 theStorageObject.freeConnection(con,stmt);
78 * make openposting to newswire
81 public void newswire() throws StorageObjectException
83 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
85 theStorageObject.executeUpdate(sql);
86 } catch (StorageObjectException e) {
87 throwStorageObjectException(e, "\n -- newswire failed");
88 } catch (SQLException e) {
89 throwStorageObjectException(e, "\n -- newswire failed");
97 public void dettach(String cid,String mid) throws StorageObjectException
101 DatabaseContentToMedia.getInstance().delete(cid,mid);
102 } catch (Exception e){
103 throwStorageObjectException(e, "\n -- failed to get instance");
105 //set Content to unproduced
114 public void attach(String mid) throws StorageObjectException
117 //write media-id mid and content-id in table content_x_media
119 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
120 } catch(StorageObjectException e){
121 throwStorageObjectException(e, "attach: could not get the instance");
123 //set Content to unproduced
126 theLog.printError("EntityContent: attach without mid");
131 * overridden method getValue to include formatted date into every
135 public String getValue(String field)
137 String returnField = null;
140 if (field.equals("date_formatted"))
142 if (hasValueForField("date"))
143 returnField = StringUtil.webdbDate2readableDate(getValue("date"));
145 else if (field.equals("description_parsed"))
146 returnField = getDescriptionParsed();
147 else if (field.equals("content_data_parsed"))
148 returnField = getContentDataParsed();
150 return super.getValue(field);
155 public TemplateModel get(java.lang.String key) throws TemplateModelException
158 if (key.equals("to_comments")) {
160 return getComments();
162 catch (Exception ex) {
163 theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
164 throw new TemplateModelException(ex.toString());
167 if (key.equals("to_media")) {
169 return getUploadedMediaForContent();
171 catch (Exception ex) {
172 theLog.printWarning("-- getUploadedMediaForContent: could not fetch data " + ex.toString());
173 throw new TemplateModelException(ex.toString());
176 else if (key.equals("to_media_icon")) {
178 return getUploadedMediaForNewswire();
180 catch (Exception ex) {
181 theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
182 throw new TemplateModelException(ex.toString());
185 else if (key.equals("to_media_list")) {
187 return getUploadedMediaForList();
189 catch (Exception ex) {
190 theLog.printWarning("-- getUploadedMediaForList: could not fetch data " + ex.toString());
191 throw new TemplateModelException(ex.toString());
194 else if (key.equals("to_topics")) {
196 HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(this));
198 catch (Exception ex) {
199 theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
200 throw new TemplateModelException(ex.toString());
204 return new SimpleScalar(getValue(key));
215 * overridden method setValues to patch creator_main_url
217 public void setValues(HashMap theStringValues) {
218 if (theStringValues != null) {
219 if (theStringValues.containsKey("creator_main_url")){
220 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
221 theStringValues.remove("creator_main_url");
222 } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
223 theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
227 super.setValues(theStringValues);
231 private String getContentDataParsed() {
232 String returnField = getValue("content_data");
233 if (returnField!=null & returnField.length()>0 ) {
234 returnField=StringUtil.deleteForbiddenTags(returnField);
235 //create http-links and email-links
236 if (getValue("is_html").equals("0")) {
237 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
238 mirconf_mailLinkName,mirconf_extLinkName,
239 mirconf_intLinkName);
241 returnField = StringUtil.decodeHTMLinTags(returnField);
247 private String getDescriptionParsed() {
248 String returnField = getValue("description");
249 if (returnField != null && returnField.length()>0) {
250 returnField = StringUtil.deleteForbiddenTags(returnField);
251 if (getValue("is_html").equals("0")) {
252 returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
253 mirconf_mailLinkName,mirconf_extLinkName,
254 mirconf_intLinkName);
256 returnField = StringUtil.decodeHTMLinTags(returnField);
262 * fetches all the comments belonging to an article
264 * @return freemarker.template.SimpleList
266 public SimpleList getComments() throws StorageObjectException {
267 return ((DatabaseContent)theStorageObject).getComments(this);
270 private SimpleHash getUploadedMediaForNewswire()
271 throws StorageObjectException, TemplateModelException
273 // fetching/setting the images
274 // return to_media_icons
275 String tinyIcon = null, iconAlt = null;
276 MirMedia mediaHandler = null;
277 Database mediaStorage;
278 EntityMedia uploadedMedia;
280 SimpleHash returnHash = new SimpleHash();
282 EntityList upMediaEntityList =
283 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
284 if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
286 for (int n=0; n < upMediaEntityList.size();n++) {
287 uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
288 mediaType = uploadedMedia.getMediaType();
290 // ############### TODO: merge these and the getURL call into one
291 // getURL helper call that just takes the Entity as a parameter
292 // along with media_type
293 mediaHandler = MediaHelper.getHandler( mediaType );
294 mediaStorage = MediaHelper.getStorage( mediaType,
295 "mircoders.storage.Database");
296 } catch (MirMediaException ex) {
297 throw new TemplateModelException(ex.toString());
299 //the "best" media type to show
300 if (mediaHandler.isVideo()) {
301 tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
304 } else if (mediaHandler.isAudio()) {
305 tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
307 } else if (tinyIcon == null && !mediaHandler.isImage()) {
308 tinyIcon = mediaHandler.getTinyIcon();
309 iconAlt = mediaHandler.getIconAlt();
312 //it only has image(s)
313 if (tinyIcon == null) {
314 tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
318 // uploadedMedia Entity list is empty.
321 tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
325 returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
326 returnHash.put("icon_alt", iconAlt);
330 private SimpleHash getUploadedMediaForList()
331 throws StorageObjectException, TemplateModelException
333 SimpleHash returnHash = new SimpleHash();
335 EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(this);
336 if (currentMediaList!=null && currentMediaList.getCount()>=1) {
337 SimpleList mediaListAudio = new SimpleList();
338 SimpleList mediaListImages = new SimpleList();
339 SimpleList mediaListVideo = new SimpleList();
340 SimpleList mediaListOther = new SimpleList();
343 SimpleHash upMediaSimpleHash;
344 MirMedia mediaHandler=null;
345 Database mediaStorage=null;
347 for (int n=0; n < currentMediaList.size();n++) {
348 upMedia = currentMediaList.elementAt(n);
349 //upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
350 mediaType = ((EntityMedia)upMedia).getMediaType();
353 // ############### TODO: merge these and the getURL call into one
354 // getURL helper call that just takes the Entity as a parameter
355 // along with media_type
356 mediaHandler = MediaHelper.getHandler( mediaType );
357 mediaStorage = MediaHelper.getStorage( mediaType,
358 "mircoders.storage.Database");
359 } catch (MirMediaException ex) {
360 throw new TemplateModelException(ex.toString());
363 // ################ TODO: see getUploadedMediaForContent
364 //we most likely need further info
365 upMedia = mediaStorage.selectById(upMedia.getId());
367 // ############### ditto
368 String url = mediaHandler.getListView(upMedia, mediaType);
369 //upMediaSimpleHash.put("url", url);
370 upMedia.setValueForProperty("url", url);
371 theLog.printDebugInfo("url is "+ url);
373 catch (Exception ex) {
374 throw new TemplateModelException(ex.toString());
377 // putting media in the apropriate list container
378 if (upMedia.getValue("is_published").equals("1")) {
379 if (mediaHandler.isImage()) {
380 //mediaListImages.add(upMediaSimpleHash);
381 mediaListImages.add(upMedia);
382 } else if (mediaHandler.isAudio()) {
383 //mediaListAudio.add(upMediaSimpleHash);
384 mediaListAudio.add(upMedia);
385 } else if (mediaHandler.isVideo()) {
386 //mediaListVideo.add(upMediaSimpleHash);
387 mediaListVideo.add(upMedia);
389 //mediaListOther.add(upMediaSimpleHash);
390 mediaListOther.add(upMedia);
392 } //end if is_published
394 returnHash.put("media_audio", mediaListAudio);
395 returnHash.put("media_images", mediaListImages);
396 returnHash.put("media_video", mediaListVideo);
397 returnHash.put("media_other", mediaListOther);
398 } //end if currentMediaList != null
402 private SimpleList getUploadedMediaForContent()
403 throws StorageObjectException, TemplateModelException
405 SimpleList returnList=null;
406 EntityList currentMediaList =
407 DatabaseContentToMedia.getInstance().getUploadedMedia(this);
408 if (currentMediaList!=null && currentMediaList.getCount()>=1)
410 System.err.println("LIST NOT NULL "+this.getId());
411 Entity upMedia,mediaType;
412 SimpleHash upMediaSimpleHash;
413 MirMedia mediaHandler=null;
414 Database mediaStorage=null;
416 returnList = new SimpleList();
418 for (int n=0; n < currentMediaList.size();n++) {
419 upMedia = currentMediaList.elementAt(n);
420 //upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
421 mediaType = ((EntityMedia)upMedia).getMediaType();
423 // ############### TODO: merge these and the getURL call into one
424 // getURL helper call that just takes the Entity as a parameter
425 // along with media_type
426 mediaHandler = MediaHelper.getHandler( mediaType );
427 mediaStorage = MediaHelper.getStorage( mediaType,
428 "mircoders.storage.Database");
429 } catch (MirMediaException ex) {
430 throw new TemplateModelException(ex.toString());
433 // ########## TODO: this should be transparent
434 //we most likely need further info
435 upMedia = mediaStorage.selectById(upMedia.getId());
437 //upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
438 // ################# TODO: this is broken cause of field checking
439 // in Entity. must find a workaround
440 upMedia.setValueForProperty("url",
441 mediaHandler.getURL(upMedia, mediaType));
442 System.err.println("URL "+mediaHandler.getURL(upMedia, mediaType));
443 System.err.println("URK'ED? "+upMedia.get("url"));
444 System.err.println("PUB'ED? "+upMedia.get("is_published"));
446 catch (Exception ex) {
447 throw new TemplateModelException(ex.toString());
449 //upMediaSimpleHash.put("type",mediaType.getValue("classname"));
450 upMedia.setValueForProperty("type",mediaType.getValue("classname"));
451 returnList.add(upMedia);
453 } //end if currentMediaList != null