various fixes/cleanup: old producers are now completely gone, old logfile class too
[mir.git] / source / mircoders / servlet / ServletModuleUploadedMedia.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.servlet;
33
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.net.URLEncoder;
37 import java.util.HashMap;
38
39 import javax.servlet.ServletContext;
40 import javax.servlet.ServletOutputStream;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import javax.servlet.http.HttpSession;
44
45 import mir.config.MirPropertiesConfiguration;
46 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
47 import mir.entity.Entity;
48 import mir.entity.EntityList;
49 import mir.log.LoggerWrapper;
50 import mir.media.MediaHelper;
51 import mir.media.MirMedia;
52 import mir.misc.FileHandler;
53 import mir.misc.FileHandlerException;
54 import mir.misc.FileHandlerUserException;
55 import mir.misc.WebdbMultipartRequest;
56 import mir.module.ModuleException;
57 import mir.servlet.ServletModule;
58 import mir.servlet.ServletModuleException;
59 import mir.servlet.ServletModuleUserException;
60 import mir.storage.StorageObjectFailure;
61 import mircoders.entity.EntityUploadedMedia;
62 import mircoders.entity.EntityUsers;
63 import mircoders.media.MediaRequest;
64 import mircoders.storage.DatabaseMediafolder;
65 import freemarker.template.SimpleHash;
66 import freemarker.template.SimpleList;
67
68 /*
69  *  ServletModuleBilder -
70  *  liefert HTML fuer Bilder
71  *
72  * @version $Id: ServletModuleUploadedMedia.java,v 1.16 2003/02/23 05:00:15 zapata Exp $
73  * @author RK, the mir-coders group
74  */
75
76 public abstract class ServletModuleUploadedMedia
77         extends mir.servlet.ServletModule {
78
79   //private static DatabaseRights dbRights;
80
81   public static ServletModule getInstance() {
82     return null;
83   }
84
85   public ServletModuleUploadedMedia() {
86     super();
87     logger = new LoggerWrapper("ServletModule.UploadedMedia");
88   }
89
90   public void insert(HttpServletRequest req, HttpServletResponse res)
91           throws ServletModuleException, ServletModuleUserException {
92     try {
93       EntityUsers user = _getUser(req);
94       MediaRequest mediaReq =  new MediaRequest(user.getId(), false);
95       WebdbMultipartRequest mp = new WebdbMultipartRequest(req, (FileHandler)mediaReq);
96       EntityList mediaList = mediaReq.getEntityList();
97
98       SimpleHash mergeData = new SimpleHash();
99       SimpleHash popups = new SimpleHash();
100       mergeData.put("contentlist", mediaList);
101       if (mediaList.getOrder() != null) {
102         mergeData.put("order", mediaList.getOrder());
103         mergeData.put("order_encoded", URLEncoder.encode(mediaList.getOrder()));
104       }
105       mergeData.put("count", (new Integer(mediaList.getCount())).toString());
106       mergeData.put("from", (new Integer(mediaList.getFrom())).toString());
107       mergeData.put("to", (new Integer(mediaList.getTo())).toString());
108       if (mediaList.hasNextBatch())
109         mergeData.put("next", (new Integer(mediaList.getNextBatch())).toString());
110       if (mediaList.hasPrevBatch())
111           mergeData.put("prev", (new Integer(mediaList.getPrevBatch())).toString());
112       //fetch the popups
113       popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
114       // raus damit
115       deliver(req, res, mergeData, popups, templateListString);
116     } catch (FileHandlerUserException e) {
117       logger.error("ServletModuleUploadedMedia.insert: " + e.getMessage());
118       throw new ServletModuleUserException(e.getMessage());
119     } catch (FileHandlerException e) {
120       throw new ServletModuleException(
121               "upload -- media handling exception " + e.toString());
122     } catch (StorageObjectFailure e) {
123       throw new ServletModuleException("upload -- storageobjectexception "
124                                       + e.toString());
125     } catch (IOException e) {
126       throw new ServletModuleException("upload -- ioexception " + e.toString());
127     } catch (PropertiesConfigExc e) {
128       throw new ServletModuleException("upload -- configexception " + e.toString());
129     }
130   }
131
132   public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
133
134     try {
135       EntityUsers user = _getUser(req);
136       WebdbMultipartRequest mp = new WebdbMultipartRequest(req, null);
137       HashMap parameters = mp.getParameters();
138
139       parameters.put("to_publisher", user.getId());
140       parameters.put("is_produced", "0");
141       if (!parameters.containsKey("is_published"))
142         parameters.put("is_published", "0");
143
144       String id = mainModule.set(parameters);
145       logger.debug("update: media ID = " + id);
146       _edit(id, req, res);
147     }
148     catch (IOException e) {
149       throw new ServletModuleException("upload -- ioexception " + e.toString());
150     }
151     catch (ModuleException e) {
152       throw new ServletModuleException("upload -- moduleexception " + e.toString());
153     }
154     catch (Exception e) {
155       throw new ServletModuleException("upload -- exception " + e.toString());
156     }
157
158   }
159
160
161   public void list(HttpServletRequest req, HttpServletResponse res)
162           throws ServletModuleException {
163     // Parameter auswerten
164     SimpleHash mergeData = new SimpleHash();
165     SimpleHash popups = new SimpleHash();
166
167     String query_text = req.getParameter("query_text");
168     mergeData.put("query_text", query_text);
169     if (query_text != null) mergeData.put("query_text_encoded", URLEncoder.encode(query_text));
170     String query_field = req.getParameter("query_field");
171     mergeData.put("query_field", query_field);
172     String query_is_published = req.getParameter("query_is_published");
173     mergeData.put("query_is_published", query_is_published);
174     String query_media_folder = req.getParameter("query_media_folder");
175     mergeData.put("query_media_folder", query_media_folder);
176     String offset = req.getParameter("offset");
177     if (offset == null || offset.equals("")) offset = "0";
178     mergeData.put("offset", offset);
179
180     String order = req.getParameter("order");
181     if (order == null || order.equals("")) order = "webdb_lastchange desc";
182
183     // if in connection mode to content
184     String cid = req.getParameter("cid");
185     mergeData.put("cid", cid);
186
187
188     // sql basteln
189     String whereClause = "";
190     boolean isFirst = true;
191     if (query_text != null && !query_text.equalsIgnoreCase("")) {
192       whereClause += "lower(" + query_field + ") like lower('%" + query_text + "%')";
193       isFirst = false;
194     }
195     if (query_is_published != null && !query_is_published.equals("")) {
196       if (isFirst == false) whereClause += " and ";
197       whereClause += "is_published='" + query_is_published + "'";
198       isFirst = false;
199     }
200     if (query_media_folder != null && !query_media_folder.equals("")) {
201       if (isFirst == false) whereClause += " and ";
202       whereClause += "to_media_folder='" + query_media_folder + "'";
203     }
204     //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
205
206     // fetch and deliver
207     try {
208       if (query_text != null || query_is_published != null || query_media_folder != null) {
209         EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(), 10);
210         if (theList != null) {
211           mergeData.put("contentlist", theList);
212           if (theList.getOrder() != null) {
213             mergeData.put("order", theList.getOrder());
214             mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
215           }
216           mergeData.put("count", (new Integer(theList.getCount())).toString());
217           mergeData.put("from", (new Integer(theList.getFrom())).toString());
218           mergeData.put("to", (new Integer(theList.getTo())).toString());
219           if (theList.hasNextBatch())
220             mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
221           if (theList.hasPrevBatch())
222             mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
223         }
224       }
225       popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
226
227       deliver(req, res, mergeData, popups, templateListString);
228     }
229     catch (ModuleException e) {
230       throw new ServletModuleException(e.toString());
231     }
232     catch (Exception e) {
233       throw new ServletModuleException(e.toString());
234     }
235   }
236
237
238   public void add(HttpServletRequest req, HttpServletResponse res)
239           throws ServletModuleException {
240     try {
241       SimpleHash mergeData = new SimpleHash();
242       mergeData.put("new", "1");
243       SimpleHash popups = new SimpleHash();
244       popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
245       String maxMedia = MirPropertiesConfiguration.instance().getString("ServletModule.OpenIndy.MaxMediaUploadItems");
246       String numOfMedia = req.getParameter("medianum");
247       if(numOfMedia==null||numOfMedia.equals("")){
248         numOfMedia="1";
249       } else if(Integer.parseInt(numOfMedia) > Integer.parseInt(maxMedia)) {
250         numOfMedia = maxMedia;
251       }
252
253       int mediaNum = Integer.parseInt(numOfMedia);
254       SimpleList mediaFields = new SimpleList();
255       for(int i =0; i<mediaNum;i++){
256         Integer mNum = new Integer(i+1);
257         mediaFields.add(mNum.toString());
258       }
259       mergeData.put("medianum",numOfMedia);
260       mergeData.put("mediafields",mediaFields);
261       deliver(req, res, mergeData, popups, templateObjektString);
262     } catch (Exception e) {
263       throw new ServletModuleException(e.toString());
264     }
265   }
266
267   public void edit(HttpServletRequest req, HttpServletResponse res)
268           throws ServletModuleException {
269     String idParam = req.getParameter("id");
270     _edit(idParam, req, res);
271   }
272
273   private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res)
274           throws ServletModuleException {
275     if (idParam != null && !idParam.equals("")) {
276       try {
277         SimpleHash popups = new SimpleHash();
278         popups.put("mediafolderPopupData", DatabaseMediafolder.getInstance().getPopupData());
279         deliver(req, res, mainModule.getById(idParam), popups,
280                 templateObjektString);
281       }
282       catch (ModuleException e) {
283         throw new ServletModuleException(e.toString());
284       }
285       catch (StorageObjectFailure e) {
286         throw new ServletModuleException(e.toString());
287       }
288     }
289     else {
290       throw new ServletModuleException("ServletmoduleUploadedMedia :: _edit without id");
291     }
292   }
293
294
295   /** @todo should be in ServletModule.java */
296   private EntityUsers _getUser(HttpServletRequest req) {
297     HttpSession session = req.getSession(false);
298     return (EntityUsers) session.getAttribute("login.uid");
299   }
300
301   public void getMedia(HttpServletRequest req, HttpServletResponse res)
302     throws ServletModuleException
303   {
304     String idParam = req.getParameter("id");
305     if (idParam!=null && !idParam.equals("")) {
306       try {
307         EntityUploadedMedia ent = (EntityUploadedMedia)mainModule.getById(idParam);
308         Entity mediaType = ent.getMediaType();
309         MirMedia mediaHandler;
310
311         ServletContext ctx = MirPropertiesConfiguration.getContext();
312         String fName = ent.getId()+"."+mediaType.getValue("name");
313
314         mediaHandler = MediaHelper.getHandler(mediaType);
315         InputStream in = mediaHandler.getMedia(ent, mediaType);
316
317         res.setContentType(ctx.getMimeType(fName));
318         //important that before calling this res.getWriter was not called first
319         ServletOutputStream out = res.getOutputStream();
320
321         int read ;
322         byte[] buf = new byte[8 * 1024];
323         while((read = in.read(buf)) != -1) {
324           out.write(buf, 0, read);
325         }
326         in.close();
327         out.close();
328       }
329
330       catch (IOException e) {
331         throw new ServletModuleException(e.toString());
332       }
333       catch (ModuleException e) {
334         throw new ServletModuleException(e.toString());
335       }
336       catch (Exception e) {
337         throw new ServletModuleException(e.toString());
338       }
339     }
340     else logger.error("id not specified.");
341     // no exception allowed
342   }
343
344   public void getIcon(HttpServletRequest req, HttpServletResponse res)
345     throws ServletModuleException
346   {
347     String idParam = req.getParameter("id");
348     if (idParam!=null && !idParam.equals("")) {
349       try {
350         EntityUploadedMedia ent = (EntityUploadedMedia)mainModule.getById(idParam);
351         Entity mediaType = ent.getMediaType();
352         MirMedia mediaHandler;
353
354         ServletContext ctx = MirPropertiesConfiguration.getContext();
355         String fName = ent.getId()+"."+mediaType.getValue("name");
356
357         mediaHandler = MediaHelper.getHandler(mediaType);
358         InputStream in = mediaHandler.getIcon(ent);
359
360         res.setContentType(ctx.getMimeType(fName));
361         //important that before calling this res.getWriter was not called first
362         ServletOutputStream out = res.getOutputStream();
363
364         int read ;
365         byte[] buf = new byte[8 * 1024];
366         while((read = in.read(buf)) != -1) {
367           out.write(buf, 0, read);
368         }
369         in.close();
370         out.close();
371       }
372
373       catch (IOException e) {
374         throw new ServletModuleException(e.toString());
375       }
376       catch (ModuleException e) {
377         throw new ServletModuleException(e.toString());
378       }
379       catch (Exception e) {
380         throw new ServletModuleException(e.toString());
381       }
382     }
383     else logger.error("getIcon: id not specified.");
384     // no exception allowed
385   }
386
387 }
388
389