code cleaning, new config
[mir.git] / source / mircoders / producer / ProducerMedia.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.producer;
33
34 import java.io.PrintWriter;
35
36 import mir.entity.Entity;
37 import mir.entity.EntityList;
38 import mir.media.MediaHelper;
39 import mir.media.MirMedia;
40 import mir.module.ModuleException;
41 import mir.storage.Database;
42 import mir.storage.StorageObjectFailure;
43 import mircoders.entity.EntityUsers;
44 import mircoders.storage.DatabaseUploadedMedia;
45
46 abstract public class ProducerMedia extends Producer {
47
48   abstract Database getStorage() throws StorageObjectFailure;
49
50   public void handle(PrintWriter htmlout, EntityUsers user, boolean force,
51     boolean sync) throws StorageObjectFailure, ModuleException {
52     handle(htmlout,user,force,sync,null);
53   }
54
55   public void handle(PrintWriter htmlout,EntityUsers user,boolean force,
56     boolean sync, String id) throws StorageObjectFailure, ModuleException
57   {
58     long                sessionConnectTime = 0;
59     long                startTime = (new java.util.Date()).getTime();
60     String              whereClause;
61     String              orderBy;
62     Entity              currentMedia;
63     MirMedia            currentMediaHandler;
64     EntityList          batchEntityList;
65
66     int contentBatchsize =
67             Integer.parseInt(configuration.getString("Producer.Content.Batchsize"));
68     orderBy = "webdb_lastchange desc";
69
70     // get batch of non-produced medias, that are to be published
71     whereClause="is_published='1'";
72     if (id!= null) {
73       whereClause += " and id="+id;
74       // optimization to avoid select count(*)..
75       contentBatchsize = -1;
76     }
77     if (force==false) whereClause += " and is_produced='0'";
78
79     batchEntityList = getStorage().selectByWhereClause(whereClause,
80                                                 orderBy, 0, contentBatchsize);
81
82     while (batchEntityList != null) {
83       for(int i=0;i<batchEntityList.size();i++) {
84         currentMedia = (Entity)batchEntityList.elementAt(i);
85         try {
86           Entity currentMediaType =
87                 DatabaseUploadedMedia.getInstance().getMediaType(currentMedia);
88           currentMediaHandler = MediaHelper.getHandler( currentMediaType );
89
90           // now produce
91           currentMediaHandler.produce(currentMedia,currentMediaType);
92           currentMedia.setValueForProperty("publish_server",
93                                         currentMediaHandler.getPublishHost());
94           currentMedia.setValueForProperty("icon_is_produced", "1");
95           currentMedia.setValueForProperty("is_produced", "1");
96           currentMedia.update();
97           logHTML(htmlout,"produced media id "+currentMedia.getId()
98                   +": "+currentMediaType.getValue("mime_type")+" success");
99         } catch (Exception e) {
100           // don't throw and exception here, just log.
101           // we don't want to make the admin interface unuseable
102           theLog.printError("media exception: "+currentMedia.getId()+
103                             e.toString());
104           logHTML(htmlout, "problem with media id: "+currentMedia.getId()+
105                   " <font color=\"Red\"> failed!</font>: "+e.toString());
106           e.printStackTrace(System.out);
107         }
108       }
109
110       // if next batch get it...
111       if (batchEntityList.hasNextBatch()){
112         batchEntityList = uploadedMediaModule.getByWhereClause(whereClause,
113           orderBy, batchEntityList.getNextBatch(),contentBatchsize);
114       } else {
115         batchEntityList=null;
116       }
117     }
118     // Finish
119     sessionConnectTime = new java.util.Date().getTime() - startTime;
120     logHTML(htmlout, "Producer.Media finished: " + sessionConnectTime + " ms.");
121   }
122
123 }