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