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