first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[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 import mir.storage.*;
19
20
21
22 public class ProducerMedia extends Producer {
23
24         public static void main(String argv[]){
25                 try {
26                         new ProducerContent().handle(new PrintWriter(System.out), null, false,false);
27                 } catch(Exception e) { System.err.println(e.toString()); }
28         }
29
30         public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync)
31                 throws StorageObjectException, ModuleException {
32                 handle(htmlout,user,force,sync,null);
33         }
34
35         public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)
36                 throws StorageObjectException, ModuleException
37         {
38                 long                sessionConnectTime = 0;
39                 long                startTime = (new java.util.Date()).getTime();
40         boolean             iconProduced = false;
41         boolean             mediaProduced = false;
42                 String              whereClause;
43                 String              mediaHost;
44                 String              id2=null;
45                 String              iconPath;
46                 String              mediaPath;
47                 String              iconFilename;
48                 String              mediaFilename;
49                 String              productionPath;
50                 Entity              currentMedia;
51                 EntityList          batchEntityList;
52
53         int contentBatchsize = Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
54
55                 // get batch of non-produced medias, that are to be published
56                 whereClause="is_published='1'";
57                 if (id!= null)
58                         whereClause += " and id="+id;
59                 if (force==false) whereClause += " and is_produced='0'";
60
61                 batchEntityList = uploadedMediaModule.getByWhereClause(whereClause, null, 0, contentBatchsize);
62
63                 while (batchEntityList != null) {
64                         for(int i=0;i<batchEntityList.size();i++) {
65                 try {
66                     currentMedia = (Entity)batchEntityList.elementAt(i);
67
68                     Entity currentMediaType = DatabaseUploadedMedia.getInstance().getMediaType(currentMedia);
69                     String mediaHandlerName = currentMediaType.getValue("classname");
70                     Class mediaHandlerClass = Class.forName("mircoders.media.MediaHandler"+mediaHandlerName);
71                     MirMedia currentMediaHandler = (MirMedia)mediaHandlerClass.newInstance();
72
73                     String date = currentMedia.getValue("date");
74                     String datePath = StringUtil.webdbDate2path(date);
75
76                     // do icon
77                     if (!currentMedia.getValue("icon_is_produced").equals("1")) {
78                         iconPath = currentMediaHandler.getIconStoragePath();
79                         iconFilename = producerDocRoot+iconPath+datePath+
80                             currentMedia.getId()+"."+currentMediaType.getValue("name");
81                         iconProduced = produceFile(iconFilename, currentMediaHandler.getIcon(currentMedia), htmlout, true);
82                         if(iconProduced) {
83                             currentMedia.setValueForProperty("icon_path",iconFilename);
84                             logHTML(htmlout,"icon : " + iconFilename + (( iconProduced==true )? " succeded":" <font color=\"Red\"> failed!</font>"));
85                         }
86
87                     } else {
88                         iconProduced = true;
89                     }
90
91                     //now produce the media content
92
93                     mediaHost = currentMediaHandler.getPublishHost();
94                     mediaPath = currentMediaHandler.getStoragePath();
95                     id2 = currentMedia.getId();
96                     mediaFilename = currentMedia.getId()+"."+currentMediaType.getValue("name");
97                     //hack: make this a config option. -mh
98                     productionPath = mediaPath+datePath+"/"+ mediaFilename ;
99                     //productionPath = mediaPath+"/"+mediaFilename ;
100
101                     //hack: see above. -mh.
102                     currentMedia.setValueForProperty("publish_path",datePath+"/"+mediaFilename);
103                     //currentMedia.setValueForProperty("publish_path",mediaFilename);
104                     currentMedia.setValueForProperty("publish_server", mediaHost);
105
106                     theLog.printError("ABOUT OT FILE"+id2);
107                     mediaProduced = produceFile(productionPath, currentMediaHandler.get(currentMedia, currentMediaType), htmlout, false);
108                     logHTML(htmlout,"media: " + productionPath + ((mediaProduced==true)?" succeded":" <font color=\"Red\"> failed!</font>"));
109
110                     // update media-data
111                     if (iconProduced && mediaProduced) {
112                         currentMedia.setValueForProperty("icon_is_produced", "1");
113                         currentMedia.setValueForProperty("is_produced", "1");
114                         currentMedia.update();
115                     }
116                 } catch (MirMediaException e) {
117                     theLog.printError("media exception: "+id+e.toString());
118                     logHTML(htmlout, "problem with media id: "
119                         +id+" <font color=\"Red\"> failed!</font>: "
120                         +e.toString());
121                 } catch (Exception e) {
122                     theLog.printError("failed in reflection: "+id+e.toString());
123                     logHTML(htmlout, "reflection problem in media id: "
124                         +id+" <font color=\"Red\"> failed!</font>: "
125                         +e.toString());
126                 }
127                         }
128
129                         // if next batch get it...
130                         if (batchEntityList.hasNextBatch()){
131                                 batchEntityList = uploadedMediaModule.getByWhereClause(whereClause, null, batchEntityList.getNextBatch(),contentBatchsize);
132                         } else {
133                                 batchEntityList=null;
134                         }
135                 }
136                 // Finish
137                 sessionConnectTime = new java.util.Date().getTime() - startTime;
138                 logHTML(htmlout, "Producer.Media finished: " + sessionConnectTime + " ms.");
139
140         }
141
142 }