a44dc43ee6a53b76cabede4bd93e21c38514b96a
[mir.git] / source / mircoders / producer / ProducerMedia.java
1 package mircoders.producer;
2
3 import java.io.*;
4 import java.lang.*;
5 import java.util.*;
6
7 //import freemarker.template.*;
8
9 import mir.misc.*;
10 import mir.storage.*;
11 import mir.module.*;
12 import mir.entity.*;
13 import mir.media.*;
14
15 import mircoders.media.*;
16 import mircoders.entity.*;
17 import mircoders.storage.*;
18
19 abstract public class ProducerMedia extends Producer {
20
21   abstract Database getStorage() throws StorageObjectException;
22
23   public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
24     boolean sync) throws StorageObjectException, ModuleException {
25     handle(htmlout,user,force,sync,null);
26   }
27
28   public void handle(PrintWriter htmlout,EntityUsers user,boolean force,
29     boolean sync, String id) throws StorageObjectException, ModuleException
30   {
31     long                sessionConnectTime = 0;
32     long                startTime = (new java.util.Date()).getTime();
33     String              whereClause;
34     String              orderBy;
35     Entity              currentMedia;
36     MirMedia            currentMediaHandler;
37     EntityList          batchEntityList;
38
39     int contentBatchsize =
40             Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
41     orderBy = "date desc, webdb_lastchange desc";
42
43     // get batch of non-produced medias, that are to be published
44     whereClause="is_published='1'";
45     if (id!= null) {
46       whereClause += " and id="+id;
47       // optimization to avoid select count(*)..
48       contentBatchsize = -1;
49     }
50     if (force==false) whereClause += " and is_produced='0'";
51
52     batchEntityList = getStorage().selectByWhereClause(whereClause,
53                                                 orderBy, 0, contentBatchsize);
54
55     while (batchEntityList != null) {
56       for(int i=0;i<batchEntityList.size();i++) {
57         currentMedia = (Entity)batchEntityList.elementAt(i);
58         try {
59           Entity currentMediaType =
60                 DatabaseUploadedMedia.getInstance().getMediaType(currentMedia);
61           currentMediaHandler = MediaHelper.getHandler( currentMediaType );
62
63           // now produce
64           currentMediaHandler.produce(currentMedia,currentMediaType);
65           currentMedia.setValueForProperty("publish_server",
66                                         currentMediaHandler.getPublishHost());
67           currentMedia.setValueForProperty("icon_is_produced", "1");
68           currentMedia.setValueForProperty("is_produced", "1");
69           currentMedia.update();
70           logHTML(htmlout,"produced media id "+currentMedia.getId()
71                   +": "+currentMediaType.getValue("mime_type")+" success");
72         } catch (Exception e) {
73           // don't throw and exception here, just log.
74           // we don't want to make the admin interface unuseable
75           theLog.printError("media exception: "+currentMedia.getId()+
76                             e.toString());
77           logHTML(htmlout, "problem with media id: "+currentMedia.getId()+
78                   " <font color=\"Red\"> failed!</font>: "+e.toString());
79         }
80       }
81
82       // if next batch get it...
83       if (batchEntityList.hasNextBatch()){
84         batchEntityList = uploadedMediaModule.getByWhereClause(whereClause,
85           orderBy, batchEntityList.getNextBatch(),contentBatchsize);
86       } else {
87         batchEntityList=null;
88       }
89     }
90     // Finish
91     sessionConnectTime = new java.util.Date().getTime() - startTime;
92     logHTML(htmlout, "Producer.Media finished: " + sessionConnectTime + " ms.");
93   }
94
95 }