1 package mircoders.servlet;
8 import java.lang.reflect.*;
9 import javax.servlet.*;
10 import javax.servlet.http.*;
12 import freemarker.template.*;
13 import com.oreilly.servlet.multipart.*;
14 import com.oreilly.servlet.*;
23 import mircoders.entity.*;
24 import mircoders.storage.*;
25 import mircoders.module.*;
26 import mircoders.producer.*;
29 * ServletModuleBilder -
30 * liefert HTML fuer Bilder
36 public class ServletModuleImages extends mir.servlet.ServletModule
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;
46 // Singelton / Kontruktor
47 private static ServletModuleImages instance = new ServletModuleImages();
48 public static ServletModule getInstance() { return instance; }
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");
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();
65 catch (StorageObjectException e) {
66 theLog.printDebugInfo("servletmodulebilder konnte nicht initialisiert werden");
71 public void insert(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
74 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
75 HashMap parameters = mp.getParameters();
77 MpRequest mpReq = (MpRequest)mp.requestList.get(0);
78 byte[] imageData=mpReq.getMedia();
79 String fileName=mpReq.getFilename();
80 String contentType= mpReq.getContentType();
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");
89 String id = mainModule.add(parameters);
90 EntityImages entImage = (EntityImages)mainModule.getById(id);
92 if (imageData!=null && fileName!=null) {
93 String fileType = "-1";
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+"'";
101 EntityList mediaTypesList = DatabaseMediaType.getInstance().selectByWhereClause(wc);
102 String mediaTypeId = null;
103 String mediaStorageName = null;
104 String mediaHandlerName = null;
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);
115 //load the classes via reflection
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();
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));
131 //were done with mpReq at this point, dereference it. as it contains
132 //mucho mem. -mh 01.10.2001
136 new ProducerMedia().handle(null, null, false, false, mediaId);
138 } catch (Exception e) {
139 theLog.printError("setting uploaded_media failed: "+e.toString());
143 if (!fileType.equals("-1"))
144 entImage.setImage(imageData, fileType);
146 theLog.printError("Wrong file uploaded!");
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());}
157 public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
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();
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");
174 String id = mainModule.set(parameters);
175 theLog.printError("Image ID"+id);
178 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
179 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
184 public void showimg(HttpServletRequest req, HttpServletResponse res)
185 throws ServletModuleException
187 String idParam = req.getParameter("id");
188 if (idParam!=null && !idParam.equals("")) {
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
194 byte[] outbytes = entImage.getImage();
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());}
203 else theLog.printDebugInfo("id nicht angeben.");
204 // darf keine exception werfen
207 public void showicon(HttpServletRequest req, HttpServletResponse res)
208 throws ServletModuleException
210 String idParam = req.getParameter("id");
211 if (idParam!=null && !idParam.equals("")) {
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
217 byte[] outbytes = entImage.getIcon();
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());}
226 else throw new ServletModuleException("id nicht angeben.");
229 public void list(HttpServletRequest req, HttpServletResponse res)
230 throws ServletModuleException
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);
247 String order = req.getParameter("order");
248 if (order==null) order="webdb_lastchange desc";
250 // if in connection mode to content
251 String cid = req.getParameter("cid");
252 mergeData.put("cid",cid);
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+"'";
264 if (query_media_folder != null && !query_media_folder.equals("")) {
265 if (isFirst==false) whereClause+=" and ";
266 whereClause += "to_media_folder='"+query_media_folder+"'";
268 //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
270 // fetch und ausliefern
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()));
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());
292 HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, mergeData, res.getWriter());
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());}
300 public void add(HttpServletRequest req, HttpServletResponse res)
301 throws ServletModuleException
304 SimpleHash mergeData = new SimpleHash();
305 mergeData.put("new", "1");
306 mergeData.put("mediafolderPopupData", mediafolderModule.getPopupData());
307 deliver(req, res, mergeData, templateObjektString);
309 catch (Exception e) { throw new ServletModuleException(e.toString());}
312 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
314 String idParam = req.getParameter("id");
315 _edit(idParam, req, res);
318 private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
319 if (idParam!=null && !idParam.equals("")) {
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);
330 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
332 else throw new ServletModuleException("ServletmoduleImage :: _edit without id");
336 /** @todo should be in ServletModule.java */
337 private EntityUsers _getUser(HttpServletRequest req)
339 HttpSession session=req.getSession(false);
340 return (EntityUsers)session.getAttribute("login.uid");
345 public void upload(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
347 //theLog.printDebugInfo("-- trying to upload");
348 String idParam = req.getParameter("id");
349 if (idParam!=null && !idParam.equals("")) {
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();
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);
367 theLog.printError("Wrong file uploaded!");
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());}
374 throw new ServletModuleException("Keine id angegeben");