change the oderBy from the usual last update to ID no. for media.
[mir.git] / source / mircoders / producer / ProducerMedia.java
index 5d38acb..64546f6 100755 (executable)
@@ -4,7 +4,7 @@ import java.io.*;
 import java.lang.*;
 import java.util.*;
 
-import freemarker.template.*;
+//import freemarker.template.*;
 
 import mir.misc.*;
 import mir.storage.*;
@@ -15,128 +15,84 @@ import mir.media.*;
 import mircoders.media.*;
 import mircoders.entity.*;
 import mircoders.storage.*;
-import mir.storage.*;
-
-
-
-public class ProducerMedia extends Producer {
-
-       public static void main(String argv[]){
-               try {
-                       new ProducerContent().handle(new PrintWriter(System.out), null, false,false);
-               } catch(Exception e) { System.err.println(e.toString()); }
-       }
-
-       public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync)
-               throws StorageObjectException, ModuleException {
-               handle(htmlout,user,force,sync,null);
-       }
-
-       public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)
-               throws StorageObjectException, ModuleException
-       {
-               long                sessionConnectTime = 0;
-               long                startTime = (new java.util.Date()).getTime();
-        boolean             iconProduced = false;
-        boolean             mediaProduced = false;
-               String              whereClause;
-               String              mediaHost;
-               String              id2=null;
-               String              iconPath;
-               String              mediaPath;
-               String              iconFilename;
-               String              mediaFilename;
-               String              productionPath;
-               Entity              currentMedia;
-               EntityList          batchEntityList;
-
-        int contentBatchsize = Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
-
-               // get batch of non-produced medias, that are to be published
-               whereClause="is_published='1'";
-               if (id!= null)
-                       whereClause += " and id="+id;
-               if (force==false) whereClause += " and is_produced='0'";
-
-               batchEntityList = uploadedMediaModule.getByWhereClause(whereClause, null, 0, contentBatchsize);
-
-               while (batchEntityList != null) {
-                       for(int i=0;i<batchEntityList.size();i++) {
-                try {
-                    currentMedia = (Entity)batchEntityList.elementAt(i);
-
-                    Entity currentMediaType = DatabaseUploadedMedia.getInstance().getMediaType(currentMedia);
-                    String mediaHandlerName = currentMediaType.getValue("classname");
-                    Class mediaHandlerClass = Class.forName("mircoders.media.MediaHandler"+mediaHandlerName);
-                    MirMedia currentMediaHandler = (MirMedia)mediaHandlerClass.newInstance();
-
-                    String date = currentMedia.getValue("date");
-                    String datePath = StringUtil.webdbDate2path(date);
-
-                    // do icon
-                    if (!currentMedia.getValue("icon_is_produced").equals("1")) {
-                        iconPath = currentMediaHandler.getIconStoragePath();
-                        iconFilename = producerDocRoot+iconPath+datePath+
-                            currentMedia.getId()+"."+currentMediaType.getValue("name");
-                        iconProduced = produceFile(iconFilename, currentMediaHandler.getIcon(currentMedia), htmlout, true);
-                        if(iconProduced) {
-                            currentMedia.setValueForProperty("icon_path",iconFilename);
-                            logHTML(htmlout,"icon : " + iconFilename + (( iconProduced==true )? " succeded":" <font color=\"Red\"> failed!</font>"));
-                        }
-
-                    } else {
-                        iconProduced = true;
-                    }
-
-                    //now produce the media content
-
-                    mediaHost = currentMediaHandler.getPublishHost();
-                    mediaPath = currentMediaHandler.getStoragePath();
-                    id2 = currentMedia.getId();
-                    mediaFilename = currentMedia.getId()+"."+currentMediaType.getValue("name");
-                    //hack: make this a config option. -mh
-                    productionPath = mediaPath+datePath+"/"+ mediaFilename ;
-                    //productionPath = mediaPath+"/"+mediaFilename ;
-
-                    //hack: see above. -mh.
-                    currentMedia.setValueForProperty("publish_path",datePath+"/"+mediaFilename);
-                    //currentMedia.setValueForProperty("publish_path",mediaFilename);
-                    currentMedia.setValueForProperty("publish_server", mediaHost);
-
-                    theLog.printError("ABOUT OT FILE"+id2);
-                    mediaProduced = produceFile(productionPath, currentMediaHandler.get(currentMedia, currentMediaType), htmlout, false);
-                    logHTML(htmlout,"media: " + productionPath + ((mediaProduced==true)?" succeded":" <font color=\"Red\"> failed!</font>"));
-
-                    // update media-data
-                    if (iconProduced && mediaProduced) {
-                        currentMedia.setValueForProperty("icon_is_produced", "1");
-                        currentMedia.setValueForProperty("is_produced", "1");
-                        currentMedia.update();
-                    }
-                } catch (MirMediaException e) {
-                    theLog.printError("media exception: "+id+e.toString());
-                    logHTML(htmlout, "problem with media id: "
-                        +id+" <font color=\"Red\"> failed!</font>: "
-                        +e.toString());
-                } catch (Exception e) {
-                    theLog.printError("failed in reflection: "+id+e.toString());
-                    logHTML(htmlout, "reflection problem in media id: "
-                        +id+" <font color=\"Red\"> failed!</font>: "
-                        +e.toString());
-                }
-                       }
-
-                       // if next batch get it...
-                       if (batchEntityList.hasNextBatch()){
-                               batchEntityList = uploadedMediaModule.getByWhereClause(whereClause, null, batchEntityList.getNextBatch(),contentBatchsize);
-                       } else {
-                               batchEntityList=null;
-                       }
-               }
-               // Finish
-               sessionConnectTime = new java.util.Date().getTime() - startTime;
-               logHTML(htmlout, "Producer.Media finished: " + sessionConnectTime + " ms.");
 
-       }
+abstract public class ProducerMedia extends Producer {
+
+  abstract Database getStorage() throws StorageObjectException;
+
+  public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
+    boolean sync) throws StorageObjectException, ModuleException {
+    handle(htmlout,user,force,sync,null);
+  }
+
+  public void handle(PrintWriter htmlout,EntityUsers user,boolean force,
+    boolean sync, String id) throws StorageObjectException, ModuleException
+  {
+    long                sessionConnectTime = 0;
+    long                startTime = (new java.util.Date()).getTime();
+    String              whereClause;
+    String              orderBy;
+    Entity              currentMedia;
+    MirMedia            currentMediaHandler;
+    EntityList          batchEntityList;
+
+    int contentBatchsize =
+            Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
+    //orderBy = "date desc, webdb_lastchange desc";
+    // media should stay in uploaded order. this is especially important
+    // for photo stories which require a specific chronologic order.
+    orderBy = "id";
+
+    // get batch of non-produced medias, that are to be published
+    whereClause="is_published='1'";
+    if (id!= null) {
+      whereClause += " and id="+id;
+      // optimization to avoid select count(*)..
+      contentBatchsize = -1;
+    }
+    if (force==false) whereClause += " and is_produced='0'";
+
+    batchEntityList = getStorage().selectByWhereClause(whereClause,
+                                                orderBy, 0, contentBatchsize);
+
+    while (batchEntityList != null) {
+      for(int i=0;i<batchEntityList.size();i++) {
+        currentMedia = (Entity)batchEntityList.elementAt(i);
+        try {
+          Entity currentMediaType =
+                DatabaseUploadedMedia.getInstance().getMediaType(currentMedia);
+          currentMediaHandler = MediaHelper.getHandler( currentMediaType );
+
+          // now produce
+          currentMediaHandler.produce(currentMedia,currentMediaType);
+          currentMedia.setValueForProperty("publish_server",
+                                        currentMediaHandler.getPublishHost());
+          currentMedia.setValueForProperty("icon_is_produced", "1");
+          currentMedia.setValueForProperty("is_produced", "1");
+          currentMedia.update();
+          logHTML(htmlout,"produced media id "+currentMedia.getId()
+                  +": "+currentMediaType.getValue("mime_type")+" success");
+        } catch (Exception e) {
+          // don't throw and exception here, just log.
+          // we don't want to make the admin interface unuseable
+          theLog.printError("media exception: "+currentMedia.getId()+
+                            e.toString());
+          logHTML(htmlout, "problem with media id: "+currentMedia.getId()+
+                  " <font color=\"Red\"> failed!</font>: "+e.toString());
+        }
+      }
+
+      // if next batch get it...
+      if (batchEntityList.hasNextBatch()){
+        batchEntityList = uploadedMediaModule.getByWhereClause(whereClause,
+          orderBy, batchEntityList.getNextBatch(),contentBatchsize);
+      } else {
+        batchEntityList=null;
+      }
+    }
+    // Finish
+    sessionConnectTime = new java.util.Date().getTime() - startTime;
+    logHTML(htmlout, "Producer.Media finished: " + sessionConnectTime + " ms.");
+  }
 
 }