first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[mir.git] / source / mircoders / producer / ProducerImages.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
14 import mircoders.entity.*;
15 import mir.storage.*;
16
17
18
19 public class ProducerImages extends Producer {
20
21         public static void main(String argv[]){
22                 try {
23                         new ProducerContent().handle(new PrintWriter(System.out), null,
24                                         false,false);
25                 } catch(Exception e) { System.err.println(e.toString()); }
26         }
27
28         public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
29                         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,
35                         boolean sync, String id)
36                 throws StorageObjectException, ModuleException
37         {
38                 long                sessionConnectTime = 0;
39                 long                startTime = (new java.util.Date()).getTime();
40                 String              whereClause;
41                 String              iconFilename;
42                 String              imageFilename;
43                 String              productionPath;
44                 EntityImages         currentImage;
45                 EntityList          batchEntityList;
46         String              orderBy = "date desc, webdb_lastchange desc";
47
48         int contentBatchsize = Integer.parseInt(MirConfig.getProp(
49                                         "Producer.Content.Batchsize"));
50         String imageHost = MirConfig.getProp("Producer.Image.Host");
51         String imagePath = MirConfig.getProp("Producer.Image.Path");
52         String iconPath = MirConfig.getProp("Producer.Image.IconPath");
53
54                 // get batch of non-produced images, 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 icon_is_produced='0'";
59
60                 batchEntityList = imageModule.getByWhereClause(whereClause, orderBy, 0,
61                                                         contentBatchsize);
62                 theLog.printDebugInfo("whereclause: " + whereClause);
63
64
65                 while (batchEntityList != null) {
66                         for(int i=0;i<batchEntityList.size();i++) {
67                                 theLog.printDebugInfo("trying image: " + i);
68                                 currentImage = (EntityImages)batchEntityList.elementAt(i);
69
70             try {
71                                 // make filenames
72                                 String date = currentImage.getValue("date");
73                 String datePath = StringUtil.webdbDate2path(date);
74
75                                 iconFilename=producerDocRoot + iconPath + datePath +
76                              currentImage.getId() +
77                              ((currentImage.getValue("to_img_type").equals("0"))
78                              ? ".jpg":".gif");
79
80                                 imageFilename=datePath + currentImage.getId() +
81                              ((currentImage.getValue("to_img_type").equals("0"))
82                              ? ".jpg":".gif");
83                                 productionPath = imagePath + "/" + imageFilename ;
84
85                                 currentImage.setValueForProperty("icon_path",iconFilename);
86                 currentImage.setValueForProperty("publish_path",imageFilename);
87                                 currentImage.setValueForProperty("publish_server", imageHost);
88
89                                 if (currentImage.getValue("icon_data")!= null &&
90                     currentImage.getValue("image_data")!= null) {
91                                         // make icon
92                                         boolean iconProduced = produceFile(iconFilename,
93                                                        currentImage.getIcon(),
94                                                        htmlout, true);
95                                         logHTML(htmlout,"icon : "+iconFilename+((
96                             iconProduced==true )?
97                             " succeded":" <font color=\"Red\" failed!</font>"));
98                                         // make image
99                                         boolean imageProduced = produceFile(productionPath,
100                                                         currentImage.getImage(),
101                                                         htmlout, false);
102                                         logHTML(htmlout,"image: "+productionPath +
103                             ((imageProduced==true)?
104                             " succeded":" <font color=\"Red\" failed!</font>"));
105
106                                         // update image-data
107                                         if (iconProduced && imageProduced) {
108                                                 currentImage.setValueForProperty("icon_is_produced",
109                                                         "1");
110                                                 currentImage.update();
111                                         }
112
113                                 }
114             } catch (Exception e) {
115                logHTML(htmlout, "Producer.Images ERROR with: image ID "+
116                        currentImage.getId() +
117                        " <font color=\"Red\"> failed!</font>");
118             }
119                         }//end for
120
121                         // if next batch get it...
122                         if (batchEntityList.hasNextBatch()){
123                                 batchEntityList = imageModule.getByWhereClause(whereClause, 
124                                     orderBy, batchEntityList.getNextBatch(),
125                                     contentBatchsize);
126                         } else {
127                                 batchEntityList=null;
128                         }
129                 }
130                 // Finish
131                 sessionConnectTime = new java.util.Date().getTime() - startTime;
132                 logHTML(htmlout, "Producer.Images finished: " + sessionConnectTime +
133                 " ms.");
134
135         }
136
137 }