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 //the where clause to find the media_type entry
94 //from the content-type.
95 //we use the media type entry to lookup the
96 //media Handler/Storage classes
97 String wc = " mime_type='"+contentType+"'";
99 EntityList mediaTypesList = DatabaseMediaType.getInstance().selectByWhereClause(wc);
100 String mediaTypeId = null;
101 String mediaStorageName = null;
102 String mediaHandlerName = null;
104 //if we found an entry matching the
105 //content-type int the table.
106 if (mediaTypesList.size() > 0) {
107 //get the class names from the media_type table.
108 mediaTypeId = mediaTypesList.elementAt(0).getId();
109 mediaStorageName = mediaTypesList.elementAt(0).getValue("tablename");
110 mediaHandlerName = mediaTypesList.elementAt(0).getValue("classname");
111 parameters.put("to_media_type",mediaTypeId);
113 //load the classes via reflection
116 Class mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
117 Method m = mediaStorageClass.getMethod("getInstance", null);
118 Database mediaStorage = (Database)m.invoke(null, null);
119 Entity mediaEnt = (Entity)mediaStorage.getEntityClass().newInstance();
120 mediaEnt.setStorage(mediaStorage);
121 mediaEnt.setValues(parameters);
122 mediaId = mediaEnt.insert();
124 Class mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
125 MirMedia mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
126 //save and store the media data/metadata
127 mediaHandler.set(mpReq.getMedia(), mediaEnt,mediaTypesList.elementAt(0));
129 //were done with mpReq at this point, dereference it. as it contains
130 //mucho mem. -mh 01.10.2001
134 new ProducerMedia().handle(null, null, false, false, mediaId);
136 } catch (Exception e) {
137 theLog.printError("setting uploaded_media failed: "+e.toString());
141 entImage.setImage(imageData);
146 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
147 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
148 catch (StorageObjectException e) { throw new ServletModuleException("StorageObjectException" + e.toString());}
152 public void update(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
156 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
157 HashMap parameters = mp.getParameters();
158 //MpRequest mpReq = (MpRequest)mp.requestList.get(0);
159 //byte[] imageData=mpReq.getMedia();
160 //String fileName=mpReq.getFilename();
161 //String contentType=mpReq.getContentType();
163 EntityUsers user = _getUser(req);
164 parameters.put("to_publisher", user.getId());
165 parameters.put("is_produced", "0");
166 if (!parameters.containsKey("is_published"))
167 parameters.put("is_published","0");
169 String id = mainModule.set(parameters);
170 theLog.printError("Image ID"+id);
173 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
174 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
179 public void showimg(HttpServletRequest req, HttpServletResponse res)
180 throws ServletModuleException
182 String idParam = req.getParameter("id");
183 if (idParam!=null && !idParam.equals("")) {
185 EntityImages entImage =(EntityImages)mainModule.getById(idParam);
186 res.setContentType("image/jpeg"); // testweise
187 ServletOutputStream out = res.getOutputStream(); // wichtig, dass vorher kein res.getwriter() gelaufen ist
189 byte[] outbytes = entImage.getImage();
194 catch (IOException e) {throw new ServletModuleException(e.toString());}
195 catch (ModuleException e) {throw new ServletModuleException(e.toString());}
196 catch (Exception e) {throw new ServletModuleException(e.toString());}
198 else theLog.printDebugInfo("id nicht angeben.");
199 // darf keine exception werfen
202 public void showicon(HttpServletRequest req, HttpServletResponse res)
203 throws ServletModuleException
205 String idParam = req.getParameter("id");
206 if (idParam!=null && !idParam.equals("")) {
208 EntityImages entImage =(EntityImages)mainModule.getById(idParam);
209 res.setContentType("image/jpeg"); // testweise
210 ServletOutputStream out = res.getOutputStream(); // wichtig, dass vorher kein res.getwriter() gelaufen ist
212 byte[] outbytes = entImage.getIcon();
217 catch (IOException e) {throw new ServletModuleException(e.toString());}
218 catch (ModuleException e) {throw new ServletModuleException(e.toString());}
219 catch (Exception e) {throw new ServletModuleException(e.toString());}
221 else throw new ServletModuleException("id nicht angeben.");
224 public void list(HttpServletRequest req, HttpServletResponse res)
225 throws ServletModuleException
227 // Parameter auswerten
228 SimpleHash mergeData = new SimpleHash();
229 String query_text = req.getParameter("query_text");
230 mergeData.put("query_text",query_text);
231 if (query_text!=null) mergeData.put("query_text_encoded",URLEncoder.encode(query_text));
232 String query_field = req.getParameter("query_field");
233 mergeData.put("query_field",query_field);
234 String query_is_published = req.getParameter("query_is_published");
235 mergeData.put("query_is_published",query_is_published);
236 String query_media_folder = req.getParameter("query_media_folder");
237 mergeData.put("query_media_folder",query_media_folder);
238 String offset = req.getParameter("offset");
239 if (offset==null || offset.equals("")) offset="0";
240 mergeData.put("offset",offset);
242 String order = req.getParameter("order");
243 if (order==null) order="webdb_lastchange desc";
245 // if in connection mode to content
246 String cid = req.getParameter("cid");
247 mergeData.put("cid",cid);
251 String whereClause=""; boolean isFirst=true;
252 if (query_text!=null && !query_text.equalsIgnoreCase("")) {
253 whereClause += "lower("+query_field+") like lower('%"+query_text+"%')"; isFirst=false;}
254 if (query_is_published != null && !query_is_published.equals("")) {
255 if (isFirst==false) whereClause+=" and ";
256 whereClause += "is_published='"+query_is_published+"'";
259 if (query_media_folder != null && !query_media_folder.equals("")) {
260 if (isFirst==false) whereClause+=" and ";
261 whereClause += "to_media_folder='"+query_media_folder+"'";
263 //theLog.printDebugInfo("sql-whereclause: " + whereClause + " order: " + order + " offset: " + offset);
265 // fetch und ausliefern
267 mergeData.put("mediafolderPopupData", mediafolderModule.getPopupData());
268 mergeData.put("mediafolderHashdata", mediafolderModule.getHashData());
269 if (query_text!=null || query_is_published!=null || query_media_folder!=null) {
270 EntityList theList = mainModule.getByWhereClause(whereClause, order, (new Integer(offset)).intValue(),10);
271 if (theList != null) {
272 mergeData.put("contentlist",HTMLTemplateProcessor.makeSimpleList(theList));
273 if(theList.getOrder()!=null) {
274 mergeData.put("order", theList.getOrder());
275 mergeData.put("order_encoded", URLEncoder.encode(theList.getOrder()));
277 mergeData.put("count", (new Integer(theList.getCount())).toString());
278 mergeData.put("from", (new Integer(theList.getFrom())).toString());
279 mergeData.put("to", (new Integer(theList.getTo())).toString());
280 if (theList.hasNextBatch())
281 mergeData.put("next", (new Integer(theList.getNextBatch())).toString());
282 if (theList.hasPrevBatch())
283 mergeData.put("prev", (new Integer(theList.getPrevBatch())).toString());
287 HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateListString, mergeData, res.getWriter());
289 catch (ModuleException e) {throw new ServletModuleException(e.toString());}
290 catch (IOException e) {throw new ServletModuleException(e.toString());}
291 catch (Exception e) {throw new ServletModuleException(e.toString());}
295 public void add(HttpServletRequest req, HttpServletResponse res)
296 throws ServletModuleException
299 SimpleHash mergeData = new SimpleHash();
300 mergeData.put("new", "1");
301 mergeData.put("mediafolderPopupData", mediafolderModule.getPopupData());
302 deliver(req, res, mergeData, templateObjektString);
304 catch (Exception e) { throw new ServletModuleException(e.toString());}
307 public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
309 String idParam = req.getParameter("id");
310 _edit(idParam, req, res);
313 private void _edit(String idParam, HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
314 if (idParam!=null && !idParam.equals("")) {
316 SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(mainModule.getById(idParam));
317 mergeData.put("mediafolderPopupData", mediafolderModule.getPopupData());
318 mergeData.put("rightsHashdata", dbRights.getHashData());
319 mergeData.put("imgformatHashdata", dbImageFormat.getHashData());
320 mergeData.put("imgcolorHashdata", dbImageColor.getHashData());
321 mergeData.put("imgtypeHashdata", dbImageType.getHashData());
322 mergeData.put("imglayoutHashdata", dbImageLayout.getHashData());
323 deliver(req, res, mergeData, templateObjektString);
325 catch (ModuleException e) { throw new ServletModuleException(e.toString());}
327 else throw new ServletModuleException("ServletmoduleImage :: _edit without id");
331 /** @todo should be in ServletModule.java */
332 private EntityUsers _getUser(HttpServletRequest req)
334 HttpSession session=req.getSession(false);
335 return (EntityUsers)session.getAttribute("login.uid");
340 public void upload(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
342 //theLog.printDebugInfo("-- trying to upload");
343 String idParam = req.getParameter("id");
344 if (idParam!=null && !idParam.equals("")) {
347 WebdbMultipartRequest mp = new WebdbMultipartRequest(req);
348 HashMap withValues = mp.getParameters();
349 MpRequest mpReq = (MpRequest)mp.requestList.get(0);
350 byte[] imageData=mpReq.getMedia();
351 String fileName=mpReq.getFilename();
352 String contentType=mpReq.getContentType();
354 EntityImages entImage = (EntityImages)mainModule.getById(idParam);
355 entImage.setImage(imageData);
357 catch (IOException e) { throw new ServletModuleException("upload -- ioexception " + e.toString());}
358 catch (ModuleException e) { throw new ServletModuleException("upload -- moduleexception " + e.toString());}
359 catch (StorageObjectException e) { throw new ServletModuleException("upload -- storageobjectexception " + e.toString());}
362 throw new ServletModuleException("Keine id angegeben");