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(this.getClass().getName());
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 != "-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 EntityImages entImage = (EntityImages)mainModule.getById(id);
177 if (imageData!=null && fileName!=null) {
178 String fileType = "-1";
179 if (contentType.equals("image/jpeg")) fileType="0";
180 if (contentType.equals("image/gif")) fileType="1";
181 if (fileType != "-1")
182 entImage.setImage(imageData, fileType);
184 theLog.printError("Wrong file uploaded!");
188 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
189 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
194 public void showimg(HttpServletRequest req, HttpServletResponse res)
195 throws ServletModuleException
197 String idParam = req.getParameter("id");
198 if (idParam!=null && !idParam.equals("")) {
200 EntityImages entImage =(EntityImages)mainModule.getById(idParam);
201 res.setContentType("image/jpeg"); // testweise
202 ServletOutputStream out = res.getOutputStream(); // wichtig, dass vorher kein res.getwriter() gelaufen ist
204 byte[] outbytes = entImage.getImage();
209 catch (IOException e) {throw new ServletModuleException(e.toString());}
210 catch (ModuleException e) {throw new ServletModuleException(e.toString());}
211 catch (Exception e) {throw new ServletModuleException(e.toString());}
213 else theLog.printDebugInfo("id nicht angeben.");
214 // darf keine exception werfen
217 public void showicon(HttpServletRequest req, HttpServletResponse res)
218 throws ServletModuleException
220 String idParam = req.getParameter("id");
221 if (idParam!=null && !idParam.equals("")) {
223 EntityImages entImage =(EntityImages)mainModule.getById(idParam);
224 res.setContentType("image/jpeg"); // testweise
225 ServletOutputStream out = res.getOutputStream(); // wichtig, dass vorher kein res.getwriter() gelaufen ist
227 byte[] outbytes = entImage.getIcon();
232 catch (IOException e) {throw new ServletModuleException(e.toString());}
233 catch (ModuleException e) {throw new ServletModuleException(e.toString());}
234 catch (Exception e) {throw new ServletModuleException(e.toString());}
236 else throw new ServletModuleException("id nicht angeben.");
239 public void list(HttpServletRequest req, HttpServletResponse res)
240 throws ServletModuleException
242 // Parameter auswerten
243 SimpleHash mergeData = new SimpleHash();
244 String query_text = req.getParameter("query_text");
245 mergeData.put("query_text",query_text);
246 if (query_text!=null) mergeData.put("query_text_encoded",URLEncoder.encode(query_text));
247 String query_field = req.getParameter("query_field");
248 mergeData.put("query_field",query_field);
249 String query_is_published = req.getParameter("query_is_published");
250 mergeData.put("query_is_published",query_is_published);
251 String query_media_folder = req.getParameter("query_media_folder");
252 mergeData.put("query_media_folder",query_media_folder);
253 String offset = req.getParameter("offset");
254 if (offset==null || offset.equals("")) offset="0";
255 mergeData.put("offset",offset);
257 String order = req.getParameter("order");
258 if (order==null) order="webdb_lastchange desc";
260 // if in connection mode to content
261 String cid = req.getParameter("cid");
262 mergeData.put("cid",cid);
266 String whereClause=""; boolean isFirst=true;
267 if (query_text!=null && !query_text.equalsIgnoreCase("")) {
268 whereClause += "lower("+query_field+") like lower('%"+query_text+"%')"; isFirst=false;}
269 if (query_is_published != null && !query_is_published.equals("")) {
270 if (isFirst==false) whereClause+=" and ";
271 whereClause += "is_published='"+query_is_published+"'";
274 if (query_media_folder != null && !query_media_folder.equals("")) {
275 if (isFirst==false) whereClause+=" and ";
276 whereClause += "to_media_folder='"+query_media_folder+"'";
278 //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
280 // fetch und ausliefern
282 mergeData.put("mediafolderPopupData", mediafolderModule.getPopupData());
283 mergeData.put("mediafolderHashdata", mediafolderModule.getHashData());
284 if (query_text!=null || query_is_published!=null || query_media_folder!=null) {
285 EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(),10);
286 if (theList != null) {
287 mergeData.put("contentlist",HTMLTemplateProcessor.makeSimpleList(theList));
288 if(theList.getOrder()!=null) {
289 mergeData.put("order", theList.getOrder());
290 mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
292 mergeData.put("count", (new Integer(theList.getCount())).toString());
293 mergeData.put("from", (new Integer(theList.getFrom())).toString());
294 mergeData.put("to", (new Integer(theList.getTo())).toString());
295 if (theList.hasNextBatch())
296 mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
297 if (theList.hasPrevBatch())
298 mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
302 HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, mergeData, res.getWriter());
304 catch (ModuleException e) {throw new ServletModuleException(e.toString());}
305 catch (IOException e) {throw new ServletModuleException(e.toString());}
306 catch (Exception e) {throw new ServletModuleException(e.toString());}
310 public void add(HttpServletRequest req, HttpServletResponse res)
311 throws ServletModuleException
314 SimpleHash mergeData = new SimpleHash();
315 mergeData.put("new", "1");
316 mergeData.put("mediafolderPopupData", mediafolderModule.getPopupData());
317 deliver(req, res, mergeData, templateObjektString);
319 catch (Exception e) { throw new ServletModuleException(e.toString());}
322 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
324 String idParam = req.getParameter("id");
325 _edit(idParam, req, res);
328 private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
329 if (idParam!=null && !idParam.equals("")) {
331 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(mainModule.getById(idParam));
332 mergeData.put("mediafolderPopupData", mediafolderModule.getPopupData());
333 mergeData.put("rightsHashdata", dbRights.getHashData());
334 mergeData.put("imgformatHashdata", dbImageFormat.getHashData());
335 mergeData.put("imgcolorHashdata", dbImageColor.getHashData());
336 mergeData.put("imgtypeHashdata", dbImageType.getHashData());
337 mergeData.put("imglayoutHashdata", dbImageLayout.getHashData());
338 deliver(req, res, mergeData, templateObjektString);
340 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
342 else throw new ServletModuleException("ServletmoduleImage :: _edit without id");
346 /** @todo should be in ServletModule.java */
347 private EntityUsers _getUser(HttpServletRequest req)
349 HttpSession session=req.getSession(false);
350 return (EntityUsers)session.getAttribute("login.uid");
355 public void upload(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
357 //theLog.printDebugInfo("-- trying to upload");
358 String idParam = req.getParameter("id");
359 if (idParam!=null && !idParam.equals("")) {
362 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
363 HashMap withValues = mp.getParameters();
364 MpRequest mpReq = (MpRequest)mp.requestList.get(0);
365 byte[] imageData=mpReq.getMedia();
366 String fileName=mpReq.getFilename();
367 String contentType=mpReq.getContentType();
369 String fileType = "-1";
370 if (contentType.equals("image/jpeg")) fileType="0";
371 if (contentType.equals("image/gif")) fileType="1";
372 if (fileType != "-1") {
373 EntityImages entImage = (EntityImages)mainModule.getById(idParam);
374 entImage.setImage(imageData, fileType);
377 theLog.printError("Wrong file uploaded!");
379 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
380 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
383 throw new ServletModuleException("Keine id angegeben");