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