producerContent debugging / pageCount inserted
[mir.git] / source / mircoders / entity / EntityContent.java
index 28707fe..958f697 100755 (executable)
@@ -56,9 +56,11 @@ public class EntityContent extends Entity
 
        public void setProduced(boolean yesno) throws StorageObjectException
        {
-               Connection con=null;Statement stmt=null;
                String value = (yesno) ? "1":"0";
-               String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
+               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!! */
@@ -164,52 +166,54 @@ public class EntityContent extends Entity
           throw new TemplateModelException(ex.toString());
         }
       }
-      if (key.equals("to_media_content")) {
+      if (key.equals("to_media_images")) {
         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;
+          return getImagesForContent();
         }
         catch (Exception ex) {
-          System.err.println("ASKED EXCE");
-          theLog.printWarning("-- getUploadedMediaForContent: could not fetch data " + ex.toString());
+          theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
           throw new TemplateModelException(ex.toString());
         }
       }
-      else if (key.equals("to_media_icon")) {
+      if (key.equals("to_media_audio")) {
         try {
-          return getUploadedMediaForNewswire();
+          return getAudioForContent();
         }
         catch (Exception ex) {
-          theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
+          theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
           throw new TemplateModelException(ex.toString());
         }
       }
-      else if (key.equals("to_media_list")) {
+      if (key.equals("to_media_video")) {
+        try {
+          return getVideoForContent();
+        }
+        catch (Exception ex) {
+          theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
+          throw new TemplateModelException(ex.toString());
+        }
+      }
+      if (key.equals("to_media_other")) {
+        try {
+          return getOtherMediaForContent();
+        }
+        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 {
-          return getUploadedMediaForList();
+          return getUploadedMediaForNewswire();
         }
         catch (Exception ex) {
-          theLog.printWarning("-- getUploadedMediaForList: could not fetch data " + ex.toString());
+          theLog.printWarning("-- getUploadedMediaForNewswire: could not fetch data " + ex.toString());
           throw new TemplateModelException(ex.toString());
         }
       }
       else if (key.equals("to_topics")) {
         try {
-          HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(this));
+          DatabaseContentToTopics.getInstance().getTopics(this);
         }
         catch (Exception ex) {
           theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
@@ -283,6 +287,7 @@ public class EntityContent extends Entity
                return ((DatabaseContent)theStorageObject).getComments(this);
        }
 
+  // @todo this needs to optimized. expensive SQL
   private SimpleHash getUploadedMediaForNewswire()
     throws StorageObjectException, TemplateModelException
   {
@@ -290,8 +295,7 @@ public class EntityContent extends Entity
     // return to_media_icons
     String        tinyIcon = null, iconAlt = null;
     MirMedia      mediaHandler = null;
-    Database      mediaStorage;
-    EntityMedia   uploadedMedia;
+    EntityUploadedMedia uploadedMedia;
     Entity        mediaType;
     SimpleHash    returnHash = new SimpleHash();
 
@@ -300,15 +304,10 @@ public class EntityContent extends Entity
     if (upMediaEntityList!=null && upMediaEntityList.getCount()>=1) {
 
       for (int n=0; n < upMediaEntityList.size();n++) {
-        uploadedMedia = (EntityMedia)upMediaEntityList.elementAt(n);
+        uploadedMedia = (EntityUploadedMedia)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());
         }
@@ -330,94 +329,41 @@ public class EntityContent extends Entity
         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()
+  //######## @todo all of the following getBlahForContent should have
+  // and optimized version where LIMIT=1 sql for list view.
+  private EntityList getImagesForContent()
     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();
+    return DatabaseContentToMedia.getInstance().getImages(this);
+  }
 
-        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());
-        }
+  private EntityList getAudioForContent()
+    throws StorageObjectException, TemplateModelException
+  {
+    return DatabaseContentToMedia.getInstance().getAudio(this) ;
+  }
 
-        // ################ TODO: see getUploadedMediaForContent
-        //we most likely need further info
-        upMedia = mediaStorage.selectById(upMedia.getId());
-
-        // 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 EntityList getVideoForContent()
+    throws StorageObjectException, TemplateModelException
+  {
+    return DatabaseContentToMedia.getInstance().getVideo(this) ;
+  }
 
-  private SimpleList getUploadedMediaForContent()
+  private EntityList getOtherMediaForContent()
     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) );
+    return DatabaseContentToMedia.getInstance().getOther(this);
   }
 
 }