Got rid of AbstractEntity.java / deprecated Entity.getValues() and HTMLTemplateProces...
[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              iconPath;
44                 String              mediaPath;
45                 String              iconFilename;
46                 String              mediaFilename;
47                 String              productionPath;
48                 Entity              currentMedia;
49                 EntityList          batchEntityList;
50
51     int contentBatchsize = Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
52
53                 // get batch of non-produced medias, that are to be published
54                 whereClause="is_published='1'";
55                 if (id!= null)
56                         whereClause += " and id="+id;
57                 if (force==false) whereClause += " and is_produced='0'";
58
59                 batchEntityList = uploadedMediaModule.getByWhereClause(whereClause, null, 0, contentBatchsize);
60
61                 while (batchEntityList != null) {
62                         for(int i=0;i<batchEntityList.size();i++) {
63                 try {
64                     currentMedia = (Entity)batchEntityList.elementAt(i);
65
66                     Entity currentMediaType = DatabaseUploadedMedia.getInstance().getMediaType(currentMedia);
67                     String mediaHandlerName = currentMediaType.getValue("classname");
68                     Class mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
69                     MirMedia currentMediaHandler = (MirMedia)mediaHandlerClass.newInstance();       
70                         
71                     String date = currentMedia.getValue("date");
72                     String datePath = StringUtil.webdbDate2path(date);
73
74                     // do icon
75                     if (currentMedia.getValue("icon_is_produced") != "1") {
76                         iconPath = currentMediaHandler.getIconStoragePath();
77                         iconFilename = producerDocRoot+iconPath+datePath+
78                             currentMedia.getId()+"."+currentMediaType.getValue("name");
79                         iconProduced = produceFile(iconFilename, currentMediaHandler.getIcon(currentMedia), htmlout, true);
80                         if(iconProduced) {
81                             currentMedia.setValueForProperty("icon_path",iconFilename);
82                             logHTML(htmlout,"icon : " + iconFilename + (( iconProduced==true )? " succeded":" <font color=\"Red\" failed!</font>"));
83                         }
84
85                     } else {
86                         iconProduced = true;
87                     }
88                     
89                     //now produce the media content
90
91                     mediaHost = currentMediaHandler.getPublishHost();
92                     mediaPath = currentMediaHandler.getStoragePath();
93                     mediaFilename = currentMedia.getId()+"."+currentMediaType.getValue("name");
94                     //hack: make this a config option. -mh
95                     //productionPath = mediaPath+datePath+"/"+ mediaFilename ;
96                     productionPath = mediaPath+"/"+mediaFilename ;
97
98                     //hack: see above. -mh.
99                     //currentMedia.setValueForProperty("publish_path",datePath+"/"+mediaFilename);
100                     currentMedia.setValueForProperty("publish_path",mediaFilename);
101                     currentMedia.setValueForProperty("publish_server", mediaHost);
102
103                     mediaProduced = produceFile(productionPath, currentMediaHandler.get(currentMedia, currentMediaType), htmlout, false);
104                     logHTML(htmlout,"media: " + productionPath + ((mediaProduced==true)?" succeded":" <font color=\"Red\" failed!</font>"));
105
106                     // update media-data
107                     if (iconProduced && mediaProduced) {
108                         currentMedia.setValueForProperty("icon_is_produced", "1");
109                         currentMedia.setValueForProperty("is_produced", "1");
110                         currentMedia.update();
111                     }
112
113                 } catch (Exception e) {
114                     theLog.printError("failed in reflection: "+e);
115                 }
116                         }
117
118                         // if next batch get it...
119                         if (batchEntityList.hasNextBatch()){
120                                 batchEntityList = uploadedMediaModule.getByWhereClause(whereClause, null, batchEntityList.getNextBatch(),contentBatchsize);
121                         } else {
122                                 batchEntityList=null;
123                         }
124                 }
125                 // Finish
126                 sessionConnectTime = new java.util.Date().getTime() - startTime;
127                 logHTML(htmlout, "Producer.Media finished: " + sessionConnectTime + " ms.");
128
129         }
130
131 }