Added icon changes code to MediaGeneratingProducerNode
[mir.git] / source / mircoders / producer / MediaGeneratingProducerNode.java
index e76831b..27295ab 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002  The Mir-coders group
+ * Copyright (C) 2001, 2002 The Mir-coders group
  *
  * This file is part of Mir.
  *
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with the com.oreilly.servlet library, any library
- * licensed under the Apache Software License, The Sun (tm) Java Advanced
- * Imaging library (JAI), The Sun JIMI library (or with modified versions of
- * the above that use the same license as the above), and distribute linked
- * combinations including the two.  You must obey the GNU General Public
- * License in all respects for all of the code used other than the above
- * mentioned libraries.  If you modify this file, you may extend this exception
- * to your version of the file, but you are not obligated to do so.  If you do
- * not wish to do so, delete this exception statement from your version.
+ * the code of this program with  any library licensed under the Apache Software License,
+ * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
+ * (or with modified versions of the above that use the same license as the above),
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
+ * If you do not wish to do so, delete this exception statement from your version.
  */
 
 package mircoders.producer;
 
-import java.util.*;
-import java.io.*;
-import mir.util.*;
-import mir.misc.*;
-import mir.media.*;
-import mir.producer.*;
-import mir.generator.*;
-import mir.entity.*;
-import mir.entity.adapter.*;
-//import mircoders.global.*;
-//import mircoders.localizer.*;
-import mircoders.entity.*;
-import mircoders.storage.*;
-
-public class MediaGeneratingProducerNode implements ProducerNode {
+import mir.changetracker.ChangeTracker;
+import mir.changetracker.ChangeType;
+import mir.entity.Entity;
+import mir.entity.adapter.EntityAdapter;
+import mir.log.LoggerWrapper;
+import mir.media.MediaHandler;
+import mir.producer.AbstractProducerNode;
+import mir.producer.ProducerExc;
+import mir.util.ParameterExpander;
+import mircoders.entity.EntityUploadedMedia;
+import mircoders.global.MirGlobal;
+import mircoders.media.MediaHelper;
+import mircoders.storage.DatabaseUploadedMedia;
+
+import java.util.Map;
+
+public class MediaGeneratingProducerNode extends AbstractProducerNode {
   private String mediaEntityKey;
 
   public MediaGeneratingProducerNode(String aMediaEntityKey) {
     mediaEntityKey = aMediaEntityKey;
   }
 
-  public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure {
+  public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) {
     Object data;
     Entity entity;
-    Entity currentMediaType;
-    MirMedia currentMediaHandler;
+    EntityUploadedMedia uploadedMediaEntity = null;
+    Entity mediaType = null;
+    MediaHandler currentMediaHandler;
 
     try {
       data = ParameterExpander.findValueForKey( aValueMap, mediaEntityKey );
 
       if (!(data instanceof EntityAdapter)) {
-        throw new ProducerFailure("MediaGeneratingProducerNode: value of '"+mediaEntityKey+"' is not an EntityAdapter, but an " + data.getClass().getName(), null);
+        throw new ProducerExc("MediaGeneratingProducerNode: value of '" +
+            mediaEntityKey + "' is not an EntityAdapter, but an " + data.getClass().getName());
       }
 
       entity = ((EntityAdapter) data).getEntity();
-      if (! (entity instanceof EntityUploadedMedia)) {
-        throw new ProducerFailure("MediaGeneratingProducerNode: value of '"+mediaEntityKey+"' is not an uploaded media EntityAdapter, but a " + entity.getClass().getName() + " adapter", null);
+      if (!(entity instanceof EntityUploadedMedia)) {
+        throw new ProducerExc("MediaGeneratingProducerNode: value of '" +
+            mediaEntityKey + "' is not an uploaded media EntityAdapter, but a " +
+            entity.getClass().getName() + " adapter");
       }
 
-      currentMediaType = DatabaseUploadedMedia.getInstance().getMediaType(entity);
-
-      currentMediaHandler = MediaHelper.getHandler( currentMediaType );
-      currentMediaHandler.produce(entity,currentMediaType);
-      entity.setValueForProperty("publish_server", currentMediaHandler.getPublishHost());
-      entity.setValueForProperty("icon_is_produced", "1");
-      entity.setValueForProperty("is_produced", "1");
-      entity.update();
-    }
-    catch (Throwable t) {
-      aLogger.println("Error while generating media: " + t.getMessage());
-      t.printStackTrace(aLogger);
-
-      throw new ProducerFailure(t.getMessage(), t);
-    }
-  }
-}
-
-
-/*
-
+      uploadedMediaEntity = (EntityUploadedMedia) entity;
 
-  abstract Database getStorage() throws StorageObjectException;
+      mediaType = DatabaseUploadedMedia.getInstance().getMediaType(entity);
 
-  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";
+      currentMediaHandler = MediaHelper.getHandler( mediaType );
+      currentMediaHandler.produce(entity, mediaType);
+      entity.setFieldValue("publish_server", currentMediaHandler.getPublishHost());
+      entity.setFieldValue("icon_is_produced", "1");
+      entity.setFieldValue("is_produced", "1");
+      entity.update();
 
-    // 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;
+      // Inform the Change Reporter of the change
+      StringBuffer imagepath = new StringBuffer(currentMediaHandler.getBaseStoragePath());
+      imagepath.append(entity.getFieldValue("publish_path").substring(1));
+      ChangeTracker tracker = MirGlobal.getChangeEngine().getTracker();
+      tracker.addChange(imagepath.toString(), ChangeType.MODIFICATION);
+      
+      // Log the icon change also
+      String iconpath = entity.getFieldValue("icon_path");
+      tracker.addChange(iconpath, ChangeType.MODIFICATION);
+      
+      aLogger.info("media with id " + uploadedMediaEntity.getFieldValue("id") +
+          ", mediaType " + mediaType.getFieldValue("name") + " successfully produced");
     }
-    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());
-          e.printStackTrace(htmlout);
+    catch (Throwable t) {
+      String message = "Error while generating media";
+      try {
+        if (uploadedMediaEntity!=null)
+          message = message +  " with id "+uploadedMediaEntity.getFieldValue("id");
+        if (mediaType!=null) {
+          message = message + ", mediaType " + mediaType.getFieldValue("name");
         }
       }
-
-      // if next batch get it...
-      if (batchEntityList.hasNextBatch()){
-        batchEntityList = uploadedMediaModule.getByWhereClause(whereClause,
-          orderBy, batchEntityList.getNextBatch(),contentBatchsize);
-      } else {
-        batchEntityList=null;
+      catch (Throwable s) {
       }
+
+      message = message + ": " + t.getMessage();
+      aLogger.error(message);
     }
-    // Finish
-    sessionConnectTime = new java.util.Date().getTime() - startTime;
-    logHTML(htmlout, "Producer.Media finished: " + sessionConnectTime + " ms.");
   }
-*/
+}
+