a couple of trivial cleanups: check to see if order is empty in addition to null...
[mir.git] / source / mircoders / servlet / ServletModuleUploadedMedia.java
index 8bc74d4..d242516 100755 (executable)
@@ -39,10 +39,7 @@ import mir.media.MediaHelper;
 import mir.media.MirMedia;
 import mir.media.MirMediaException;
 import mir.media.MirMediaUserException;
-import mir.misc.MirConfig;
-import mir.misc.MpRequest;
-import mir.misc.StringUtil;
-import mir.misc.WebdbMultipartRequest;
+import mir.misc.*;
 import mir.module.ModuleException;
 import mir.servlet.ServletModule;
 import mir.servlet.ServletModuleException;
@@ -50,6 +47,7 @@ import mir.servlet.ServletModuleUserException;
 import mir.storage.Database;
 import mir.storage.StorageObjectException;
 import mircoders.entity.EntityUsers;
+import mircoders.entity.EntityUploadedMedia;
 import mircoders.storage.DatabaseMediaType;
 import mircoders.storage.DatabaseMediafolder;
 import mircoders.media.MediaRequest;
@@ -58,8 +56,10 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 import javax.servlet.ServletContext;
+import javax.servlet.ServletOutputStream;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URLEncoder;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
@@ -68,8 +68,8 @@ import java.util.HashMap;
  *  ServletModuleBilder -
  *  liefert HTML fuer Bilder
  *
- *
- * @author RK
+ * @version $Id: ServletModuleUploadedMedia.java,v 1.8.2.4 2002/12/10 10:01:50 mh Exp $
+ * @author RK, the mir-coders group
  */
 
 public abstract class ServletModuleUploadedMedia
@@ -84,19 +84,41 @@ public abstract class ServletModuleUploadedMedia
   public void insert(HttpServletRequest req, HttpServletResponse res)
           throws ServletModuleException, ServletModuleUserException {
     try {
-      WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
       EntityUsers user = _getUser(req);
-      EntityList mediaList =
-        new MediaRequest(mp, user.getId()).getMedia(false, false);
-      list(req, res);
+      MediaRequest mediaReq =  new MediaRequest(user.getId(), false, false);
+      WebdbMultipartRequest mp = new WebdbMultipartRequest(req, (FileHandler)mediaReq);
+      EntityList mediaList = mediaReq.getEntityList();
+
+      SimpleHash mergeData = new SimpleHash();
+      SimpleHash popups = new SimpleHash();
+      mergeData.put("contentlist", mediaList);
+      if (mediaList.getOrder() != null) {
+        mergeData.put("order", mediaList.getOrder());
+        mergeData.put("order_encoded", URLEncoder.encode(mediaList.getOrder()));
+      }
+      mergeData.put("count", (new Integer(mediaList.getCount())).toString());
+      mergeData.put("from", (new Integer(mediaList.getFrom())).toString());
+      mergeData.put("to", (new Integer(mediaList.getTo())).toString());
+      if (mediaList.hasNextBatch())
+        mergeData.put("next", (new Integer(mediaList.getNextBatch())).toString());
+      if (mediaList.hasPrevBatch())
+          mergeData.put("prev", (new Integer(mediaList.getPrevBatch())).toString());
+      //fetch the popups
+      popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
+      // raus damit
+      deliver(req, res, mergeData, popups, templateListString);
     }
-    catch (MirMediaUserException e) {
+    catch (FileHandlerUserException e) {
       throw new ServletModuleUserException(e.getMsg());
     }
-    catch (MirMediaException e) {
+    catch (FileHandlerException e) {
       throw new ServletModuleException(
               "upload -- media handling exception " + e.toString());
     }
+    catch (StorageObjectException e) {
+      throw new ServletModuleException("upload -- storageobjectexception "
+                                      + e.toString());
+    }
     catch (IOException e) {
       throw new ServletModuleException("upload -- ioexception " + e.toString());
     }
@@ -105,10 +127,10 @@ public abstract class ServletModuleUploadedMedia
   public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
 
     try {
-      WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
+      EntityUsers user = _getUser(req);
+      WebdbMultipartRequest mp = new WebdbMultipartRequest(req, null);
       HashMap parameters = mp.getParameters();
 
-      EntityUsers user = _getUser(req);
       parameters.put("to_publisher", user.getId());
       parameters.put("is_produced", "0");
       if (!parameters.containsKey("is_published"))
@@ -124,6 +146,9 @@ public abstract class ServletModuleUploadedMedia
     catch (ModuleException e) {
       throw new ServletModuleException("upload -- moduleexception " + e.toString());
     }
+    catch (Exception e) {
+      throw new ServletModuleException("upload -- exception " + e.toString());
+    }
 
   }
 
@@ -148,7 +173,7 @@ public abstract class ServletModuleUploadedMedia
     mergeData.put("offset", offset);
 
     String order = req.getParameter("order");
-    if (order == null) order = "webdb_lastchange desc";
+    if (order == null || order.equals("")) order = "webdb_lastchange desc";
 
     // if in connection mode to content
     String cid = req.getParameter("cid");
@@ -175,22 +200,20 @@ public abstract class ServletModuleUploadedMedia
 
     // fetch und ausliefern
     try {
-      if (query_text != null || query_is_published != null || query_media_folder != null) {
-        EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(), 10);
-        if (theList != null) {
-          mergeData.put("contentlist", theList);
-          if (theList.getOrder() != null) {
-            mergeData.put("order", theList.getOrder());
-            mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
-          }
-          mergeData.put("count", (new Integer(theList.getCount())).toString());
-          mergeData.put("from", (new Integer(theList.getFrom())).toString());
-          mergeData.put("to", (new Integer(theList.getTo())).toString());
-          if (theList.hasNextBatch())
-            mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
-          if (theList.hasPrevBatch())
-            mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
+      EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(), 10);
+      if (theList != null) {
+        mergeData.put("contentlist", theList);
+        if (theList.getOrder() != null) {
+          mergeData.put("order", theList.getOrder());
+          mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
         }
+        mergeData.put("count", (new Integer(theList.getCount())).toString());
+        mergeData.put("from", (new Integer(theList.getFrom())).toString());
+        mergeData.put("to", (new Integer(theList.getTo())).toString());
+        if (theList.hasNextBatch())
+          mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
+        if (theList.hasPrevBatch())
+          mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
       }
       //fetch the popups
       popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
@@ -269,6 +292,94 @@ public abstract class ServletModuleUploadedMedia
     return (EntityUsers) session.getAttribute("login.uid");
   }
 
+  public void getMedia(HttpServletRequest req, HttpServletResponse res)
+    throws ServletModuleException
+  {
+    String idParam = req.getParameter("id");
+    if (idParam!=null && !idParam.equals("")) {
+      try {
+        EntityUploadedMedia ent = (EntityUploadedMedia)mainModule.getById(idParam);
+        Entity mediaType = ent.getMediaType();
+        MirMedia mediaHandler;
+
+        ServletContext ctx =
+                    (ServletContext)MirConfig.getPropAsObject("ServletContext");
+        String fName = ent.getId()+"."+mediaType.getValue("name");
+
+        mediaHandler = MediaHelper.getHandler(mediaType);
+        InputStream in = mediaHandler.getMedia(ent, mediaType);
+
+        res.setContentType(ctx.getMimeType(fName));
+        //important that before calling this res.getWriter was not called first
+        ServletOutputStream out = res.getOutputStream();
+        int read ;
+        byte[] buf = new byte[8 * 1024];
+        while((read = in.read(buf)) != -1) {
+          out.write(buf, 0, read);
+        }
+        in.close();
+        out.close();
+      }
+
+      catch (IOException e) {
+        throw new ServletModuleException(e.toString());
+      }
+      catch (ModuleException e) {
+        throw new ServletModuleException(e.toString());
+      }
+      catch (Exception e) {
+        throw new ServletModuleException(e.toString());
+      }
+    }
+    else theLog.printDebugInfo("id nicht angeben.");
+    // darf keine exception werfen
+  }
+
+  public void getIcon(HttpServletRequest req, HttpServletResponse res)
+    throws ServletModuleException
+  {
+    String idParam = req.getParameter("id");
+    if (idParam!=null && !idParam.equals("")) {
+      try {
+        EntityUploadedMedia ent = (EntityUploadedMedia)mainModule.getById(idParam);
+        Entity mediaType = ent.getMediaType();
+        MirMedia mediaHandler;
+
+        ServletContext ctx =
+                    (ServletContext)MirConfig.getPropAsObject("ServletContext");
+        String fName = ent.getId()+"."+mediaType.getValue("name");
+
+        mediaHandler = MediaHelper.getHandler(mediaType);
+        InputStream in = mediaHandler.getIcon(ent);
+        res.setContentType(ctx.getMimeType(fName));
+        //important that before calling this res.getWriter was not called first
+        ServletOutputStream out = res.getOutputStream();
+
+        int read ;
+        byte[] buf = new byte[8 * 1024];
+        while((read = in.read(buf)) != -1) {
+          out.write(buf, 0, read);
+        }
+        in.close();
+        out.close();
+      }
+
+      catch (IOException e) {
+        throw new ServletModuleException(e.toString());
+      }
+      catch (ModuleException e) {
+        throw new ServletModuleException(e.toString());
+      }
+      catch (Exception e) {
+        throw new ServletModuleException(e.toString());
+      }
+    }
+    else theLog.printDebugInfo("id nicht angeben.");
+    // darf keine exception werfen
+  }
+
 }