wiped AsSimpleList Methods from EntityRelation
[mir.git] / source / mircoders / entity / EntityContent.java
index 8e8a6e9..46fa13d 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,38 +166,54 @@ public class EntityContent extends Entity
           throw new TemplateModelException(ex.toString());
         }
       }
-      if (key.equals("to_media")) {
+      if (key.equals("to_media_images")) {
         try {
-          System.err.println("ASKED FOR MEDIA");
-          return getUploadedMediaForContent();
+          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 getUploadedMediaForList();
+          return getVideoForContent();
         }
         catch (Exception ex) {
-          theLog.printWarning("-- getUploadedMediaForList: could not fetch data " + ex.toString());
+          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 getUploadedMediaForNewswire();
+        }
+        catch (Exception ex) {
+          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());
@@ -265,10 +283,11 @@ public class EntityContent extends Entity
         *
         * @return freemarker.template.SimpleList
         */
-       public SimpleList getComments() throws StorageObjectException {
+       private EntityList getComments() throws StorageObjectException {
                return ((DatabaseContent)theStorageObject).getComments(this);
        }
 
+  // @todo this needs to optimized. expensive SQL
   private SimpleHash getUploadedMediaForNewswire()
     throws StorageObjectException, TemplateModelException
   {
@@ -276,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();
 
@@ -286,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());
         }
@@ -316,148 +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();
-        
-        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());
-        }
+    return DatabaseContentToMedia.getInstance().getImages(this);
+  }
 
-        // ################ 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());
-        }
+  private EntityList getAudioForContent()
+    throws StorageObjectException, TemplateModelException
+  {
+    return DatabaseContentToMedia.getInstance().getAudio(this) ;
+  }
 
-        // 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
   {
-    SimpleList returnList=null;
-    EntityList currentMediaList =
-        DatabaseContentToMedia.getInstance().getUploadedMedia(this);
-    if (currentMediaList!=null && currentMediaList.getCount()>=1)
-    {
-      System.err.println("LIST NOT NULL "+this.getId());
-      Entity              upMedia,mediaType;
-      SimpleHash          upMediaSimpleHash;
-      MirMedia            mediaHandler=null;
-      Database            mediaStorage=null;
-
-      returnList = new SimpleList();
-
-      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: this should be transparent
-        //we most likely need further info
-        upMedia = mediaStorage.selectById(upMedia.getId());
-        SimpleHash modelRoot = new SimpleHash();
-        try {
-          //upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
-          // ################# TODO: this is broken cause of field checking
-          // in Entity. must find a workaround
-          modelRoot.put("url", mediaHandler.getURL(upMedia, mediaType));
-          modelRoot.put("is_published", upMedia.getValue("is_published"));
-          //upMedia.setValueForProperty("url",
-                                    //mediaHandler.getURL(upMedia, mediaType));
-          System.err.println("URL "+mediaHandler.getURL(upMedia, mediaType));
-          System.err.println("URK'ED? "+modelRoot.get("url"));
-          System.err.println("PUB'ED? "+modelRoot.get("is_published"));
-        }
-        catch (Exception ex) {
-          throw new TemplateModelException(ex.toString());
-        }
-        //upMediaSimpleHash.put("type",mediaType.getValue("classname"));
-        //upMedia.setValueForProperty("type",mediaType.getValue("classname"));
-        modelRoot.put("type",mediaType.getValue("classname"));
-        returnList.add(modelRoot);
-      } //end for
-    } //end if currentMediaList != null
-    return returnList;
+    return DatabaseContentToMedia.getInstance().getOther(this);
   }
 
 }