get rid of deprecated makeSimpleList and makeSimpleHash usage. this should be it...
[mir.git] / source / mircoders / servlet / ServletModuleImages.java
1 package mircoders.servlet;
2
3 import java.io.*;
4 import java.lang.*;
5 import java.sql.*;
6 import java.util.*;
7 import java.net.*;
8 import java.lang.reflect.*;
9 import javax.servlet.*;
10 import javax.servlet.http.*;
11
12 import freemarker.template.*;
13 import com.oreilly.servlet.multipart.*;
14 import com.oreilly.servlet.*;
15
16 import mir.servlet.*;
17 import mir.module.*;
18 import mir.misc.*;
19 import mir.entity.*;
20 import mir.storage.*;
21 import mir.media.*;
22
23 import mircoders.entity.*;
24 import mircoders.storage.*;
25 import mircoders.module.*;
26 import mircoders.producer.*;
27
28 /*
29  *  ServletModuleBilder -
30  *  liefert HTML fuer Bilder
31  *
32  *
33  * @author RK
34  */
35
36 public class ServletModuleImages extends mir.servlet.ServletModule
37 {
38
39   //private static DatabaseRights dbRights;
40
41   // Singelton / Kontruktor
42   private static ServletModuleImages instance = new ServletModuleImages();
43   public static ServletModule getInstance() { return instance; }
44
45
46   private ServletModuleImages() {
47     theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("ServletModule.Bilder.Logfile"));
48     templateListString = MirConfig.getProp("ServletModule.Bilder.ListTemplate");
49     templateObjektString = MirConfig.getProp("ServletModule.Bilder.ObjektTemplate");
50     templateConfirmString = MirConfig.getProp("ServletModule.Bilder.ConfirmTemplate");
51     try {
52       mainModule = new ModuleImages(DatabaseImages.getInstance());
53       //dbRights = DatabaseRights.getInstance();
54     }
55     catch (StorageObjectException e) {
56       theLog.printDebugInfo("servletmodulebilder konnte nicht initialisiert werden");
57     }
58   }
59
60
61   public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
62   {
63     try {
64       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
65       HashMap parameters = mp.getParameters();
66       String mediaId=null;
67       MpRequest mpReq = (MpRequest)mp.requestList.get(0);
68       byte[] imageData=mpReq.getMedia();
69       String fileName=mpReq.getFilename();
70       String contentType= mpReq.getContentType();
71
72       EntityUsers   user = _getUser(req);
73       parameters.put("date", StringUtil.date2webdbDate(new GregorianCalendar()));
74       parameters.put("to_publisher", user.getId());
75       parameters.put("is_produced", "0");
76       if (!parameters.containsKey("is_published"))
77         parameters.put("is_published","0");
78
79       String id = mainModule.add(parameters);
80       EntityImages entImage = (EntityImages)mainModule.getById(id);
81
82       if (imageData!=null && fileName!=null) {
83             //the where clause to find the media_type entry
84         //from the content-type.
85         //we use the media type entry to lookup the
86         //media Handler/Storage classes
87         String wc = " mime_type='"+contentType+"'";
88
89         EntityList mediaTypesList = DatabaseMediaType.getInstance().selectByWhereClause(wc);
90             String mediaTypeId = null;
91         String mediaStorageName = null;
92         String mediaHandlerName = null;
93
94         //if we found an entry matching the
95         //content-type int the table.
96         if (mediaTypesList.size() > 0) {
97           //get the class names from the media_type table.
98           mediaTypeId = mediaTypesList.elementAt(0).getId();
99           mediaStorageName = mediaTypesList.elementAt(0).getValue("tablename");
100           mediaHandlerName = mediaTypesList.elementAt(0).getValue("classname");
101           parameters.put("to_media_type",mediaTypeId);
102
103           //load the classes via reflection
104           String MediaId;
105           try {
106                 Class mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
107                 Method m = mediaStorageClass.getMethod("getInstance", null);
108                 Database mediaStorage = (Database)m.invoke(null, null);
109                 Entity mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
110                 mediaEnt.setStorage(mediaStorage);
111                 mediaEnt.setValues(parameters);
112                 mediaId = mediaEnt.insert();
113
114                 Class mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
115                 MirMedia mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
116                 //save and store the media data/metadata
117                 mediaHandler.set(mpReq.getMedia(), mediaEnt,mediaTypesList.elementAt(0));
118
119                 //were done with mpReq at this point, dereference it. as it contains
120                 //mucho mem. -mh 01.10.2001
121                 mpReq=null;
122
123               if(mediaId!=null){
124                 new ProducerMedia().handle(null, null, false, false, mediaId);
125               }
126               } catch (Exception e) {
127                 theLog.printError("setting uploaded_media failed: "+e.toString());
128           } //end try-catch
129
130
131         entImage.setImage(imageData);
132       }
133       _edit(id, req, res);
134       }
135     }
136     catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
137     catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
138     catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
139
140   }
141
142   public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
143   {
144
145     try {
146       WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
147       HashMap parameters = mp.getParameters();
148       //MpRequest mpReq = (MpRequest)mp.requestList.get(0);
149       //byte[] imageData=mpReq.getMedia();
150       //String fileName=mpReq.getFilename();
151       //String contentType=mpReq.getContentType();
152
153       EntityUsers   user = _getUser(req);
154       parameters.put("to_publisher", user.getId());
155       parameters.put("is_produced", "0");
156       if (!parameters.containsKey("is_published"))
157         parameters.put("is_published","0");
158
159       String id = mainModule.set(parameters);
160       theLog.printError("Image ID"+id);
161       _edit(id, req, res);
162     }
163     catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
164     catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
165
166   }
167
168
169   public void showimg(HttpServletRequest req, HttpServletResponse res)
170     throws ServletModuleException
171   {
172     String idParam = req.getParameter("id");
173     if (idParam!=null && !idParam.equals("")) {
174       try {
175         EntityImages entImage =(EntityImages)mainModule.getById(idParam);
176         res.setContentType("image/jpeg"); // testweise
177         ServletOutputStream out = res.getOutputStream(); // wichtig, dass vorher kein res.getwriter() gelaufen ist
178
179         byte[] outbytes = entImage.getImage();
180         out.write(outbytes);
181         out.close();
182       }
183
184       catch (IOException e) {throw new ServletModuleException(e.toString());}
185       catch (ModuleException e) {throw new ServletModuleException(e.toString());}
186       catch (Exception e) {throw new ServletModuleException(e.toString());}
187     }
188     else theLog.printDebugInfo("id nicht angeben.");
189     // darf keine exception werfen
190   }
191
192   public void showicon(HttpServletRequest req, HttpServletResponse res)
193     throws ServletModuleException
194   {
195     String idParam = req.getParameter("id");
196     if (idParam!=null && !idParam.equals("")) {
197       try {
198         EntityImages entImage =(EntityImages)mainModule.getById(idParam);
199         res.setContentType("image/jpeg"); // testweise
200         ServletOutputStream out = res.getOutputStream(); // wichtig, dass vorher kein res.getwriter() gelaufen ist
201
202         byte[] outbytes = entImage.getIcon();
203         out.write(outbytes);
204         out.close();
205       }
206
207       catch (IOException e) {throw new ServletModuleException(e.toString());}
208       catch (ModuleException e) {throw new ServletModuleException(e.toString());}
209       catch (Exception e) {throw new ServletModuleException(e.toString());}
210     }
211     else throw new ServletModuleException("id nicht angeben.");
212   }
213
214   public void list(HttpServletRequest req, HttpServletResponse res)
215     throws ServletModuleException
216   {
217       // Parameter auswerten
218       SimpleHash mergeData = new SimpleHash();
219       String query_text = req.getParameter("query_text");
220       mergeData.put("query_text",query_text);
221       if (query_text!=null) mergeData.put("query_text_encoded",URLEncoder.encode(query_text));
222       String query_field = req.getParameter("query_field");
223       mergeData.put("query_field",query_field);
224       String query_is_published = req.getParameter("query_is_published");
225       mergeData.put("query_is_published",query_is_published);
226       String query_media_folder = req.getParameter("query_media_folder");
227       mergeData.put("query_media_folder",query_media_folder);
228       String offset = req.getParameter("offset");
229       if (offset==null || offset.equals("")) offset="0";
230       mergeData.put("offset",offset);
231
232       String order = req.getParameter("order");
233       if (order==null) order="webdb_lastchange desc";
234
235       // if in connection mode to content
236       String cid = req.getParameter("cid");
237       mergeData.put("cid",cid);
238
239
240       // sql basteln
241       String whereClause=""; boolean isFirst=true;
242       if (query_text!=null && !query_text.equalsIgnoreCase("")) {
243         whereClause += "lower("+query_field+") like lower('%"+query_text+"%')"; isFirst=false;}
244       if (query_is_published != null && !query_is_published.equals("")) {
245         if (isFirst==false) whereClause+=" and ";
246         whereClause += "is_published='"+query_is_published+"'";
247         isFirst=false;
248       }
249       if (query_media_folder != null && !query_media_folder.equals("")) {
250         if (isFirst==false) whereClause+=" and ";
251         whereClause += "to_media_folder='"+query_media_folder+"'";
252       }
253       //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
254
255       // fetch und ausliefern
256       try {
257         if (query_text!=null || query_is_published!=null || query_media_folder!=null) {
258           EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(),10);
259           if (theList != null) {
260             mergeData.put("contentlist",theList);
261             if(theList.getOrder()!=null) {
262               mergeData.put("order", theList.getOrder());
263               mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
264             }
265             mergeData.put("count", (new Integer(theList.getCount())).toString());
266             mergeData.put("from", (new Integer(theList.getFrom())).toString());
267             mergeData.put("to", (new Integer(theList.getTo())).toString());
268             if (theList.hasNextBatch())
269               mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
270             if (theList.hasPrevBatch())
271               mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
272           }
273         }
274         // raus damit
275         HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString,
276                                                                                                                                                         mergeData, res.getWriter(),req.getLocale());
277       }
278       catch (ModuleException e) {throw new ServletModuleException(e.toString());}
279       catch (IOException e) {throw new ServletModuleException(e.toString());}
280       catch (Exception e) {throw new ServletModuleException(e.toString());}
281   }
282
283
284   public void add(HttpServletRequest req, HttpServletResponse res)
285   throws ServletModuleException
286   {
287     try {
288       SimpleHash mergeData = new SimpleHash();
289       mergeData.put("new", "1");
290       deliver(req, res, mergeData, templateObjektString);
291     }
292     catch (Exception e) { throw new ServletModuleException(e.toString());}
293   }
294
295   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
296   {
297     String        idParam = req.getParameter("id");
298     _edit(idParam, req, res);
299   }
300
301   private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
302     if (idParam!=null && !idParam.equals("")) {
303       try {
304         //mergeData.put("rightsHashdata", dbRights.getHashData());
305         deliver(req, res, mainModule.getById(idParam), templateObjektString);
306       }
307       catch (ModuleException e) { throw new ServletModuleException(e.toString());}
308     }
309     else throw new ServletModuleException("ServletmoduleImage :: _edit without id");
310   }
311
312
313   /** @todo should be in ServletModule.java */
314   private EntityUsers _getUser(HttpServletRequest req)
315   {
316     HttpSession session=req.getSession(false);
317     return (EntityUsers)session.getAttribute("login.uid");
318   }
319
320
321   // deprecated
322   public void upload(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
323   {
324     //theLog.printDebugInfo("-- trying to upload");
325     String idParam = req.getParameter("id");
326     if (idParam!=null && !idParam.equals("")) {
327       try {
328
329         WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
330         HashMap withValues = mp.getParameters();
331         MpRequest mpReq = (MpRequest)mp.requestList.get(0);
332         byte[] imageData=mpReq.getMedia();
333         String fileName=mpReq.getFilename();
334         String contentType=mpReq.getContentType();
335
336         EntityImages entImage = (EntityImages)mainModule.getById(idParam);
337         entImage.setImage(imageData);
338       }
339       catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
340       catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
341       catch (StorageObjectException e) { throw new ServletModuleException("upload -- storageobjectexception " + e.toString());}
342     }
343     else // keine id
344       throw new ServletModuleException("Keine id angegeben");
345     edit(req,res);
346   }
347
348
349 }