make sure that batches have the correct "ORDER BY".. same problem in ProducerImages...
[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, false,false);
24                 } catch(Exception e) { System.err.println(e.toString()); }
25         }
26
27         public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync)
28                 throws StorageObjectException, ModuleException {
29                 handle(htmlout,user,force,sync,null);
30         }
31
32         public void handle(PrintWriter htmlout, EntityUsers user, boolean force, boolean sync, String id)
33                 throws StorageObjectException, ModuleException
34         {
35                 long                sessionConnectTime = 0;
36                 long                startTime = (new java.util.Date()).getTime();
37                 String              whereClause;
38                 String              iconFilename;
39                 String              imageFilename;
40                 String              productionPath;
41                 EntityImages         currentImage;
42                 EntityList          batchEntityList;
43         String              orderBy = "date desc, webdb_lastchange desc";
44
45         int contentBatchsize = Integer.parseInt(MirConfig.getProp("Producer.Content.Batchsize"));
46         String imageHost = MirConfig.getProp("Producer.Image.Host");
47         String imagePath = MirConfig.getProp("Producer.Image.Path");
48         String iconPath = MirConfig.getProp("Producer.Image.IconPath");
49
50                 // get batch of non-produced images, that are to be published
51                 whereClause="is_published='1'";
52                 if (id!= null)
53                         whereClause += " and id="+id;
54                 if (force==false) whereClause += " and icon_is_produced='0'";
55
56                 batchEntityList = imageModule.getByWhereClause(whereClause, orderBy, 0, contentBatchsize);
57                 theLog.printDebugInfo("whereclause: " + whereClause);
58
59
60                 while (batchEntityList != null) {
61                         for(int i=0;i<batchEntityList.size();i++) {
62                                 theLog.printDebugInfo("trying image: " + i);
63                                 currentImage = (EntityImages)batchEntityList.elementAt(i);
64
65             try {
66                                 // make filenames
67                                 String date = currentImage.getValue("date");
68
69                                 iconFilename = producerDocRoot + iconPath + StringUtil.webdbDate2path(date) +
70                                         currentImage.getId() + (( currentImage.getValue("to_img_type").equals("0") ) ? ".jpg":".gif");
71
72                                 imageFilename = currentImage.getId() + (( currentImage.getValue("to_img_type").equals("0") ) ? ".jpg":".gif");
73                                 productionPath = imagePath + "/" + imageFilename ;
74
75                                 currentImage.setValueForProperty("icon_path",iconFilename);
76                 currentImage.setValueForProperty("publish_path",imageFilename);
77                                 currentImage.setValueForProperty("publish_server", imageHost);
78
79                                 if (currentImage.getValue("icon_data")!= null && currentImage.getValue("image_data")!= null) {
80                                         // make icon
81                                         boolean iconProduced = produceFile(iconFilename, currentImage.getIcon(), htmlout, true);
82                                         logHTML(htmlout,"icon : " + iconFilename + (( iconProduced==true )? " succeded":" <font color=\"Red\" failed!</font>"));
83                                         // make image
84                                         boolean imageProduced = produceFile(productionPath, currentImage.getImage(), htmlout, false);
85                                         logHTML(htmlout,"image: " + productionPath + ((imageProduced==true)?" succeded":" <font color=\"Red\" failed!</font>"));
86
87                                         // update image-data
88                                         if (iconProduced && imageProduced) {
89                                                 currentImage.setValueForProperty("icon_is_produced", "1");
90                                                 currentImage.update();
91                                         }
92
93                                 }
94             } catch (Exception e) {
95                logHTML(htmlout, "Producer.Images ERROR with: image ID " + currentImage.getId() + " <font color=\"Red\"> failed!</font>");
96             }
97                         }//end for
98
99                         // if next batch get it...
100                         if (batchEntityList.hasNextBatch()){
101                                 batchEntityList = imageModule.getByWhereClause(whereClause, 
102                                     orderBy, batchEntityList.getNextBatch(),
103                                     contentBatchsize);
104                         } else {
105                                 batchEntityList=null;
106                         }
107                 }
108                 // Finish
109                 sessionConnectTime = new java.util.Date().getTime() - startTime;
110                 logHTML(htmlout, "Producer.Images finished: " + sessionConnectTime + " ms.");
111
112         }
113
114 }