various fixes/cleanup: old producers are now completely gone, old logfile class too
[mir.git] / source / mircoders / entity / EntityContent.java
index 64133dc..87f6397 100755 (executable)
 
 package mircoders.entity;
 
-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.*;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.HashMap;
+
+import freemarker.template.SimpleScalar;
+import freemarker.template.TemplateModel;
+import freemarker.template.TemplateModelException;
+
+import mir.log.LoggerWrapper;
+import mir.entity.Entity;
+import mir.entity.EntityList;
+import mir.storage.StorageObject;
+import mir.storage.StorageObjectExc;
+import mir.storage.StorageObjectFailure;
+import mircoders.storage.DatabaseContent;
+import mircoders.storage.DatabaseContentToMedia;
+import mircoders.storage.DatabaseContentToTopics;
 
 /**
  * this class implements mapping of one line of the database table content
  * to a java object
  *
- * @version $Revision: 1.10 $ $Date: 2002/09/01 22:05:53 $
- * @author $Author: mh $
- *
- * $Log: EntityContent.java,v $
- * Revision 1.10  2002/09/01 22:05:53  mh
- * Mir goes GPL
- *
- * Revision 1.9.2.1  2002/09/01 21:31:43  mh
- * Mir goes GPL
+ * @version $Id: EntityContent.java,v 1.15 2003/02/23 05:00:13 zapata Exp $
+ * @author mir-coders group
  *
- * Revision 1.9  2002/07/19 20:55:49  mh
- * commented out decodeHTML, as we don't encodeHTML anymore
- *
- * Revision 1.8  2002/06/28 20:37:33  mh
- * make date_formatted be webdb_create_short instead to be more consistent. also make date2webdb.. use webdb_create
- *
- * 
  */
 
 
 public class EntityContent extends Entity
 {
 
-  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");
+  String mirconf_extLinkName  = configuration.getString("Producer.ExtLinkName");
+  String mirconf_intLinkName  = configuration.getString("Producer.IntLinkName");
+  String mirconf_mailLinkName = configuration.getString("Producer.MailLinkName");
+  String mirconf_imageRoot    = configuration.getString("Producer.ImageRoot");
 
   //this should always be transient i.e it can never be stored in the db
   //or ObjectStore. (so the ObjectStore should only be caching what comes
@@ -84,129 +74,112 @@ public class EntityContent extends Entity
   HashMap _entCache = new HashMap();
   Boolean _hasMedia = null;
 
-       // constructors
+  // constructors
+
+  public EntityContent()
+  {
+    super();
 
-       public EntityContent()
-       {
-               super();
-    //content_data is now filed-type "text"
-               //streamedInput = new ArrayList();
-               //streamedInput.add("content_data");
-       }
+    logger = new LoggerWrapper("Entity.Content");
+  }
 
-       public EntityContent(StorageObject theStorage) {
-               this();
-               setStorage(theStorage);
-       }
+  public EntityContent(StorageObject theStorage) {
+    this();
+    setStorage(theStorage);
+  }
 
-       //
-       // methods
+  //
+  // methods
 
- /**
-       * set is_produced flag for the article
-       */
 /**
+   * set is_produced flag for the article
+   */
 
-       public void setProduced(boolean yesno) throws StorageObjectException
-       {
-               String value = (yesno) ? "1":"0";
-               if (value.equals( getValue("is_produced") )) return;
+  public void setProduced(boolean yesno) throws StorageObjectFailure
+  {
+    String value = (yesno) ? "1":"0";
+    if (value.equals( getValue("is_produced") )) return;
 
     Connection con=null;Statement stmt=null;
     String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
-               try {
-                       con = theStorageObject.getPooledCon();
-                       /** @todo should be preparedStatement: faster!! */
-                       stmt = con.createStatement();
-                       theStorageObject.executeUpdate(stmt,sql);
-               } catch (StorageObjectException e) {
-            throwStorageObjectException(e, "\n -- set produced failed");
-               } catch (SQLException e) {
-            throwStorageObjectException(e, "\n -- set produced failed");
-               } finally {
-                       theStorageObject.freeConnection(con,stmt);
-               }
-       }
-
-
- /**
-       * make openposting to newswire
-       */
-
-       public void newswire() throws StorageObjectException
-       {
-               String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
-               try {
-                               theStorageObject.executeUpdate(sql);
-               } catch (StorageObjectException e) {
-            throwStorageObjectException(e, "\n -- newswire failed");
-               } catch (SQLException e) {
-            throwStorageObjectException(e, "\n -- newswire failed");
-               }
-       }
-
-
- /**
-       * dettach from media
-       */
-       public void dettach(String cid,String mid) throws StorageObjectException
-       {
-               if (mid!=null){
-                       try{
-                               DatabaseContentToMedia.getInstance().delete(cid,mid);
-                       } catch (Exception e){
-                throwStorageObjectException(e, "\n -- failed to get instance");
-                       }
-                       //set Content to unproduced
-                       setProduced(false);
-               }
-       }
-
- /**
-       * attach to media
-       */
-
-       public void attach(String mid) throws StorageObjectException
-       {
-               if (mid!=null) {
-                       //write media-id mid and content-id in table content_x_media
-                       try{
-                               DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
-                       } catch(StorageObjectException e){
-                               throwStorageObjectException(e, "attach: could not get the instance");
-                       }
-                       //set Content to unproduced
-                       setProduced(false);
-               }       else {
-                       theLog.printError("EntityContent: attach without mid");
-               }
-       }
-
-       /**
-        * overridden method getValue to include formatted date into every
-        * entityContent
-        */
-
-       public String getValue(String field)
+    try {
+      con = theStorageObject.getPooledCon();
+      /** @todo should be preparedStatement: faster!! */
+      stmt = con.createStatement();
+      theStorageObject.executeUpdate(stmt,sql);
+    }
+    catch (StorageObjectFailure e) {
+      throwStorageObjectFailure(e, "\n -- set produced failed");
+    }
+    catch (SQLException e) {
+      throwStorageObjectFailure(e, "\n -- set produced failed");
+    }
+    finally {
+      theStorageObject.freeConnection(con,stmt);
+    }
+  }
+
+
+  /**
+   * make openposting to newswire
+   */
+
+  public void newswire() throws StorageObjectFailure
   {
-    String returnField = null;
-    if (field!=null)
-    {
-      if (field.equals("date_formatted") || field.equals("webdb_create_short") )
-      {
-                 if (hasValueForField("date"))
-       returnField = StringUtil.webdbDate2readableDate(getValue("webdb_create"));
-               }
-      else if (field.equals("description_parsed"))
-        returnField = getDescriptionParsed();
-      else if (field.equals("description_sentence"))
-        returnField = getDescriptionSentence();
-      else if (field.equals("content_data_parsed"))
-        returnField = getContentDataParsed();
-      else
-        return super.getValue(field);
+    String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
+    try {
+      theStorageObject.executeUpdate(sql);
+    } catch (StorageObjectFailure e) {
+      throwStorageObjectFailure(e, "\n -- newswire failed");
+    } catch (SQLException e) {
+      throwStorageObjectFailure(e, "\n -- newswire failed");
     }
-    return returnField;
-       }
+  }
+
+
+  /**
+   * dettach from media
+   */
+  public void dettach(String cid,String mid) throws StorageObjectFailure
+  {
+    if (mid!=null){
+      try{
+        DatabaseContentToMedia.getInstance().delete(cid,mid);
+      }
+      catch (Exception e){
+        throwStorageObjectFailure(e, "\n -- failed to get instance");
+      }
+
+      //set Content to unproduced
+      setProduced(false);
+    }
+  }
+
+  /**
+   * attach to media
+   */
+
+  public void attach(String mid) throws StorageObjectFailure
+  {
+    if (mid!=null) {
+      //write media-id mid and content-id in table content_x_media
+      try{
+        DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
+      } catch(StorageObjectFailure e){
+        throwStorageObjectFailure(e, "attach: could not get the instance");
+      }
+      //set Content to unproduced
+      setProduced(false);
+    }
+    else {
+      logger.error("EntityContent: attach without mid");
+    }
+  }
+
+  /**
+   * overridden method getValue to include formatted date into every
+   * entityContent
+   */
 
   public TemplateModel get(java.lang.String key) throws TemplateModelException
   {
@@ -218,8 +191,10 @@ public class EntityContent extends Entity
         try {
           _entCache.put(key, getComments());
           return (TemplateModel)_entCache.get(key);
-        } catch (Exception ex) {
-          theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
+        }
+        catch (Exception ex) {
+          logger.warn("EntityContent.getComments: could not fetch data " + ex.toString());
+
           throw new TemplateModelException(ex.toString());
         }
       }
@@ -229,7 +204,7 @@ public class EntityContent extends Entity
           return (TemplateModel)_entCache.get(key);
         }
         catch (Exception ex) {
-          theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
+          logger.warn("EntityContent.getImagesForContent: could not fetch data " + ex.toString());
           throw new TemplateModelException(ex.toString());
         }
       }
@@ -239,7 +214,7 @@ public class EntityContent extends Entity
           return (TemplateModel)_entCache.get(key);
         }
         catch (Exception ex) {
-          theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
+          logger.warn("EntityContent.getAudioForContent: could not fetch data " + ex.toString());
           throw new TemplateModelException(ex.toString());
         }
       }
@@ -249,7 +224,7 @@ public class EntityContent extends Entity
           return (TemplateModel)_entCache.get(key);
         }
         catch (Exception ex) {
-          theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
+          logger.warn("EntityContent.getVideoForContent: could not fetch data " + ex.toString());
           throw new TemplateModelException(ex.toString());
         }
       }
@@ -259,28 +234,18 @@ public class EntityContent extends Entity
           return (TemplateModel)_entCache.get(key);
         }
         catch (Exception ex) {
-          theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
-          throw new TemplateModelException(ex.toString());
-        }
-      }
-      else if (key.equals("to_media_icon")) {
-        try {
-          _entCache.put(key, getUploadedMediaForNewswire());
-          return (TemplateModel)_entCache.get(key);
-        }
-        catch (Exception ex) {
-          theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
+          logger.warn("EntityContent.getOtherMediaForContent: could not fetch data " + ex.toString());
           throw new TemplateModelException(ex.toString());
         }
       }
       else if (key.equals("to_topics")) {
         try {
-          _entCache.put(key, 
+          _entCache.put(key,
                         DatabaseContentToTopics.getInstance().getTopics(this));
           return (TemplateModel)_entCache.get(key);
         }
         catch (Exception ex) {
-          theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
+          logger.warn("EntityContent.getTopics: could not fetch data " + ex.toString());
           throw new TemplateModelException(ex.toString());
         }
       }
@@ -292,137 +257,41 @@ public class EntityContent extends Entity
     return null;
   }
 
-
-
-
-       /**
-        * overridden method setValues to patch creator_main_url
-        */
-       public void setValues(HashMap theStringValues) {
-               if (theStringValues != null) {
-                       if (theStringValues.containsKey("creator_main_url")){
-                               if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
-                                       theStringValues.remove("creator_main_url");
-        } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
+  /**
+   * overridden method setValues to patch creator_main_url
+   */
+  public void setValues(HashMap theStringValues) {
+    if (theStringValues != null) {
+      if (theStringValues.containsKey("creator_main_url")){
+        if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
+          theStringValues.remove("creator_main_url");
+        }
+        else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
           theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
         }
       }
-               }
-               super.setValues(theStringValues);
-       }
-
-
-  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);
-      }
-      // commented this out as I don't think it necessary as we don't
-      // "encodeHTML" in the first place anymore.. -mh 2002.07.19
-      //returnField = StringUtil.decodeHTMLinTags(returnField);
     }
-    return returnField;
+    super.setValues(theStringValues);
   }
 
-  private String getDescriptionSentence() {
-    String returnField = getValue("description");
-    if (returnField != null && returnField.length()>0) {
-       returnField = StringUtil.removeHTMLTags(returnField);
-       int endOfFirstSentence=StringUtil.findEndOfSentence(returnField,0);
-       if (endOfFirstSentence > 0){
-        returnField = returnField.substring(0,endOfFirstSentence);
-       }
-    }
-    return returnField;
-  }
-
-  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
+   */
+  private EntityList getComments() throws StorageObjectFailure {
+    return ((DatabaseContent)theStorageObject).getComments(this);
   }
 
-       /**
-        * fetches all the comments belonging to an article
-        *
-        * @return freemarker.template.SimpleList
-        */
-       private EntityList getComments() throws StorageObjectException {
-               return ((DatabaseContent)theStorageObject).getComments(this);
-       }
-
-  // @todo this needs to optimized. expensive SQL
-  private SimpleHash getUploadedMediaForNewswire()
-    throws StorageObjectException, TemplateModelException
-  {
-    // fetching/setting the images
-    // return to_media_icons
-    String        tinyIcon = null, iconAlt = null;
-    MirMedia      mediaHandler = null;
-    EntityUploadedMedia 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 = (EntityUploadedMedia)upMediaEntityList.elementAt(n);
-        mediaType = uploadedMedia.getMediaType();
-        try {
-          mediaHandler = MediaHelper.getHandler( mediaType );
-        } 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 boolean hasMedia() throws StorageObjectException
+  private boolean hasMedia() throws StorageObjectFailure
   {
     if (_hasMedia == null) {
-      _hasMedia =
-        new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
+      try {
+        _hasMedia =
+            new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
+      } catch (StorageObjectExc e) {
+        throw new StorageObjectFailure(e);
+      }
     }
     return _hasMedia.booleanValue();
   }
@@ -430,7 +299,7 @@ public class EntityContent extends Entity
   //######## @todo all of the following getBlahForContent should have
   // and optimized version where LIMIT=1 sql for list view.
   private EntityList getImagesForContent()
-    throws StorageObjectException, TemplateModelException
+      throws StorageObjectFailure, TemplateModelException
   {
     if (hasMedia())
       return DatabaseContentToMedia.getInstance().getImages(this);
@@ -439,7 +308,7 @@ public class EntityContent extends Entity
   }
 
   private EntityList getAudioForContent()
-    throws StorageObjectException, TemplateModelException
+      throws StorageObjectFailure, TemplateModelException
   {
     if (hasMedia())
       return DatabaseContentToMedia.getInstance().getAudio(this) ;
@@ -448,7 +317,7 @@ public class EntityContent extends Entity
   }
 
   private EntityList getVideoForContent()
-    throws StorageObjectException, TemplateModelException
+      throws StorageObjectFailure, TemplateModelException
   {
     if (hasMedia())
       return DatabaseContentToMedia.getInstance().getVideo(this) ;
@@ -457,7 +326,7 @@ public class EntityContent extends Entity
   }
 
   private EntityList getOtherMediaForContent()
-    throws StorageObjectException, TemplateModelException
+      throws StorageObjectFailure, TemplateModelException
   {
     if (hasMedia())
       return DatabaseContentToMedia.getInstance().getOther(this);