really splitting log
[mir.git] / source / mircoders / entity / EntityContent.java
index 14b8971..68e4a77 100755 (executable)
@@ -4,11 +4,13 @@ import java.lang.*;
 import java.io.*;
 import java.util.*;
 import java.sql.*;
+import java.lang.reflect.*;
 
 import freemarker.template.*;
 
 import mir.entity.*;
 import mir.misc.*;
+import mir.media.*;
 import mir.storage.*;
 
 import mircoders.storage.*;
@@ -22,17 +24,19 @@ import mircoders.storage.*;
  */
 
 
-public class EntityContent extends AbstractEntity implements Entity
+public class EntityContent extends Entity
 {
 
-       private static int      instances;
+  String mirconf_extLinkName  = MirConfig.getProp("Producer.ExtLinkName");
+  String mirconf_intLinkName  = MirConfig.getProp("Producer.IntLinkName");
+  String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
+  String mirconf_imageRoot    = MirConfig.getProp("Producer.ImageRoot");
 
        // constructors
 
        public EntityContent()
        {
                super();
-               instances++;
     //content_data is now filed-type "text"
                //streamedInput = new ArrayList();
                //streamedInput.add("content_data");
@@ -43,11 +47,6 @@ public class EntityContent extends AbstractEntity implements Entity
                setStorage(theStorage);
        }
 
-       public void finalize() {
-    instances--;
-    super.finalize();
-  }
-
        //
        // methods
 
@@ -129,23 +128,105 @@ public class EntityContent extends AbstractEntity implements Entity
        }
 
        /**
-        * overridden method getValues to include formatted date into every
+        * overridden method getValue to include formatted date into every
         * entityContent
         */
 
-       public HashMap getValues() {
-               HashMap returnHash = super.getValues();
-               String date=null;
-
-               if ((date=(String)returnHash.get("date"))!=null)
-                       returnHash.put("date_formatted", StringUtil.webdbDate2readableDate(date));
-               if ((date=(String)returnHash.get("webdb_create"))!=null)
-                       returnHash.put("webdb_create_formatted", StringUtil.dateToReadableDate(date));
-               if ((date=(String)returnHash.get("webdb_lastchange"))!=null)
-                       returnHash.put("webdb_lastchange_formatted", StringUtil.dateToReadableDate(date));
-               return returnHash;
+       public String getValue(String field)
+  {
+    String returnField = null;
+    if (field!=null)
+    {
+      if (field.equals("date_formatted"))
+      {
+                 if (hasValueForField("date"))
+       returnField = StringUtil.webdbDate2readableDate(getValue("date"));
+               }
+      else if (field.equals("description_parsed"))
+        returnField = getDescriptionParsed();
+      else if (field.equals("content_data_parsed"))
+        returnField = getContentDataParsed();
+      else
+        return super.getValue(field);
+    }
+    return returnField;
        }
 
+  public TemplateModel get(java.lang.String key) throws TemplateModelException
+  {
+    if (key!=null) {
+      if (key.equals("to_comments")) {
+        try {
+          return getComments();
+        }
+        catch (Exception ex) {
+          theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
+          throw new TemplateModelException(ex.toString());
+        }
+      }
+      if (key.equals("to_media_content")) {
+        try {
+          /** @todo why this loggin to System.err and not
+           *  theLog.printDebugInfo() ? / rk */
+          System.err.println("ASKED FOR MEDIA");
+          SimpleList t = getUploadedMediaForContent();
+          //SimpleHash o = t.next();
+
+          if (t == null) {
+            System.err.println("NULL -- LIST "+this.getId());
+          } else {
+            SimpleHash o = (SimpleHash)t.next();
+            System.err.println("SCALAR: "+o.get("url"));
+          }
+
+          t.rewind();
+          //return getUploadedMediaForContent();
+          return t;
+        }
+        catch (Exception ex) {
+          System.err.println("ASKED EXCE");
+          theLog.printWarning("-- getUploadedMediaForContent: could not fetch data " + ex.toString());
+          throw new TemplateModelException(ex.toString());
+        }
+      }
+      else if (key.equals("to_media_icon")) {
+        try {
+          return getUploadedMediaForNewswire();
+        }
+        catch (Exception ex) {
+          theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
+          throw new TemplateModelException(ex.toString());
+        }
+      }
+      else if (key.equals("to_media_list")) {
+        try {
+          return getUploadedMediaForList();
+        }
+        catch (Exception ex) {
+          theLog.printWarning("-- getUploadedMediaForList: could not fetch data " + ex.toString());
+          throw new TemplateModelException(ex.toString());
+        }
+      }
+      else if (key.equals("to_topics")) {
+        try {
+          HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(this));
+        }
+        catch (Exception ex) {
+          theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
+          throw new TemplateModelException(ex.toString());
+        }
+      }
+      else {
+        return new SimpleScalar(getValue(key));
+      }
+
+    }
+    return null;
+  }
+
+
+
+
        /**
         * overridden method setValues to patch creator_main_url
         */
@@ -162,44 +243,191 @@ public class EntityContent extends AbstractEntity implements Entity
                super.setValues(theStringValues);
        }
 
-       /**
-        * return the content_data as string
-        * is obsolete, because content_data is now sql-type text
 
-  public String getContentData()
-       {
-               Connection con=null;Statement stmt=null;
-               byte[] content_data=null;
+  private String getContentDataParsed() {
+    String returnField = getValue("content_data");
+    if (returnField!=null & returnField.length()>0 ) {
+      returnField=StringUtil.deleteForbiddenTags(returnField);
+      //create http-links and email-links
+      if (getValue("is_html").equals("0")) {
+        returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
+                                            mirconf_mailLinkName,mirconf_extLinkName,
+                                            mirconf_intLinkName);
+      }
+      returnField = StringUtil.decodeHTMLinTags(returnField);
+    }
+    return returnField;
+  }
 
-               try {
-                       con = theStorageObject.getPooledCon();
-                       con.setAutoCommit(false);
-                       stmt = con.createStatement();
-                       ResultSet rs = theStorageObject.executeSql(stmt,"select content_data from content where id="+getId());
-                       if(rs!=null) {
-                               if (rs.next()) {
-                                        content_data = rs.getBytes(1);
-                               }
-                               rs.close();
-                       }
-               }
-               catch (Exception e) {theLog.printError("EntityContent :: getContent failed! "+e.toString());}
-               finally {
-                       try {con.setAutoCommit(true); } catch (Exception e) {;}
-                       theStorageObject.freeConnection(con,stmt); }
 
-               return StringUtil.encodeHtml(StringUtil.unquote( new  String(content_data) ));
-       }
-*/
+  private String getDescriptionParsed() {
+    String returnField = getValue("description");
+    if (returnField != null && returnField.length()>0) {
+      returnField = StringUtil.deleteForbiddenTags(returnField);
+      if (getValue("is_html").equals("0")) {
+        returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
+                                            mirconf_mailLinkName,mirconf_extLinkName,
+                                            mirconf_intLinkName);
+      }
+      returnField = StringUtil.decodeHTMLinTags(returnField);
+    }
+    return returnField;
+  }
 
        /**
         * fetches all the comments belonging to an article
         *
         * @return freemarker.template.SimpleList
         */
-       public SimpleList getComments() throws StorageObjectException {
+       private SimpleList getComments() throws StorageObjectException {
                return ((DatabaseContent)theStorageObject).getComments(this);
        }
 
+  private SimpleHash getUploadedMediaForNewswire()
+    throws StorageObjectException, TemplateModelException
+  {
+    // fetching/setting the images
+    // return to_media_icons
+    String        tinyIcon = null, iconAlt = null;
+    MirMedia      mediaHandler = null;
+    Database      mediaStorage;
+    EntityMedia   uploadedMedia;
+    Entity        mediaType;
+    SimpleHash    returnHash = new SimpleHash();
+
+    EntityList upMediaEntityList =
+                    DatabaseContentToMedia.getInstance().getUploadedMedia(this);
+    if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
+
+      for (int n=0; n < upMediaEntityList.size();n++) {
+        uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
+        mediaType = uploadedMedia.getMediaType();
+        try {
+          // ############### TODO: merge these and the getURL call into one
+          // getURL helper call that just takes the Entity as a parameter
+          // along with media_type
+          mediaHandler = MediaHelper.getHandler( mediaType );
+          mediaStorage = MediaHelper.getStorage( mediaType,
+                                                "mircoders.storage.Database");
+        } catch (MirMediaException ex) {
+          throw new TemplateModelException(ex.toString());
+        }
+        //the "best" media type to show
+        if (mediaHandler.isVideo()) {
+          tinyIcon = MirConfig.getProp("Producer.Icon.TinyVideo");
+          iconAlt = "Video";
+          break;
+        } else if (mediaHandler.isAudio()) {
+          tinyIcon = MirConfig.getProp("Producer.Icon.TinyAudio");
+          iconAlt = "Audio";
+        } else if (tinyIcon == null && !mediaHandler.isImage()) {
+          tinyIcon = mediaHandler.getTinyIcon();
+          iconAlt = mediaHandler.getIconAlt();
+        }
+      }
+      //it only has image(s)
+      if (tinyIcon == null) {
+        tinyIcon = MirConfig.getProp("Producer.Icon.TinyImage");
+        iconAlt = "Image";
+      }
+
+    // uploadedMedia Entity list is empty.
+    // we only have text
+    } else {
+      tinyIcon = MirConfig.getProp("Producer.Icon.TinyText");
+      iconAlt = "Text";
+    }
+
+    returnHash.put("tiny_icon", mirconf_imageRoot+"/"+tinyIcon);
+    returnHash.put("icon_alt", iconAlt);
+    return returnHash;
+  }
+
+  private SimpleHash getUploadedMediaForList()
+    throws StorageObjectException, TemplateModelException
+  {
+    SimpleHash returnHash = new SimpleHash();
+    //media to content
+    EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(this);
+    if (currentMediaList!=null && currentMediaList.getCount()>=1) {
+      SimpleList          mediaListAudio = new SimpleList();
+      SimpleList          mediaListImages = new SimpleList();
+      SimpleList          mediaListVideo = new SimpleList();
+      SimpleList          mediaListOther = new SimpleList();
+      Entity              upMedia;
+      Entity              mediaType;
+      SimpleHash          upMediaSimpleHash;
+      MirMedia            mediaHandler=null;
+      Database            mediaStorage=null;
+
+      for (int n=0; n < currentMediaList.size();n++) {
+        upMedia = currentMediaList.elementAt(n);
+        //upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
+        mediaType = ((EntityMedia)upMedia).getMediaType();
+
+        try {
+          // ############### TODO: merge these and the getURL call into one
+          // getURL helper call that just takes the Entity as a parameter
+          // along with media_type
+          mediaHandler = MediaHelper.getHandler( mediaType );
+          mediaStorage = MediaHelper.getStorage( mediaType,
+                                                "mircoders.storage.Database");
+        } catch (MirMediaException ex) {
+          throw new TemplateModelException(ex.toString());
+        }
+
+        // ################ TODO: see getUploadedMediaForContent
+        //we most likely need further info
+        upMedia = mediaStorage.selectById(upMedia.getId());
+        try {
+          // ############### ditto
+          String url = mediaHandler.getListView(upMedia, mediaType);
+          //upMediaSimpleHash.put("url", url);
+          upMedia.setValueForProperty("url", url);
+          theLog.printDebugInfo("url is "+  url);
+        }
+        catch (Exception ex) {
+          throw new TemplateModelException(ex.toString());
+        }
+
+        // putting media in the apropriate list container
+        if (upMedia.getValue("is_published").equals("1")) {
+          if (mediaHandler.isImage()) {
+            //mediaListImages.add(upMediaSimpleHash);
+            mediaListImages.add(upMedia);
+          } else if (mediaHandler.isAudio()) {
+            //mediaListAudio.add(upMediaSimpleHash);
+            mediaListAudio.add(upMedia);
+          } else if (mediaHandler.isVideo()) {
+            //mediaListVideo.add(upMediaSimpleHash);
+            mediaListVideo.add(upMedia);
+          } else {
+            //mediaListOther.add(upMediaSimpleHash);
+            mediaListOther.add(upMedia);
+          }
+        } //end if is_published
+      } //end for
+      returnHash.put("media_audio", mediaListAudio);
+      returnHash.put("media_images", mediaListImages);
+      returnHash.put("media_video", mediaListVideo);
+      returnHash.put("media_other", mediaListOther);
+    } //end if currentMediaList != null
+    return returnHash;
+ }
+
+  private SimpleList getUploadedMediaForContent()
+    throws StorageObjectException, TemplateModelException
+  {
+    /** @todo all logic related to uploaded media should be moved
+     *  to EntityUploadedMedia, selection should just take place
+     *  on published media! .. will fix it later / rk
+     *
+     * ok i moved it... let's see what happens...
+     *
+     *  */
+
+    return HTMLTemplateProcessor.makeSimpleList(
+             DatabaseContentToMedia.getInstance().getUploadedMedia(this) );
+  }
 
 }